Question: Modify the binary search function presented in chapter 9 so that it takes two parameters: a const vector of Persons (which will already be sorted

Modify the binary search function presented in chapter 9 so that it takes two parameters: a const vector of Persons (which will already be sorted by name), and the name of the Person being searched for (use the Person class from Project 4.b). Because a vector knows its own length, no size parameter is needed. If the Person object with the matching name is found in the vector, the index of its position should be returned. Otherwise it should return -1. Because the vector of Persons is being passed by const, you'll need to make the getName() method a constant member function, to reassure the compiler that the getName() method isn't changing any Person objects.

The files must be named Person.hpp, Person.cpp and personSearch.cpp

*************Existing Person.hpp*************************************************************

#include #include using namespace std;

class Person {

public: Person(void); Person(std::string n, double a); //Constructor std::string getName(); double getAge();

private: std::string nameMember; double ageMember; };

************************Existing Person.cpp******************************************

#include #include #include "Person.hpp" #include using namespace std;

Person::Person(void) { nameMember = " "; ageMember = 0; }

Person::Person(std::string nameIn, double ageIn) { std::string name = nameIn; ageMember = ageIn; }

std::string Person::getName() { return nameMember; }

double Person::getAge() { return ageMember; }

************This is the binarySearch Code that needs to be modified************************************************

#include "Person.hpp" #include #include using namespace std;

// Function prototype int binarySearch(const int [], int, int);

const int SIZE = 20;

int main() { // Create an array of ID numbers sorted in ascending order int IDnums[SIZE] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600 };

int empID, // Holds the ID to search for results; // Holds the search results

// Get an employee ID to search for cout << "Enter the employee ID you wish to search for: "; cin >> empID;

// Search for the ID results = binarySearch(IDnums, SIZE, empID);

// If binarySearch returned -1, the ID was not found if (results == -1) cout << "That number does not exist in the array. "; else { // Otherwise results contains the subscript of // the specified employee ID in the array cout << "ID " << empID << " was found in element " << results << " of the array. "; } return 0; }

/***************************************************************** * binarySearch * * This function performs a binary search on an integer array * * with size elements whose values are stored in ascending * * order. The array is searched for the number stored in the * * value parameter. If the number is found, its array subscript * * is returned. Otherwise, -1 is returned. * *****************************************************************/

int binarySearch(const int array[], int size, int value) { int first = 0, // First array element last = size - 1, // Last array element middle, // Midpoint of search position = -1; // Position of search value bool found = false; // Flag

while (!found && first <= last) { middle = (first + last) / 2; // Calculate midpoint if (array[middle] == value) // If value is found at mid { found = true; position = middle; } else if (array[middle] > value) // If value is in lower half last = middle - 1; else first = middle + 1; // If value is in upper half } return position; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!