I need some help with a C++ program I have to do. The instructions are as follow
ID: 3798195 • Letter: I
Question
I need some help with a C++ program I have to do. The instructions are as follows:
I have to Write a program that calculates the average of a group of 10 test scores, where the lowest score and the highest score in the group are dropped. It should use the following functions;
- getScore() should ask the user for a test score, validate the input. Validation: Do not accept test scores lower than 0 or higher than 100. This function should return a validated score.
-The main function should call getScore() for each of the 10 scores to be entered. The main should store the 10 scores in an array.
-finsHighest() should take the array as input parameter and find and remove the highest score. It should return the array to the main function. You will have to find a way to remove the highest score and return the remaining 9 scores.
-findLowest() should take the array as input parameter and find and remove the lowest score. It should return the array to main. You will have to find a way to remove the lowest score and return the remaining 8 scores.
- calcAverage() should take the array as input and calculate the average of the 8 remaining scores, return the average score to the main.
-The main function displays the average score.
Thank you in Advance for the help.
Explanation / Answer
// C++ code
#include <iostream>
using namespace std;
double getScore()
{
double score;
while(true)
{
cout << "Enter score: ";
cin >> score;
if (score <= 0 || score >= 100)
{
cout << "Invaild test score! ";
}
else
break;
}
return score;
}
double calcAverage(double scoreArray[], int size)
{
double avg = 0;
for (int i = 0; i < size; ++i)
{
avg= avg + scoreArray[i];
}
return avg/size;
}
int findLowest(double scoreArray[], int size)
{
double min = scoreArray[0];
int idx = 0;
for (int i = 1; i < size; ++i)
{
if(scoreArray[i] < min)
{
min = scoreArray[i];
idx = i;
}
}
// removing the lowest element
for(int i=idx; i<size-1; i++)
{
scoreArray[i] = scoreArray[i+1];
}
size--;
cout << " lowest value " << min << " removed ";
return size;
}
int findHighest(double scoreArray[], int size)
{
double max = scoreArray[0];
int idx = 0;
for (int i = 1; i < size; ++i)
{
if(scoreArray[i] > max)
{
max = scoreArray[i];
idx = i;
}
}
// removing the highest element
for(int i=idx; i<size-1; i++)
{
scoreArray[i] = scoreArray[i+1];
}
size--;
cout << " highest value " << max << " removed ";
return size;
}
int main()
{
int size = 10;
double scoreArray[size];
for (int i = 0; i < size; ++i)
{
scoreArray[i] = getScore();
}
size = findHighest(scoreArray,size);
size = findLowest(scoreArray,size);
double avg = calcAverage(scoreArray, size);
cout << " Average: " << avg << endl;
return 0;
}
/*
output:
Enter score: 1
Enter score: 2
Enter score: 3
Enter score: 4
Enter score: 5
Enter score: 6
Enter score: 7
Enter score: 8
Enter score: 9
Enter score: 10
highest value 10 removed
lowest value 1 removed
Average: 5.5
*/