C++ solution to problem 3.