data-structures · beginner · ~15 min

Turn right

Use modular arithmetic on enum values.

Challenge

Given a compass direction, return the direction 90 degrees clockwise.

Task

Define enum Dir { NORTH, EAST, SOUTH, WEST }; (listed clockwise) and implement enum Dir turn_right(enum Dir d) that returns the next direction clockwise. Turning right from WEST wraps around to NORTH.

Input

One enum Dir value d.

Output

The enum Dir one step clockwise from d.

Example

NORTH   ->   EAST
EAST    ->   SOUTH
WEST    ->   NORTH   (wraps around)

Edge cases

  • WEST wraps back to NORTH.

Input format

One enum Dir value d (constants are 0..3, clockwise).

Output format

The enum Dir 90 degrees clockwise from d.

Constraints

The four directions cycle; wrap with modulo 4.

Starter code

enum Dir { NORTH, EAST, SOUTH, WEST };

enum Dir turn_right(enum Dir d) {
    /* TODO */
    return d;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.