Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped.
- It should use the following functions (for you to code):
- • 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.
- • This function should be called by main until each of the five scores are valid.
- • 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.
- • 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.
- Input Validation: Do not accept test scores lower than 0 or higher than 100.
- */
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- double getScore() {
- double score;
- for (;;)
- {
- cout << "Enter the scores: " << endl;
- if (cin >> score && score > 0 && score < 100)
- {
- break;
- }
- else {
- cout << "Score must be a number greater than 0 and less than 100" << endl;
- cin.clear();
- cin.ignore(INT_MAX, '\n');
- }
- }
- return score;
- }
- void findLowest(vector<double> &v) {
- sort(v.begin(), v.end());
- reverse(v.begin(), v.end());
- v.pop_back();
- }
- void calcAverage(vector<double> v) {
- findLowest(v);
- double sum = 0;
- for (auto a : v)
- sum += a;
- cout << "Average of 4 highest scores: " << sum / v.size() << endl;;
- }
- int main() {
- vector<double> v;
- for (int i = 1; i <= 5; ++i)
- v.push_back(getScore());
- calcAverage(v);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement