Lesson 3.10 · Write it
Stream statistics
A whole programPython or Java7 tests, 4 of them hidden
On a phone? Put the lines in order instead
Write a program that reads whole numbers one at a time, stopping at -1, and reports five statistics about the numbers it read.
- Read one number per line. The value
-1means "no more numbers" and is not counted as data. - If at least one number was entered, print five lines in this order:
Count:followed by how many numbers were entered.Sum:followed by their total.Average:followed by the whole-number average (drop the fraction).Min:followed by the smallest number.Max:followed by the largest number.
- If
-1is typed first (no data), printNo data.
All numbers are whole numbers from 0 to 1000. Your program is run several times. Only what it prints is checked.
In Python, use // for whole-number division. In Java, / between two ints already drops the fraction.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.
three numbers (rule 2)
You type
- typed: 10
- typed: 20
- typed: 30
- typed: -1
It prints
Count: 3 Sum: 60 Average: 20 Min: 10 Max: 30
no data at all (rule 3)
You type
- typed: -1
It prints
No data
one number, all stats equal (rule 2)
You type
- typed: 5
- typed: -1
It prints
Count: 1 Sum: 5 Average: 5 Min: 5 Max: 5