I'm not expecting anyone to just hand me the answer, but I am looking for some guidance. For my C++ class we wrote a program last week where we had 5 judges each had a score that the user had to put in, then we needed to find the average without the highest and lowest score being used. I did this using loops and a lot of if statements. Now my teacher asked us to go back and use arrays, and said we should be able to just modify the original code a bit, but to now have it so there can be 5 to 20 judges, the user should be able to input that number in. I'm just having a hard time figuring out which spots could be replaced with arrays. I already have it where a person can put in the amount of judges, but I'm not sure how to put that in an array either, so judges is an unused variable at the moment.This is what my code looks like right now. Thanks in advance!
#include <iostream>
using namespace std;
//Function prototypes
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);
double judges;
//Program begins with a main function
int main()
{
//Declare variables
double judgeScore1,judgeScore2,judgeScore3;
double judgeScore4,judgeScore5;
cout<<"Scores given by all five judges: \n\n";
//Function calls to get each judge data
cout<< "How many Judges are there? (Must be 5 to 20): ";
cin>> judges;
while(judges<5||judges>20){
cout<< "Sorry there must be 5 to 20 judges\n";
cout<< "How many Judges are there? (Must be 5 to 20): ";
cin>> judges;
getJudgeData(judgeScore1);
getJudgeData(judgeScore2);
getJudgeData(judgeScore3);
getJudgeData(judgeScore4);
getJudgeData(judgeScore5);
//Function call to obtain average
calcAverage(judgeScore1,judgeScore2,judgeScore3,
judgeScore4,judgeScore5);
//Pause the system for a while
}
}
//Method definition of getJudgeData
void getJudgeData(double &x)
{
//Prompt and read the input from the user and check //input validation
cout<<"Enter score of a Judge (you will do this 5 times): ";
cin>>x;
while (x < 0 || x > 10)
{
cout<<"ERROR: Do not take judge scores lower than 0 or higher than 10.Re-enter again: ";
cin>>x;
}
}
//Method definition of findLowest
double findLowest(double a,double b,double c,
double d,double e)
{
double lowest;
lowest=a;
if (b<lowest)
lowest=b;
if (c<lowest)
lowest=c;
if (d<lowest)
lowest=d;
if (e<lowest)
lowest=e;
return lowest;
}
//Method definition of findHighest
double findHighest(double a,double b,double c,
double d,double e)
{
double highest;
highest=a;
if (b>highest)
highest=b;
if (c>highest)
highest=c;
if (d>highest)
highest=d;
if (e>highest)
highest=e;
return highest;
}
void calcAverage(double a,double b,double c,double d,
double e)
{
//Declare variables
double lowest;
double highest;
double sum;
double average;
//Function call to retrieve lowest score
lowest=findLowest(a,b,c,d,e);
//Function call to retrieve highest score
highest=findHighest(a,b,c,d,e);
//Calculate the total sum
sum=a+b+c+d+e;
//Subtract highest and lowest scores from the total
sum=sum-highest;
sum=sum-lowest;
//Calculate the average of the three number after
//dropping the highest and lowest scores
average=sum/3;
//Display output
cout<<"Highest score of five numbers:"<<highest<<endl;
cout<<"Lowest score of five numbers:"<<lowest<<endl;
cout<<"Average when highest and lowest scores are removed: "<<average<<endl;
}
Aucun commentaire:
Enregistrer un commentaire