data-structures · beginner · ~15 min

Dot product of 3-vectors

Aggregate over a typedef'd struct's fields.

Challenge

Define typedef struct { int x, y, z; } Vec3; and implement long dot(Vec3 a, Vec3 b) returning a.xb.x + a.yb.y + a.z*b.z.

Starter code

typedef struct { int x, y, z; } Vec3;

long dot(Vec3 a, Vec3 b) {
    /* TODO */
    return 0;
}

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