java - 2D Array Matrix; Reading Diagonals -


i attempting write method 5x5 matrix gives output: 00004 00030 00200 01000 00000

my output specific code is: 40000 03000 00200 00010 00000

essentially attempting read array right left rather left right. parameters can use single loop , cannot hardcode values within method.

is there way can make simple change accomplish this?

 private static void filldiagonal_2( int[][] m )  {             (int row=0; row<m.length; ++row)             {                    m[row][row]=row;                 system.out.print(m[row][row]);             }    }     

for (int row = 0; row < m.length; ++row) {        m[row][m.length - row] = row;     system.out.print(m[row][row]); } 

that should work.

edit: should declare variable outside of loop don't recall m.length every time.

int length = m.length - 1;     (int row = 0; row <= length; ++row)    {         m[row][length - row] = row;      system.out.print(m[row][row]);    } 

try that.


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -