How to calculate the average of scores?

• 05-02-2011
david.jones
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);       }```
• 05-02-2011
mike65535

Is avgScore initialized? Is it a float? An int?

See what I mean?
• 05-02-2011
Subsonics
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;```
• 05-02-2011
Salem
> 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;
• 05-02-2011
GReaper
Just sumarise all of your scores and divide the result by their number.