81.8k views
5 votes
Consider a robot that is initially facing north in a 5-by-5 grid, as shown in the diagram below. A program segment has been written to move the robot to the upper-right corner where it should face west. Starting Position of Robot (facing north) Ending Position of Robot (facing west) The following program segment is intended to move a robot from the starting position to the ending position as shown above. However, the program segment does not work correctly. Line 1: MOVE_FORWARD () Line 2: MOVE_FORWARD () Line 3: IF(CAN_MOVE (right)) Line 4: Line 5: MOVE_FORWARD () Line 6: MOVE_FORWARD () Line 7: ROTATE_RIGHT () Line 8: Line 9: ROTATE_LEFT () Line 10: ROTATE_LEFT () Which 2 lines of code in the segment above must be switched in order for the program to correctly move a robot from the starting position to the ending position? Select two answers.

1) Line 3 and Line 5
2) Line 3 and Line 7
3) Line 5 and Line 7
4) Line 5 and Line 9

User DonV
by
8.2k points

1 Answer

3 votes

Final answer:

The two lines that must be switched for the robot to reach the upper-right corner facing west are Line 5 and Line 9, changing the order of movement and rotation.

Step-by-step explanation:

To move the robot from the starting position where it is initially facing north in a 5-by-5 grid to the upper-right corner facing west, two lines of the given program need to be switched. The correct lines to switch are Line 5 and Line 9. This means placing the ROTATE_LEFT() code before executing the MOVE_FORWARD() commands two times, allowing the robot to turn west before moving forward.

Specifically, the robot needs to move forward, rotate right to face east, move forward again until it reaches the edge, then rotate left twice to face west.

User Mbiron
by
8.2k points