basics · intermediate · ~15 min

Distance between points

Pass structs by value and use `<math.h>`.

Challenge

Compute the straight-line (Euclidean) distance between two 2-D points passed as structs.

The grader supplies this struct (do not redefine it):

typedef struct { double x, y; } point_t;

Task

Implement double distance(point_t a, point_t b) returning the Euclidean distance between a and b. No main — the grader calls it.

Input

Two point_t values passed by value, each with double fields x and y.

Output

The distance sqrt((a.x-b.x)^2 + (a.y-b.y)^2) as a double. Use sqrt from <math.h>.

Example

distance({0,0}, {3,4})    ->   5
distance({1,1}, {1,1})    ->   0
distance({1,2}, {4,6})    ->   5

Edge cases

  • Identical points give 0.
  • Negative coordinates work the same (the differences are squared).

Input format

Two point_t values a and b (each with double x, y).

Output format

The Euclidean distance between them as a double.

Constraints

Use sqrt from <math.h>; do not redefine point_t.

Starter code

#include <math.h>

double distance(point_t a, point_t b) {
    /* TODO */
    return 0.0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.