40 likes | 159 Views
Problem Session. Working in pairs of two, solve the following problem. 1. 1. 2. 4. 3. 7. 4. 2. 5. 5. 6. 8. 7. 3. 6. 8. 9. 9. Problem. If a matrix is as follows:. We’ve seen how to derive a Matrix from vector<Row>. then the transpose of that matrix is as follows:.
E N D
Problem Session Working in pairs of two, solve the following problem...
1 1 2 4 3 7 4 2 5 5 6 8 7 3 6 8 9 9 Problem If a matrix is as follows: We’ve seen how to derive a Matrix from vector<Row>. then the transpose of that matrix is as follows: Intuitively, the transpose operation interchanges the rows and columns of a matrix. Write a function member Matrix::Transpose() that returns the transpose of the Matrix receiving the message.
Coding: Interface // Matrix.h // Directives have been omitted ... typedef vector<double> Row; class Matrix : public vector<Row> { public: Matrix(); Matrix(int rows, int columns); int Rows() const; int Columns() const; Matrix operator+(const Matrix & mat2); Matrix Transpose() const; void Read(istream & in); void Print(ostream & out) const; friend istream & operator>>(istream & in, Matrix & mat); friend ostream & operator<<(ostream & out, const Matrix & mat); // ...
Coding: Definition // Matrix.cpp // ... Matrix Matrix::Transpose() const // I’m m-by-n { Matrix result(myColumns, myRows); // it’s n-by-m for (int r = 0; r < myRows; r++) // for row r for (int c = 0; c < myColumns; c++) // for col c result[c][r] = (*this)[r][c]; // set result return result; }