Imagine that we are writing a Matrix class that stores a 2D array. Our class has 3 data members: double* arr (a "flattened" array storing all of the elements in the matrix), unsigned int nrows (number of rows), and unsigned int ncols (number of columns).

Implement a print function for Matrix objects that works with cout. E.g.,

Matrix M(...);

cout << M << endl;

//ostream& operator<<(ostream& out, const Matrix& M) { ... }

Your print function should separate elements on the same row with spaces, and each row should end with a newline. For example, if we printed a 3x3 identity matrix (1 in (1, 1), (2, 2), (3, 3) and 0 everywhere else), it should print as:

1 0 0

0 1 0

0 0 1

You may print an extra space at the end of each row if it makes your code simpler. You can assume that your function has access to the private data members of Matrix, and it should not call any Matrix member functions

This assignment has been answered **3 times** in private sessions.

© 2021 Codify Tutor. All rights reserved