Tuesday 26 August 2014

Matrix Multiplication.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[5][5],b[5][5],d[5][5];
int r,c;
cout<<"Enter the rows and col. of matrix:-"<<endl;
cin>>r;
cin>>c;
cout<<"Enter the values in Ist matrix:-"<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the values in IInd matrix:-"<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"Matrices are:-"<<endl;
cout<<"Ist:-"<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"IInd:-"<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
cout<<"Multiplication of the matices is:-"<<endl;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
d[i][j]=0;
for(int k=0;k<c;k++)
{
d[i][j]=d[i][j]+a[i][k]*b[k][j];
}
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<d[i][j]<<" ";
}
cout<<endl;
}
getch();
return 0;
}


No comments:

Post a Comment