i need some help see i have to write code for an array of 20 strings and i have to modify the binary search function im using to accomodate for strings instead of ints i know i still have to sort it so that binary search will work but my problem is how do i write the code for binary search of a string and selection sort of a string or bubble sort either is fine.
any help with this would be greatly appreciated
thanks in advance

// this is a program to binary search string data also problem 7 from page 596
#include <iostream>
#include <string>
using namespace std;

const int SIZE = 20;

string binarySearch(string [], int, int);


int main()
{
    string name[SIZE] =
    {"Collins, Bill",  "Smith, Bart",  "Michalski, Joe", "Griffen, Jim",
     "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",
     "Allison, Jeff",  "Moreno, Juan", "Wolfe, Bill",    "Whitman, Jean",
     "Moretti, Bella", "Wu, Hong",     "Patel, Renee",   "Harrison, Rose",
     "Smith, Cathy",   "Conroy, Pat",  "Kelly, Sean",    "Holland, Beth"};

    cout << " please enter the person's name that you wish to find in my name data base ";
    cin >> nameSearch;

    return 0;
}

string binarySearch(string name[], int size, int value)
{
    int first = 0,
        last = size - 1,
        middle,
        position = -1;
    bool found = false;

    while (!found && first <= last)
    {
        middle = (first + last) / 2;
        if (array[middle] == value)
        {
            found = true;
            position = middle;
        }
        else if (array[middle] > value)
            last = middle - 1;
        else
            first = middle + 1;
    }
    return position;
}

Recommended Answers

All 10 Replies

This assignment sounds very familiar...
anyway, why do you think you need to change the function?
std::string overloads operators <, == and >, so it should work without any changes.

Do you have to implement the sorting algorithm yourself?
If not, a

sort(name,name+SIZE);

does the trick (needs the header algorithm).

This assignment sounds very familiar...
anyway, why do you think you need to change the function?
std::string overloads operators <, == and >, so it should work without any changes.

Do you have to implement the sorting algorithm yourself?
If not, a

sort(name,name+SIZE);

does the trick (needs the header algorithm).

i dont know its a bit confusing to me what you said can you show me a code outline to help me out cause i sort of get what your saying but what sort algorithm are you talking about

The code line I posted is the "code outline".
I'm talking about this sort:
http://www.cplusplus.com/reference/algorithm/sort/

but im not using numbers or vectors im using strings and arrays look ill post the question it might be helpful
Modify the binarySearch function presented in this chapter (ch 9) so it searches an array of strings instead of an array of ints. test the function with a driver program.
Use 9-8 (program i displayed minus the binary search function) as a skeleton to complete. ( the array must be sorted before binary search will work.)

thats what it is

This sounds like an h.w or an exercise, so doing this :

std::sort( name, name + SIZE );
int pos = std::binary_search(name, name +SIZE, "JOSH");

would not be beneficial.

And as pointer out, the string class overloads the relational operators, so that means all you have to do is change the type from int to string.

but im not using numbers or vectors im using strings and arrays look ill post the question it might be helpful

What difference does that make? std::sort isn't restricted to vectors or numbers.

This sounds like an h.w or an exercise, so doing this :

std::sort( name, name + SIZE );
int pos = std::binary_search(name, name +SIZE, "JOSH");

would not be beneficial.

And as pointer out, the string class overloads the relational operators, so that means all you have to do is change the type from int to string.

yeah thank you so will that work for sorting them too
just change int to string

What difference does that make? std::sort isn't restricted to vectors or numbers.

but it is confusing when you see numbers and vectors when your working with strings and arrays so thats why i mentioned that

when i set it up switching the ints for string im getting about 60 errors what is going on this is my new code as of now

#include <iostream>
#include <string>
using namespace std;

const int SIZE = 20;

int binarySearch(string [], int, int);
void selectionSort (string[], int);


int main()
{
    string nameSearch;
    string name[SIZE] =
    {"Collins, Bill",  "Smith, Bart",  "Michalski, Joe", "Griffen, Jim",
     "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill",
     "Allison, Jeff",  "Moreno, Juan", "Wolfe, Bill",    "Whitman, Jean",
     "Moretti, Bella", "Wu, Hong",     "Patel, Renee",   "Harrison, Rose",
     "Smith, Cathy",   "Conroy, Pat",  "Kelly, Sean",    "Holland, Beth"};

    cout << " please enter the person's name that you wish to find in my name data base ";
    cin >> nameSearch;

    return 0;
}

int binarySearch(string array[], int size, int value)
{
    int first = 0,
        last = size - 1,
        middle,
        position = -1;
    bool found = false;

    while (!found && first <= last)
    {
        middle = (first + last) / 2;
        if (array[middle] == value)
        {
            found = true;
            position = middle;
        }
        else if (array[middle] > value)
            last = middle - 1;
        else
            first = middle + 1;
    }
    return position;
}
void selectionSort(string array[], int size)
{
    int startScan, minIndex, minValue;

    for(startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];
        for (int index = startScan + 1; index < size; index++)
        {
            if (array[index] < minValue)
            {
                counter++;
                minValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = minValue;
    }
    return counter;
}

#i

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.