1
votes

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;
}
1
For a start: read the documentation of the functions you use! scanf returns an int, not a float.too honest for this site
Hi are you sure that is what's causing the issue? I had it print to terminal the value of "a" each time it was set and it displayed the correct value. I believe the error is coming from the addition of the float values. My "total" var for each run, with the same inputs, has different outputs each time. I declared "flag" as an int and no change.Apple
I did not say it is causing the issue, but it would be a start using the correct types for which you have to read the documentation. And EOF is not the only value reporting a problem, which also becomes clear from the docs.too honest for this site

1 Answers

2
votes

You forgot to initialize total to 0.

Further notes:

  • the classic C idiom for an infinite loop is for (;;) { ... }.
  • flag should be defined as an int
  • you should stop the loop when flag != 1 instead of just flag == EOF. An invalid input will cause the program to loop endlessly. You can actually drop this variable completely.
  • initializing big to 0 and small to 1000000 is incorrect: what if all values are negative? what if they are all very large?

Here is a corrected version:

#include <stdio.h>

int main(void) {
    float a, big, small, total;
    int div;

    if (scanf("%f", &a) != 1)
        return 1;

    big = small = total = a;
    div = 1;
    while (scanf("%f", &a) == 1) {
        if (big < a) {
            big = a;
        }
        if (small > a) {
            small = a;
        }
        div++;
        total += a;
    }
    printf("%.2f %.2f %.2f\n", small, big, total / div);
    return 0;
}