// Name: John H. Hacker // Assg: Lab #4 // Date: June 27, 2006 // Desc: This program demonstrates the use of binary and sequential // search methods on an array of strings. #include #include #include using namespace std; const int MAX_NAMES = 5000; // Prototypes for functions used in searching void display(string names[], int size); int input(string filename, string names[]); int sequentialSearch(const string names[], int numberNames, string searchName); int binarySearch(const string names[], int numberNames, string searchName); int main() { string names[MAX_NAMES]; int numNames; numNames = input("sortednames.dat", names); cout << "The names that are in our database: " << endl; display(names, numNames); cout << endl << endl; string searchName; int searchIndex; // keep asking the user for names to search for, forever, or at least // until they enter quit at which point we break out and are done while (true) { // get a name to search for cout << "Enter a name to search for or 'quit' to stop: "; getline(cin, searchName); // check if the user is fed up with the program yet if (searchName == "quit") break; // perform a binary search for the name the user entered in the // names database cout << "Using binary search to search for name: " << searchName << endl; searchIndex = binarySearch(names, numNames, searchName); if (searchIndex == -1) cout << "Unable to find name using binary search" << endl << endl; else cout << "Found the name at index " << searchIndex << " in the names database" << endl << endl; // CODE: your task, implement the books sequential search // down below and call it here. Compare and see if you // get the same answer as the binary search } cout << "Goodbye!" << endl; } // display // // A simple function to display the values in an array of strings // We format the values onto standard output in a nice readable way // // Params: // names an array of strings whose values are to be displayed // size the number of values in the array void display(string names[], int size) { for (int i=0; i