average in an array

This is a discussion on average in an array within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a C++ program that prompts for 20 individual grades. Then I want to calculate the class ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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
    otherwise
    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, 08:51 AM
  4. average of array elements?
    By swapnil_sandy in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 11:16 AM
  5. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM

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