Representing algorithms

EasyMediumHard
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
Question 34
Medium

A robot is placed in a 4x4 grid. The position of each cell is described using coordinates in the format (column, row), where columns are numbered 1 to 4 from left to right, and rows are numbered 1 to 4 from bottom to top.

The Grid Layout

Row (y)Column 1 (x)Column 2 (x)Column 3 (x)Column 4 (x)
4
3[Object]
2↑ (Start)[Object]
1

A 4x4 grid with columns 1 to 4 from left to right and rows 1 to 4 from bottom to top. Black obstacle squares (objects) are located at (2,3) and (3,2). The robot starts at (2,2) facing North (represented by an up arrow ↑).

  • The robot starts at (2,2) facing North (upwards, towards row 3).
  • Cells labeled [Object] are black squares containing obstacles.
  • The boundary (outer edge) of the 4x4 grid also counts as an object (i.e. ObjectAhead() returns true if the robot is facing the edge of the grid).

Trace the execution of the following algorithm:

WHILE ObjectAhead() = true
  TurnRight()
  IF ObjectAhead() = true THEN
    TurnLeft()
    TurnLeft()
  ENDIF
  Forward(1)
ENDWHILE
Forward(1)

State the sequence of grid coordinates visited by the robot in order, starting from its initial position (2,2) to its final position using the format: (2,2) -> (x,y) -> ...

[3]

Representing algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Representing algorithms