2 Replies - 41624 Views - Last Post: 25 June 2009 - 10:27 PM Rate Topic: -----

#1 I_hate_scheme   User is offline

  • New D.I.C Head

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

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

Post icon  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   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 I_hate_scheme   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

View PostOler1s, 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1