2014-02-20 52 views
1

所以這裏是我的代碼(剝去頭,因爲這是irrerevelant。)如何找到用戶輸入的平均

int main() { 

float program = 0; 
float scores = 0; 
float test = 0; 
float testScores = 0; 
float e = 1; 
float exam = 0; 
float programAverage = 0; 

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; 

} 
    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 = (scores/program); 
    cout << "Program Average: " << programAverage << endl; 
} 

我有,因爲每當我編譯我的程序的編譯器只是問題的最後一部分記得用戶輸入的最後一個數字,並不會對其進行平均。我怎樣才能將所有的用戶輸入數字加在一起然後平均?

+2

提示,創建另一個變量,並使用它來彙總「輸入分數...」循環中的所有'分數'輸入。 –

+0

我很新的編碼,你可以給我一個例子或什麼? – user3320545

+0

'float total_of scores = 0;'然後在你的循環內部,在閱讀'scores','total_of_scores + = scores;'後。如此,您不斷覆蓋以前的「分數」和「測試分數」,並且沒有真正使用過去的值。如果你需要保持分數,這樣你就可以在讀完所有的時候計算stddev,你可以將它們push_back到vector中,然後遍歷它們以訪問它們......這將是一個很好的「第二階段」你學習C++。 –

回答

1
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; 
} 

所以,問題是,你沒有加起來輸入分數。 變量「分數」只有最後輸入分數的值。 您必須設置一個變量來總結迄今爲止的所有輸入分數,例如代碼中的scoresSum。 並在每次提交評分時加分。

通過查看評論行,您可以輕鬆找到代碼和我的代碼之間的區別。

+0

哎呀..我會編輯它。 – kimsy

+0

以爲我已經這樣做了......無論如何感謝:) – kimsy

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; 
+0

-1當你明顯需要學習釣魚時,你給了他魚。 –

+0

投票中沒有意義。 –

+1

@DougT。你最後也是......儘管有更多的解釋。問題是在看到代碼後,這種需求是否是不言而喻的。從我+1。 –

0

好耶,因爲這個循環,scores總是已經進入了最後一個值:

for (int i = 1; i <= program; i++){ 
    cout << "Enter the score for assignment # " << i <<": "; cin >> scores; 
} 

的平均值被定義爲總和通過實例的數量劃分。你沒有總結,當你做cin >> scores時,你只是用最後一次讀取的值保留「分數」。所以這個問題可以重申爲「我如何計算用戶輸入的所有數字?」電腦做正好你告訴他們,你需要弄清楚如何完全告訴它總結所有輸入scores

那麼你會如何做到這一點在現實生活中?你可以保持所有分數的運行,也許可以通過計算器添加它們。你首先初始化計數:

double sum = 0.0; 

然後換行後`COUT < <「輸入成績......」你加來概括:

sum = sum + scores; 

或者C++有方便的速記符號

sum += scores