The following program moves a robot in a grid of squares. The initial position and direction of the robot is shown below. Click on the grid to move the robot to the final position after the program is run. You must click within the box to rotate the robot to the correct final direction (click near the top of the box to rotate up, etc..) Feel free to move the robot along as you follow the code. reset robot PROCEDURE Mystery (x) 2. REPEAT x TIMES 4 { ROTATE_RIGHT() MOVE FORWARD () 8 9 10 11 REPEAT 3 TIMES 12 MOVE_FORWARD () Mystery (i) i Fi - 1 13 14 15 16 } 17 MOVE FORWARD ()
Given that, the initial value of i=3 and the loop executes 3 times for the values i=3, i=2 and i=1.
For i=3:
First the robot forwards and then Mystery(3) function executes.
In this function the loop executes 3 times and each time it rotates right and then moves forwards.
Then the i value becomes 2.
For i=2:
First the robot forwards and then Mystery(2) function executes.
In this function the loop executes 2 times and each time it rotates right and then moves forwards.
Then the i value becomes 1.
For i=1:
First the robot forwards and then Mystery(1) function executes.
In this function the loop executes 1 time and it rotates right and then moves forwards.
Then the i value becomes 0, then it moves forward and the program halts.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps