Adding diagonal in multi-array

Quote:

>Subject: Adding diagonal in multi-array

>Date: 13 Feb 1995 13:41:19 GMT

>Help! I'm so close to figuring this out, but for some reason I'm hitting a

>mental block. When finding the the sum of a row in a multi-dimensional array,

>I am using the following:

>for (x=0; x < MAXROW; x++){

> for (y=0; y < MAXCOL; y++)

> counter+=(matrix[a][b]);{

> printf("The sum of row %d is: %5d\n", x + 1, counter);

> }

^ Shouldn't this be before the printf line?

Quote:

>}

>I use the same setup for calculating the sum of the columns, only reversing

>y and x. My problem is, I need to find the sum of the left diagonal and the

>right diagonal of the matrix! I know I need to simultaneously increment the

>rows and columns by one and store that value, but I just can't get the syntax

>right. Any help would be most appreciated! Thank you!

This ought to work: (assuming square matrices)

for (x=0; x < MAXROW; x++) {

left_diagonal += matrix[x][x];

right_diagonal += matrix[MAXROW-x][x];

Quote:

}

If the matrix isn't square (?? are diagonals defined and/or useful then ??)

then use the minimum of MAXROW or MAXCOL in place of MAXROW.

Hopefully, this is ok.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Chris Sheppard