Thread: How to calculate the average of scores?

  1. #1
    Registered User
    Join Date
    Apr 2011

    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:

    //avg score
       for (index = 0 ; index < score[index] ; index++)
             avgScore = (avgScore + score[index] / index);

  2. #2
    Registered User
    Join Date
    Mar 2011
    Need the rest of your code. Please post compilable code.

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

    See what I mean?

  3. #3
    Registered User
    Join Date
    Jan 2009
    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:

    for(i = 0; i < array_size; i++) {
        sum += array[i];
    average = sum / array_size;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Just sumarise all of your scores and divide the result by their number.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 08-19-2009, 07:59 AM
  2. calculate average
    By archriku in forum C Programming
    Replies: 23
    Last Post: 04-10-2009, 04:27 AM
  3. calculate average from a file
    By mrsirpoopsalot in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2009, 02:25 PM
  4. SAT Scores
    By holden in forum A Brief History of
    Replies: 24
    Last Post: 04-16-2004, 05:42 PM
  5. C++ Program that Calculates average of three scores
    By dccog in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2002, 12:03 AM