u

Friday 14 September 2012

C++ Program to print the product of two matrices

C++ Program to print the product of two matrices


#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int i,j,a[3][4],b[4][3],c[3][3];
//Creation of matrix A[3][4]
cout<<"Enter elements of array A"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter elements of array B"<<endl;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}
// storing product in array C
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
c[i][j]=0;
for(int k=0;k<4;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
cout<<endl<<"The matrix A is "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<"The matrix B is "<<endl;
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<"The matrix C is "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}

getch();
return 0;
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More