Maximum element of secondary diagonal of a matrix

This loop is supposed to find the maximum element of a secondary diagonal of a 4X4 matrix. But I can't really understand it fully. Why is i=0 and j=2? Is there an easier way of looping through the secondary diagonal?

1
2
3
4
5
6
7
  for (int i = 1, j = 2; i < 4, j >= 0; i++, j--)
{
  if (max < a[i][j])
  {
     max = a[i][j];
  }
}
Last edited on
An instance of the such matrix:
2 5 3 9
1 4 3 7
3 9 6 2
2 3 5 4

i - indicator of rows.
j - indicator of columns.

For example, the first position of loop-counter (P) [according your loop]:
2 P 3 9
1 4 3 7
3 9 6 2
2 3 5 4

The second position etc.
2 5 3 9
P 4 3 7
3 9 6 2
2 3 5 4

The right loop will be:
1
2
3
4
5
6
7
8
9
max = [1][4];

for (int i = 1, j = 4; i < 4, j >= 0; i++, j--)
{
  if (max < a[i][j])
  {
     max = a[i][j];
  }
}

And better will be:
1
2
3
4
5
6
7
8
9
10
max = [1][4];

for(int i = 1; i < 4; i++) {
		for(j = 4; j >= 0; j--) {
 if (max < a[i][j])
  {
     max = a[i][j];
   }
 }
}

(If you mean "secondary diagonal" in the following sense: http://www.thefreedictionary.com/secondary+diagonal)
Last edited on
Topic archived. No new replies allowed.