如果我沒有正確格式化我的問題,我表示歉意。我是這個網站的新手,也是編程新手。我的輸出顯示不正確
我目前正在進行一項C作業,我相信我已經完成了大部分代碼,但有一些調整我似乎無法弄清楚。我會很感激任何反饋。這裏是我的代碼
#include <stdio.h>
#include <stdlib.h>
#define SENTINAL -1
double sumOfScores = 0;
double examScore = 0;
double sumOfExams = 0;
double average = 0;
double calculateAverage();
double main(void)
{
int i;
for (i = 1; i <= 4; ++i)
{
calculateAverage();
}
return 0;
}
double calculateAverage()
{
printf("Enter %d to terminate program. \n", SENTINAL);
while(examScore != SENTINAL)
{
printf("Enter test score: \n");
scanf("%lf", &examScore);
sumOfScores += examScore;
sumOfExams++;
average = sumOfScores/sumOfExams;
}
printf("The average of the test scores entered thus far is %.2lf \n\n", average);
return 0;
}
這裏是我的輸出
Enter -1 to terminate program.
Enter test score:
99
Enter test score:
98
Enter test score:
97
Enter test score:
96
Enter test score:
-1
The average of the test scores entered thus far is 77.80
Enter -1 to terminate program.
The average of the test scores entered thus far is 77.80
Enter -1 to terminate program.
The average of the test scores entered thus far is 77.80
Enter -1 to terminate program.
The average of the test scores entered thus far is 77.80
這是我想它看起來像
Enter -1 to terminate program.
Enter test score:
99
Enter test score:
98
Enter test score:
97
Enter test score:
96
Enter test score:
-1
The average of the test scores entered thus far is 77.80
Enter -1 to terminate program.
Enter test score:
95
Enter test score:
94
Enter test score:
93
Enter test score:
92
Enter test score:
-1
The average of the test scores entered thus far is (avg goes here)
我並沒有包括在額外的兩組號碼是什麼我想要的輸出,但我希望能夠用四組數字來做到這一點。只要我輸入(-1)來終止第一組數字,它就會自動將我剩下的3組數據的平均值拋出,然後我甚至可以輸入我想輸入的數字。另外,爲什麼當它應該在90年代上漲時,它爲我提供了第一套價值77.8的平均值?
在「終止」calculateAverage函數後,「examScore」的值是多少?解決方法是不使用全局變量。 –
謝謝@JoachimPileborg快速回復。我按照你的說法(以及另一個用戶說的)來做,並解決了其中一個問題。但是我得到的「平均值」的價值仍然不正確。 – lopezgera92
'雙主(void)'???從'void main()'做一個改變'我想...... –