cannot get a diagonal sum in a 2D array.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 10960 Views - Last Post: 05 July 2011 - 08:31 PM Rate Topic: -----

#1 kl2eativ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • 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.

:)
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


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

Was This Post Helpful? 0
  • +
  • -

#4 kl2eativ  Icon User is offline

  • New D.I.C Head

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

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

Posted 04 July 2011 - 10:50 PM

thank you for your fast reply, i want to add diagonally for example in a 3x3.

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

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


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

#6 kl2eativ  Icon User is offline

  • New D.I.C Head

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

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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];
} 

Was This Post Helpful? 0
  • +
  • -

#8 kl2eativ  Icon User is offline

  • New D.I.C Head

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

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • 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];
	} 

Was This Post Helpful? 0
  • +
  • -

#10 kl2eativ  Icon User is offline

  • New D.I.C Head

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

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

Posted 05 July 2011 - 01:52 PM

View Postsmohd, 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);

Was This Post Helpful? 0
  • +
  • -

#11 Mila  Icon User is offline

  • D.I.C Head
  • member icon

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

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

Posted 05 July 2011 - 02:00 PM

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

#12 kl2eativ  Icon User is offline

  • New D.I.C Head

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

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

Posted 05 July 2011 - 02:03 PM

View PostMila, on 05 July 2011 - 02:00 PM, said:

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

#13 Mila  Icon User is offline

  • D.I.C Head
  • member icon

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

Was This Post Helpful? 0
  • +
  • -

#14 kl2eativ  Icon User is offline

  • New D.I.C Head

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

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

Posted 05 July 2011 - 02:21 PM

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

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

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

Posted 05 July 2011 - 02:57 PM

Lets me ask you something which I have already asked you before:
- Where did you feed values to your array?
- That is why I asked you, are you adding indexes or values at those index? The sum is 900 because you are adding indexes,
- Is the correct answer 900, if so then you are adding indexes, or you are just believe to be .....
- If you are adding indexes(which is seen here because you dont give values to your array) then change the expression in the second loop to be the same as in left-right one)
- You get 0 because you have not give values to your array and it is default to 0
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2