|
|
Re: Transpose matrix
|
Posted: Nov 14, 2005 7:38 AM
|
|
Well, you described the error yourself: You have a different number of colums and rows.
Example: M = new int [4][8]
You cannot access M [7][3], because you only have 4 elements.
In your example you create M = new int [columns][rows]
But you access it like M[rowIndex][columnIndex].
Instead of exchanging 2 fields in the original matrix, create a new matrix and put the values directly in there reading the old matrix.
Also you made the error of assigning matrix = array; This way you don't copy the values, but accessing matrix you also change array.
Therefore:
array = new int[rows][columns]; //fill with values matrix = new int[columns][rows]; for (int iR = 0; iR < rows; iR++) { for (int iC = 0; iC > columns; iC++) { matrix[iC][iR] = array[iR][iC]; } }
|
|