#include<stdio.h>
#include<conio.h>
void traverse(float res[10][10],int,int);
void add(float mat[10][10],float mat1[10][10],int,int,int,int);
void mult(float mat[10][10],float mat1[10][10],int,int,int,int);
void sym(float res[10][10],int,int);
void trans(float res[10][10],int,int);
void traverse(float res[10][10],int row,int col)
{
int i,j;
printf("\nthe resultant matrix is given as\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%f\t",res[i][j]);
}
printf("\n");
}
}
void add(float mat[10][10],float mat1[10][10],int r,int c,int r1,int c1)
{
int i,j;
float res[10][10];
char x;
if(r!=r1 || c!=c1)
printf("\nthe matrices are not compatible");
else
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
res[i][j]=mat[i][j]+mat1[i][j];
}
}
while(1)
{
printf("\n\nenter t. for traversing the resultant matrix");
printf("\nenter s. to know symmetry of the matrix");
printf("\nenter p. to transpose the resultant matrix");
printf("\nenter e. to exit from the function");
printf("\nenter your choice ");
fflush(stdin);
scanf("%c",&x);
switch(x)
{
case 't':
traverse(res,r,c);
break;
case 's':
sym(res,r,c);
break;
case 'p':
trans(res,r,c);
break;
case 'e':
goto z;
default:
printf("\nyou have entered a wrong choice");
}
}
}
z:}
void mult(float mat[10][10],float mat1[10][10],int r,int c,int r1,int c1)
{
int i,j,k;
float res[10][10];
char x;
if(c!=r1)
printf("\nthe multiplication is not possible");
else
{
for(i=0;i<r;i++)
{
for(j=0;j<c1;j++)
{
res[i][j]=0;
for(k=0;k<c;k++)
{
res[i][j]=res[i][j]+(mat[i][k]*mat1[k][j]);
}
}
}
while(1)
{
printf("\n\nenter t. for traversing the resultant matrix");
printf("\nenter s. to know symmetry of the matrix");
printf("\nenter p. to transpose the resultant matrix");
printf("\nenter e. to exit from the function");
printf("\nenter your choice ");
fflush(stdin);
scanf("%c",&x);
switch(x)
{
case 't':
traverse(res,r,c1);
break;
case 's':
sym(res,r,c1);
break;
case 'p':
trans(res,r,c1);
break;
case 'e':
goto z;
default:
printf("\nyou have entered a wrong choice");
}
}
}
z: }
void sym(float res[10][10],int row,int col)
{
int flag=0,i,j;
if(row!=col)
printf("\nthe matrix is not symmetric");
else
{
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(res[j][i]!=res[i][j])
{
flag=1;
}
}
}
if(flag==0)
printf("\nit is a symmetric mat.");
else
printf("\nit is not a symmetric matrix.");
}
}
void trans(float res[10][10],int row,int col)
{
float res1[10][10];
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
res1[j][i]=res[i][j];
}
}
printf("\nthe transpose matrix is\n");
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
printf("%f\t",res1[i][j]);
}
printf("\n");
}
}
void main()
{
int r,r1,c,c1,i,j;
float mat[10][10],mat1[10][10];
char n,n1;
clrscr();
printf("\nenter the no. of rows and columns of first matrix");
scanf("%d%d",&r,&c);
printf("\nenter the no. of rows and columns of the second matrix");
scanf("%d%d",&r1,&c1);
printf("\nenter the elements in the first matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%f",&mat[i][j]);
}
}
printf("\nenter the elements in the second matrix");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%f",&mat1[i][j]);
}
}
while(1)
{
printf("\n\nenter a. for addition");
printf("\nenter m. for multiplication");
printf("\nenter e. for exit");
printf("\nenter your choice ");
fflush(stdin);
scanf("%c",&n);
switch(n)
{
case 'a':
add(mat,mat1,r,c,r1,c1);
break;
case 'm':
mult(mat,mat1,r,c,r1,c1);
break;
case 'e':
exit(0);
default:
printf("\nyou have entered a wrong choice");
}
}
getch();
}