【问题标题】:New to C++ Looking for help revising a program by using arraysC++ 新手 寻求使用数组修改程序的帮助
【发布时间】: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


【解决方案1】:

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

double judgeScore[20];

变量“judges”应该声明为“int”,因为它是法官的数量。

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

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

函数变为:

double findLowest(double s[]);

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

【讨论】:

    【解决方案2】:

    修改您的函数以使用数组。例如,这是一个用于数组的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");
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 2011-04-05
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多