我非常困惑。我必须遵循以下特定准则:
制作一个程序,该程序将计算并输出用户输入的10个考试成绩的平均值。完成功能,以便主电源正常工作。也可以随意修改主体。 我不认为我们需要更改主变量,也不相信应该使用10个变量。这是我必须添加的代码,我相信主体已经完成。非常感谢您的帮助!!!!
#include <iostream>
using namespace std;
NOT ADD CODE HERE!!!
float calculateAverageTestScore(int amountOfScores);
int main(void)
{
const int NUMBER_OF_SCORES = 10;
cout << "The average test score was: " << calculateAverageTestScore(NUMBER_OF_SCORES) << endl;
return 0;
}
//INPUT: an amount of test scores
//OUTPUT: the average of these scores
//This function will obtain amountOfScores test scores from the user, and return
//their average - to be used elsewhere.
float calculateAverageTestScore(int amountOfScores)
{
float average;
//add code here
return average;
}
答案 0 :(得分:0)
应该做到这一点。
#include <iostream>
#include <string>
float calculateAverageTestScore(int amountOfScores);
int main() {
const int NUMBER_OF_SCORES = 10;
const float score = calculateAverageTestScore(NUMBER_OF_SCORES);
std::cout << "The average test score was: " << score << '\n';
}
float calculateAverageTestScore(int amountOfScores) {
float sum = 0;
std::string buffer;
for (int i = 0; i < amountOfScores; ++i) {
std::cout << "score #" << i + 1 << ": ";
std::getline(std::cin, buffer);
sum += std::stof(buffer); // TODO: Error handling
}
return sum / amountOfScores;
}