Requirements for this task are as follows:
- Read floating point values from stdin, each separated by a newline, and terminated by EOF.
- The input values are in the range [-100,000 to +100,000].
- The input will contain at least one floating-point value.
- The input may contain blank lines: these should be ignored.
- Apart from possible blank lines, the input is otherwise well-formed.
At EOF, output:
- the smallest value seen
- the largest value seen
- the arithmetic mean of all the values seen
all accurate to two decimal places. The output values must be separated by a single space character and followed by a newline character.
Examples:
Input:
7
5.6
6
Output:
5.60 7.00 6.20
Input:
11
Output:
11.00 11.00 11.00
In my code when I input 7, 5.6, and 6, my output is 5.60 7.00 5.77. I know where the issue is but not sure how to fix it. My total variable says its value at EOF is 17.322826 which is definitely not correct.
#include <stdio.h>
int main() {
int i = 0;
float big = 0;
float small = 1000000;
float total;
float div = 0;
while (i == 0) {
float a = 0;
float flag = scanf("%f", &a);
if (flag == EOF) {
printf("%.2f %.2f %.2f %f %f\n", small, big, total / div, total, div);
break;
}
if (a > big) {
big = a;
}
if (a < small) {
small = a;
}
div++;
total = total + a;
}
return 0;
}
scanf
returns anint
, not afloat
. – too honest for this siteEOF
is not the only value reporting a problem, which also becomes clear from the docs. – too honest for this site