Sum of diagonal in matrix

How to find the sum of the main diagonal in matrix.
For ex. I need to find the sum of 1 2 and 3 in this matrix:

1 5 6
5 2 8
4 9 3

i found the sum of the whole matrix in this way

for (i=0; i<m; i++) {
for (j=0; j<n; j++)
sum+=x[i][j];
}
Well, for that diagonal, you should note the following:
Sum up the digits that have their "x" and their "y" equal.

If you want to do the other diagonal too, think about this:
Sum up the digits that have their "x" and "y" sum up to the length of the square - 1.
( x + y = s )
( y = s - x )
Note what Kyon said. You just need to add which pairs have the same i and j. I'll give you a cool hint, you only need to use one loop and the items you'll add loop like this:
x[i][i]
Topic archived. No new replies allowed.