Introduction to Computer
Science:
Quiz 12: Binary Search
Sewickley Academy, 2000-2001
See Course Home Page.
For this quiz, starting from a blank Visual C++ project, write a program
which uses the functions loadVector, sortVector, printVector, and binarySearch
so that it repeatedly asks for a size of a vector (<0 to exit), creates
a vector that size with random elements, sorts it, prints it, then repeatedly
asks for an element to test for membership (<0 to exit the inner loop
and continue with the outer loop with a new vector) and indicates whether
or not the element is in the vector.
-
loadVector must load the vector with random elements. Anything reasonable
will do.
-
sortVector may use any sorting function. I recommend insertionSort
or selectionSort, due to their simplicity, but any will do.
-
printVector may do anything reasonable.
-
binarySearch must perform binary search (of course), and return true or
false. binarySearch should not do the printing of a message to the
user -- leave that to the main function.
-
The main function will call all the functions listed above.
Finally, you may use the following 3 lines of code if you wish:
typedef apvector<int> intVector;
typedef intVector& intVectorByReference;
typedef int& intByReference;
Good luck!
DK
See Course Home Page.