Exercises

1. In Figure 1.1 suppose there are n = 100 houses and you must visit houses 3, 10, 50, 7, and 82 (in this order). How much distance must you travel in each town?

2. Why do no more nonprimes remain in candidates when factor > ?

3. Explain the difference between a function and program segment implementation of a refinement.

4. a. What determines the largest value of n for which each of the two array implementations for candidates will work?

b. Discuss any significant differences between set, insert, omit, and belongs for the two implementations of candidates.

c. Modify the second implementation so that it works for larger n?/FONT>say 20 times its current limit.

d. Write a function copy with parameters c1 and c2. It must treat them as data abstractions with the operations set, insert, omit, and belongs and is to return with c2's contents identical to c1's and c1's contents unaltered.

5. Change primes so that even numbers are not considered as values for factor or nextmultiple.

6. Change your solution to Exercise 5 so that even numbers, except 2, do not appear in candidates. Use only enough storage for 2 and the odd numbers between 2 and n.

7. This is the same as Exercise 6 except that candidates must use only enough storage for 2 and the odd primes between 2 and n.

8. Change remove so that instead of increasing factor by 1, factor is increased to the value of the next prime in candidates. You may choose to do this by starting with remove from your solution to Exercise 7.

9. Analyze the time and storage required for your solutions to Exercises 5 to 8.

10. For the prime problem, find a solution that stores no primes.

11. Explain why we must know the data type of the information stored in variables A and B in order to multiply their contents correctly.

12. Suppose that integers are represented as in the example of Section 1.3.2. Write an algorithm, in English, that will allow someone who knows addition tables up to 9 to compute the correct sum of the contents of A and B. The algorithm must yield the correct result no matter what integers are stored in A and B.

13. This is the same as Exercise 12 except that reals are represented, and the algorithm should yield the correct sum represented as a real number.

14. Express the Bowling Scores program just as the prime number solution is expressed in Figure 1.3. What are its local, nonlocal, and global variables?

15. Modify the flowcharts and program for the bowling problem to obtain flowcharts and a program solving the same problem but also printing out the number of rolls for each game.

16. What functions might be inserted in Bowling Scores to check the validity of its input and output? Where would they go in the program?

17. What elements of style have been incorporated in the programs of Sections 1.9.2 and 1.9.3?