Program 16.6 from chapter 16