To write a simple C program to read two matrices A(MxN) and B(PxQ) and compute the product A and B after checking compatibility for multiplication.Output the input matrices and the resultant matrix with suitable headings and format.
#include<stdio.h>
main()
{
int a[100][100],b[100][100],c[100][100],m,n,p,q,i,j,k;
printf("Enter the order of the matrix A and B\n");
scanf("%d%d%d%d",&m,&n,&p,&q);
if(n==p)
{
printf("Enter the elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
c[i][j] += a[i][k]+b[k][j];
}
}
}
printf("The original matrices are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("Resultant matrix is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t"c[i][j]);
}
printf("\n");
}
} //end of if statement
else
printf("multiplication not possible since n is not equal to p\n");
}//end of program
#include<stdio.h>
main()
{
int a[100][100],b[100][100],c[100][100],m,n,p,q,i,j,k;
printf("Enter the order of the matrix A and B\n");
scanf("%d%d%d%d",&m,&n,&p,&q);
if(n==p)
{
printf("Enter the elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
c[i][j] += a[i][k]+b[k][j];
}
}
}
printf("The original matrices are\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("Resultant matrix is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t"c[i][j]);
}
printf("\n");
}
} //end of if statement
else
printf("multiplication not possible since n is not equal to p\n");
}//end of program
No comments:
Post a Comment