
Reading In the While Test
#include <stdio.h>
main()
{
int cnt = 0, /* Count of numbers read. */
tot = 0; /* Total of numbers read. */
/* Read until a read fails. */
int num;
while(scanf("%d", &num) == 1) {
tot = tot + num;
cnt = cnt + 1;
}
/* Print the average. */
if(cnt)
printf("Average is %g\n", (double)tot / (double)cnt);
else
printf("No numbers.\n");
}
Reading: Ch.6