Stupid question but problems with binary search

Hey all...

I'm feeling stupid but I'm having trouble converting this binary search with an int array to use a string array..

Can anyone help me please?

Code:

#include <iostream>
#include <string>

using namespace std;

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

const int SIZE = 20; // Array size

int main()
{

string tests[SIZE] = {"101", "142", "147", "189", "199", "207", "222",
"234", "289", "296", "310", "319", "388", "394",
"417", "429", "447", "521", "536", "600"};
int empID;
int results;


cout << "Enter the Employee ID you wish to search for: ";
cin >> empID;

results = binarySearch(tests, 0, SIZE - 1, empID);


if (results == -1)
cout << "That number does not exist in the array.\n";
else
{
cout << "That ID is found at element " << results;
cout << " in the array\n";
}
return 0;
}

You kind of left out your binarySearch() function.

Also, use [code] format tags.
sorry forgot to copy that in.. :/

#include <iostream>
#include<string>

using namespace std;

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

const int SIZE = 20; // Array size

int main()
{

string tests[SIZE] = {"101", "142", "147", "189", "199", "207", "222",
"234", "289", "296", "310", "319", "388", "394",
"417", "429", "447", "521", "536", "600"};
int empID;
int results;


cout << "Enter the Employee ID you wish to search for: ";
cin >> empID;

results = binarySearch(tests, 0, SIZE - 1, empID);


if (results == -1)
cout << "That number does not exist in the array.\n";
else
{
cout << "That ID is found at element " << results;
cout << " in the array\n";
}
return 0;
}

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

while (!found && 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;
}
You also have the prototype wrong, it needs to know the value it's looking for.
Topic archived. No new replies allowed.