8 Replies - 596 Views - Last Post: 09 November 2009 - 08:19 PM Rate Topic: -----

#1 chumpster804  Icon User is offline

  • D.I.C Head

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

Summing row in array

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

  • D.I.C Head

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

Re: Summing row in array

Posted 09 November 2009 - 06:07 PM

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

#3 Overachiever  Icon User is offline

  • New D.I.C Head

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

#4 jtphenom  Icon User is offline

  • D.I.C Head

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

Re: Summing row in array

Posted 09 November 2009 - 06:24 PM

View PostOverachiever, 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;


Was This Post Helpful? 0
  • +
  • -

#5 chumpster804  Icon User is offline

  • D.I.C Head

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

Was This Post Helpful? 0
  • +
  • -

#6 jtphenom  Icon User is offline

  • D.I.C Head

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

Re: Summing row in array

Posted 09 November 2009 - 07:15 PM

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

Please make the corrections to your code, try it for yourself, and post your results.
Was This Post Helpful? 0
  • +
  • -

#7 chumpster804  Icon User is offline

  • D.I.C Head

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

#8 jtphenom  Icon User is offline

  • D.I.C Head

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

Re: Summing row in array

Posted 09 November 2009 - 08:00 PM

View Postchumpster804, 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

Was This Post Helpful? 0
  • +
  • -

#9 theautokustomizer  Icon User is offline

  • D.I.C Regular

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1