I am having a problem getting Exercise 3 from chapter 4 of the book to work. At first, I tried to solve the problem on my own and could not. Finally, I resorted to the code in Appendix A and studied it very carefully to see if I understood it. I am still struggling to grasp its logic, but basically understand it.

I then entered the code from Appendix A, p. 665. With very minor deviation from what appears in the text, here is my code: