Adding diagonal in multi-array
Author Message Adding diagonal in multi-array

Quote:

>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++){

Here you need an additional counter=0;

Quote:
>    for (y=0; y < MAXCOL; y++)
>         counter+=(matrix[a][b]);{

You probably mean   matrix[x][y]?

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

>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!

In the right diagonal (you hopefully mean the upper right triangle?)
you have x>y and in the lower one x<y. Do it with an if or do two
loops for every row. You have counteru and counterl and initialize
them outside both loops.

--
Best Regards, Dr. Peter Kittel       //

Now ex-employee of Commodore, Class of '95.

Mon, 04 Aug 1997 06:34:45 GMT  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

Tue, 05 Aug 1997 05:12:39 GMT  Adding diagonal in multi-array

[...]
: >         My problem is, I need to find the sum of the left diagonal and the
: >right diagonal of the matrix!

: In the right diagonal (you hopefully mean the upper right triangle?)
: you have x>y and in the lower one x<y. Do it with an if or do two
: loops for every row. You have counteru and counterl and initialize
: them outside both loops.

What a waste!  An extra test for *every* matrix element?  That, *plus*
double the number of iterations necessary?

counter = 0;
for (x = 0; x < MAX_ROWS; x++)
for (y = x; y < MAX_COLS; y++)
counter += array[x][y];

and variations thereof.

--
+----------------------------------+
Martin Sohnius          | The first rule in politics:      |
Novell Labs Europe      |  "When in a hole, stop digging." |
Bracknell, England      |     - Denis Lord Healey          |
+44-1344-724031         +----------------------------------+
(I speak for myself, not for Novell or anyone else.)

Sat, 16 Aug 1997 04:37:26 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages