# Add all the diagonal values in this array (from 0,0 to 9,9), and print

Page 1 of 1

## 6 Replies - 2159 Views - Last Post: 15 October 2011 - 12:56 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251369&amp;s=c9ac141a378f2d360d630933e40448ab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kendav

Reputation: 0
• Posts: 19
• Joined: 14-October 11

# Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 14 October 2011 - 11:03 PM

Did i do these right? Add all the diagonal values in this array (from 0,0 to 9,9), and print the total
Did i do these right? Add all the diagonal values in this array (from 0,9 to 9,0), and print the total

```for (int row = 0; row < Num2dArray.length; row++)
02
{
03
for (int col = 0; col < Num2dArray.length; col++)
04
total2 += (row + row);
05
}
06
System.out.println("\nThe total sum of each position diagonal from left to right 10x10 ");
07

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

This post has been edited by jon.kiparsky: 14 October 2011 - 11:13 PM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: Add all the diagonal values in this array (from 0,0 to 9,9), and print

### #2 Manbearpig101

Reputation: 15
• Posts: 62
• Joined: 17-June 10

## Re: Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 14 October 2011 - 11:09 PM

Nobody will want to help you if you don't use the code tags.

If you want the sum of all the diagonals, you'll want something like:

```for each (array[i][j]):
if (i == j):
sum += array[i][j];

```

Specifically, if you want to bound the range which you can search, you just do it with the condition of the for loop.

For example searching from (0, 9) to (9, 0):

```for (int row = 0; row <= 9; row++) {
for (int col = 9; col >= 0; col--) {
//blah blah, sum goes here if i equals j
}
}

```

However, I'm not sure if that's quite what you want.

This post has been edited by Manbearpig101: 14 October 2011 - 11:20 PM

### #3 jon.kiparsky

• Beginner

Reputation: 12029
• Posts: 20,387
• Joined: 19-March 11

## Re: Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 14 October 2011 - 11:17 PM

```for (int row = 0; row < Num2dArray.length; row++)
{
for (int col = 0; col < Num2dArray.length; col++)
total2 += (row + row);

}

```

You're adding up the sum of integers between 1 and the length of the array, and doubling it. You want to use row as an index into the array. Since you say it's a 2-dimensional array, you'll want to use col as a second index into the array.

This post has been edited by jon.kiparsky: 14 October 2011 - 11:19 PM

### #4 kendav

Reputation: 0
• Posts: 19
• Joined: 14-October 11

## Re: Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 14 October 2011 - 11:54 PM

Manbearpig101, on 14 October 2011 - 11:09 PM, said:

Nobody will want to help you if you don't use the code tags.

If you want the sum of all the diagonals, you'll want something like:

```for each (array[i][j]):
if (i == j):
sum += array[i][j];

```

Specifically, if you want to bound the range which you can search, you just do it with the condition of the for loop.

For example searching from (0, 9) to (9, 0):

```for (int row = 0; row <= 9; row++) {
for (int col = 9; col >= 0; col--) {
//blah blah, sum goes here if i equals j
}
}

```

However, I'm not sure if that's quite what you want.

### #5 kendav

Reputation: 0
• Posts: 19
• Joined: 14-October 11

## Re: Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 15 October 2011 - 08:47 AM

what is the "code tag"?

Aww thanks, i see where i went wrong. Im new to java, its my first week trying to learn on my own.

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12772
• Posts: 45,965
• Joined: 27-December 08

## Re: Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 15 October 2011 - 08:49 AM

You can use code tags like so: .

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Add all the diagonal values in this array (from 0,0 to 9,9), and print

Posted 15 October 2011 - 12:56 PM

The diagonal ? You do not need two loops

```int total = 0;
for(int i = 0; i < array.length; ++i)
total += array[i][i];

```