Program to print the multiplication of two matrices
#include<stdio.h>
main()
{
int a[5][5],b[5][5],c[5][5];
int i,j,k,m,n,p,q;
//enter the order of matrix
printf("\nEnter the order of first matrix.\n");
scanf("%d%d",&m,&n);
printf("\nEnter the order of second matrix.\n");
scanf("%d%d",&p,&q);
if (n!=p)
{
printf("\n\n****WARNING**** CAN NOT MULTIPLY ");
printf("IF DIFFER THE MATHES RULE, so carefully give the order.\n\n");
//abort();
}
//data for first matrix
printf("Enter the value for first matrix.\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
//data for second matrix
printf("Enter the value for second matrix.\n");
for(j=0;j<m;j++)
{
for(k=0;k<n;k++)
{
scanf("%d",&b[j][k]);
}
}
//mutiply the matrices
for(i=0;i<m;i++)
{
for(k=0;k<q;k++)
{
c[i][k]=0;
for(j=0;j<n;j++)
{
c[i][k]=c[i][k]+(a[i][j]*b[j][k]);
}
}
}
//printing the matrix.
printf("The product matrix is :\n");
for(i=0;i<m;i++)
{
printf("\n");
for(k=0;k<q;k++)
{
printf("%d\t",c[i][k]);
}
}
getch();
}
Comments
Post a Comment