user6650767
user6650767

Reputation:

Calculations Not working properly

I am learning C from a book called "C by Example"

At the end of each chapter there is an exercise to complete. As you can see from the code below, the exercise is written at the top

/* 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;

}

The problem here is that every time I input numbers (when running the code) It always gives me a HUGE number back (like at least 20 numbers long)

Is there a way to limit the number, or am I doing something wrong?

Upvotes: 1

Views: 86

Answers (3)

Paul R
Paul R

Reputation: 212949

You are missing some parentheses here:

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

Change this to:

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

You also need to move this line down so that it comes after the input stage (i.e. after all the calls to scanf), since it makes no sense to try and calculate something before you have all the input values.


You might want to read up on operator precedence, and also loops, since your program can be written much more simply and concisely:

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

Upvotes: 3

xenteros
xenteros

Reputation: 15842

The following code will work. Your code doesn't work because you try to calculate numbers before they are scanned. Still you have to remember that C keeps order of the operations so / is executed before +. Add missing brackets (). In C the code is processed from top to the bottom!

#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;

}

Upvotes: 1

MSalters
MSalters

Reputation: 179779

The number is indeed wrong, because you don't understand how C executes code. It works top to botom. So if you need the value of score1...score 10, you first need to do scanf and only then use +.

Upvotes: 1

Related Questions