0

我正在做一个开始的教科书 c++ 作业。它是从删除最高和最低分数的数组中进行的基本计算。每次运行它,我都会得到 0 作为答案。我假设问题出在主要或计算分数上。我感谢任何愿意花几分钟时间来看看这个的人。

#include <iostream>
using namespace std;

void printHeader(int judges);
void enterData (float scores[], int judges, float difficulty);
float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least);
int indexofLeast(float scores[], const int judges);
int indexofMax(float scores[], const int judges);

int main () {
    const int judges = 7;
    float scores [judges];
    float difficulty = 0;
    int maxScore = indexofMax(scores, judges);
    int least = indexofLeast(scores, judges);
    float finalscore = calculateScore(scores, judges, difficulty, maxScore, least);

    printHeader (judges);
    enterData (scores, judges, difficulty);  // get user input
    indexofLeast(scores, judges); // find lowest score 
    indexofMax(scores, judges); // find highest score
    calculateScore (scores, judges, difficulty, maxScore, least); // get final score
    cout << "The final score is " << finalscore << '\n';

    return 0;
}

void printHeader(const int judges) {
    cout << "This program calculates a divers score over" << judges << "judges" << endl;
}

void enterData(float scores[], const int judges, float difficulty) {
    for (int i = 0; i < judges; i++){
        cout <<"Enter score for judge " << i+1 << endl; 
        cin >> scores[i];
    }
    cout << "Enter difficulty: "<< endl;
    cin >> difficulty;
}

float calculateScore(float scores[], const int judges, float difficulty, int maxScore, int least) {
    float sum = 0;
        for (int i = 0; i < judges; i++) {
        sum += scores[i];
    }
    return (sum - scores[least] - scores[maxScore]) * difficulty * 0.6;
}

int indexofLeast(float scores[], const int judges) {
    int least = 0;
    for (int i = 1; i< judges; i++) {
        if (scores[i] < scores[least])
            least = i;
    }
    return least;
}

int indexofMax(float scores[], const int judges) {
    int maxScore = 0;
    for (int i = 1; i< judges; i++) {
        if (scores[i] > scores[maxScore]) 
            maxScore = i; 
        }
    return  maxScore;
}

对于预期输入,我输入了 7 个分数,(介于 0 和 10 之间):1、2、2、4、5、8、10 所以降低最高和最低 1 和 10。所以总和 = 21 1.2 和 4.0 之间的难度: 3.0 所以 21 * 3.0 * 0.6 = 37.8 对于预期的输入/输出我实际上得到 -0

4

4 回答 4

2

因为您将难度设置为零:

    float difficulty = 0;

然后在你清除的功能中

 void enterData(float scores[], const int judges, float difficulty)

这意味着您按价值获得它。所以值没有改变,你保持为零。

您需要传递对难度的引用:

 void enterData(float scores[], const int judges, float &difficulty)
于 2013-07-02T23:58:41.553 回答
2

您需要difficulty作为参考传递到enterData.

void enterData(float scores[], const int judges, float &difficulty)
                                                       ^

问题是您希望它包含用户输入的值。您将其初始化为零,然后调用enterData. 但是当该函数返回时,该值仍然为零,因为参数是局部变量。稍后,您将结果乘以difficulty,因此结果始终为零。

使用引用返回用户输入的值应该可以解决您的问题。从语法上讲,您唯一需要更改的是添加&到函数定义中,如我所示。您仍然以相同的方式调用该函数。

我敦促您培养自己批判性地分析和测试代码的技能,而不是询问他人。如果您认为问题出在分数计算中,您可以轻松显示所有进入分数计算的值,然后从那里向后工作...... 即“为什么难度为零?它在什么时候变为零?”


为了解决您在评论中的问题,您的代码以错误的顺序做事。您需要在用户输入数据后分配返回值:

const int judges = 7;
float scores [judges];
float difficulty = 0;

printHeader (judges);
enterData (scores, judges, difficulty);  // get user input

int maxScore = indexofMax(scores, judges);
int least = indexofLeast(scores, judges);
float finalscore = calculateScore(scores, judges, difficulty, maxScore, least);
于 2013-07-03T00:00:19.657 回答
2

你是乘以从 0 到永远不会改变的难度。如果你想改变难度,请将 enterData 的原型和定义更改为以下

void enterData(float scores[], const int judges, float &difficulty)

有关按引用传递的更详细说明,请参阅http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

于 2013-07-03T00:02:25.917 回答
0

为了扩展以前的答案,我认为您在理解“=”在 C/C++ 中的工作原理方面陷入了一个常见错误,通常是从像代数一样思考变量中得到的。

int maxScore = indexofMax(scores, judges);

这并没有描述“maxScore”是什么或将是什么,它为它分配了一个值。考虑#include

int f(int n);  // some mathematical function.

int main(int argc, const char** argv)
{
   int a = 1;
   int b = f(a);
   a = 15;
   std::cout << "b = " << b << std::endl;
   return 0;
}

int f(int n) { return n * 2; } // f(n) = 2f

该程序将打印“2”,而不是“30”。

int b = f(a);
// is equivalent to
int b;
b = f(a);

在这两种情况下,b都已根据当时对 f(a) 的评估来分配一个值。超过此点更改“a”无效。

于 2013-07-03T01:08:21.350 回答