943,994 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 497
  • C++ RSS
Oct 26th, 2009
0

Noob: inputing 5 grades and averaging them

Expand Post »
I was asked "This program asks for a series of five test scores and calculates the average score in a function. The function should receive the total of the five scores as an argument and return the average of the five scores. The program should not accept scores less than zero or greater than one hundred. The output should look like the example below."

Here's my code. It works, but I think I could do WAY better. Could anobody nudge me in the right direction? I really need to check each input as is comes in, but can't figure out a way to do that.

BTW I'm really green with C++ so be gentle.

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<string>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int average(int a, int b, int c, int d, int e); // prototype average
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15.  
  16. // declare variables
  17.  
  18. int testscore = 0;
  19. int iaverage = 0;
  20. int f = 0;
  21. int g = 0;
  22. int h = 0;
  23. int i = 0;
  24. int j = 0;
  25.  
  26. // get input
  27.  
  28. cout << "Please enter 5 test scores ";
  29. cin >> f >> g >> h >> i >> j;
  30.  
  31. if(f <= 0 || f > 100)
  32. {
  33. cout << "Please enter a value between 0 - 100";
  34. cout << endl;
  35. }
  36. else
  37. {
  38. iaverage = average(f, g, h, i, j);
  39. testscore = (f+g+h+i+j);
  40. cout << "Total of all scores: " << testscore << endl;
  41. cout << "Average score: " << iaverage << endl;
  42. }//end if-else
  43.  
  44.  
  45. return 0;
  46.  
  47. }//end main
  48.  
  49. int average(int a, int b, int c, int d, int e)
  50. {
  51. int daverage;
  52. daverage = (a+b+c+d+e) / 5;
  53. return daverage;
  54. } //end average
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadji.kahn is offline Offline
12 posts
since Oct 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
Do you know how to use arrays? If you are comfortable with arrays, try using them to replace the different mark variables.

Btw, this program doesn't check if the other variables apart from f, have been initialised to a value greater than 100 or lesser 0. Examine that you only are checking that for the variable 'f'.

the #include <string> is completely un-necessary!
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 26th, 2009
1
Re: Noob: inputing 5 grades and averaging them
Hello kadji.kahn,

I agree with everything that Sky Diploma said. In addition, you can shorten up your function a bit. This is what you currently have:
C++ Syntax (Toggle Plain Text)
  1. int average(int a, int b, int c, int d, int e)
  2. {
  3. int daverage;
  4. daverage = (a+b+c+d+e) / 5;
  5. return daverage;
  6. } //end average
I would first suggest that you make your return type a double, unless you are okay with rounding to the nearest whole number. You can get rid of the daverage variable and simplify the function to the following:
C++ Syntax (Toggle Plain Text)
  1. double average(int a, int b, int c, int d, int e)
  2. {
  3. return (a+b+c+d+e) / 5.0;
  4. }
If you decide to go the route that Sky Diploma suggested with arrays, the function wouldn't change drastically, you would just replace your arguements with an array of the correct size and replace a, b, c, d, and e with the appropriate array index.

I hope that helps a little.

-D
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
Thanks for the quick reply!

I haven't go to arrays yet, so I can't do that.

Any idea on how to analyze each input w/o excessive "if" statements?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadji.kahn is offline Offline
12 posts
since Oct 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
I appreciate the info on shorting up my function! Thats great, I see why the other code is excessive.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadji.kahn is offline Offline
12 posts
since Oct 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
well you could use the logical && to join up checking for all !! and use one if statement only!
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
Ha! While its a nasty looking if statement that does work.

Cheers!

I appreciate the info.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadji.kahn is offline Offline
12 posts
since Oct 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
Also, keep in mind that an if statement will only check the condition once (unless it is in a loop). If you have studied while loops, they may serve your purpose a little better as a while loop will allow the user to continue to input incorrect data until the data meets the specifications.
Reputation Points: 55
Solved Threads: 7
Junior Poster in Training
dgr231 is offline Offline
76 posts
since Aug 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
you're input was great, I was at least able to clean some things up, making the program a little better.

Have a great monday!

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int average(int a, int b, int c, int d, int e); // prototype average
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13.  
  14. // declare variables
  15.  
  16. int testscore = 0;
  17. int iaverage = 0;
  18. int f = 0;
  19. int g = 0;
  20. int h = 0;
  21. int i = 0;
  22. int j = 0;
  23.  
  24. // get input
  25.  
  26.  
  27.  
  28. while(cin)
  29. {
  30. cout << "Please enter 5 test scores ";
  31. cin >> f >> g >> h >> i >> j;
  32. cout << endl;
  33.  
  34. if(f <= 0 || f > 100 || g <= 0 || g > 100 || h <= 0 || h > 100 || i <= 0 || i > 100 || j <= 0 || j > 100)
  35. {
  36. cout << "Please enter a value between 0 - 100";
  37. cout << endl;
  38. }
  39. else
  40. {
  41. iaverage = average(f, g, h, i, j);
  42. testscore = (f+g+h+i+j);
  43. cout << "Total of all scores: " << testscore << endl;
  44. cout << "Average score: " << iaverage << endl;
  45. return 0;
  46. }//end if-else
  47. }
  48.  
  49.  
  50.  
  51. }//end main
  52.  
  53. int average(int a, int b, int c, int d, int e)
  54. {
  55. return (a+b+c+d+e) / 5.0;
  56. } //end average
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kadji.kahn is offline Offline
12 posts
since Oct 2009
Oct 26th, 2009
0
Re: Noob: inputing 5 grades and averaging them
It's undersirable (in my opinion) to make the user re-enter all five scores just because ONE of them was wrong. Ask for each score individually using a for (or while) loop. You can also use "switch-case" statements in place of "if/else if" statements.

Some considerations when writing a program include: execution time (not so important when first learning to program; more important later)--fewer lines of code do not necessarily mean less execution time, user experience --how easy is the program to use, what are it's limitations (what user input could cause it to crash), what are it's features, readability and maintainability--how easy is it for someone else to read, understand, and modify your code (ex: use descriptive variable names: score1,score2, score3 instead of: a,b,c). I'm sure you can come up with more, but these are a few to get you started.

You might consider making all your variables of type "double" to allow for non-integer values. ex: 94.5
Last edited by cgeier; Oct 26th, 2009 at 1:58 pm.
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I don't know how to make this float function work correctly.
Next Thread in C++ Forum Timeline: getline





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC