Question: Please update my code to answer exercise2. This is the answer for exercise 1: The code: #include using namespace std; template class Matrix { private:

 Please update my code to answer exercise2. This is the answer

for exercise 1: The code: #include using namespace std; template class Matrix

Please update my code to answer exercise2.

This is the answer for exercise 1:

The code:

#include using namespace std; template class Matrix { private: T** arr; int row; int col; public:

Matrix(int r = 4, int c = 4) :row(r), col(c) {

arr = new T * [row]; for (int i = 0; i

} Matrix(const Matrix& rhs) { row = rhs.row; col = rhs.col;

arr = new T * [row];

for (int i = 0; i

for (int i = 0; i

}

Matrix operator=(const Matrix& rhs) {

row = rhs.row; col = rhs.col; arr = new T * [row]; for (int i = 0; i

arr[i][j] = rhs.arr[i][j]; } } return *this;

} bool operator == (const Matrix& rhs) { if (row != rhs.row || col != rhs.col) return false; for (int i = 0; i

} Matrix operator += (const Matrix& rhs) { if (row != rhs.row) exit(1); if (col != rhs.col) exit(2); for (int i = 0; i

}

Matrix operator -= (const Matrix& rhs) {

if (row != rhs.row) exit(1); if (col != rhs.col) exit(2); for (int i = 0; i

friend Matrix operator + (const Matrix& m1, const Matrix& m2) { if (m1.row != m2.row) { cout

}

friend Matrix operator - (const Matrix& m1, const Matrix& m2) { if (m1.row != m2.row) { cout

}

friend ostream& operator

friend istream& operator>>(istream& in, const Matrix& rhs) { int value; cout > value; rhs.arr[i][j] = value;

} } return in;

}

T& operator () (const int r_index, const int c_index) {

if (r_index >= 0 && c_index >= 0 && r_index

}

const T& operator () (const int r_index, const int c_index) const { if (r_index >= 0 && c_index >= 0 && r_index

};

int main() {

Matrix a(2, 2), b(2, 2); cout > a; cout > b; cout d = a; cout e; e = b; cout

//Testing addition + and subtraction - operators. //Testing addition assignment += and subtraction assignment - = operators. cout

//Testing comparison operator = cout

//Testing operator () cout > index1; cout > index2; cout > value; a(index1, index2) = value; cout

cout

//Ex2 Bonus

return 0; }

Exercise 1: Create a template class Matrix that has two-dimensional array of any number of rows (with a default of 4 rows) and any number of columns (with a default of 4 columns). Matrix should make use of a standard C++, dynamically allocated two-dimensional array to store its elements. Design, implement and test the Matrix class. Your class should contain at least the following features, namely: A default constructor and a destructor. A copy constructor and an assignment operator =". A comparison operator "=". Matrix addition "+" and subtraction - operators. Matrix addition assignment =" and subtraction assignment- =" operators. Matrix insertion ">" operators (using row-major format). operator () to provide indexing. 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 & so an element of a Matrix can be used as an lvalue and one that returns const int & so an element of a Matrix can be used as an rvalue. Two versions of operator ++: post and pre increment operators that adds 1 to every element of the matrix. Note: You must create an array of pointers to implement Matrix. Class definition and driver is provided in Lab1.cpp file. Bonus and optional Exercise 2: Matrix A represents the number of wins and losses for three teams in one year and B represents the number of wins and losses for the next year. Lions Tigers Bears W L 5 8 94 = A 7 6 Lions Tigers Bears W L 7 5 6 6 4 8 =B Create matrices A and B using the Matrix class and initialize them with respective number of wins and losses. Print the teams' records for the two years combined. If the three seasons record for these teams is represented by C, Lions Tigers Bears W L 20 19 22 17=C 16 23 How many games did each team win and lose in the third year? Calculate and print the average of total wins and losses. Use () operator and update matrix class as required

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!