20.5k views
2 votes
This program is to read in a list of integers until the value of -999 is input. Complete the program so that it prints out the number of values read, the total of these values, and the average of these values. 1. Declare the proper variables for the number of values (count), total, and average 2. In the while loop, update your variables after reading a new value 3. Calculate the average 4. Display average, count, and total. The printf statements are already given in the base code.

The code I need to edit is below in C programming language.

#include

int main (int argc, char** argv)

{

int val;



/* prompt the user for input */

printf ("Enter in a list of numbers followed by the terminal value of -999\\");

/* loop until the user enters -999 */

scanf ("%d", &val);

while (val != -999)

{

scanf("%d", &val);

}

/* calculate the average of the values read in */




/* display the results */

/* use the following printf statements to display the results */

/* remove the comments */

//printf ("For the list of %d numbers with a total of %d\\", count, total);

//printf (" the average is: %15.5f\\", average);

return 0;

}

User Mazatwork
by
5.5k points

1 Answer

1 vote

Answer:

#include <stdio.h>

int main()

{

int val, total = 0, count = 0;

float average = 0;

printf ("Enter in a list of numbers followed by the terminal value of -999\\");

scanf ("%d", &val);

while (val != -999){

if(val != -999){

total += val;

count++;

}

scanf("%d", &val);

}

average = (float)total / count;

printf ("For the list of %d numbers with a total of %d\\", count, total);

printf (" the average is: %15.5f\\", average);

return 0;

}

Step-by-step explanation:

Declare the total, count and average variables

In the while loop:

Add the entered number to the total and increment the count by 1 unless it is -999

Calculate average, divide total by count (Typecast the total as float to get decimal result)

User Sigcont
by
5.5k points