Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The code is not running in terminal

Tags:

c

terminal

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));
    }
}
like image 764
abuchay Avatar asked Dec 18 '22 20:12

abuchay


1 Answers

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.

like image 177
P.P Avatar answered Jan 05 '23 02:01

P.P