/* * Age Statistics, E.Hehner, 1997 Sep.3 * Modified by Alan Rosenthal, Oct 2000 * This is the shorter version. * * 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 most common age. */ #include int main() { int count[120]; /* count[x] is count of people who are x years old */ int i, a, mostcommon; /* 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]++; } /* find mostcommon */ /* note that mostcommon starts off as indicating count[0], which is ok */ mostcommon = 0; for (i = 0; i < 120; i++) if (count[i] > count[mostcommon]) mostcommon = i; printf("The most common age is %d.\n", mostcommon); return 0; }