Complete the following program that calculates the average of a group of test sc
ID: 3552672 • Letter: C
Question
Complete the following 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:
void getScore( ) should ask the user for a test score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered.
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.
Do not accept test scores lower than 0 or higher than 100.
#include <iostream>
using namespace std;
void getScore(int &a)
int main()
{
int score1; // To hold the 1st test score
int score2; // To hold the 2nd test score
int score3; // To hold the 3rd test score
int score4; // To hold the 4th test score
int score5; // To hold the 5th test score
// Get the five test scores.
// Calculate and display the test score average,
// after dropping the lowest score.
return 0;
}
// ********************************************************
// The getScore function asks the user for a test score, stores it in a
// a reference parameter variable, and validates it.
// ********************************************************
void getScore(int &score)
{
// Ask the user for a test score.
cout << "Enter a test score: ";
cin >> score;
// Validate the score.
}
// ********************************************************
// The calcAverage function calculates and displays the average of
// the four highest scores.
// ********************************************************
void calcAverage(int score1, int score2, int score3, int score4, int score5)
{
// Get the lowest score.
// Sum all of the scores.
// Drop the lowest score and calculate the average.
int average = (total - lowest) / 4;
// Display the average.
}
// ********************************************************
// The findLowest function finds and returns the lowest of the five
// scores passed to it.
// ********************************************************
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
int lowest; // To hold the lowest score.
// Determine the lowest of the five scores.
if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5)
lowest = score1;
else if
(score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5)
lowest = score2;
else if
(score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5)
lowest = score3;
else if
(score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5)
lowest = score4;
else
lowest = score5;
// Return the lowest score.
}
Explanation / Answer
#include <iostream>
using namespace std;
void getScore(int &a);
int findLowest(int , int , int , int , int );
void calcAverage(int , int , int , int , int );
int main()
{
int score1; // To hold the 1st test score
int score2; // To hold the 2nd test score
int score3; // To hold the 3rd test score
int score4; // To hold the 4th test score
int score5; // To hold the 5th test score
// Get the five test scores.
getScore(score1);
getScore(score2);
getScore(score3);
getScore(score4);
getScore(score5);
// Calculate and display the test score average,
// after dropping the lowest score.
calcAverage(score1,score2,score3,score4,score5);
return 0;
}
// ********************************************************
// The getScore function asks the user for a test score, stores it in a
// a reference parameter variable, and validates it.
// ********************************************************
void getScore(int &score)
{
// Ask the user for a test score.
cout << "Enter a test score: ";
cin >> score;
// Validate the score.
if (score <0 || score >100)
{
cout<<"The test score entered by you is not valid.";
}
}
// ********************************************************
// The calcAverage function calculates and displays the average of
// the four highest scores.
// ********************************************************
void calcAverage(int score1, int score2, int score3, int score4, int score5)
{
// Get the lowest score.
int lowest = findLowest(score1,score2,score3,score4,score5);
// Sum all of the scores.
int total = score1 + score2 + score3 + score4 + score5;
// Drop the lowest score and calculate the average.
int average = (total - lowest) / 4;
// Display the average.
cout<<"Average of 4 highest scores : "<<average;
}
// ********************************************************
// The findLowest function finds and returns the lowest of the five
// scores passed to it.
// ********************************************************
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
int lowest; // To hold the lowest score.
// Determine the lowest of the five scores.
if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5)
lowest = score1;
else if
(score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5)
lowest = score2;
else if
(score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5)
lowest = score3;
else if
(score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5)
lowest = score4;
else
lowest = score5;
// Return the lowest score.
return lowest;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.