# cannot get a diagonal sum in a 2D array.

• (2 Pages)
• 1
• 2

## 22 Replies - 24568 Views - Last Post: 05 July 2011 - 08:31 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=238150&amp;s=ee27bfb260d56a5c63e03ef9aeb2d4f7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

# cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 10:22 PM

Hello guys, I am trying to do a diagonal sum from 0,0 to 9,9 and from 0,9 to 9,0 in a 10x10 2D array. I believe that I got the first one right from 0,0 to 9,9, which is from left to right which is 900 ?, but i cannot figure out the other way around. Here is how it looks..
```for (int row = 0; row < Num2dArray.length; row++)
{
for (int col = 0; col < Num2dArray.length; col++)
total2 += (row + row);
}
System.out.println("\nThe total sum of each position diagonal from left to right is = " + total2);

for (int row = 0; Num2dArray.length < row; row++)
{
for (int col = 0; col < Num2dArray.length; col++)
total3 += Num2dArray[row][col];
}
System.out.println("\nThe total sum of each position diagonal from right to left is = " + total3);
```

This post has been edited by kl2eativ: 04 July 2011 - 10:26 PM

Is This A Good Question/Topic? 0

## Replies To: cannot get a diagonal sum in a 2D array.

### #2 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,914
• Joined: 18-April 07

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 10:36 PM

Diagonals are easy. The first one has the same first and second index... 0,0 / 1,1 / 2,2 etc from 0 to 9. The other is the same except the first index is incremented while the second is decremented. This loop below should produce what you are looking for.

```for (int i = 9; i >= 0; i--) {
total += Num2dArray[9 - i][i];
}

```

Notice that as the value of i decreases, it is subtracted from 9 causing the first index to increase. First 9,0 and then 1,8 and then 2,7 etc.

### #3 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 10:38 PM

What are total, total1.... are carring? I see only many loops and you just change their values in every iterate?
Also where you feed your array values?

If yu want to add diagonally like at index 0,0 1,1 2,2 .... This means you need only one nested loop with an if if statement inside to check if row == col. then add the value at that index to total....
Please elaborate what exactly you are doing in those loops..

EDIT: and what Martyr2 did is the best one

This post has been edited by smohd: 04 July 2011 - 10:40 PM

### #4 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 10:50 PM

0 1 2
1 2 3
2 3 4

I will need to add 2 + 1+3 + 0+2+4 + 1+3 + 2

And the total2 and 3 is because at the end i need to subtract both results.

### #5 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 11:06 PM

So you are adding all values in your array or all indices?

### #6 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 11:11 PM

All values!!

### #7 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 11:20 PM

If all values then this is not diagonal! Just loop in array like:
``` int total = 0;
for (int row = 0; row < Num2dArray.length; row++)
{
for (int col = 0; col < Num2dArray[row].length; col++)
total += Num2dArray[row][col];
}
```

### #8 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

## Re: cannot get a diagonal sum in a 2D array.

Posted 04 July 2011 - 11:41 PM

That should take care from 0,0 to 9,9 (left to right) how about from 0,9 to 9,0 (right to left) ??

### #9 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 01:36 AM

Just change the loop to start from up to down, like:
```int total = 0;
for (int row = Num2dArray.length -1; row >= 0; row--)
{
for (int col = Num2dArray[row].length -1; col >=0 ; col--)
total += Num2dArray[row][col];
}
```

### #10 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 01:52 PM

smohd, on 05 July 2011 - 01:36 AM, said:

Just change the loop to start from up to down, like:
```int total = 0;
for (int row = Num2dArray.length -1; row >= 0; row--)
{
for (int col = Num2dArray[row].length -1; col >=0 ; col--)
total += Num2dArray[row][col];
}
```

I tried all the loops you guys suggested and it still outputting "0".. Any thoughts?

``` for (int row = 0; row < Num2dArray.length; row++)
{
for (int col = 0; col < Num2dArray.length; col++)
total2 += (row + row);
}
System.out.println("\nThe total sum of each position diagonal from left to right is = " + total2);

for (int row = Num2dArray.length -1; row >= 0; row--)
{
for (int col = Num2dArray[row].length -1; col >=0 ; col--)
total3 += Num2dArray[row][col];
}
System.out.println("\nThe total sum of each position diagonal from right to left is = " + total3);
```

### #11 Mila

Reputation: 34
• Posts: 193
• Joined: 28-October 06

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 02:00 PM

kl2eativ, on 05 July 2011 - 02:52 PM, said:

```            total2 += (row + row);

```

Why do you do this? Shouldn't you be doing total2+= Num2dArray[row][col];?

Mila

### #12 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 02:03 PM

Mila, on 05 July 2011 - 02:00 PM, said:

kl2eativ, on 05 July 2011 - 02:52 PM, said:

```            total2 += (row + row);

```

Why do you do this? Shouldn't you be doing total2+= Num2dArray[row][col];?

Mila

If i change it to Num2dArray[row][col.. it gives me zero, and how I have it it outputs 900, which is the right sum I believe.

Output:

The total sum of each position diagonal from left to right is = 900

The total sum of each position diagonal from right to left is = 0

The difference between the diagonal sum from right to left minus the diagonal sum from left to right is = -900

Why is the second one still giving me 0? I tried all the loops.

### #13 Mila

Reputation: 34
• Posts: 193
• Joined: 28-October 06

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 02:14 PM

What about a while-loop for the right/downwards diagonal? Something like:

```int rightDiagonal = 0;
int offset = 0;
while (offset < Num2dArray.length) {
int row = Num2dArray.length-1-offset;
int col = 0 + offset;
rightDiagonal += Num2dArray[row][col];
offset++;
}
```

This should return every value from the top right corner on a straight line through the bottom left corner, summed.

Mila

This post has been edited by Mila: 05 July 2011 - 02:17 PM

### #14 kl2eativ

Reputation: 0
• Posts: 33
• Joined: 11-June 11

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 02:21 PM

Mila, on 05 July 2011 - 02:14 PM, said:

What about a while-loop for the right/downwards diagonal? Something like:

```int rightDiagonal = 0;
int offset = 0;
while (offset < Num2dArray.length) {
int row = Num2dArray.length-1-offset;
int col = 0 + offset;
rightDiagonal += Num2dArray[row][col];
offset++;
}
```

This should return every value from the top right corner on a straight line through the bottom left corner, summed.

Mila

Which loop should this one substitute?

### #15 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

## Re: cannot get a diagonal sum in a 2D array.

Posted 05 July 2011 - 02:57 PM