data-structures · beginner · ~15 min

Turn right

Use modular arithmetic on enum values.

Challenge

Define enum Dir { NORTH, EAST, SOUTH, WEST }; (clockwise) and implement enum Dir turn_right(enum Dir d) returning the next direction clockwise (WEST turns to NORTH).

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.