1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype.
void getScore(int score1, int score2, int score3, int score4, int score5); // Gets user inputs
int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest); // Finds the lowest score & drops it
void calcAverage(int score1, int score2, int score3, int score4, int score5); // Calculates and displays the average score.
int score1, score2, score3,score4, score5; // Defines integers used for the scores
void getScore(int score1, int score2, int score3, int score4, int score5) // Gets the test scores from user inputs
{
cout << "Enter score #1: "; // Asks for test score #1
cin >> score1;
if ((score1 > 100) || (score1 < 0))
{
cout << "Please enter a score between 0 and 100: ";
cin >> score1;
}
cout << "Enter score #2: "; // Asks for test score #1
cin >> score2;
if ((score2 > 100) || (score2 < 0))
{
cout << "Please enter a score between 0 and 100: ";
cin >> score2;
}
cout << "Enter score #3: "; // Asks for test score #1
cin >> score3;
if ((score3 > 100) || (score3 < 0))
{
cout << "Please enter a score between 0 and 100: ";
cin >> score3;
}
cout << "Enter score #4: "; // Asks for test score #1
cin >> score4;
if ((score4 > 100) || (score4 < 0))
{
cout << "Please enter a score between 0 and 100: ";
cin >> score4;
}
cout << "Enter score #5: "; // Asks for test score #1
cin >> score5;
if ((score5 > 100) || (score5 < 0))
{
cout << "Please enter a score between 0 and 100: ";
cin >> score5;
}
}
int findLowest(int score1, int score2, int score3, int score4, int score5, int lowest) // Function that finds and drops the lowest score
{
if (score2 < lowest) lowest = score2;
if (score3 < lowest) lowest = score3;
if (score4 < lowest) lowest = score4;
if (score5 < lowest) lowest = score5;
return lowest;
}
void calcAverage (int score1, int score2, int score3, int score4, int score5) // Function that calcualtes and displays the average test score.
{ double average;
int lowest = findLowest(score1,score2,score3,score4,score5,lowest);
average = (( score1 + score2 + score3 + score4 + score5) - lowest) / 4.0;
cout << "The average of the 4 highest test scores is: " << average << endl;
}
int main()
{
int score1, score2, score3,score4, score5;
cout << "This program will average 5 test scores, with the lowest dropped, ";
cout << "Please enter scores to calculate the average: \n";
getScore(score1,score2,score3,score4,score5); // Calls function getScore
calcAverage(score1,score2,score3,score4,score5); //Calls function calcAverage
return 0;
}
|