In this program some display order problem when getting the order of matrix 2 and its elements. Addition and subtraction works perfectly but multiplication do not shows the correct resultsCODE
/* Addition , Subtraction and Multiplication of Matrices */
#include<stdio.h>
main()
{
int a[20][20], b[20][20], i,j,m,n,p,q;
clrscr();
/*Getting the element of the Matrix1 */
printf("Enter the order of first matrix\n");
scanf("%d %d", &m, &n);
printf("%d %d", m,n);
printf("\n Enter the element of first matrix\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
}
/*Getting the element of the Matrix2 */
printf("\n Enter the order of second matrix\n");
scanf("%d %d ", &p,&q);
printf("%d %d\n", p,q);
printf("\n Enter the element of second matrix\n");
for(i=0; i<p; i++)
for(j=0; j<q; j++)
{
scanf("%d", &b[i][j]);
}
printf("\n");
/* Displaying Matrix1 and Matrix2 */
clrscr();
printf("\n\t\t Addition, Subtraction, Multiplication of Matrixes\n\n");
printf("First matrix:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("\t%d", a[i][j]);
printf("\n");
}
printf("\n");
printf("Second Matrix: \n");
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
printf("\t%d", b[i][j]);
printf("\n");
}
if(m==p & n==q) /*Checks whether the orders of the Matrixes are equal */
{
add(a,b,m,n); /*If equal sends to the functions add & mul */
mul(a,b,m,n,q);
}
else
{
if(n==p) /* If columns(Mat.1) = Rows (Mat.2) it Multiplication only possible */
{
printf("\n Addition and Subtraction not possible with this order");
mul(a,b,n,q);
}
else
{
printf("\n The order of the matrix are not proper");
}
}
getch();
}
mul(int m1[20][20], int m2[20][20], int r1, int c1, int c2)
{
int i,j,k,p[20][20];
for(i=0; i<r1; i++)
for(j=0; j<c2; j++)
{
p[i][j] =0;
for(k=0; k<c1; k++)
p[i][j] += m1[i][k]*m2[k][i];
}
printf("\n\t The product: \n");
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("\t\t %d", p[i][j]);
}
printf("\n\n");
}
}
add(int m1[20][20],int m2[20][20],int r,int c)
{
int i,j,sum[20][20],diff[20][20];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
sum[i][j]=m1[i][j]+m2[i][j];
diff[i][j]=m1[i][j]-m2[i][j];
}
printf("\n");
printf("\t The sum:\n");
for(i=0; i<r; i++)
{
for(j=0;j<c;j++)
printf("\t%d\t",sum[i][j]);
printf("\n\n");
}
printf("\t The difference:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
printf("\t%d\t", diff[i][j]);
printf("\n\n");
}
}