/* * Program to read floating-point numbers from input and report * how many fall into different ranges. (Not an entirely sensible * program, but an example of where an array makes sense.) * * Input: Zero or more floating-point numbers, read from standard * input, terminated by anything other than a number. * * Output: Error messages for any input numbers that are "out of range" * (less than 0, or greater than or equal to N (=10 here)), then counts * of how many input numbers n are in each of the following ranges: * * 0 <= n < 1 * 1 <= n < 2 * .... * N-1 <= n < N */ #include #include #define N 10 int main(void) { int counts[N] = { 0 }; float input; int index; int i; printf("enter numbers (>= 0 and < %d), non-number to end:\n", N); while (scanf("%f", &input) == 1) { if ((input < 0) || (input >= N)) { printf("%f out of range\n", input); } else { index = (int) input; counts[index] += 1; } } for (i = 0; i < N; ++i) { printf("count of inputs >= %d and < %d: %d\n", i, i+1, counts[i]); } return EXIT_SUCCESS; }