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));
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With