How to calculate the average of scores?

This is a discussion on How to calculate the average of scores? within the C Programming forums, part of the General Programming Boards category; Hey guys, for some reason I can't figure out how to calculate the average from a number of scores in ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

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

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    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
    Posts
    1,485
    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. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,622
    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, 01:25 PM
  4. SAT Scores
    By holden in forum A Brief History of Cprogramming.com
    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-27-2002, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21