How do I receive elements into an array using scanf?

Viewed 79

I'm learning C programming and I've encountered a problem using scanf for initializing values into the array. In this example, 10, 32 and 20 were input as values for the array; 20 should be in grades[2] but its value is 0.

Why doesn't the program register the last value that is input?

That is the relevant code.

I'll appreciate any help in understanding what went wrong with the program.

#include <math.h>
#include <stdio.h>
#include <stdbool.h>

#define N 50
#define MaximalSTD 10

int main() {
    printf("Please enter the grades of the examinees");
    printf(" followed by the expected mean\n");
    double grades[N], ReqMean; 
    int numgrade = 0;
    for (int i = 0; i < N; i++) {
        if (scanf("%lf", &grades[i]) == 1) {
            numgrade++;
        } else
            break;
    }
    ReqMean = grades[numgrade - 1];
    printf("numgrade: %d\nReqMean: %d\n", numgrade, ReqMean);
    return 0;
}
1 Answers
printf("numgrade: %d\nReqMean: %d\n" , numgrade,ReqMean);

You are using the wrong format specifier for a double value (ReqMean in this case). Try Instead.

printf("numgrade: %d\nReqMean: %lf\n" , numgrade,ReqMean);
Related