Project 1: Matrix manipulation implementing overloaded operators using arrays Wr
ID: 3735404 • Letter: P
Question
Project 1: Matrix manipulation implementing overloaded operators using arrays Write a C++ program to declare a class called Matrix. The private data of the class are Row, Column, and matrix. The Matrix class should at least have the following member functions: . Constructor to initialize the content of the matrix to zero 5. Read Matrix Overloaded operator plus to add two matrices Overloaded operator minus to subtract two matrices Overloaded operator multiplication to multiply two matrices Overloaded operator division to divide two matrices Find transpose of each matrix Display a matrix Destructor In the main function, do as follows: Declare object Matrix A; Declare object Matrix B; Declare object Matrix_Add; Declare object Matrix_Dif; Declare object Matrix Mul; Declare object Matrix_Div; Call Matrix_A.Read_MatrixO Call Matrix B.Read_Matrixo Call overloaded operator+0 to add two matrices and store the result in Matix_AddExplanation / Answer
#include<iostream>
using namespace std;
#define s 20
class Matrix
{
private:
int a[s][s],row,column;
public:
void Read_Matrix();
void Display_Matrix();
Matrix operator+(Matrix);
Matrix operator-(Matrix);
Matrix operator*(Matrix);
Matrix transpose();
};
void Matrix::Read_Matrix()
{
cout<<"Enter the order of matrix "<<" : ";
cin>>row>>column;
cout<<"Enter the matrix "<<" : ";
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
cin>>a[i][j];
}
void Matrix::Display_Matrix()
{
for(int i=0;i<row;i++)
{
cout<<" ";
for(int j=0;j<column;j++)
cout<<a[i][j]<<" ";
}
cout<<" ";
}
Matrix Matrix::operator+(Matrix b)
{
Matrix r;
if((row!=b.row)||(column!=b.column))
{
cout<<" Matrix Addition is not possible the result is incorrect ";
r.row=0;
r.column=0;
}
else
{
r.row=row;
r.column=column;
}
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
r.a[i][j]=a[i][j]+b.a[i][j];
return r;
}
Matrix Matrix::operator-(Matrix b)
{
Matrix r;
if((row!=b.row)||(column!=b.column))
{
cout<<" Matrix subtraction is not possible the result is incorrect ";
r.row=0;
r.column=0;
}
else
{
r.row=row;
r.column=column;
}
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
r.a[i][j]=a[i][j]-b.a[i][j];
return r;
}
Matrix Matrix::operator*(Matrix b)
{
Matrix r;
if((row!=b.column)||(column!=b.row))
{
cout<<" Matrix Multiplication is not possible the result is incorrect ";
r.row=0;
r.column=0;
}
else
{
r.row=row;
r.column=b.column;
}
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
r.a[i][j]=0;
for(int i=0;i<row;i++)
for(int j=0;j<b.column;j++)
for(int k=0;(k<column)||(k<b.row);k++)
r.a[i][j]+=a[i][k]*b.a[k][j];
return r;
}
Matrix Matrix::transpose()
{
Matrix r;
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
r.a[i][j]=a[j][i];
r.row=row;
r.column=column;
for(int i=0;i<row;i++)
for(int j=0;j<column;j++)
a[i][j]=r.a[j][i];
}
int main()
{
Matrix Matrix_A;
Matrix Matrix_B;
Matrix Matrix_Add;
Matrix Matrix_Dif;
Matrix Matrix_Mul;
Matrix Transpose_A;
Matrix Transpose_B;
Matrix_A.Read_Matrix();
Matrix_B.Read_Matrix();
Matrix_Add = Matrix_A + Matrix_B;
Matrix_Dif = Matrix_A - Matrix_B;
Matrix_Mul = Matrix_A * Matrix_B;
Matrix_A.Display_Matrix();
Matrix_B.Display_Matrix();
Matrix_Add.Display_Matrix();
Matrix_Dif.Display_Matrix();
Matrix_Mul.Display_Matrix();
Transpose_A.Display_Matrix();
Transpose_B.Display_Matrix();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.