Implemented C++ solution to problem 4.