/* * Program to read grades (numbers between 0 and 100 inclusive) input * and report how many fall into ranges 0 .. 9, 10 .. 19, etc. * * Input: Zero or more integers, read from standard input, terminated * by anything other than a number. * * Output: Error messages for any input numbers that are "out of range" * (not between 0 and 100), then counts of how many input numbers are * in each of the following ranges: * * 0 <= n < 9 * 10 <= n < 19 * .... * 90 <= n < 100 */ #include int main(void) { int input; int counts[10] = { 0 }; int index; int low, high; printf("enter grades (between 0 and 100), non-number to end:\n"); while (scanf("%d", &input) == 1) { /* if input is not in 0 .. 100 range, print error message */ if ((input < 0) || (input > 100)) { printf("grades must be between 0 and 100\n"); } /* figure out which counter to update and update it */ else { if (input == 100) index = 9; else index = input / 10; counts[index] += 1; } } /* print counts */ for (index = 0; index <= 9; ++index) { /* compute start and end of each grade range */ low = index * 10; if (index == 9) { high = 100; } else { high = low + 9; } printf("%d grades between %d and %d\n", counts[index], low, high); } return 0; }