Social Icons

Sunday, 16 February 2014

Program to multiply two matrices

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
cout<<"\n Enter rows and columns of first matrix : ";
cin>>m>>n;
cout<<"\n Enter rows and columns of second matrix : ";
cin>>p>>q;

if(n==p)
{
cout<<"\n Enter first matrix (in matrix form)\n";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
cin>>a[i][j];

cout<<"\n Enter second matrix (in matrix form)\n";
for(i=0;i<p;++i)
for(j=0;j<q;++j)
cin>>b[i][j];

cout<<"\n Matrix after multiplaction\n";

for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
else
cout<<"\n Matrix multiplication can't be done...";
getch();
}

                                                            Output

No comments:

Post a Comment