hw2solns

hw2solns - CMPS 10 - Homework Assignment 2 Problems:...

This preview shows pages 1–3. Sign up to view the full content.

CMPS 10 - Homework Assignment 2 Problems: Chapter 2 (p.75): 12, 13, 15, 16, 17, 18, 22, 23 12.) Develop a formal argument that “proves” that the sequential search algorithm shown in figure 2.13 cannot have an infinite loop; that is, prove that it will always stop after a finite number of operations. Solution: Steps 1, 2, 5, 6, 7, 9, and 10 are sequential operations and thus cannot go into an infinite loop. Steps 4 and 8 are conditionals, and also cannot go into an infinite loop. Step 3 is the only one left to consider, and since it is a repeating loop, it could potentially become an infinite loop. The looping conditions is “if Found = no and i 10,000.” If Found ever becomes yes (the algorithm finds the name), then the condition is no longer satisfied and the loop will end. If NAME is never found, then line 7 adds 1 to i per iteration. Since i starts at 1 (from line 2), it will advance to 10,001 after 10,000 iterations of the loop. At this point, the second portion of the condition will no longer be true and the loop will halt. Then lines 8, 9, and 10 will be executed, and the program will stop. 13.) Modify the sequential search algorithm of Figure 2.13 so that it works correctly even if the names in the directory are not unique, that is, if the desired name occurs more than once. Your modified algorithm should find every occurrence of NAME in the directory and print out the telephone numbers corresponding to every match. In addition, after all the numbers have been displayed, your algorithm should print out how many occurrences of NAME were located. For example, if NAME occurred three times, the output of the algorithm might look something like this: 528-5638 922-7874 488-2020 A total of three occurrences were located. Solution: Here is one possible solution to the problem. Let N denote names and T denote telephone numbers from the directory. Step Operation 1.) Get values for NAME , N 1 , … , N 10000 and T 1 , … , T 10000 2.) Set the value of i to 1, and set the value of NumberFound to 0. 3.) Repeat steps 4 through 7 while i 10,000. 4.) If N i = NAME , then 5.) Print T i 6.) Set the value of NumberFound to NumberFound + 1.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
7.) Add 1 to the value of i . 8.) Print “A total of <NumberFound> occurrences were located.” 9.) STOP. 15.) With regards to the Find Largest algorithm of Figure 2.14, if the numbers in our list were not unique and therefore the largest number could occur more than once, would the algorithm find the first occurrence? The last occurrence? Every occurrence? Explain
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/15/2011 for the course CMPS 10 taught by Professor Tantalo during the Fall '08 term at UCSC.

Page1 / 5

hw2solns - CMPS 10 - Homework Assignment 2 Problems:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online