8. Write a complete C program that will input a list of positive integers from t
ID: 3593916 • Letter: 8
Question
8. Write a complete C program that will input a list of positive integers from the user and then output the smallest and largest values entered plus the average of all the values. Write your program so that it will continue to input values until -1 has been entered by the user. See the sample runs below. (18 pts.) Terminal tosh 43x10 liberty:~/docs/classes/egre245/% a.out Positive values? C-1 to quit): 2468 -1 smallest - 2 largest = 8 average 5.000 liberty:-/docs/classes/egre2451% a.out Positive values? (-1 to quit): -1 no val ues entered liberty: /docs/classes/egre245/%IExplanation / Answer
#include <stdio.h>
int main()
{
int sum = 0, i, c= 0, n, min,max;
printf("Positive values?: (-1 to quit): ");
scanf("%d", &n);
min = n;
max = n;
while(n != -1) {
c++;
sum+=n;
if(min > n){
min = n;
}
if(max < n) {
max = n;
}
scanf("%d", &n);
}
if(c != 0) {
printf("smallest = %d ", min);
printf("largest = %d ", max);
printf("average = %lf ",(sum/(double)c));
} else {
printf("No values entered. ");
}
return 0;
}
Output:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.