Thread: average in an array

  1. #1
    Registered User
    Join Date
    Mar 2005

    average in an array

    I'm trying to write a C++ program that prompts for 20 individual grades. Then I want to calculate the class average and then list the grades in the order they were entered and how many points (+ or -) the individual grade was from the average. I'm setting this up using an array. I'm not looking for the full code, I'm just stuck on the code to display the "(+ or -) the individual grade was from the average."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well can you calculate the average or not?

    Then it's simply
    cout << score - average;
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    for those scores below average, subtracting average from score will be negative. If you want to be able to say "Current score is x points below average" rather than "current score is -x points", then you can do something like:

    result = abs(score - average)

    to show the difference whether it is higher or lower than the average (that is, the absolute value of the difference), or, if you aren't supposed to use abs(), or equivalent, you can roll your own by doing something like

    assign score - average to result
    if result is greater than zero
    output results
    assign minus one times result to result
    output results
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. average of array elements?
    By swapnil_sandy in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 12:16 PM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM