Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A robot vacuum is controlled by this pseudocode. The proximity sensor gives the distance to the nearest object in centimetres.
CONSTANT TooClose ← 20
Turns ← 0
FOR Count ← 1 TO 8
INPUT Distance
IF Distance <= TooClose
THEN
OUTPUT "Turn"
Turns ← Turns + 1
ELSE
OUTPUT "Forward"
ENDIF
NEXT Count
OUTPUT TurnsThe eight readings are 65, 42, 20, 18, 50, 21, 9 and 33.
A robot has three characteristics: a mechanical structure or framework; electrical components, such as sensors, microprocessors and actuators; and it is programmable. A machine needs all three. A remote-controlled toy is not programmable, and a chat program has no mechanical structure, so neither is a robot.
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.
(a) Go through the readings one at a time, testing Distance <= 20.
Turns becomes Turns becomes Turns becomes The last line outputs Turns, which is .
(b) It shows that the robot is programmable: its behaviour comes from a stored program. To make it turn earlier, the owner changes the constant to a larger value, such as CONSTANT TooClose ← 30.
Watch out. With < instead of <=, the reading would not cause a turn, and the answer would be .
Log in to join the discussion.
Loading…