Results 1 to 6 of 6
- 03-05-2011, 01:21 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
2 dimensional array, average student
I'm building a 2d array:
this is de the output
Wk1,Wk2,Wk3,Wk4,Wk5,Wk6,Wk7
Student 0| 1.0 9.0 7.0 6.0 5.0 7.0 6.0
Student 1| 3.0 3.0 6.0 2.0 9.0 1.0 8.0
Student 2| 6.0 1.0 7.0 9.0 9.0 3.0 8.0
Student 3| 6.0 5.0 4.0 2.0 1.0 9.0 7.0
Student 4| 2.0 6.0 9.0 9.0 6.0 5.0 2.0
I can't seem to get the right average per row, what am i doing wrong:
I think you can reset the sum to 0 every loop, but then its not punctual to.Java Code:public void fillResults(){ double sum = 0.0; double average =0.0; for(row=0; row<students.size();row++) { for(column=0; column <results.length ; column++) { results [row][column] = (int)(Math.random()*10); sum += results[row][column]; if (results[row][column]==0.0) results [row][column]=1.0; } average = sum / 7; System.out.println("The average for student " + row + " is " + average); } }
- 03-05-2011, 01:26 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
You're setting sum to 0.0 at the very beginning of the method, but you should set it to zero before you compute the sum of each row.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-05-2011, 01:43 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Staring to long
Like this?
I have to initialize sum, otherwise i get an error.
But i get this output:
The average for student 0 is 1.0
The average for student 1 is 0.7142857142857143
The average for student 2 is 0.42857142857142855
The average for student 3 is 0.42857142857142855
The average for student 4 is 0.2857142857142857
Wk1,Wk2,Wk3,Wk4,Wk5,Wk6,Wk7
Student 0| 1.0 2.0 4.0 1.0 7.0 9.0 5.0
Student 1| 1.0 1.0 2.0 4.0 4.0 7.0 2.0
Student 2| 6.0 1.0 1.0 9.0 7.0 8.0 7.0
Student 3| 2.0 1.0 4.0 4.0 3.0 3.0 1.0
Student 4| 1.0 2.0 9.0 8.0 2.0 5.0 9.0
Java Code:public void fillResults(){ double sum =0.0; double average =0.0; for(row=0; row<students.size();row++) { for(column=0; column <results.length ; column++) { sum=0.0; results [row][column] = (int)(Math.random()*10); sum += (double)results[row][column]; if (results[row][column]==0.0) results [row][column]=1.0; } average = sum / 7; System.out.println("The average for student " + row + " is " + average); } }
- 03-05-2011, 01:49 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Yes you have to initialize sum but not at the location you did it; also add a cell value to sum after it is set to its final value; like this:
kind regards,Java Code:public void fillResults(){ double sum; double average =0.0; for(row=0; row<students.size();row++) { sum= 0.0; for(column=0; column <results.length ; column++) { results [row][column] = (int)(Math.random()*10); if (results[row][column]==0.0) results [row][column]=1.0; sum += (double)results[row][column]; } average = sum / 7; System.out.println("The average for student " + row + " is " + average); } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-05-2011, 02:03 PM #5
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Hi Jos
I have changed the things in my code into the changes in your example.
It works fine now. Well its quite logical if you say it like this.
Thanks for the example and most important for explaining. Because i'm still learning it!
Kind regards,
André
- 03-05-2011, 03:12 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
2 dimensional array
By sehudson in forum New To JavaReplies: 5Last Post: 02-20-2011, 11:56 PM -
Can't get correct average using array
By nevets93 in forum New To JavaReplies: 2Last Post: 02-11-2011, 10:33 AM -
Two dimensional array
By niu_niu in forum New To JavaReplies: 4Last Post: 06-13-2010, 12:34 AM -
Delete Student from Array Help
By kazit in forum New To JavaReplies: 4Last Post: 02-25-2009, 02:42 AM -
two-dimensional array
By kHim in forum New To JavaReplies: 4Last Post: 11-16-2008, 07:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks