The program below demostrates how to multiply any two matrices in c.Before multiplication the order of the matrix is checked to ensure multiplication is possible.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[50][50],B[50][50],C[50][50];
int i,j,p,r1,r2,c1,c2;
clrscr();
printf("\nMATRIX MULTIPLICATION");
printf("\nEnter The order Of First Matrix:");
scanf("%d%d",&r1,&c1);
printf("\nEnter The Elements Of First Matrix:");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&A[i][j]);
printf("\n\nEnter The order Of Second Matrix:");
scanf("%d%d",&r2,&c2);
printf("\nEnter The Elements Of Second Matrix:");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&B[i][j]);
if(c1!=r2)
{
printf("\n\nThe Matrix Cant Be Multipied!!!");
getch();
exit(0);
}
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
C[i][j]=0;
for(p=0;p<c1;p++)
C[i][j]+=A[i][p]*B[p][j];
}
printf("\n\nThe Product Of Matrices Is:");
for(i=0;i<r1;i++)
{ printf("\n");
for(j=0;j<c2;j++)
printf("%d ",C[i][j]);
}
getch();
}
}
No comments:
Post a Comment