. ModifythesequentialsearchalgorithmofFigure2.13 so that it works correctly even
ID: 3710042 • Letter: #
Question
.
ModifythesequentialsearchalgorithmofFigure2.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 number 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.
FIGURE 2.13 Step Operation 1 Get values for NAME, N 2 T,,T ,o and 10000 Set the value of i to 1 and set the value of Found to NO While both (Found NO)and (is 10,000) do Steps 4 through 7 4 If NAME is equal to the th name on the list N then Print the telephone number of that person, T Set the value of Found to YES Else (NAME is not equal to N) Add 1 to the value ofi 8 If (Found NO) then Print the message Sorry, this name is not in the directory Stop 10 The sequential search algorithmExplanation / Answer
If you find this helpful, then please leave a like.
I have introduced a variable COUNT that keeps track of the number of matches found.
Step Operation
1 Get values for NAME, N1, ... , N10,000, and T1, ... , T10,000
2 Set the value of i to 1
3 Set the value of COUNT to 0
4 While (i<=10,000) do Steps 5 through 8
5 If NAME is equal to the ith name on the list Ni then
6 Print the telephone number of that person, Ti
7 Add 1 to the value of COUNT
8 Add 1 to the value of i
9 If (COUNT==0) then
10 Print the message 'Sorry, this name is not in the directory'
11 Stop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.