Example 1 — Find the farthest point
An array of structs plus one operation.
#include <stdio.h>
struct Point { int x, y; };
int main(void) {
struct Point pts[] = {{3, 4}, {1, 1}, {6, 0}};
int best = 0;
for (int i = 1; i < 3; i++) {
int d = pts[i].x * pts[i].x + pts[i].y * pts[i].y;
int db = pts[best].x * pts[best].x + pts[best].y * pts[best].y;
if (d > db) best = i;
}
printf("farthest: (%d, %d)\n", pts[best].x, pts[best].y);
return 0;
} How It Works
- The squared distances are 25, 2, and 36.
- The loop remembers the index with the largest value.
- The struct array is the structure; the loop is the operation.
