4

I just started learning C and prior to that I have no knowledge of programming.

So I am trying to run this one stat program, that will read input and give you mean, variance and etc. I used terminal to run the program. I pretty much copied the code from the book I am using.

There was no error when I ran the code but when I enter the input, it didn't do anything. The code is below.

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

int main()
{
    float x, max, min, sum, mean, sum_of_squares, variance;
    int count;
    printf("Enter data: "); /* not included in the original code*/

    if( scanf("%f", &x) == EOF )
        printf("0 data items read.\n");
    else{
        max = min = sum = x;
        count = 1;
        sum_of_squares = x * x;
        while(scanf("%f", &x) != EOF) {
            count += 1;
            if (x > max)
                max = x;
            if ( x < min)
                min = x;
            sum += x;
            sum_of_squares += x * x;
        }
        printf("%d data items read\n", count);
        printf("maximum value read = %f\n", max);
        printf("minimum value read = %f\n", min);
        printf("sum of all values read = %f\n", sum);
        mean = sum/count;
        printf("mean = %f\n", mean);
        variance = sum_of_squares / count - mean * mean;
        printf("variance = %f\n", variance);
        printf("standard deviation = %f\n", sqrt(variance));
    }
}
abuchay
  • 151
  • 3
  • Possible duplicate of [End of File (EOF) in C](http://stackoverflow.com/questions/4358728/end-of-file-eof-in-c) – molbdnilo Jan 12 '16 at 21:36

1 Answers1

11

The code is fine as it is. You probably don't understand the "terminating condition". The program reads input in an infinite loop and you have to send EOF to terminate the loop.

To send EOF, you can use ctrl + D on unix systems and ctrl + Z on windows.

P.P
  • 117,907
  • 20
  • 175
  • 238