data-structures · beginner · ~15 min
Use modular arithmetic on enum values.
Given a compass direction, return the direction 90 degrees clockwise.
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.
One enum Dir value d.
The enum Dir one step clockwise from d.
NORTH -> EAST
EAST -> SOUTH
WEST -> NORTH (wraps around)
WEST wraps back to NORTH.One enum Dir value d (constants are 0..3, clockwise).
The enum Dir 90 degrees clockwise from d.
The four directions cycle; wrap with modulo 4.
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.