Another Average Example
Code Examples
Boolean Operations
multav1.c
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;
int numread = scanf("%d", &num);
while(numread == 1) {
tot = tot + num;
cnt = cnt + 1;
numread = scanf("%d",&num);
}
/* Print the average. */
if(cnt > 0)
printf("Average is %g\n", (double)tot / (double)cnt);
else
printf("No numbers.\n");
}
Reading: Ch.6
Boolean Operations
Reading In the While Test