# Find the sum of diagonal elements of a 2D array...C++

Page 1 of 1

## 2 Replies - 41624 Views - Last Post: 25 June 2009 - 10:27 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=111889&amp;s=cf24eebd86dd00b11a96d9de5b7b05bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 I_hate_scheme

Reputation: 0
• Posts: 22
• Joined: 28-October 08

# Find the sum of diagonal elements of a 2D array...C++

Posted 25 June 2009 - 09:28 PM

Hey guys...Ive just started to learn C++, and Ive encountered a small issue.

Im trying to diagonally sum the numbers in a 2D array(8 X 8), and for some reason, I am only able to display the diagonal numbers, and not add them up....lol

```#include <iostream>
using namespace std;

int main()
{

int twoDimArray[8][8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
twoDimArray[i][j] = (i + 1) * (j + 1);
}
}
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << twoDimArray[i][j] <<" ";
}
cout << endl;
}
for(int i = 0;i < 8;i++)
{
int sum = 0;

sum += twoDimArray[i][i]; // I believe the problem is  here...
cout << sum  << endl;
}
return 0;
}

```

Any help would be nice....Thanks in advance.

----------------
Now playing: Matisyahu - Time Of Your Song
via FoxyTunes

Is This A Good Question/Topic? 0

## Replies To: Find the sum of diagonal elements of a 2D array...C++

### #2 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: Find the sum of diagonal elements of a 2D array...C++

Posted 25 June 2009 - 10:18 PM

Quote

for some reason, I am only able to display the diagonal numbers, and not add them up....lol
How do you know you can't? How can you claim your program doesn't add up the diagonal?

Did you by any chance run the program? Did you get output? Was the output different from what you expected?

If you answer yes to all three questions, I have yet one more. Why do you not tell us any information on what you did, what you observed, and what you expected?

I'm going to take a guess, as I have to do when not given information. I guess that instead of seeing a properly accumulating value, you see the diagonal elements listed. Right?

The problem? int sum=0. Where in relation to the for loop is it placed? What happens on each iteration of the loop? sum=0.

By the way, there's only two things that could have gone wrong. The first is that sum was somehow being augmented, and the second is that the elements were somehow not being added to sum manually.

You could have checked by: using a debugger, adding in more output statements, summing up some of the elements manually and seeing if the syntax also resulted in the correct behavior. And so on.

### #3 I_hate_scheme

Reputation: 0
• Posts: 22
• Joined: 28-October 08

## Re: Find the sum of diagonal elements of a 2D array...C++

Posted 25 June 2009 - 10:27 PM

Oler1s, on 25 Jun, 2009 - 09:18 PM, said:

Quote

for some reason, I am only able to display the diagonal numbers, and not add them up....lol
How do you know you can't? How can you claim your program doesn't add up the diagonal?

Did you by any chance run the program? Did you get output? Was the output different from what you expected?

If you answer yes to all three questions, I have yet one more. Why do you not tell us any information on what you did, what you observed, and what you expected?

I'm going to take a guess, as I have to do when not given information. I guess that instead of seeing a properly accumulating value, you see the diagonal elements listed. Right?

The problem? int sum=0. Where in relation to the for loop is it placed? What happens on each iteration of the loop? sum=0.

By the way, there's only two things that could have gone wrong. The first is that sum was somehow being augmented, and the second is that the elements were somehow not being added to sum manually.

You could have checked by: using a debugger, adding in more output statements, summing up some of the elements manually and seeing if the syntax also resulted in the correct behavior. And so on.

Well, I did compile the code,and I got the 2D array and all the diagonal numbers (top left to bottom right) to display....So yes you were pretty much right about the output....

....missed a for loop....now I get the sum to print!!!
THANKS!!!

----------------
Now playing: Basshunter - Hello There
via FoxyTunes