data-structures · beginner · ~15 min
Declare a struct and access its members.
Compute the Manhattan (grid) distance between two 2D points.
Define struct Point { int x, y; }; and implement int manhattan(struct Point a, struct Point b) that returns the Manhattan distance |a.x - b.x| + |a.y - b.y|.
Two struct Point values passed by value. Each has int fields x and y.
The sum of the absolute differences of the coordinates, as an int.
a={0,0}, b={3,4} -> 7
a={5,5}, b={5,5} -> 0
Two struct Point values (each with int x, y), passed by value.
int: |a.x-b.x| + |a.y-b.y|.
struct Point { int x, y; };
int manhattan(struct Point a, struct Point b) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.