0% found this document useful (0 votes)
36 views11 pages

EXP9

Uploaded by

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

EXP9

Uploaded by

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

Name Arnav Kolhe

UID no. 2024800060

Experiment No. 9

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

Program 1

PROBLEM Write a function using pointers to swap 2 numbers


STATEMENT :

PROGRAM: #include<stdio.h>

void swap(int *x, int *y);

void main() {
int a=10,b=20;

swap(&a,&b);
printf("a= %d, b=%d", a, b);
}

void swap(int *x, int *y) {


int temp;
temp=*x;
*x=*y;
*y=temp;

RESULT:

Program 2
PROBLEM Write a function add(a,b,c) that receives a and b as parameter value a c as
STATEMENT : reference, adds a and b and store it in c.

PROGRAM: #include<stdio.h>

void sum( int a, int b, int *c);

void main() {
int a,b,add;
printf("enter any 2 numbers");
scanf("%d %d", &a, &b);

sum(a,b,&add);
printf("the sum is: %d", add);
}

void sum(int a, int b, int *c) {


*c=a+b;
}

RESULT:

Program 3

PROBLEM Write a program to swap the smallest and largest element in an array using
STATEMENT: pointers.

PROGRAM: #include<stdio.h>

int swap(int *a, int *b);


void main() {
int *min, *max;
int a[100], n;

printf("Enter the number of elements in the array :");


scanf("%d", &n);

for(int i = 0; i < n; i ++) {


scanf("%d", a + i);
}
max = a;
min = a;

for(int i = 1;i < n; i ++) {


if(*(a+i) > *max) {
max = a+i;
}
if(*(a+i) < *min) {
min = a+i;
}
}

printf("\nOriginal Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}

swap(min, max);

printf("\n\nArray after swapping the smallest and largest elements:\n");


for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
}

int swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;

return (*a, *b);


}
RESULT:

Program 4

PROBLEM Write a program to reverse the position of elements in the array using pointers.
STATEMENT:

PROGRAM: #include<stdio.h>

void main () {
int arr[100], n;
int *ptr;
printf("Enter the number of elements in the array :");
scanf("%d", &n);

printf("Enter the elements of the array:\n");


for(int i = 0; i < n; i ++) {
scanf("%d", &arr[i]);
}

ptr = arr;
printf("The original array is :\n");
for(int i = 0; i < n; i ++) {
printf("%d ", *(ptr + i));
}

printf("\nThe reversed array is :\n");


for(int i = n-1; i >= 0; i --) {
printf("%d ", *(ptr + i));
}
}

RESULT:

Program 5

PROBLEM Write a program to perform matrix multiplication using pointers. Dimensions of


STATEMENT: matrices will be decided by the user.

PROGRAM: #include<stdio.h>

void main() {
int a, b , c;
printf("NOTE:- THE COLUMN OF MAT1 AND ROW OF MAT2
MUST BE EQUAL.");
printf("\nEnter the row and column of matrix 1 :\n");
scanf("%d %d", &a, &b);
printf("Enter the row and column of matrix 1 :\n");
scanf("%d %d", &b, &c);

int mat1[a][b], mat2[b][c], solMat[a][c];


//PRINT THE MATRIX 1.
printf("Enter the first matrix :\n");
for(int i = 0; i < a; i ++) {
for(int j = 0; j < b; j ++) {
scanf("%d", (*(mat1 + i) + j));
}
}

//PRINT THE MATRIX 2.


printf("Enter the seond matrix :\n");
for(int i = 0; i < b; i ++) {
for(int j = 0; j < c; j ++) {
scanf("%d", (*(mat2 + i) + j));
}
}

//INITIALIZING THE THE ELEMENTS OF SOLUTION MATRIX TO


0.
for(int i = 0; i < a; i ++) {
for(int j = 0; j < c; j ++) {
*(*(solMat + i) + j) = 0;
}
}

//MULTIPLICATION OF MATRIX 1 AND MATRIX 2.


for(int i = 0; i < a; i ++) {
for(int j = 0; j < c; j ++) {
for(int k = 0; k < b; k ++) {
*(*(solMat + i) + j) += *(*(mat1 + i) + k) * *(*(mat2 + k) + j);
}
}
}

//PRINT MATRIX 1.
printf("\n The matrix 1 is :\n");
for(int i = 0; i < a; i ++) {
for(int j = 0; j < b; j ++) {
printf("%d\t", *(*(mat1 + i) + j));
}
printf("\n");
}

//PRINT MATRIX 2.
printf("\n The resultant matrix is :\n");
for(int i = 0; i < b; i ++) {
for(int j = 0; j < c; j ++) {
printf("%d\t", *(*(mat2 + i) + j));
}
printf("\n");
}

//PRINT SOLUTION MATRIX.


printf("\n The resultant matrix is :\n");
for(int i = 0; i < a; i ++) {
for(int j = 0; j < c; j ++) {
printf("%d\t", *(*(solMat + i) + j));
}
printf("\n");
}

}
RESULT:

Program 6

PROBLEM Write a program to calculate the addition of matrices using pointers.


STATEMENT: Dimensions of the matrix will be decided by the user.

PROGRAM: #include<stdio.h>
void main() {
int row, column;
printf("Enter the value of rows and columns of both matrices :\n");
scanf("%d %d", &row, &column);
int mat1[row][column], mat2[row][column], solMat[row][column];

//input matrix 1.
printf("Enter the elements of matrix 1 :\n");
for(int i = 0; i < row; i ++) {
for(int j = 0; j < column; j ++) {
scanf("%d", (*(mat1 + i) + j));
}
}

//input matrix 2.
printf("Enter the elements of matrix 2 :\n");
for(int i = 0; i < row; i ++) {
for(int j = 0; j < column; j ++) {
scanf("%d", (*(mat2 + i) + j));
}
}

//print matrix 1.
printf("\nMatrix 1 :\n");
for(int i = 0; i < row; i ++) {
for(int j = 0; j < column; j ++) {
printf("%d\t", *(*(mat1 + i) + j));
}
printf("\n");
}

//print matrix 2.
printf("\nMatrix 2 :\n");
for(int i = 0; i < row; i ++) {
for(int j = 0; j < column; j ++) {
printf("%d\t", *(*(mat2 + i) + j));
}
printf("\n");
}
//addition of matrices
for(int i = 0; i < row; i ++) {
for(int j = 0; j < column; j ++) {
*(*(solMat + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j);
}
}

//print the required matrix.


printf("\nMatrix required :\n");
for(int i = 0; i < row; i ++) {
for(int j = 0; j < column; j ++) {
printf("%d\t", *(*(solMat + i) + j));
}
printf("\n");
}

}
RESULT:

CONCLUSION:

You might also like