------------------------------------------------------------------------------
MC logo
Reading In the While Test
[^] Code Examples
------------------------------------------------------------------------------
<<Another Average Example multav2.c Using Update Operators>>
#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
<<Another Average Example Using Update Operators>>