My Binary search keeps returning zero I'm not sure why. Maybe I'm passing my arguments in incorrectly? Or maybe I'm not returning it right? I'm not quite sure what is wrong, if anyone could explain how I can return the values I want instead of zero thanks. my array is filled with words I didn't include that part of my code because it is being filled in by a file. I'm not entering the first word so I shouldnt be getting zero. I've tried multiple words.
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
//function prototypes
void selectionSort(string[], int);
int binarySearch(string[], int, string);
int main()
{
int open;
int name[8];
ifstream inputFile;
string filename;
int i = 0;
int size = 1024;
int newSize;
string exit;
string words[1024];
string word;
string newArray[1024];
string search;
int results;
string createdFile;
int startScan, minIndex, minValue;
//newArray[0]="This";
cout << "Please Enter the Filename: ";
cin >> filename;
inputFile.open(filename.c_str());
if (!inputFile) {
cout << "Your File could not be opened!";
}
else if (inputFile) {
while (i<1024) {
inputFile >> word;
//cout << i;
if (word.length()<2) { //gets rid of single character words
i++;
}
else if(inputFile.eof()) {
//cout<<newSize<<endl;
newSize=i;
//cout<< "DONE!"<<newSize;
break;
}
else{
newArray[i] = word;
i++;
//cout<<newArray[i]<<" " ;
//newSize=1;
//cout<<newSize;
}
//cout << newArray;
//i++;
}
}
inputFile.close();
//Take values stored in array and put them in another array with i number of elements
string finalArray[i];
size=i;
for(int j = 0; j<i;j++)
{
finalArray[j]=newArray[j];
//cout<<finalArray[j];
}
cout << "Your document has been sorted. " << endl;
selectionSort(finalArray, size);
cout<< "Out putting Your array"<<endl;
for (int z = 0; z<size; z++) {
cout << finalArray[z] << " ";
}
cout << "Please insert a word to search!: " << endl;
cin >> search;
results = binarySearch(finalArray, size, search);
cout << results << endl;
//results = binarySearch(finalArray, size, search);
//cout << results;
cout << "To exit press 0, or enter another word..." << endl;
}//end of main
void selectionSort(string array[], int size) {
int startScan, minIndex;
string 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) {
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
int binarySearch(string words[], int numElems, string word) {
int first = 0, //first array element
last = numElems - 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; //this finds the mid point
if (words[middle] == word) {
found = true;
position = middle;
}
else if (words[middle]> word) // if it's in the lower half
{
last = middle - 1;
}
else {
first = middle + 1; //if it's in the upper half
}
return position;
}
}//end binarySearch
The problem is that you have return position;
inside the while
loop, but not in any of the if/else
blocks, so it returns after the first iteration even when it hasn't yet found the search string. You should move that to after the loop, or do it in the if
block that sets position
when it finds a matching element.
Another problem is that the while
condition is true. You want to keep looping while first
is not equal to last
. Actually, that's not the right condition, either, because if the array only has one element, first
and last
will always be equal, and you'll stop before you test whether it matches. So the correct test is while (first <= last)
.
int binarySearch(string words[], int numElems, string word)
{
int first = 0, //first array element
last = numElems - 1, //last array element
middle; //mid point of search
while (first <= last)
{
middle = (first + last) / 2; //this finds the mid point
if (words[middle] == word) {
return middle;
}
else if (words[middle]> word) // if it's in the lower half
{
last = middle - 1;
}
else {
first = middle + 1; //if it's in the upper half
}
}
return -1; // not found
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.