C++ solution for problem 17.