Your implementation looks correct.
I know this is for an exam, so you don't have a choice, but in other circumstances, you should use something like an std::vector of std::vectors as wolle...