C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-22-2005, 11:31 AM   #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."
s_ny33 is offline   Reply With Quote
Old 04-22-2005, 11:35 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,689
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.

Salem is offline   Reply With Quote
Old 04-22-2005, 01:03 PM   #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.
elad is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:41 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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