C++ 如何找到用户输入的平均值
下面是我的代码(去掉了标题,因为这是无关的。)C++ 如何找到用户输入的平均值,c++,C++,下面是我的代码(去掉了标题,因为这是无关的。) intmain(){ 浮动程序=0; 浮动分数=0; 浮动试验=0; 浮动测试分数=0; 浮点数e=1; 浮动检验=0; 浮点数平均值=0; cout>程序; 对于(int i=1;ifloat\u sum=0; 对于(int i=1;i嗯,是的,因为这个循环,分数总是输入最后一个值: for (int i = 1; i <= program; i++){ cout << "Enter the score for assign
intmain(){
浮动程序=0;
浮动分数=0;
浮动试验=0;
浮动测试分数=0;
浮点数e=1;
浮动检验=0;
浮点数平均值=0;
cout>程序;
对于(int i=1;ifloat\u sum=0;
对于(int i=1;i嗯,是的,因为这个循环,分数总是输入最后一个值:
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
然后在'coutintmain()的行之后{
浮动程序=0;
浮动分数=0;
浮动试验=0;
浮动测试分数=0;
浮点数e=1;
浮动检验=0;
浮点数平均值=0;
float scoresSum=0;//将所有输入分数相加的变量
cout>程序;
对于(int i=1;i hint),创建另一个变量并使用它对“输入分数…”中的所有分数进行求和循环。我对编码非常陌生。你能给我举个例子吗?float total_of scores=0;
然后在你的循环中,在阅读scores
之后,total_of_scores+=scores;
。就像这样,你不断地用连续的值覆盖以前的scores
和testScores
y使用上一个值。如果需要保留分数,以便在全部读取后计算(比如说stddev),则可以将它们推回到向量
,然后迭代访问它们……这将是一个很好的“第二阶段”对于你学习C++的问题,计算机要按照你的说法去做,你需要精确地告诉他们如何把这些数字加起来。你需要在每个迭代中创建一个新的变量并添加<代码>分数>代码。请看我的答案。还有我的提示和谷歌,你可以走很远。谷歌“在C++中创建变量”然后是谷歌。“add to variable”。@DougT.最后你也这么做了…尽管有更多的解释。问题是,在他看到代码后,这种需要是否是不言而喻的。+1来自我。
float _sum=0;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
_sum+=i;
}
programAverage = (_sum/program);
cout << "Program Average: " << programAverage << endl;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
double sum = 0.0;
sum = sum + scores;
sum += scores
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
float scoresSum = 0; // variable that adds up all the input scores
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
scoresSum += scores; // adds up all the scores
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scoresSum/program); // divide the total score out of program number
cout << "Program Average: " << programAverage << endl;
}