Simvik

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:

7

The same point

point_t a = { 5, 5 };
printf("%d\n", manhattan(a, a));

Expected output:

0

Learn 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