// Write a program to find the product of two compatible matrices
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],p[5][5],i,j,k,r1,c1,r2,c2;
clrscr();
cout<<"Enter the order of the first matrix:\n";
cin>>r1>>c1;
cout<<"Enter the order of the second matrix:\n";
cin>>r2>>c2;
if(r1==c2)
{
cout<<"Enter the elements of the first matrix:\n";
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
cin>>a[i][j];
cout<<"Enter the elements of the second matrix:\n";
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
cin>>b[i][j];
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
p[i][j]=0;
for(k=0;k<c1;k++)
p[i][j]=p[i][j]+a[i][k]*b[k][j];
}
cout<<"The resultant matrix is:\n";
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
cout<<" "<<p[i][j];
cout<<endl;
}
}
else
cout<<"Matrices are not compatible";
getch();
}

No comments: