Multiply 2 matrices
PROBLEM :
For 2 given matrix of size 3*3 multiply them and print the matrix.
Input:
The first line contains an integer 'T' denoting the total number of test cases.
In each test cases, it consists of six lines and each line has three integers. First three line denoted the first matrix and then next three line denotes the second matrix.
Output:
Print three lines and in each line three integers which is resultant matrix after multiplication of 2 matrices.
Constraints:
1<=T<=30
1<=Number<=100
Example:
Input:
1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Output:
3 3 3
3 3 3
3 3 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t ,i,j,k,sum ;
int a[100][100],b[100][100],c[100][100];
cin>>t ;
while(t--)
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j] ;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j] ;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0 ;
for(k=0;k<3;k++)
sum=sum+a[i][k]*b[k][j] ;
c[i][j]=sum ;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<c[i][j]<<" " ;
cout<<endl ;
}
}
return 0;
}
---------------------------------------------------------------------------------
For 2 given matrix of size 3*3 multiply them and print the matrix.
Input:
The first line contains an integer 'T' denoting the total number of test cases.
In each test cases, it consists of six lines and each line has three integers. First three line denoted the first matrix and then next three line denotes the second matrix.
Output:
Print three lines and in each line three integers which is resultant matrix after multiplication of 2 matrices.
Constraints:
1<=T<=30
1<=Number<=100
Example:
Input:
1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
Output:
3 3 3
3 3 3
3 3 3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int t ,i,j,k,sum ;
int a[100][100],b[100][100],c[100][100];
cin>>t ;
while(t--)
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>a[i][j] ;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>b[i][j] ;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0 ;
for(k=0;k<3;k++)
sum=sum+a[i][k]*b[k][j] ;
c[i][j]=sum ;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<c[i][j]<<" " ;
cout<<endl ;
}
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment