Saturday 5 July 2014

Matrix Multiplication


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[10][10],b[10][10],c[10][10],row1,row2,col1,col2,k;

cout<<"Enter row size & column size of the first matrix:";
cin>>row1>>col1;
cout<<"Enter the elements for"<<row1<<"*"<<col1<<"matrix:";
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the row size & column size of second matrix:";
cin>>row2>>col2;
if(col1==row2)
{
cout<<"Enter the elements for"<<row2<<"*"<<col2<<"matrix:";
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
cin>>b[i][j];
}
}
}
else
{
cout<<"multiplication is not possible";
}

for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
c[i][j]=0;
for(k=0;k<row2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

cout<<"Resultant matrix is:";
for(i=0;i<row1;i++)
{
cout<<"\n";
for(j=0;j<col2;j++)
{
cout<<"\t"<<c[i][j];
}
}
getch();

}

No comments:

Post a Comment