Exercise A solution in C++