Rewrite the solution to the Star Search problem, see page 370 Programming Challe
ID: 3574205 • Letter: R
Question
Rewrite the solution to the Star Search problem, see page 370 Programming Challenges, to use a dynamic array. Read the number of judges from the standard input device. Make sure the user enter numeric input; make sure that number of judges is greater than 0. Create a dynamic array, using the new operator, to store the judges' scores. here is my solution to the Star Search. #include #include using namespace std; void getJudgeData(double &x); double findLowest(double, double, double, double, double); double findHighest(double, double, double, double, double); void calcAverage(double, double, double, double, double); void main() { double score1, score2, score3, score4, score5; cout << "Score collection of all the five judges "; getJudgeData(score1); getJudgeData(score2); getJudgeData(score3); getJudgeData(score4); getJudgeData(score5); calcAverage(score1, score2, score3, score4, score5); system("pause"); } void getJudgeData(double &x) { do { cout << "Enter score of judge : "; cin >> x; if (x < 0 || x > 10 || cin.fail()) { cout << "Error please enter valid input! " << endl; cin.clear(); cin.ignore(); } } while (x < 0 || x > 10 || cin.fail()); } double findLowest(double a, double b, double c, double d, double e) { double lowest; lowest = a; if (blargest) largest = b; if (c>largest) largest = c; if (d>largest) largest = d; if (e>largest) largest = e; return largest; } void calcAverage(double a, double b, double c, double d, double e) { double lowest; double highest; double sum; lowest = findLowest(a, b, c, d, e); highest = findHighest(a, b, c, d, e); sum = a + b + c + d + e; sum = sum - highest; sum = sum - lowest; sum = sum / 3; cout << "Highest score of five numbers: " << highest << endl; cout << "Lowest score of five numbers: " << lowest << endl; cout << " After droping highest and lowest scores which you recieved, "; cout << "Your average score is " << sum << endl; }
Explanation / Answer
Please find the solution with dynamic array below:
#include<iostream>
#include<cstdlib>
using namespace std;
int numJudges;
void getJudgeData(double *);
double findLowest(double *);
double findHighest(double *);
double calcSum(double *);
void calcAverage(double *);
int main()
{
// take integer input from user for number of judges
cout<<"Please enter number of judges : ";
cin >> numJudges;
if(numJudges<=0){
cout<< "Invalid number of judges entered. Number of judges should be > 0.";
return 1;
}
double *scores = new double[numJudges];
cout << "Score collection of all "<<numJudges<<" judges ";
getJudgeData(scores);
calcAverage(scores);
system("pause");
}
void getJudgeData(double *a)
{
double x;
for (int i =0;i<numJudges;i++)
{
cout << "Enter score of judge # "<<(i+1)<<": ";
cin >> x;
if (x < 0 || x > 10 || cin.fail())
{
cout << "Error please enter valid input! " << endl;
cin.clear();
cin.ignore();
i--;
}else{
a[i]=x;
}
}
}
double findLowest(double *a)
{
double lowest=11;
for (int i =0;i<numJudges;i++)
{
if (a[i]<lowest)
lowest = a[i];
}
return lowest;
}
double findHighest(double *a)
{
double highest=0;
for (int i =0;i<numJudges;i++)
{
if (a[i]>highest)
highest = a[i];
}
return highest;
}
double calcSum(double *a){
double total=0;
for (int i =0;i<numJudges;i++)
{
total += a[i];
}
return total;
}
void calcAverage(double *scores)
{
double lowest=findLowest(scores);
double highest= findHighest(scores);
double sum= calcSum(scores);
double average = sum/numJudges;
cout << "Highest score of five numbers: " << highest << endl;
cout << "Lowest score of five numbers: " << lowest << endl;
cout << "Your average score is (including highest and lowest scores which you received) :" << average << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.