C - Program to Implement Matrix Operations using Pointers
/* Operations covered :
1) Create() : for creating and reading m x n elements
in the given matrix using 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.
1) Create() : for creating and reading m x n elements
in the given matrix using 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.
*/
#include<stdio.h>
#include<conio.h>
/* 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<m;i++)
*(a+i)=(int*)malloc(n*sizeof(int));
printf("\n Enter the data:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",(*(a+i)+j));
return(a);
}
void create1(int a[][10],int m , int n)
{ int i,j;
/*creating a matrix */
printf("\n Enter the data:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void print(int **a,int m ,int n)
{ int i,j;
for(i=0;i<m;i++)
{ printf("\n");
for(j=0;j<n;j++)
printf("%5d",*(*(a+i)+j));
}
}
void print1(int a[][10],int m ,int n)
{ int i,j;
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
}
}
void transpose(int **a,int m ,int n)
{ int i,j,temp;
if(m==n)
{ for(i=1;i<m;i++)
for(j=0;j<i;j++)
{
temp=*(*(a+i)+j);
*(*(a+i)+j)=*(*(a+j)+i);
*(*(a+j)+i)=temp;
}
}
}
int ** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2)
{ int i,j; int **c;
c=(int**)malloc(m1*sizeof(int*));
for(i=0;i<m1;i++)
*(c+i)=(int*)malloc(n1*sizeof(int));
if(m1==m2 && n1==n2)
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
*(*(c+i)+j)=*(*(a+i)+j) + *(*(b+i)+j);
return(c);
}
void multmat(int a[][10],int m1,int n1 ,int b[][10],int m2,int n2,int c[][10])
{ int i,j,k,temp;
if(n1==m2)
{ for(i=0;i<m1;i++)
for(j=0;j<n2;j++)
{
temp=0;
for(k=0;k<n1;k++)
temp=temp + a[i][k] * b[k][j];
c[i][j]=temp;
}
}
}
int saddle(int a[][10],int m,int n)
{ int i,j,small,large,col_of_small,row_of_large;
for(i=0;i<m;i++) /* find saddle point row wise */
{ small=a[i][0];
col_of_small=0;
for(j=1;j<n;j++)
if(a[i][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;j<m;j++)
if(a[j][col_of_small]>large)
{ 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);
}