Lesson 39: Survey the wall
Measure, store, then work it out
- One reading tells you little. Many readings in a list can be compared.
- First the robot collects the data. Then the program finds the highest, the lowest and the average.
- These are standard methods of solution: they come up again and again in programs and in the exam.
Words for this lesson
| Word | Meaning |
|---|---|
| total | all the values added up |
| average | the total ÷ the number of values |
| highest / lowest | the biggest / smallest value |
Highest, lowest and total in one loop
| Step | What to do |
|---|---|
| before the loop | highest and lowest = the first value; total = 0 |
| in the loop | bigger than highest? keep it; smaller than lowest? keep it; add to total |
| after the loop | average = total ÷ number of values |
In the exam
- 0478/22/F/M/23 Q2(b): write an algorithm with a single loop that outputs the average of 50 numbers stored in an array. Mark scheme: set the total to zero before the loop; loop through the array; add each element (using its index) inside the loop; calculate the average and output it after the loop.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
A wall runs along the top of the field, but it is not straight. The robot drives along it and stops 5 times. At each stop, turn left to face the wall, measure the gap with the distance sensor, store it in the list gaps (rounded), and turn back. At the end the program prints the list, for example Gaps: [610, 310, 810, 1110, 510].
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
Now use the list. With one loop, find the highest gap, the lowest gap and the общая сумма. The average is the total ÷ the number of gaps, rounded. Print one line, for example Highest 1110, lowest 310, average 670.
Нажмите Запустить, чтобы увидеть результат здесь.