Reputation: 3544
I'm trying to write a simple spell checking program for homework, basically by putting a dictionary of 80,000 words into an array and then doing a binary search to see if the word is in the array.
I believe the problem is to do with the first if statement, with my break and return 1 lines, but I may be wrong. What I'm trying to do is make it so that if the word at the midpoint of the array is the same as the string, then it should break out of the if statement and the while loop and return 1, if it's not the same I want it to continue searching.
Currently every time I run the program it says the word is not found, whether the word is actually in the dictionary or not.
Any help with where I am going wrong is greatly appreciated.
int binarySearch(char string[9]) {
int low = 0;
int high = NUMBEROFDICTIONARYWORDS;
int midPoint;
char midPointWord[9];
while(high > (low + 1)) {
midPoint = (low + high) / 2;
midPointWord[9] = dictionary[midPoint];
if(strcmp(string, midPointWord) == 0) {
return 1;
break;
break;
} else
if(strcmp(string, midPointWord) < 0) {
high = midPoint;
} else
low = midPoint;
}
return 0;
}
Upvotes: 0
Views: 132
Reputation: 146
midPointWord[9] = dictionary[midPoint];
only assigns index 9 of midPointWord, which in this case is an invalid thing to do because it is past the end of the array. I'm guessing dictionary
is something like a char**
, in which case you want to declare midPointWord as char*
.
Upvotes: 3