Skip to content

Exercises

These are meant to be done as you go. If you get stuck, check the hints (or jump to Solutions).

Exercise 1 — Add a “sum” function

Add a function sum_vec(v) that returns the sum of a vector.

Success criteria:

  • sum_vec([1,2,3]) == 6

Hint:

  • implement in C++ using std::accumulate.

Exercise 2 — Normalize a vector

Implement normalize(v) returning a vector scaled so that max(abs(v)) == 1.

Success criteria:

  • normalize([2, -4]) == [0.5, -1.0] (within float tolerance)

Hint:

  • compute scale as 1.0 / max_abs.