184k views
1 vote
4.15 LAB: Varied amount of input data

Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the max and average. A negative integer ends the input and is not included in the statistics. Assume the input contains at least one non-negative integer.

Output the average with two digits after the decimal point followed by a newline, which can be achieved as follows:
printf("%0.2lf\\", average);

Ex: When the input is:

15 20 0 3 -1
the output is:

20 9.50
In C programming language NOT C++

User XZVASFD
by
6.9k points

1 Answer

1 vote

Final answer:

The C program should calculate the max and average of an undetermined number of non-negative integers, ending input with any negative integer, and then output these values.

Step-by-step explanation:

The task is to write a C program that calculates statistical data such as maximum value (max) and average from a series of non-negative integers and outputs these figures, terminating the input when a negative integer is encountered. The program must ignore the negative integer and must provide the average to two decimal places using the specified printf format.

Example Program Flow:

  1. Initialize variables to store sum, count, max, and a temporary input.
  2. Use a loop to read numbers until a negative integer is inputted.
  3. Update sum, count, and max with each inputted non-negative integer.
  4. Calculate the average by dividing the sum by the count.
  5. Output the max and average as specified.

User Gaston
by
7.4k points