Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help me with this assignment? Problem 1: Design, implement and test t

ID: 3811081 • Letter: C

Question

Can anyone help me with this assignment?

Problem 1: Design, implement and test the Matrix class. The elements of Matrix should be of the integer type. Your Matrix class should contain at least the following features, namely:

A default constructor that sets the matrix elements to zero.

A copy constructor

A destructor

The get and set functions

Overloaded functions for as many relevant C++ built-in operators as you can (the number of overloaded operators will be one of the factors to be used in determining your grade in this mini-project). This should include the overloading of operators such as matrix negation “-“ (a unary operator that multiplies each element of a matrix with -1), matrix pre-increment “++”, matrix pre-decrement “--“, matrix assignment “=”, matrix addition “+” (adds two matrices to generate a sum matrix, matrix subtraction “-“, matrix multiplication “*” (vector multiplication),   matrix “+=”, matrix equality “= =” and matrix inequality “!=”

Overload the operator( ) to perform the double scripting operations. For example, in a 3-by-5 Matrix called A, the programmer could write A (1, 3) to access the element at row 1 and column 3. There should be two versions of operator ( ), one that returns int & and one that returns const int &.

Overload the insertion “<<” operator (to output the entire Matrix in a row-major format) and the extraction “>>” (to input an entire matrix in a row-major format).

Make sure that the implemented operators should be able to cascade

Explanation / Answer

Here is the code for you:

#include <iostream>
#include<stdlib.h>
using namespace std;
class myMatrix
{
private:
int row,col,**a;
public:
myMatrix(int, int, string);
myMatrix(int,int);
//member function
friend ostream& operator<<(ostream& os, const myMatrix& m);
void getmatrix();
myMatrix operator+ (const myMatrix& m);
myMatrix operator- (const myMatrix& m);
//matrix operator* (const matrix& s);
};

//constructor
myMatrix::myMatrix(int r, int c)
{
row = r;
col = c;
a= new int*[row];
for(int i = 0; i < row; ++i)
{
a[i] = new int[col];
for(int j = 0; j < col; j++)
a[i][j] = 0;
}
}
myMatrix::myMatrix(int r, int c, string type)
{
row = r;
col = c;
a= new int*[row];
bool ones;
if(type.compare("allOne") == 0)
> for(int i = 0; i < row; ++i)
{
a[i] = new int[col];
for(int j = 0; j < col; j++)
if(ones)
a[i][j] = 1;
else
a[i][j] = random() % 10;
}
}
//overloading operator +
myMatrix myMatrix::operator+(const myMatrix& m)
{
myMatrix c (row,col);
if((row == m.row)&&(col == m.col))
{
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
c.a[i][j] = a[i][j]+m.a[i][j];
}
else
{
// this is the only thing that comes out for some reason
cout << "Order of two matrixes should be same" << endl;
exit(0);
}
return c;
}
//overloading operator -
myMatrix myMatrix::operator-(const myMatrix& m)
{
myMatrix c(row,col);
if((row == m.row) && (col == m.col))
{
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
c.a[i][j] = a[i][j]-m.a[i][j];
}
else
{
cout << "Order of two matrixes should be same" << endl;
exit(0);
}
return c;
}
//overloading operator *
/*myMatrix myMatrix::operator*(const myMatrix& m)
{
myMatrix c(row,col);
if (col == m.row)
{
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
{
for(int k=0; k<m.col; k++)
c.a[i][j] += a[i][k] * m.a[k][j];
}
}
else
{
cout << "Multiplication not possible, enter matrix of order m(pXq) and n(qxr) " << endl;
exit(0);
}
return c;
}*/
//overloading ostream operator
ostream& operator<<(ostream& os, const myMatrix& m)
{
for(int r = 0; r<m.row; r++)
{
os << endl;
for (int c=0; c<m.col; c++)
{
os << m.a[r][c];
os << " ";
}
}
return os;
}
//main method
/*int main()
{
int r,p,x,y,c;// variable
//display menu
cout << "1 -->Addition" << endl << "2-->Subtraction" << endl << "3-->Multiplication" << endl << "4-->Exit" << endl;
//inputting choice
cout << "Enter your choice " << endl;
cin >> c;
//inputting number of rows columns for matrix1
cout << "enter the number of rows and columns for matrix1 " << endl;
cin >> r >> p;
//inputting number of rows columns for matrix2
cout << "enter the number of rows and columns for matrix2 " << endl;
cin >> x >> y;
//function calls
matrix d(r,p);
matrix e(x,y);
matrix f(r,p);
matrix s(r,p);
matrix m(r,y);
switch(c)
{
case 1:
//input 2 matrix
d.getmatrix();
e.getmatrix();
//operator overloading
f = d+e;
cout << "First matrix: " << d;
cout << "Second matrix: " << e;
cout << "Resultant matrix: " << f;
break;
case 2:
//input 2 matrix
d.getmatrix();
e.getmatrix();
//operator overloading
s = d-e;
cout << "First matrix: " << d;
cout << "Second matrix: " << e;
cout << "Resultant matrix: " <<s;
break;
case 3:
//input 2 matrix
d.getmatrix();
e.getmatrix();
//operator overloading
m = d*e;
cout << "First matrix: " << d;
cout << "Second matrix: " << e;
cout << "Resultant matrix: " << m;
break;
default:
cout << "Invalid Choice" << endl;
break;
}// end of case
return 0;
}*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote