Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A school weather station records the rain each day, in millimetres. One week the readings, Monday to Sunday, are , , , , , and . This algorithm processes them.
Total ← 0
Dry ← 0
FOR Day ← 1 TO 7
INPUT Rain
Total ← Total + Rain
IF Rain = 0
THEN
Dry ← Dry + 1
ENDIF
NEXT Day
OUTPUT Total, " ", Dry, " ", Total / 7Find the three values it outputs: the total rain, the number of dry days and the average rain per day.
Totalling: Total ← 0 before the loop, Total ← Total + Value inside. Counting: Count ← 0 before, Count ← Count + 1 inside (often in an IF). Maximum and minimum: start with the first value or an extreme value, and replace it with any larger (or smaller) value. Average: Total / Count after the loop.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. Total. millimetres.
Step 2. Count. The dry days are Monday, Thursday and Friday: days.
Step 3. Average. millimetres a day.
Answer: the output is 28 3 4.
Check. A dry day still counts as a day when finding the average, so we divide by , not by .
Log in to join the discussion.
Loading…