Most effecient way to average scores?

Hello all i am wondering what would be the best way to have a user input up to but not required to 20 test scores, average them, and display the average of all the test scores.


I was thinking I could have a function that uses an array to hold the 20 test scores, calculate them, display the result and return to the main function.

Is that possible and is it the most efficient way to write it.

I will update with the code if anyone is interested. Feel free to upload your version as well once there are some answers.

Thanks all

I was thinking I could have a function that uses an array to hold the 20 test scores, calculate them, display the result and return to the main function.
An alternative it to have a function read the user input and store the values in a standard collection (a vector, for example).

You could then write a function that takes the average of your collected values. It only does the calculation on a parameter, it doesn't store anything.

main would look something like:
1
2
3
values = get_values
average = get_average( values )
display average
Last edited on
> a function that uses an array to hold the 20 test scores ... Is that possible

Yes.


> and is it the most efficient way to write it.

No. We don't need to store each number to calculate the average.

1
2
3
4
5
6
7
8
9
10
double total = 0 ;
int count = 0 ;

loop:
      get a number entered by the user
      add it to the total
      increment count
      repeat till there are no more numbers

double average = total / count ;
Well thank all of you for the input i was away for a couple days but, I will post my code once it is written. thank you all for your help
Topic archived. No new replies allowed.