# Thread: 2 dimensional array, average student

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:

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);

}
}```
I think you can reset the sum to 0 every loop, but then its not punctual to.

2. 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,

Jos

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);

}
}```

4. Originally Posted by aborgeld
Like this?
I have to initialize sum, otherwise i get an error.
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:

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);

}
}```
kind regards,

Jos

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é

6. Originally Posted by aborgeld
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é
You're welcome of course and yep, that's how computers are: they do exactly what you tell them to do, not what you have in mind ;-)

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•