Complete the following program in C, which reads in numbers from the keyboard, o
ID: 3754873 • Letter: C
Question
Complete the following program in C, which reads in numbers from the keyboard, one per line, until it gets a zero. Then print out (in this order) the number of values entered, the sum of all values, the largest number, the smallest number, and the average of all the numbers. Do not count the zero (Use 0 to print results). Example of wanted output is at the bottom of the program. NO INFINITE LOOPS, just COMPLETE the program to get to the OUTPUT shown at the bottom.
#include #de fine BUFSIZE 256 int main() int double item- -1 double max; double min; double sum; int howmanY.= 0; // how many valid numbers were read int char ine [BUFSIZE]; // fgets) knows about the NULL itemsread 0; // initial value so loop starts /i check last item wasn't zero; if not // read the line from input and copy it to memory as a string while ( item !=0&& fgets (line, BUFSIZE, stdin) != NULL ) { // scan the number from the line extra stuff on line is ignored itemsread sscanf(line, "slf", &item); // check to see if we got valid input if (itemsread1 && item0) if (howmany0) I this is first item maxitem minitem if item max if item min else t maxitem; min item; howmany+ sum += item; // this will not get stuck in an infinite loop, // plus we can examine the input data if (itemsread0) printf("bad input "); printf("# numbers: if howmany > 0) %d ", howmany); printf("maximum: slf ", max); printf("minimum: f ", min); printf("sum: .. " , sum); return 0 WANTED Result (Output) # items: 5 Sum: 15 Max: 12 Min: -9 Mean: 3Explanation / Answer
#include<stdio.h>
#define BUFSIZE 256
int main()
{
//variable declaration
int itemsread=0;
double item =-1;
double max;
double min;
double sum;
int howmany =0;
int c;
char line[BUFSIZE];
while(item!=0 && fgets(line,BUFSIZE,stdin)!=NULL)
{//reading numbers
itemsread = sscanf(line,"%lf",&item);
if(itemsread == 1 && item!=0)
{if(howmany ==0)
{//finding max and min
max = item;
min = item;
}
else
{
if(max<item)max=item;
if(item<min)min=item;
}
howmany++;
sum += item;//finding sum
}
if(itemsread==0)
{
printf("bad input ");
}
}
printf("# items: %d ",howmany);
if(howmany>0)//displaying output
{
printf("maximum: %lf ",max);
printf("minimum: %lf ",min);
printf("sum: %lf ",sum);
//finding mean and displaying
printf("mean: %lf ",sum/howmany);
}
return 0;
}
//pls give a thumbs up if you find it helpful, it helps me alot thanks
output:
2
4
6
12
-9
0
# items: 5
maximum: 12.000000
minimum: -9.000000
sum: 15.000000
mean: 3.000000
Process exited normally.
Press any key to continue . . .