6. WRITE A C PROGRAM TO MULTIPLY TWO MATRICES THAT SATISFY
THE CONSTRAINT OF MATRIX MULTIPLICATION
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[10][10],b[10][10],c[10][10];
int r1,r2,c1,c2;
//clrscr();
printf("Enter 1st matrix size\n");
scanf("%d%d",&r1,&c1);
printf("Enter 2nd matrix size\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
printf("Matrix multiplication is not possible\n");
else
{
printf("Enter elements to 1st matrix\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter elements to 2nd matrix\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("Product of 2 matrix is\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
//getch();
}
0 comments:
Post a Comment