Tuesday 17 January 2012

multiplication of matrices


#include<iostream.h>
#include<conio.h>
void main()
{
int mat1[3][3],mat2[3][3],mat3[3][3],i,j,k;
clrscr();
cout<<"Enter the elements in the first matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter the elements in the second matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat2[i][j];
}
}
//loop to multiply
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat3[i][j]=0;
for(k=0;k<3;k++)
{
mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]);
}
}
}
//loop to display the final matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat3[i][j]<<"\t";
}
cout<<endl;
}
getch();
}

1 comment: