Hi guys, so i need to pass one array element at a time to a search function.I know I need to use a loop to pass each word separately into the search function but after hours of trying I couldnt really figure it out. While I was playing with my code I found out that it does read into my array but it goes to the last word of the array instead of going down the list one by one.So how do I get it to pass one array element at a time and not just the last words from the arrays? Ps the arrays keywords and dictionaryWords already have words stored in them and their sorted too.
do
{
for(int i=0; i < KEYWORD_SIZE; i++ ) // loop that I tried to use to go through array
{
word = keywords[i] ; // me trying to give word and element form the array
results=search(word,dictionaryWords,DICTIONARY_SIZE); // searches to see if the word obtained keywords array is in dictionaryWords array
}
if(results ==-1) // if word not found it enters here
{
wordsMissing+=1; // counts words missing
cout << word <<endl; // I wrote this here just to see what word was being taken fromthe array
}
}while(wordsMissing != 25);
cout << "words missing" << " " << wordsMissing <<endl; // displays how many words are missing
outputFile.close();
}
return 0;
}
int search(string &searchValue , string a[], int size)
{
int low, high, middle;
low = 0;
high = size-1;
while (low <= high) {
middle = (low + high) / 2;
if (searchValue < a[middle]) {
high = middle - 1;
}
elseif (searchValue > a[middle]) {
low = middle + 1;
}
else {
return middle;
}
}
// Return -1 if searchValue not found
return -1;
}
I don't see the purpose of the outer do-while loop
do { /* what is purpose of this loop??? */ } while (wordsMissing != 25);
I suspect it was a kind of hack to try to get the program to do something, when the problem was elsewhere.
Anyway, I think you need something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int wordsMissing = 0;
for (int i = 0; i < KEYWORD_SIZE; i++ )
{
int results = search(keywords[i], dictionaryWords, DICTIONARY_SIZE);
if (results == -1)
{
wordsMissing++;
cout << keywords[i] << endl;
}
}
cout << "words missing" << " " << wordsMissing <<endl;
A couple of comments on what might be considered good practice.
Since you pass the search word by reference, make it a constant so it is understood by both the human reader and the compiler that it will not be modified.
int search(const string &searchValue , string a[], int size)
I notice you seem to declare variables first, possibly a long time before they are needed. Usually it is better to wait until you are ready to assign a value before declaring a variable. For example like this:
Thank you for the help chervil, and yea that do while loop was just my attempt to see if that would fix it but it didnt. Also its kinda weird because I did attempt to do this int results = search(keywords[i], dictionaryWords, DICTIONARY_SIZE); but when I tried it, it keep getting me errors but for some reason now it works fine.Maybe it was because I didnt put that inside the for loop the first time but anyways thanks again for the help!