updated c++ solution to problem #3