2016-08-05 7 views
1

"C by Example"という本からCを学んでいます計算が正しく機能しない

各章の終わりに、練習があります。あなたは以下のコードからわかるように、運動がトップ

/* Chapter 7 Review Exercises #2 
- You are a college professor and you have to get the average grades for 10 students. Write a program which prompts you for 10 different grades and then 
displays their average */ 

#include <stdio.h> 

int main() 
{ 

    float score1, score2, score3, score4, score5, score6, score7, score8, score9, score10; 

    float average = score1+score2+score3+score4+score5+score6+score7+score8+score9+score10/10; 

    printf("Please input the scores of the students: \n"); 
    scanf(" %f",&score1); 
    scanf(" %f",&score2); 
    scanf(" %f",&score3); 
    scanf(" %f",&score4); 
    scanf(" %f",&score5); 
    scanf(" %f",&score6); 
    scanf(" %f",&score7); 
    scanf(" %f",&score8); 
    scanf(" %f",&score9); 
    scanf(" %f",&score10); 

    printf("The average score is: %.2f" , average); 

    return 0; 

} 

で書かれているここでの問題は、私は入力番号たびに(コードを実行している場合)ということであるそれは常に(バック私の膨大な数を提供します少なくとも20の数字のように)

番号を限定する方法はありますか、何か間違っていますか?

+0

[mcve]を提供してください。私たちがあなたの入力を知っていれば、答えが簡単になります。 – xenteros

+1

コンポーネントの値を入力した後で、 'average' **を計算する必要があります。あるいは、' average'を必要に応じて常に読み直す関数にします。つまり、初期化されていない変数を読み込み(10)、 'average'に格納します。これは' score'のどれかが変更されても変更されません。 –

+0

私はあなたに訂正されたコードで答えを与えました。お気軽にご利用ください。それは2度upvoted upvotedと答えを促進するために他の回答者によって2度downvotedしました:/正しい答えを緑のチックと正しい答えをupvoteの解決策としてマークするのが良い習慣です。 – xenteros

答えて

1

次のコードは動作します。スキャンする前に数値を計算しようとしたため、コードが機能しません。それでも、Cは操作の順序を維持するので、+の前に/が実行されることを覚えておく必要があります。括弧が足りない場合は()を追加してください。 Cでは、コードは上から下に処理されます!

#include <stdio.h> 
int main() { 

    float score1, score2, score3, score4, score5, score6, score7, score8, score9, score10; 

    printf("Please input the scores of the students: \n"); 
    scanf(" %f",&score1); 
    scanf(" %f",&score2); 
    scanf(" %f",&score3); 
    scanf(" %f",&score4); 
    scanf(" %f",&score5); 
    scanf(" %f",&score6); 
    scanf(" %f",&score7); 
    scanf(" %f",&score8); 
    scanf(" %f",&score9); 
    scanf(" %f",&score10); 

    float average = (score1+score2+score3+score4+score5+score6+score7+score8+score9+score10)/10; 
    printf("The average score is: %.2f" , average); 

    return 0; 

} 
+1

OKの答えが動作し、これはxenterosに対するコメントではありませんが、それを行う非常に効率的な方法ではありません。ループを使用してみてください。しかし、まだ学習していないかもしれません。 –

3

あなたがここにいくつかの括弧が欠落しています

float average = score1+score2+score3+score4+score5+score6+score7+score8+score9+score10/10; 

変更これに:

float average = (score1+score2+score3+score4+score5+score6+score7+score8+score9+score10)/10; 

ます。また、それは入力段の後が来るようにダウンこの行を移動する必要があります(つまり、 scanfへのすべての呼び出しの後)、の前にの何かを試して計算するのは意味をなさないので、すべての入力値があります。


あなたのプログラムがはるかに簡単かつ簡潔に書くことができるので、あなたは、演算子の優先順位上に読んでもらいたい、ともループがあります

#include <stdio.h> 

int main() 
{ 
    const int n = 10; 
    float sum = 0.0f; 
    float average; 
    int i; 

    printf("Please input the scores of the students: \n"); 

    for (i = 0; i < n; ++i) 
    { 
     float score; 

     scanf(" %f", &score); 
     sum += score; 
    } 

    average = sum/n; 

    printf("The average score is: %.2f" , average); 

    return 0; 
} 
+0

ああ、私はあなたがそれをしなければならないことを知らなかった!本当にありがとうございます:D – ImperfectLion

1

をあなたはドンので数は、確かに間違っています」 Cがどのようにコードを実行するかを理解する。それはボトムの頂点で働く。だからscore1 ...スコア10の値が必要な場合は、最初にを実行し、scanfを実行してから+を使用する必要があります。

+0

それを修正したああ!助けてくれてありがとう! C: – ImperfectLion

関連する問題