Computing Average Test Scores

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 20
Reputation: cole davidson is an unknown quantity at this point 
Solved Threads: 0
cole davidson cole davidson is offline Offline
Newbie Poster

Computing Average Test Scores

 
0
  #1
Oct 30th, 2009
Hi guys. I am trying to write a program that asks the user how many test scores they have, then reads the test scores, averages them, then displays the average and a pass/fail grade (50+ is a pass), using a seperate function to average the input values.

I have been working on it for about 3 hours now, and I don't even know if I am going in the right direction. Worse, I don't know how to proceed. I have the program asking for the number of test scores, and reading the test scores. I have no idea how to write the function to average them.

Any help would be greaty appreciated:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. cout << "This program computes the average of a set of test scores."<<endl;
  9. cout << "How many test scores would you like to average? ";
  10. int numValues;
  11. cin >> numValues;
  12. for (int count = 1; count <= numValues; count++)
  13. {
  14. cout << "\nEnter the test score: ";
  15. int n;
  16. cin >> n;
  17. }
  18. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 522
Reputation: jonsca is on a distinguished road 
Solved Threads: 63
jonsca jonsca is offline Offline
Posting Pro
 
0
  #2
Oct 30th, 2009
Well, your int n; is local to that block so it's going to disappear shortly thereafter. A lot of this depends on the specification of your problem. Can you use arrays? You don't have to, and it's more effective without them, but if that helps you understand then go for it. Write out an average of a set of numbers on a piece of paper and see how you do it, step by step.... i.e., add numbers up, divide by count of numbers, etc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,572
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #3
Oct 30th, 2009
you will need to declare an array of ints that hold the individual test scores so that they can be averaged later. Then pass that array to the function to computes the average. Declare and allocate the array between lines 11 and 12 of the code you posted. int* arry = new int[numValues];

Then delete line 15, and change line 16 to cin >> arry[count];
Last edited by Ancient Dragon; Oct 30th, 2009 at 2:42 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,572
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #4
Oct 30th, 2009
Originally Posted by jonsca View Post
Can you use arrays? You don't have to,
Since the average has to be done in a separate function I don't see any other way to do it but use an array. The number of scores is unknown until runtime so its not possible to just declare a bunch of integers.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: cole davidson is an unknown quantity at this point 
Solved Threads: 0
cole davidson cole davidson is offline Offline
Newbie Poster
 
0
  #5
Oct 30th, 2009
Thanks! I think I understand what you guys are saying, however we have not learned arrays yet. Are there no other ways? Or if there aren't, could you explain how to use arrays?
Last edited by cole davidson; Oct 30th, 2009 at 2:54 pm.
---------Cole Davidson---------
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,572
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #6
Oct 30th, 2009
There are lots of tutorials on the net, here's just one of them.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 522
Reputation: jonsca is on a distinguished road 
Solved Threads: 63
jonsca jonsca is offline Offline
Posting Pro
 
0
  #7
Oct 30th, 2009
I see your point Ancient. I guess I supposed that passing a running total and a number of scores into a function constructed thusly might be just as valid in this context.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 20
Reputation: cole davidson is an unknown quantity at this point 
Solved Threads: 0
cole davidson cole davidson is offline Offline
Newbie Poster
 
0
  #8
Oct 31st, 2009
Ok. I have managed to figure it out. I have the program working as far as returning the average, but now I am having trouble getting it to display the pass or fail. I think I have written the function correctly for it (please correct me if I'm wrong), but the stupid thing is I can't the main function to display it! Agh! It frustrates me so much sometimes!

Anyways, here's the code. How do I get the main function to display the result of the pass or fail function (string grade), if that function is even correct!?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. string grade(double sum, int num);
  6. void average (int num)
  7. {
  8. double sum =0;
  9. double score;
  10. for (int count = 1; count <= num; count++)
  11.  
  12. {
  13.  
  14. cout <<"\nEnter the test score: ";
  15.  
  16. cin >>score;
  17. sum =sum +score;
  18. }
  19. cout <<"The average is " <<sum/num<<endl;
  20. }
  21.  
  22.  
  23.  
  24. int main()
  25. {
  26.  
  27. cout <<"This program computes the average of a set of test scores."<<endl;
  28. cout <<"How many test scores would you like to average? ";
  29. int numValues;
  30. cin >> numValues;
  31.  
  32. average (numValues);
  33.  
  34.  
  35. }
  36. string grade(double sum, int num)
  37. {
  38. if (sum/num >= 50)
  39. return "This student has passed!";
  40. else if (sum/num < 50)
  41. return "This student has failed.";
  42. }
---------Cole Davidson---------
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,572
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning
 
-7
  #9
Oct 31st, 2009
make the function averate() return the average so that main() can determine whether it is pass or fail.
  1. int average(int n)
  2. {
  3. ...
  4. ... <snip>
  5. return sum/num;
  6. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
0
  #10
Oct 31st, 2009
Or another thing you can do is to call the grade function from Average ().
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC