0

I'm having a lot of difficulties with this problem since there are lots of concepts that I don't know. I'm trying to treat dictionary as an array of strings as below:

bool lookup(string s)
{
int min = 0;
int max = WORDS - 1;
while ( min <= max)
{
    int middle = round(max + min / 2);
    for (int i = 0, n = strlen(s); i < n; i++)
    if (s[i] < dictionary[middle][i])
    {
        max = middle -1;
    }
    else if (s[i] > dictionary[middle][i])
    {
        min = middle +1;
    }
    else if (s[i] == dictionary[middle][i])
    {
        return true;
        found = true;
    }
    else
    {
        return false;
    }
}
}

I really don't understand these lines:

typedef struct
{
bool found;
char letters[LETTERS + 1];
}
word;


struct
{
int size;
word words[WORDS];
}
dictionary;

Could someone please explain how can I use dictionary as an array of strings, what should I do with word to have found in my lookup function as a "declared identifier"

I'm getting these errors:

scramble.c:444:20: error: use of undeclared identifier 'words'
    if (s[i] < words[middle][i])
               ^
scramble.c:448:25: error: use of undeclared identifier 'words'
    else if (s[i] > words[middle][i])
                    ^
scramble.c:452:26: error: use of undeclared identifier 'words'
    else if (s[i] == words[middle][i])
                      ^
scramble.c:455:13: error: use of undeclared identifier 'found'
        found = true;
        ^
4 errors generated.
make: *** [scramble] Error 1
1

dictionary contains an array of words and each of those words contain an array of letters. So dictionary.words[0].letters would represent the first word in the dictionary.

You're on the right track, but it looks like you are trying to access the first character in the word? I'm not sure that is going to give you what you need, because what happens if two words have the same first letter?

I suggest investigating strcmp instead, which will return a value that will help you determine whether you need to search the left or right side of the array. (it returns 0 when the strings are the same, and a negative or positive number depending on alphabetical order difference.)

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .