/* * Age Statistics, E.Hehner, 1997 Sep.3 * modified by Alan Rosenthal, Oct 2000 * * Standard input is a sequence of numbers representing people's ages. * (E.g. if five people are 18 years old, "18" appears five times in the * input.) Output states the maximum age, average age, and most common age. */ #include int main() { int count[120]; /* count[x] is count of people who are x years old */ int i, a; int inputsize, mostcommon, total, max; /* initialize whole array to zero */ for (i = 0; i < 120; i++) count[i] = 0; /* read ages, count histogram info */ while (scanf("%d", &a) != EOF) { if (a < 0 || a >= 120) printf("Age %d ignored.\n", a); else count[a]++; } /* calculate totals and max */ /* note that mostcommon starts off as indicating count[0], which is ok */ inputsize = mostcommon = total = 0; max = -1; for (i = 0; i < 120; i++) { inputsize += count[i]; total += i * count[i]; if (count[i] > count[mostcommon]) mostcommon = i; if (count[i] != 0) max = i; } if (inputsize > 0) printf("The average age is %g, the most common age is %d, " "and the maximum age is %d.\n", total / (double)inputsize, mostcommon, max); return 0; }