178k views
5 votes
4.16 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 average and max. A negative integer ends the input and is not included in the statistics. Ex: If the input is: 15 20 0 5 -1 the output is: 10 20 You can assume that at least one non-negative integer is input. 254058.1561406

1 Answer

5 votes

Final answer:

To solve this problem, you can create a program that takes input from the user until a negative integer is entered and calculate the average and maximum.

Step-by-step explanation:

To solve this problem, you can create a program that takes input from the user until a negative integer is entered. Then, calculate the average and maximum of the non-negative integers entered. Here is a Python example:

x = int(input())
numbers = []
while x >= 0:
numbers.append(x)
x = int(input())

average = sum(numbers) / len(numbers)
maximum = max(numbers)

print(average, maximum)

In this program, we use a while loop to collect input numbers until a negative number is entered. Then, we calculate the average using the sum() and len() functions, and the maximum using the max() function. Finally, we print the average and maximum.

User Uelb
by
6.8k points