This program is supposed to do a binary search for an array of strings, but I\'m
ID: 3620949 • Letter: T
Question
This program is supposed to do a binary search for an array of strings, but I'm having some troubles.
Here is the code that I have so far:
#include
#include
using namespace std;
string search(string [], int, string);
int main()
{
const int SIZE = 20;
string name[SIZE] =
{"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, 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"};
// Insert your code to complete this program.
string person;
int results;
cout << "Enter the name of the person you wish to search for: ";
getline(cin, person);
results = search(name, SIZE, person);
if (results == -1)
cout << "That person doesn't exist in the array. ";
else
cout << "Name " << person << " was found in the element " << results << " of the array. ";
return 0;
}
int search(int 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;
}
Explanation / Answer
please rate - thanks
binary serach only works on a sorted array.
need to do string compare
#include <iostream>
#include <cstring>
using namespace std;
int search(string [], int, string);
void sortnames(string[],int);
void printnames(string[],int);
int main()
{
const int SIZE = 20;
string name[SIZE] =
{"Collins, Bill", "Smith, Bart", "Michalski, Joe", "Griffin, 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"};
// Insert your code to complete this program.
string person;
int results;
cout<<"The names unsorted ";
printnames(name,SIZE);
sortnames(name,SIZE);
cout<<" The names sorted ";
printnames(name,SIZE);
cout << "Enter the name of the person you wish to search for: ";
getline(cin, person);
results = search(name, SIZE, person);
if (results == -1)
cout << "That person doesn't exist in the array. ";
else
cout << "Name " << person << " was found in the element " << results << " of the array. ";
system("pause");
return 0;
}
void sortnames(string a[],int n)
{int i,j;
string t;
for(i=0;i<n-1;i++)
for(j=i;j<n;j++)
if(a[i].compare(a[j])>0)
{t=a[i];
a[i]=a[j];
a[j]=t;
}
}
void printnames(string a[],int n)
{int i;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
}
int search(string array[], int size, string value)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (array[middle].compare(value)==0)
{
found = true;
position = middle;
}
else if (array[middle].compare(value)>0)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.