Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. /*
  2. Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped.
  3.  
  4. It should use the following functions (for you to code):
  5.  
  6. •  double getScore() should ask the user for a test score, store it in a variable, and validate it.  If valid, return the score, otherwise return -1.  
  7. •  This function should be called by main until each of the five scores are valid.
  8. •  void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by  main and should be passed the five scores.
  9. •  int findLowest() should find and return the lowest of the five scores passed to it. It should be called by calcAverage , which uses the function to determine which of the five scores to drop.
  10.  
  11. Input Validation: Do not accept test scores lower than 0 or higher than 100.
  12. */
  13.  
  14. #include <iostream>
  15. #include <vector>
  16. #include <algorithm>
  17. using namespace std;
  18.  
  19. double getScore() {
  20.     double score;
  21.     for (;;)
  22.     {  
  23.         cout << "Enter the scores: " << endl;
  24.         if (cin >> score && score > 0 && score < 100)
  25.         {
  26.             break;
  27.         }
  28.         else {
  29.             cout << "Score must be a number greater than 0 and less than 100" << endl;
  30.             cin.clear();
  31.             cin.ignore(INT_MAX, '\n');
  32.         }
  33.     }
  34.     return score;
  35. }
  36.    
  37. void findLowest(vector<double> &v) {
  38.     sort(v.begin(), v.end());
  39.     reverse(v.begin(), v.end());
  40.     v.pop_back();
  41. }
  42.  
  43. void calcAverage(vector<double> v) {
  44.     findLowest(v);
  45.     double sum = 0;
  46.     for (auto a : v)
  47.         sum += a;
  48.     cout << "Average of 4 highest scores: " << sum / v.size() << endl;;
  49. }
  50.  
  51.  
  52.  
  53. int main() {
  54.     vector<double> v;
  55.     for (int i = 1; i <= 5; ++i)
  56.         v.push_back(getScore());
  57.  
  58.     calcAverage(v);
  59.  
  60.     system("pause");
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement