java - Transpose a matrix -
hey assignment we're working class , clients. class transpose method isn't printing matrix way should be. here method:
public matrix transpose(){ matrix a= this; for(int r=0; r<size;r++){ for(int c=0;c<size;c++){ a.table[r][c]=this.table[c][r]; } } return a; }
and here client calling method:
case 5: first.init(low,up); system.out.println("the original matrix is:"); first.print(); result=first.transpose(); system.out.println("the resulting matrix is:"); result.print(); break;
and print method if need see it:
public void print(){ for(int r=0;r<size;r++){ for(int c=0;c<size;c++) system.out.printf("%5d",table[r][c]); system.out.println(); } }
but output isn't right:
the original matrix is: 9 7 1 7 1 1 5 5 7 resulting matrix is: 9 7 5 7 1 5 5 5 7
the rows , columns aren't printing right numbers after call. fix great!
the problem in transpose method, both matrix , refer same object. can try
1. matrix a= new matrix(...);//... since depends on constructor
or
you can loop lower triangle of matrix , swap element [i][j] [j][i] since 'trying to' swap twice.
so make sure don't swap twice , swapping correctly
Comments
Post a Comment