c++ - Implement binary search with string array instead of int array -


the program supposed incorporate it. created of code, second part of lab have go , input strings binary search instead. don't how search strings if have remove idnum , results , empid.

    const int num_names = 20;        string names[num_names] = {"collins, bill", "smith, bart", "allen, jim",                                    "griffin, jim", "stamey, marty", "rose, geri",                                    "taylor, terri", "johnson, jill", "allison, jeff",                                    "looney, joe", "wolfe, bill", "james, jean",                                    "weaver, jim", "pore, bob", "rutherford, greg",                                    "javens, renee", "harrison, rose", "setzer, cathy",                                    "pike, gordon", "holland, beth" }; 

so, binary search work strings instead of int. don't understand how it.

    // function prototype     int binarysearch(const int [], int, int);     const int size = 20;      int main()     {          // array employee ids 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 results;   // hold search results        int empid;     // hold employee id         // employee id search for.        cout << "enter employee id wish search for: ";        cin >> empid;         // search id.        results = binarysearch(idnums, size, empid);         // if results contains -1 id not found.        if (results == -1)           cout << "that number not exist in array.\n";        else        {           // otherwise results contains subscript of           // specified employee id in array.           cout << "that id found @ element " << results;           cout << " in array.\n";        }        return 0;     } 

the binarysearch function performs binary search on integer array. array, has maximum of size elements, searched number stored in value. if number found, array subscript returned. otherwise, -1 returned indicating value not in array.

int binarysearch(const int array[], int size, int value) {    int first = 0,             // first array element        last = size - 1,       // last array element        middle,                // mid point of search        position = -1;         // position of search value    bool found = false;        // flag     while (!found && first <= last)    {       middle = (first + last) / 2;     // calculate mid point       if (array[middle] == value)      // if value found @ mid       {          found = true;          position = middle;       }       else if (array[middle] > value)  // if value in lower half          last = middle - 1;       else          first = middle + 1;           // if value in upper half    }    return position; } 

change

int binarysearch(const int array[], int size, int value) 

to

int binarysearch(const std::string array[], int size, const std::string &value) 

and use follows

std::string name; std::getline(std::cin, name); // read name console std::sort(names, names + num_names); // make sure array sorted results = binarysearch(names, num_names, name); 

you make use of templates code more generic

template<typename t> int binarysearch(const t array[], int size, const t &value) 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -