I am stumped on a particular problem involing a recursive binary search. Particularly, I am not sure on how to pass the alotted variables, char/pointer. Every time I try something different I just get more errors. I need some assistance on figuring out how to pass these variables and make them usable. The code I have currently is as follows:
Code:
// Binary Search, Recursive Implementation
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int binarySearch(char *names, char nameInput, int leftIndex, int rightIndex){
while(leftIndex <= rightIndex){
int middleIndex = (leftIndex + rightIndex) / 2;
if (strcmp(searchKey, names[middleIndex]) == 0){
return middleIndex;
} else if(strcmp(searchKey, names[middleIndex]) < 0){
rightIndex = middleIndex - 1;
binarySearch();
} else {
leftIndex = middleIndex + 1;
binarySearch();
}
}
return -1;
}
int main() {
char *names[] = {"Carl", "George", "Hellen", "John", "Kevin", "Mary",
"Rachel", "Ralph", "Steve", "Susan", "Timothy", "Zero"};
char nameInput[64];
int leftIndex=0;
int rightIndex=12
cout << "Name Search Engine" << endl;
cout << "Enter Quit to terminate the program" << endl << endl;
while(1) {
cout << "Enter a name: ";
cin.getline(nameInput,64);
if (strcmp(nameInput,"Quit") == 0) {
cout << "End of program" << endl;
break;
} else if(strcmp(nameInput,names[5])) {
cout << "Mary is found at index 5" << endl << endl;
}
binarySearch(*names, nameInput, leftIndex, rightIndex);
}
return 0;
}