updated c++ solution for problem #9