Noob: inputing 5 grades and averaging them

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 12
Reputation: kadji.kahn is an unknown quantity at this point 
Solved Threads: 0
kadji.kahn kadji.kahn is offline Offline
Newbie Poster

Noob: inputing 5 grades and averaging them

 
0
  #1
Oct 26th, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
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: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
0
  #2
Oct 26th, 2009
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!
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 73
Reputation: dgr231 is on a distinguished road 
Solved Threads: 7
dgr231's Avatar
dgr231 dgr231 is offline Offline
Junior Poster in Training
 
1
  #3
Oct 26th, 2009
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:
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: kadji.kahn is an unknown quantity at this point 
Solved Threads: 0
kadji.kahn kadji.kahn is offline Offline
Newbie Poster
 
0
  #4
Oct 26th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: kadji.kahn is an unknown quantity at this point 
Solved Threads: 0
kadji.kahn kadji.kahn is offline Offline
Newbie Poster
 
0
  #5
Oct 26th, 2009
I appreciate the info on shorting up my function! Thats great, I see why the other code is excessive.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
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: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster
 
0
  #6
Oct 26th, 2009
well you could use the logical && to join up checking for all !! and use one if statement only!
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: kadji.kahn is an unknown quantity at this point 
Solved Threads: 0
kadji.kahn kadji.kahn is offline Offline
Newbie Poster
 
0
  #7
Oct 26th, 2009
Ha! While its a nasty looking if statement that does work.

Cheers!

I appreciate the info.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 73
Reputation: dgr231 is on a distinguished road 
Solved Threads: 7
dgr231's Avatar
dgr231 dgr231 is offline Offline
Junior Poster in Training
 
0
  #8
Oct 26th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: kadji.kahn is an unknown quantity at this point 
Solved Threads: 0
kadji.kahn kadji.kahn is offline Offline
Newbie Poster
 
0
  #9
Oct 26th, 2009
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!

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 103
Reputation: cgeier is an unknown quantity at this point 
Solved Threads: 14
cgeier cgeier is offline Offline
Junior Poster
 
0
  #10
Oct 26th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC