Summing row in array

Page 1 of 1

8 Replies - 1041 Views - Last Post: 09 November 2009 - 08:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=137909&amp;s=2a9d0602307b70dbde881a71ab480370&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 chumpster804

Reputation: 0
• Posts: 62
• Joined: 16-April 09

Summing row in array

Posted 09 November 2009 - 05:54 PM

I had an idea to have a 2-D array to complete golfers' scorecard. I think that this will work and total each golfer's score. Is this right?
int [] [] scorecard = new int [4] [18]
for (row = 0; row < scorecard.length; row++){
for (col = 0; col < scorecard[0].length; col++){
scores[row][col] = 0;
int total = 0;
total += scorecard[row] [column];
System.out.println ("The total score for " + column +  " is " + total);
}
}

Is This A Good Question/Topic? 0

Replies To: Summing row in array

#2 jtphenom

Reputation: 12
• Posts: 129
• Joined: 06-November 09

Re: Summing row in array

Posted 09 November 2009 - 06:07 PM

chumpster804, on 9 Nov, 2009 - 04:54 PM, said:

I had an idea to have a 2-D array to complete golfers' scorecard. I think that this will work and total each golfer's score. Is this right?
int [] [] scorecard = new int [4] [18]
for (row = 0; row < scorecard.length; row++){
for (col = 0; col < scorecard[0].length; col++){
scores[row][col] = 0;
int total = 0;
total += scorecard[row] [column];
System.out.println ("The total score for " + column +  " is " + total);
}
}

What happens when you run the program?

#3 Overachiever

Reputation: 4
• Posts: 36
• Joined: 09-September 09

Re: Summing row in array

Posted 09 November 2009 - 06:13 PM

int [] [] scorecard = new int [4] [18]
for (row = 0; row < scorecard.length; row++){
int total = 0;
for (col = 0; col < scorecard[0].length; col++){
scores[row][col] = 0;
total += scorecard[row] [col];
}
System.out.println ("The total score for " + row +  " is " + total);
}

This is good start!
Just a few things with it, though. You are going to want to initialize the total variable before the nested loop, and also print the score just after the nested loop. This way the total is not reset every time you check each column of a row. Also, when you do print the score, if you are going to have the total score for each row on the card, then you can just use the row variable from the outside loop instead of column.

Oh, just one more thing. Right here: total += scorecard[row] [column];, you can use col instead of column.

Keep up the practice and I assure you, you will be great at it!

#4 jtphenom

Reputation: 12
• Posts: 129
• Joined: 06-November 09

Re: Summing row in array

Posted 09 November 2009 - 06:24 PM

Overachiever, on 9 Nov, 2009 - 05:13 PM, said:

int [] [] scorecard = new int [4] [18]
for (row = 0; row < scorecard.length; row++){
int total = 0;
for (col = 0; col < scorecard[0].length; col++){
scores[row][col] = 0;
total += scorecard[row] [col];
}
System.out.println ("The total score for " + row +  " is " + total);
}

This is good start!
Just a few things with it, though. You are going to want to initialize the total variable before the nested loop, and also print the score just after the nested loop. This way the total is not reset every time you check each column of a row.

The total will not be reset every time a column is checked. There are no braces used in the second for loop, so the second for loop only includes the line

scores[row][col] = 0;

#5 chumpster804

Reputation: 0
• Posts: 62
• Joined: 16-April 09

Re: Summing row in array

Posted 09 November 2009 - 06:27 PM

so more like:
int total = 0;
int [] [] scorecard = new int [4] [18];
for (row = 0; row < scorecard.length; row++){
for (column = 0; column < scorecard[0].length; column++){
scores[row][column] = 0;
total += scorecard[row] [column];
System.out.println ("The total score for " + column +  " is " + total);
}
}

This post has been edited by chumpster804: 09 November 2009 - 06:30 PM

#6 jtphenom

Reputation: 12
• Posts: 129
• Joined: 06-November 09

Re: Summing row in array

Posted 09 November 2009 - 07:15 PM

chumpster804, on 9 Nov, 2009 - 05:27 PM, said:

so more like:
int total = 0;
int [] [] scorecard = new int [4] [18];
for (row = 0; row < scorecard.length; row++){
for (column = 0; column < scorecard[0].length; column++){
scores[row][column] = 0;
total += scorecard[row] [column];
System.out.println ("The total score for " + column +  " is " + total);
}
}

Again, have you tried this code? If not, you really should try it for yourself to see if it works.

I do see one glaring problem... you have two different array names.. scores and scorecard. Which one are you using? Also, where are your scores coming in? And you need curly braces in your second for loop if it is more than one line.

#7 chumpster804

Reputation: 0
• Posts: 62
• Joined: 16-April 09

Re: Summing row in array

Posted 09 November 2009 - 07:26 PM

No I haven't tried it because I'm not writing a complete program. I guess I could, but I was trying to avoid it.

#8 jtphenom

Reputation: 12
• Posts: 129
• Joined: 06-November 09

Re: Summing row in array

Posted 09 November 2009 - 08:00 PM

chumpster804, on 9 Nov, 2009 - 06:26 PM, said:

No I haven't tried it because I'm not writing a complete program. I guess I could, but I was trying to avoid it.

You're not writing an application to test this code? What's the point of the code, then?

The reality is that you should always test code before you ask someone else if it will work? Find out for yourself. If it doesn't work, then post your problems.

This post has been edited by jtphenom: 09 November 2009 - 08:01 PM

#9 theautokustomizer

• D.I.C Regular

Reputation: 16
• Posts: 250
• Joined: 20-September 09

Re: Summing row in array

Posted 09 November 2009 - 08:19 PM

I would have to tend to agree, you basically just bitch-smacked the people who are on here, that are nice enough to help others on their own time....Granted, it might have been a quick question, but don't waste peoples' time on here please. TY

This post has been edited by theautokustomizer: 09 November 2009 - 08:20 PM