Solves Problem 7 in C++