Need help with C++ HW. In the main function please print out the matrix done by
ID: 3700039 • Letter: N
Question
Need help with C++ HW. In the main function please print out the matrix done by different operations!
For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C++ operators to achieve matrix multiplication. 1. [10pts] Create a custom class called Matrix a. Private Members i. int rows; ii. int cols; ii. double* data; b. Provide NO default constructor c. Constructor i. Matrix(int,int) takes rows and cols ii. Inside constructor allocate and initialize data d. Destructor i. Matrix( ii. Inside destructor delete memory allocation of data completely 2. [10pts] Overload Copy constructor 3. [10pts] Overload a. For input, user inputs the values of the matrix in row then column format i. HINT: using a space you can separate column data and enter for row b. For output, use tab and return line to create a matrix-like output 4. [10pts] Overload the assignment operator () 5. [20pts] Overload +), (+), (), and ( operators and (++), () for post and pre type 6. [30pts] Overload the () operator to perform matrix multiplication. a. Check the dimensions of both matrices to ensure proper dimensions, output an error on cerr and exit(1) if dimensions are invalid. b. Must be able to work on any 2 valid matrices. 7. [10pts] Program Correctness a. Prompt user for number rows and columns for two matrices. b. Prompt the user to enter data for these matrices.Explanation / Answer
Solution:
part1 to 5 is done, please repost others.
code:
#include <iostream>
using namespace std;
class Matrix //class matrix
{
private: //private variables
int row, col;
double *m[10];
public:
Matrix(int r , int c) //constructor for row col and matrix initialization
{
row=r;
col=c;
for(int i=0;i<row;i++)
m[i]= new double[col];
}
/* Matrix(Matrix const &ma);*/
friend ostream & operator << (ostream &out, const Matrix &c); //output stream function
friend istream & operator >> (istream &in, Matrix &c); //input stream function
void operator=(Matrix &x); //operator overloding functions =
void operator+=(Matrix &x); //operator overloding functions +=
void operator-=(Matrix &x); //operator overloding functions -=
void operator--(); //operator overloding functions --
void operator++(); //operator overloding functions ++
};
void Matrix :: operator=(Matrix &x) //operator overloding functions = defination
{
for(int i=0;i<x.row;i++)
for(int j=0;j<x.col;j++)
m[i][j]=x.m[i][j];
}
void Matrix :: operator++() //operator overloding functions ++ defination
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]++;
}
void Matrix :: operator--()//operator overloding functions -- defination
{
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]--;
}
void Matrix ::operator+=(Matrix &x) //operator overloding functions += defination
{
for(int i=0;i<x.row;i++)
for(int j=0;j<x.col;j++)
m[i][j]+=x.m[i][j];
}
void Matrix ::operator-=(Matrix &x) //operator overloding functions -= defination
{
for(int i=0;i<x.row;i++)
for(int j=0;j<x.col;j++)
m[i][j]-=x.m[i][j];
}
/*Matrix::Matrix(Matrix const &ma)
{
row=ma.row;
col=ma.col;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]=ma.m[i][j];
} */
ostream & operator << (ostream &out, const Matrix &c) //operator overloding functions << defination
{
for(int i=0;i<c.row;i++)
{
for(int j=0;j<c.col;j++)
{
out << c.m[i][j];
cout<<" ";
}
cout<<endl;
}
return out;
}
istream & operator >> (istream &in, Matrix &c)//operator overloding functions >> defination
{
cout << "Matrix elements";
for(int i=0;i<c.row;i++)
for(int j=0;j<c.col;j++)
in >> c.m[i][j];
return in;
}
int main() //main program
{
Matrix A(3,3),B(A),C(A),D(A); //object declaration and call copy constructor
cin >> A; //take input through ifstream
cout << "Given Matrix is "<<endl;
cout << A; //display output through ofstream
B=A; //= operator over loading
C=A;
D=A;
cout<<"copy object is "<<endl;
cout<<B;
B+=A; //+= operator loading
cout<<"after apply += matrix is "<<endl;
cout<<B;
cout<<"After apply -= matrix is "<<endl;
C-=D; //-= operator loading
cout<<D;
++D; //++ operator loading
cout<<"after applying ++ operator"<<endl;
cout<<D;
--D; //-- operator loading
cout<<"after applying -- operator"<<endl;
cout<<D;
return 0;
}
part 7:
#include <iostream>
using namespace std;
/* You can use this function for operator overloading in your class
istream& operator>>(istream& in,const int data)
{
in>>data;
return in;
}
*/
int main() {
// char errorString[]="Download failed...";
// cerr << "Error message" <<errorString<< endl; // prints !!!Hello World!!!
int m1,m2,n1,n2;//dimension of matrix A,B
cout<<">>Enter dimension of first matrix"<<endl;
cin>>m1>>n1;
cout<<">>Enter dimension of second matrix"<<endl;
cin>>m2>>n2;
cout<<"Enter "<<m1<<"*"<<n1<<"matrix"<<endl;
int A[m1][n1];
int B[m2][n2];
for(int i=0;i<m1;i++)
for(int j=0;j<n1;j++)
cin>>A[i][j];
cout<<"Enter "<<m2<<"*"<<n2<<"matrix"<<endl;
for(int i=0;i<m2;i++)
for(int j=0;j<n2;j++)
cin>>B[i][j];
cout<<"Enter any choice below"<<endl; //you need to call a function for each choice
cout<<">> 1 -Addition//+,+="<<endl
<<">> 2 -Subtract//-,-="<<endl
<<">> 3 -Multiply//* opt"<<endl
<<">> 4 -Increment each cell"<<endl //Enter your respective input choice in particular function
<<">> 5 -Show both MAtrix"<<endl
<<">> 0 -Exit"<<endl;
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)