Problem Session Working in pairs of two, solve the following problem...

advertisement
Problem Session
Working in pairs of two,
solve the following problem...
Problem
We’ve seen how to derive a Matrix from vector<Row>.
If a matrix is as follows:
then the transpose of that
matrix is as follows:
1
2
3
4
5
6
7
8
9
1
4
7
2
5
8
3
6
9
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
{
Matrix result(myColumns, myRows);
// I’m m-by-n
// 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;
}
Download