0% found this document useful (0 votes)
21 views10 pages

Psipl Exp No 9

Uploaded by

anuharshpawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

Psipl Exp No 9

Uploaded by

anuharshpawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Name Anuharsh Vijay Pawar

UID no. 2022300004


Experiment No. 9

AIM:
Demonstrate the use of pointers to solve a given
problem.
Program 1

PROBLEM Write a program to swap smallest and largest element in


STATEMENT:
an array using
pointers
ALGORITHM: Step 1: Read function void swap(int a[], int *ptr1,int
*ptr2)
Step 2: Write function void swap(int a[], int *ptr1,int
*ptr2)
Step 3: Read int temp;
temp = a[*ptr1];
a[*ptr1] = a[*ptr2];
a[ *ptr2] = temp;
Step 4: Write main function
Step 5: Read a [100], i, n, min, max, min_index,
max_index
Step 6: Print size of array and elements from user
Step 7: Use for loop for reading elements
Step 8: Check for condition for(int i=0;i<n;i++)
Step 9: Check if condition
if(min>a[i]){
min=a[i];
min_index=i;
}
Step 10: Check another if condition
if(max<a[i]){
max=a[i];
max_index=i;
Step 11: Call swap function
(a,&min_index,&max_index)
Step 12: Print array after swapping
PROGRAM: #include <stdio.h>
void swap(int a[], int *ptr1,int *ptr2);
int main()
{
int a[100],i,n,min,max,min_index,max_index;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i]){
min=a[i];
min_index=i;
}
if(max<a[i]){
max=a[i];
max_index=i;
}
}
swap(a,&min_index,&max_index);
printf("Array after swapping : ");
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
return 0;
}
void swap (int a[100], int *ptr1,int *ptr2)
{
int temp;
temp = a[*ptr1];
a[*ptr1] = a[*ptr2];
a[ *ptr2] = temp;
}

RESULT:

Program 2
PROBLEM Write a program to reverse the position of all elements in
STATEMENT:
the array using
pointers
ALGORITHM: Step 1: Read void swap(int* a, int* b);
void reverse(int array[], int array_size);
void print(int* array, int array_size);
Step 2: Read int array[]
Step 3: Call print array function
Step 4: Call reverse array function
Step 5: Call print array function
Step 6: Write swap function
Step 7: Read temp = *a
*a = *b;
*b = temp;
Step 8: Write print array function
Step 9: Read int *length = array + array_size,
*position = array;
Step 10: Check for condition
for (position = array; position < length; position++)
Step 11: Write reverse(int array[], int array_size);
Step 12: Read int *ptr1 = array,
*ptr2 = array + array_size - 1;
Step 13: Check while condition
while (ptr1 < ptr2) {
swap(ptr1, ptr2);
ptr1++; ptr2--
PROGRAM: #include <stdio.h>
void swap(int* a, int* b);
void reverse(int array[], int array_size);
void print(int* array, int array_size);
int main()
{
int array[] = { 1 , 2 , 3 , 4 , 5 , 6 };

printf("Original ");
print(array, 6);
printf("\n");
printf("Reverse ");
reverse(array, 6);
print(array, 6);

return 0;
}
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

void reverse(int array[], int array_size)


{
int *ptr1 = array,
*ptr2 = array + array_size - 1;
while (ptr1 < ptr2) {
swap(ptr1, ptr2);
ptr1++;
ptr2--;
}
}
void print(int* array, int array_size)
{
int *length = array + array_size,
*position = array;
printf("Array = ");
for (position = array; position < length; position++)
printf("%d ", *position);
}

RESULT:

Program 3

PROBLEM Write a program to perform matrix multiplication using


STATEMENT:
pointers.
Dimensions of matrices will be decided by the user.
ALGORITHM: Step 1: Declare void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL],
int res[][COL]);
Step 2: Write function void matrixInput(int mat[]
[COL]) function using two for loops
Step 3: Write function void matrixPrint(int mat[][COL])
function using two for loops
Step 4: Write function void matrixMultiply(int mat1[]
[COL], int mat2[][COL], int res[][COL])
Step 5: Read sum = 0;
Step 6: check for conditon for (i = 0; i < COL; i++)
Step 1: If above condition is true, then
sum += (*(*(mat1 + row) + i)) * (*(*(mat2 + i) + col))
and *(*(res + row) + col) = sum
Step 7: In main function, read int mat1[ROW][COL];
int mat2[ROW][COL];
int product[ROW][COL];
Step 8: Take elements from user
PROGRAM: #include <stdio.h>
#define ROW 3
#define COL 3
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL],
int res[][COL]);
int main()
{
int mat1[ROW][COL];
int mat2[ROW][COL];
int product[ROW][COL];
printf("Enter elements in first matrix of size %dx%d\
n", ROW, COL);
matrixInput(mat1);
printf("Enter elements in second matrix of size %dx
%d\n", ROW, COL);
matrixInput(mat2);
matrixMultiply(mat1, mat2, product);
printf("Product of both matrices is : \n");
matrixPrint(product);
return 0;
}

void matrixInput(int mat[][COL])


{
int row, col;

for (row = 0; row < ROW; row++)


{
for (col = 0; col < COL; col++)
{
scanf("%d", (*(mat + row) + col));
}
}
}

void matrixPrint(int mat[][COL])


{
int row, col;

for (row = 0; row < ROW; row++)


{
for (col = 0; col < COL; col++)
{
printf("%d ", *(*(mat + row) + col));
}

printf("\n");
}
}
void matrixMultiply(int mat1[][COL], int mat2[][COL],
int res[][COL])
{
int row, col, i;
int sum;
for (row = 0; row < ROW; row++)
{
for (col = 0; col < COL; col++)
{
sum = 0;
for (i = 0; i < COL; i++)
{
sum += (*(*(mat1 + row) + i)) * (*(*(mat2 +
i) + col));
}
*(*(res + row) + col) = sum;
}
}
}

RESULT:
CONCLUSION: It helps to learn about pointers.
As well as it also helps in logical thinking.

You might also like