Question
Need in the program FLOWGORITHM please, Using the Binary Search Algorithm Constance manages a cooking school that employees six instructors. She has asked you to
Need in the program FLOWGORITHM please,
Using the Binary Search Algorithm
Constance manages a cooking school that employees six instructors. She has asked you to design a program that she can use to look up an instructor's phone number. You decide to use two parallel arrays: one named names that will hold the instructors' last names, and another named phones that will hold each instructors' last names, and another named phones that will hold each instructors' phone number. Here is the general algorithm:
1. Get an instructor's last name from the user.
2. Search for the name in the names array.
3. If the name is found, get its subscript. Use the subscript to display the contents of the parallel phones array. If the name is not found, display a message indicating so.
Program 9-8 shows the pseudocode for the program. Note that the array contents are already sorted in ascending order. This is important because the program uses the binary search algorithm to locate a name in the names array.
1. Modul Main()
2. // Constant. for array sizes
3. Constant integer SIZE =6
4.
5. // array of instructor names, already sorted in
6. // ascending order.
7. Declare String names[SIZE] = "Hall", "Harrison",
8. "Hoyle", "Kimura",
9. "Lopez", "Pike"
10.
11. // Parallel array of instructor phone numbers.
12. Declare String phones[SIZE] = "555-6783", "555-0199"
13. "555-9974", "555-2377",
14. "555-7772", "555-1716"
15.
16. //variable to hold the last name to search for.
17. declare String searchname
18.
19 // variable to hold the subscript of the name.
20. declare integer index
21.
22. // variable to control the loop.
23. declare String again == "Y"
24.
25. While (again == "Y" OR again == "Y" )
26. get the name to search for,
27. Display "Enter a last name to search for."
28. input searchName
29.
30 //Search for the last name.
31 index = binary searchnames, searchname, SIZE
32
33 If index 1 = -1 Then
34 // Display the phone number.
35 Display the phone number is ", phones [index]
36 Else
37 // the name was not found in the array.
38 Display searchName, "was nor found."
39 End If
40
41 // Search again?
42 Display "Do you want to search again? (Y=yes, N=No)"
43 Input again
44 end While
45
46 End Module
47 // This is for FUNCTION
48 // the binarySearch function accepts as arguments a String
49 // array, a value to search the array for, and the size
50 // of the array. If the value is found in the array, its
51 // subscript is returned. Otherwise, -1 is returned,
52 // indicating that the value was not found in the array.
53 Function Integer binarySearch(string array[], String value,
54 Integer arrayZize)
55 // Variable to hold the subscript of the first element.
56 declare Integer first = 0
57
58 // variable to hold the subscript of the least element.
59 Declare Integer last = arraySize - 1
60
61 //position of the search value
62 Declare Integer position = -1
63
64 // Flag
65 Declare Boolean found = False
66
67 // Variable to hold the subscript of the midpoint
68 declare Integer middle
69
70 While (NOT found) AND (first<= last)
71 // calculate the midpoint
72 Set middle = (first + last) / 2
73
74 /// see if the value is found at the midpoint.....
75 If array(middle) == value then
76 Set found = True
77 Set position = middle
78
79 // Else, if the value is in the lower half....
80 else if array(middle)> value Then
81 set last = middle - 1
82
83 // Else, if the value is in the upper half...
84 Else
85 Set first = middle + 1
86 End If
87 End While
89 // return the position of the item, or -1
90 // if the item was not found,
91 Return position
92 end Function
Program Output (with input Shown In Bold)
enter a last name to search for.
Lopez [Enter]
The phone number is 555-7772
Do you want to search again? (Y=yes, N=no)
Y[enter]
Enter a last name to search for.
Harrison [enter]
The phone number is 555-0199
Do you want to search again? IY=yes, N=No)
Y (Enter)
Enter a last name to search for ,
Lee (Enter)
lee was not found.
Do you want to search again (Y=yes, N=N0)
N[Enter]
Again, please answer with screenshots from the FLOWGORITHM program...thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started