【发布时间】:2016-03-31 00:41:29
【问题描述】:
我不希望任何人直接给我答案,但我正在寻找一些指导。对于我的 C++ 课程,我们上周编写了一个程序,我们有 5 名评委,每个评委都有一个用户必须输入的分数,然后我们需要在不使用最高和最低分数的情况下找到平均值。我使用循环和很多 if 语句来做到这一点。
现在老师让我们回去用数组,说我们应该可以把原来的代码稍微修改一下,但是现在有5到20个评委,用户应该可以输入那个数字。我只是很难弄清楚哪些点可以用数组替换。
我已经有一个人可以放入法官数量的地方,但我也不确定如何将它放入数组中,所以法官目前是一个未使用的变量。这就是我的代码的样子现在。提前致谢!
#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;
}
【问题讨论】:
-
我建议使用数组来跟踪评委分数,例如
double judgeScore[nbrOfJudges]。现在您可以通过judgeScore[2]访问评委分数。如果允许您使用 std::vector,我建议您使用它而不是数组。 -
您的问题是什么?正确缩进你的代码。
标签: c++ arrays loops if-statement project