1
Write the algorithm
Computing 2: Unit 8, pp. 106–121
GoalWrite an algorithm for a robot from a picture of the path it must follow.
New words
corner · a place where the path turnsalgorithm · a set of steps, in order, that tells how to do a tasktrace · to follow the steps one by one to see what they do
Explanation
To write an algorithm from a picture, first find the start and the way the robot faces. Next, count the squares in each straight line. At each corner, write turn left or turn right. Put start and marker down at the beginning, and stop at the end. Last, trace your algorithm with a finger to check that it matches the picture.
Worked examples
The number 2. The robot starts at the top left, facing right. The first line has 2 squares. At the first corner the path goes down, so the robot turns right.
| Step | Instruction |
|---|---|
| 1 | Start |
| 2 | Marker down |
| 3 | Forward 2 |
| 4 | Turn right |
| 5 | Forward 2 |
| 6 | Turn right |
| 7 | Forward 2 |
| 8 | Turn left |
| 9 | Forward 2 |
| 10 | Turn left |
| 11 | Forward 2 |
| 12 | Stop |
How do we know whether to turn left or right? Imagine you are the robot. Here the robot faces right, and the path goes down. Down is on the robot’s right-hand side, so the turn is a turn right. If the path goes to the robot’s left-hand side, turn left.
Class activity
Chalk shapes: with an adult, draw a path on the ground with chalk using straight lines and right-angle turns, or draw one on squared paper. Write an algorithm for it. Swap with a friend. Your friend is the robot and walks your algorithm. Did they walk your shape? If not, find the step that is wrong.
Practice
1
Write the algorithm for this path. The robot starts at the top right, facing left.
Start, marker down, forward 2, turn left, forward 2, turn left, forward 2, turn right, forward 2, turn right, forward 2, stop.
2
Write the algorithm for this path. The robot starts at the top left, facing right.
Start, marker down, forward 2, turn right, forward 3, turn right, forward 2, stop.
3
In the number 2, the robot faces right and the path goes down at the first corner. Which turn is this?
Turn right.
4
How many steps does the algorithm for the first path in this practice have?
12 steps: start, marker down, 9 moves and turns, and stop.