I have been given the following code in a test.cpp file to implement:
cout << "Case 2: the non-static Transpose function" << endl;
{
double column[4] = {2, 1, 0, -1};
double row[3] = {2, 0, -1};
Matrix matrix = Matrix::Toeplitz(column, 4, row, 3);
cout << "The original Matrix = " << endl;
cout << matrix << endl; //This part of the code works
matrix.Transpose(); //How do I implement this?
cout << "The transposed version = " << endl;
cout << matrix << endl;
cout << "Press any key to continue ..." << flush;
system("read");
cout << endl;
}
The way Matrix::Toeplitz(column, 4, row, 3) works is as follows:
Matrix Matrix::Toeplitz(const double* column, const int noOfRows, const double* row, const int noOfColumns){
Matrix outT(column, noOfRows, row, noOfColumns);
return outT;
}
So how would I implement matrix.Transpose()? My code so far is as follows:
Matrix& Matrix::Transpose () {
double newrow[noOfRows];
for(int i=0; i<noOfRows; i++){
int index = GetIndex(i,0);
newrow[i] = data[index];
}
double newcol[noOfColumns];
for(int i=0; i<noOfColumns; i++){
int index = GetIndex(0,i);
newcol[i] = data[index];
}
Matrix outT(newcol, noOfColumns, newrow, noOfRows);
}
This has no effect on cout<<matrix<<endl;
I was thinking that Matrix outT(newcol, noOfColumns, newrow, noOfRows);
should give new information (i.e. switching the column and row arrays) to the matrix object when implementing matrix.Transpose but it hasn't been working.
Is this the correct format Matrix& Matrix::Transpose () for implementing matrix.Transpose()?