Lesson 39: Survey the wall
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
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 |
English
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 |
English
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.
実行ボタンをクリックして出力を確認してください。