Distance between two points
Read the members of two structs passed by value. An easy embedded C problem (Structs, enums and typedef), run and graded in the browser.
easy · Structs, enums and typedef · structs
The Manhattan distance between two points is how far apart they are across plus how far apart they are up and down: |a.x - b.x| + |a.y - b.y|.
Return the Manhattan distance between a and b.
Starter code
typedef struct {
int x;
int y;
} point_t;
int manhattan(point_t a, point_t b) {
return 0;
}Example tests
Two points
point_t a = { 1, 2 };
point_t b = { 4, 6 };
printf("%d\n", manhattan(a, b));Expected output:
7The same point
point_t a = { 5, 5 };
printf("%d\n", manhattan(a, a));Expected output:
0Learn it first
- Structs, enums and typedef · Grouping values into structs, the dot and the arrow, naming choices with enum, and shorter names with typedef.
More Structs, enums and typedef problems
- Build a struct · Fill in a struct's members and return it.
- Move a point · Change the caller's struct through a pointer.
- Average the valid readings · Loop over an array of structs and use only some of them.
- The next traffic light colour · Step through the states of an enum.
- Count the LEDs that are on · Combine a struct, an enum member and a loop.