# Thread: How to calculate the average of scores?

1. ## How to calculate the average of scores?

Hey guys, for some reason I can't figure out how to calculate the average from a number of scores in an array.

I've tried although I get weird numbers e.g 1003434343.43 or inf. Here's my attempt:

Code:
```//avg score
for (index = 0 ; index < score[index] ; index++)
{
avgScore = (avgScore + score[index] / index);
}``` Is avgScore initialized? Is it a float? An int?

See what I mean? 3. What you want is to sum all elements in the array, then divide by the amount of elements. So the general case would look something like this:

Code:
```for(i = 0; i < array_size; i++) {
sum += array[i];
}

average = sum / array_size;``` 4. > avgScore = (avgScore + score[index] / index);
Well this is going to be division by zero on the first pass.

First you calculate the total, with say
total = total + score[index];

Then you calculate the average
average = total / n; 5. Just sumarise all of your scores and divide the result by their number. Popular pages Recent additions 