Write a C program that prompts for a variable number of integers, adds them up, averages them, and prints out the average.
The user will enter either an integer to be averaged or a sentinel of 999 indicating that no more numbers are to be entered. When the sentinel is entered, the program will exit the repetition loop. It will then compute and display the average of numbers entered.
I got a 85 but I feel I accomplished the task and should've been scored higher. The instructors feedback was Use int num and compare it with 999 to stop.
That way, two scanf() statements are not required.
insert
Code:
#include <stdio.h>
int main()
{
float average, num, total;
int count, stop=0;
count=0;
total=0.0;
while (stop != 999)
{
printf("\nenter a number ");
scanf("%f", &num);
count++;
total += num;
printf("the total is now %.f\n", total);
printf("\nenter 999 to calculate the avg or 0 to continue ");
scanf("%d", &stop);
}
average =total / count;
printf("The average of all the numbers is: %.f", average);
return 0;
}