我正在开始教科书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.所以sum = 21 难度在1.2和4.0之间:3.0 因此预期输入/输出为21 * 3.0 * 0.6 = 37.8 我实际上得到-0
答案 0 :(得分:2)
因为您将难度设置为零:
float difficulty = 0;
然后在你发现的功能中
void enterData(float scores[], const int judges, float difficulty)
这意味着你可以通过价值得到它。所以价值不会改变,你保持零。
你需要通过参考难度:
void enterData(float scores[], const int judges, float &difficulty)
答案 1 :(得分: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);
答案 2 :(得分:2)
你乘以难度从未改变为0.如果你想改变难度,将enterData的原型和定义改为以下
void enterData(float scores[], const int judges, float &difficulty)
有关通过引用传递的更详细说明,请参阅http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/。
答案 3 :(得分: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”超出此点无效。