C++ 新手 寻求使用数组修改程序的帮助

New to C++ Looking for help revising a program by using arrays

我不希望任何人直接给我答案,但我正在寻找一些指导。对于我的 C++ class,我们上周编写了一个程序,其中有 5 位评委,每人都有一个用户必须输入的分数,然后我们需要在不使用最高分和最低分的情况下找到平均值。我使用循环和大量 if 语句来完成此操作。

现在我的老师让我们回去用数组,说我们可以把原来的代码稍微修改一下,但是现在有了它可以有5到20个法官,用户应该是能够输入那个数字。我只是很难弄清楚哪些点可以用数组替换。

我已经有了它,人们可以在其中输入法官的数量,但我也不确定如何将其放入数组中,所以法官是 moment.This 处未使用的变量是什么我的代码现在看起来像。提前致谢!

#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;    
}

使用数组来保存评委的分数,而不是将它们存储在 judgeScore1, ... , judgeScore5 中。数组的大小应该是20,也就是法官的最大数量:

double judgeScore[20];

变量"judges"应该声明为"int",因为它是评委的人数。

这些函数应该接受一个双精度数组作为参数,而不是 5 个双精度值。所以,而不是:

double findLowest(double ,double ,double ,double,double);

函数变为:

double findLowest(double s[]);

函数体应使用变量 "judges" 作为 "for" 循环的限制以执行计算。

修改您的函数以使用数组。例如,这里有一个用于数组的 get_max 函数:

#include <iostream>

double get_max(double judge_scores[], int size)
{
    double max = judge_scores[0];
    for (int i = 0; i < size; ++i)
    {
        if (judge_scores[i] > min)
        {
            max = judge_scores[i];
        }
    }
    return max;
}

像那个那样改变你所有的功能。我对您的问题进行了编码,这是 main 我得到的:

int main()
{
    const int MAX_NUM_JUDGES = 20;

    // make everything in the array 0
    double judge_scores[MAX_NUM_JUDGES] = { 0 };

    // make it return the "amount" of array you need- the number of judges
    int num_judges = get_num_judges();

    // fill the array up to that "amount"
    // internally uses int get_judge_data() to get a score in the correct range
    fill_score_array(judge_scores, num_judges);

    // get the average. Internally, this
    // - computes the sum
    // - calls the max and min functions and subtracts those from the sum
    // - divides that sum by (num_judges - 2)
    // (because you're throwing away the max and min) and returns that.
    double avg = calculate_curved_average(judge_scores, num_judges);

    //print everything
    std::cout << " Highest: " << get_max(judge_scores, num_judges) << std::endl;
    std::cout << " Lowest: " << get_min(judge_scores, num_judges) << std::endl;
    std::cout << " Avg when highest and lowest scores are removed: " << avg << std::endl;

    // If you're not on Windows, don't use this :)
    system("PAUSE");
}