6 Replies - 2049 Views - Last Post: 15 October 2011 - 12:56 PM Rate Topic: -----

#1 kendav   User is offline

  • New D.I.C Head

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

  • D.I.C Head
  • member icon

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

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11069
  • View blog
  • Posts: 18,907
  • 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);

         }




To answer your question: no.

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

Was This Post Helpful? 0
  • +
  • -

#4 kendav   User is offline

  • New D.I.C Head

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

View PostManbearpig101, 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.

Was This Post Helpful? 0
  • +
  • -

#5 kendav   User is offline

  • New D.I.C Head

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

#6 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12315
  • View blog
  • Posts: 45,414
  • 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: :code:.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

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

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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1