Design a program to perform matrix multiplication. The product C of two matrices
ID: 655306 • Letter: D
Question
Design a program to perform matrix multiplication. The product C of two matrices A and B is defined as
Ci k = Aij Bjk,
where j is summed over for all possible values of i and k
In order for matrix multiplication to be defined, the dimensions of the matrices must satisfy
(n*m)(m*p)=(n*p),
where (a*b) denotes a matrix with a rows and b columns. Writing out the product explicitly,
where
C11= a11b11+a12b21+.....+a1mbm1
C12=a11b12 + a12b22 + .... + a1mbm2
For example,
Requirements:
Your program needs to prompt the user for input file directory and file name.
Your program needs to be able to handle up to 6 by 6 matrix calculation.
Your program needs to determine if the two input matrices satisfy the condition expressed in equation (2), don
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
class Matrix
{
int a[6][6];
int b[6][6];
int c[6][6];
int i,j,k;
int m,n,x,y;
public:
void Mult();
void Input_Matrix();
void Output_Matrix();
};
void Matrix::Input_Matrix()
{
cout<<" Enter size og matrix 1: rows and columns";
cin>>m>>n;
cout << "Enter the values for the first matrix";
cout << " Matrix 1:";
for (i=0; i<m; i++)
{
for (j=0; j<n; j++)
{
cin >> a[i][j];
}
}
cout<<" Enter size og matrix2: rows and columns";
cin>>x>>y;
cout << "Enter the values for the second matrix";
for (i=0; i<x; i++)
{
for (j=0; j<y; j++)
{
cin >> b[i][j];
}
}
if(m != y && n != x)
{
cout<<"The two input matrices cannot be multiplied";
}
elseif(m > 6 || n > 6 || x > 6 || y > 6)
{
cout<<"The input matrices are larger than 6X6";
}
else
{
cout<<" Please wait while processing....";
}
}
void Matrix::Mult()
{
for (i=0; i<m; i++)
{
for (j=0; j<y; j++)
{
c[i][j]=0;
for (k=0; k<6; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
void Matrix::Output_Matrix()
{
cout << "The Resultant Matrix is: ";
for (i=0; i<m; i++)
{
for (j=0; j<y; j++)
{
cout << c[i][j];
}
cout << endl;
}
}
int main()
{
Matrix x;
x.Input_Matrix();
x.Mult();
x.Output_Matrix();
system ("pause");
ofstream ofs( "result.txt" ) ;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.