data-structures · beginner · ~15 min
Aggregate over a typedef'd struct's fields.
Compute the dot product of two 3D integer vectors.
Define typedef struct { int x, y, z; } Vec3; and implement long dot(Vec3 a, Vec3 b) that returns a.x*b.x + a.y*b.y + a.z*b.z.
Two Vec3 values passed by value. Each has int fields x, y, z.
long: the sum of the element-wise products.
a={1,2,3}, b={4,5,6} -> 32
a={1,2,3}, b={0,0,0} -> 0
long so large components do not overflow.Two Vec3 values (each int x, y, z), passed by value.
long: a.x*b.x + a.y*b.y + a.z*b.z.
Accumulate in long to avoid int overflow.
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.