我正在做一个开始的教科书 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