C programming
Question: Write a program that reads integers from the keyboard until the user enters the sentinel value -999. The program should then print the minimum integer read, the maximum integer read and the average of all the integers. (Excluding the sentinel value -999)
I know how to post maxes & mins and set the sentinel value. I don't know how to tell the computer to sum up the values given and take the average since I need a generic function that would work for any number of values.
My code for everything else:
#include
int main()
{
int num;
int min;
int max;
int avg;
printf("Enter a number (-999 to quit): ");
scanf("%d",&num);
//putting the first number the user entered in variable min and max
min = num;
max = num;
while(num!=-999)
{
if(nummax)
{
max = num;
}
printf("Enter another number (-999 to quit): ");
scanf("%d",&num);
}
printf("\nThe smallest number entered is %d\n",min);
printf("\nThe largest number entered is %d\n",max);
return 0;
}
Reply
Answers
The code you need to add would look something like this: int sum = 0; int count = 0; while(num!=-999) { sum += num; count++; ... } avg = sum/count; printf("\nThe average of all the numbers entered is: %d\n", avg);