How to pass one array element at a time to a search function

closed account (EAk1vCM9)
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  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;
        }
        else if (searchValue > a[middle]) {
            low = middle + 1;
        }
        else {
            return middle;
        }
    }

    // Return -1 if searchValue not found
    return -1;
}


My output:
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
xor_eq
words missing 25



Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int search(const string &searchValue , string a[], int size)
{
    int low  = 0;
    int high = size - 1;

    while (low <= high)
    {
        int middle = (low + high) / 2;
        if (searchValue < a[middle])
        {
            high = middle - 1;
        }
        else if (searchValue > a[middle]) 
        {
            low = middle + 1;
        }
        else
        {
            return middle;
        }
    }

    // Return -1 if searchValue not found
    return -1;
}



(Indentation style is to some extent a personal preference - so long as it is consistent).
Last edited on
closed account (EAk1vCM9)
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!
Topic archived. No new replies allowed.