Problem 24: solved in C++