Program for Matrix operations with pointers
2) Create1() : for reading m x n elements in the given matrix without using pointers.
3) print() : diaplays the given matrix using pointers
4) Print1() : displays the given matrix without using pointers.
3) Transpose() : Creates transpose of the given square matrix using pointers.
4) addmat() : adds two matrices and returns the resultant matrix using pointers.
5) multmat() : multipiles two matrices without using pointers.
6) saddle() : checks whether the given matrix has a saddle point without using pointers.
;j++)>;j++)>;i++)>;i++)>
#include
#include
/* Protype of functions */
int** create(int m ,int n); // Returns a 2D matrix.
void create1(int a[][10],int m , int n);
void print(int **a,int m ,int n);
void print1(int a[][10], int m , int n);
void transpose(int **a,int m ,int n);
/* Transpose will be calculated if m is equal to n.
Transpose will be strored in the same matrix a */
int** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2);
/* Two matrices a and b will be added and the result
will be returned.
Matrix a is of the size m1 x n1 and the matrix b
is of the size m2 x n2 */
void multmat(int a[][10],int m1,int n1 ,int b[][10],int m2,int n2,int c[][10]);
/* Two matrices a and b will be multiplied and the result
will be stored in c .
Matrix a is of the size m1 x n1 and the matrix b
is of the size m2 x n2 */
int saddle(int a[][10],int m,int n);
/*An m x n matrix is said to have a saddle point if some entry a[i][j] is
smallest value in row i and the largest value of column j */
void main()
{ int **a,**b,**c,m1,n1,m2,n2,m3,n3;
int a1[10][10],b1[10][10],c1[10][10];
int opt;
clrscr();
do
{ printf("\n1)Transpose of the Matrix:(transpose stored in the same matrix)");
printf("\n2)Add two matrices");
printf("\n3)Multiply two matrices");
printf("\n4)Saddle point in the matrix");
printf("\n5)Quit");
printf("\nEnter Your Choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1: printf("\n Enter the size of the matrix :");
scanf("%d%d",&m1,&n1);
a=create(m1,n1);
if(m1==n1)
{
transpose(a,m1,n1);
printf("\nResult=\n");
print(a,m1,n1);
}
else
printf("\Not a square matrix :");
break;
case 2: printf("\n Enter the size of the 1st matrix :");
scanf("%d%d",&m1,&n1);
a=create(m1,n1);
printf("\n Enter the size of the 2nd matrix :");
scanf("%d%d",&m2,&n2);
b=create(m2,n2);
if(m1==m2 & n1==n2)
{ c=addmat(a,m1,n1,b,m2,n2);
printf("\nResult=\n");
print(c,m1,n1);
}
else
printf("\n Can not be added ");
break;
case 3: printf("\n Enter the size of the 1st matrix :");
scanf("%d%d",&m1,&n1);
create1(a1,m1,n1);
printf("\n Enter the size of the 2nd matrix :");
scanf("%d%d",&m2,&n2);
create1(b1,m2,n2);
if(n1==m2)
{ multmat(a1,m1,n1,b1,m2,n2,c1);
printf("\nResult=\n");
print1(c1,m1,n2);
}
else
printf("\n Can not multiply");
break;
case 4: printf("\n Enter the size of the 1st matrix :");
scanf("%d%d",&m1,&n1);
create1(a1,m1,n1);
saddle(a1,m1,n1);
break;
}
}while(opt!=5);
}
int ** create(int m ,int n)
{ int i,j; int **a;
/*creating a matrix */
a=(int**)malloc(m*sizeof(int*));
for(i=0;i /*creating a matrix */
printf("\n Enter the data:");
for(i=0;i/* find saddle point row wise */
{ small=a[i][0];
col_of_small=0;
for(j=1;j< small)
{
small=a[i][j];
col_of_small=j;
}
/* find the largest element in "col_of_small"*/
large= a[0][col_of_small];
row_of_large=0;
for(j=1;jlarge)
{ large=a[j][col_of_small];
row_of_large=j;
}
if(i==row_of_large)
{
printf("\n Saddle point exist at (%d,%d) with value as %d",i,col_of_small
,a[i][col_of_small]);
return(1);
}
}
printf("\nSaddle point does not exist ");
return(0);
}
Output
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 1
Enter the size of the matrix :3 3
Enter the data:1 2 3
4 5 6
7 8 9
Result=
1 4 7
2 5 8
3 6 9
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 2
Enter the size of the 1st matrix :3 3
Enter the data:1 2 3
4 5 6
7 8 9
Enter the size of the 2nd matrix :3 3
Enter the data:9 8 7
6 5 4
3 2 1
Result=
10 10 10
10 10 10
10 10 10
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 3
Enter the size of the 1st matrix :2 2
Enter the data:6 8
3 5
Enter the size of the 2nd matrix :2 2
Enter the data:7 9
2 4
Result=
58 86
31 47
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 4
Enter the size of the 1st matrix :2 2
Enter the data:5 6
1 2
Saddle point exist at (0,0) with value as 5
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 5


Leave a Reply