In: Computer Science
write a c++ program.
Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.
There should be two public methods within the class definition namely:
Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
Screenshot :-
Code:-
#include <iostream>
using namespace std;
class Matrix
{
int m[3][3];
public:
void setMatrixValue(int i, int j)
{
cout<<"\nEnter the value: ";
cin>>m[i][j];
}
int getMatrixValue(int i, int j)
{
return m[i][j];
}
};
void CrossProduct(Matrix m1, Matrix m2)
{
int i,j,k;
int result[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
result[i][j]=0;
}
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
for(k = 0; k < 3; k++)
{
result[i][j] += m1.getMatrixValue(i,k) *
m2.getMatrixValue(k,j);
}
}
}
cout<<"\n";
cout<<"The Matrix multiplication is: \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<result[i][j]<<"\t";
cout<<"\n";
}
}
int main()
{
int i,j;
Matrix m1,m2;
cout<<"Enter first matrix:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m1.setMatrixValue(i,j);
}
cout<<"\nEnter Second matrix:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
m2.setMatrixValue(i,j);
}
CrossProduct(m1,m2);
return 0;
}