EXP9
EXP9
Experiment No. 9
AIM:
To Demonstrate the use of pointers to
solve a given problem.
Program 1
PROGRAM: #include<stdio.h>
void main() {
int a=10,b=20;
swap(&a,&b);
printf("a= %d, b=%d", a, b);
}
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 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);
}
RESULT:
Program 3
PROBLEM Write a program to swap the smallest and largest element in an array using
STATEMENT: pointers.
PROGRAM: #include<stdio.h>
printf("\nOriginal Array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
swap(min, max);
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);
ptr = arr;
printf("The original array is :\n");
for(int i = 0; i < n; i ++) {
printf("%d ", *(ptr + i));
}
RESULT:
Program 5
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);
//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");
}
}
RESULT:
Program 6
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);
}
}
}
RESULT:
CONCLUSION: