Checking if word is in the dictionary
Oct 15, 2013 at 1:45pm UTC
I can't seem to check if the word is in the dictionary. It always that the word is not in the dictionary. I think it's because of my comparing. Can somebody help on how to do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
while ( lower <= upper ) {
middle = (lower + upper) / 2;
printf("%d\n" , middle);
if (dict[middle] == word){
printf("%s\n" , dict[middle]);
m = 1;
break ;
}else if (dict[middle] < word) {lower = middle+1; }
else { upper = middle-1; printf("%d\n" , upper);}
}m = 0;
if (m==0){
printf("%s\n" , word);
printf("%s\n" , dict[middle]);
printf("%d\n" , middle);
printf("%s doesn't exist in the scrabble dictionary\n" , word);
break ;
}
Oct 15, 2013 at 1:55pm UTC
Oct 15, 2013 at 1:57pm UTC
I've change it to this now but it's still not working properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while ( lower <= upper ) {
middle = (lower + upper) / 2;
printf("%d\n" , middle);
printf("%d\n" , strcmp(word, dict[middle]));
if (strcmp(word, dict[middle]) == 0){
printf("%s\n" , dict[middle]);
m = 1;
break ;
}else if (strcmp(word, dict[middle]) == 1) lower = middle+1;
else upper = middle-1;
}m = 0;
if (m==0){
printf("%s\n" , word);
printf("%s\n" , dict[middle]);
printf("%d\n" , middle);
printf("%s doesn't exist in the scrabble dictionary\n" , word);
break ;
}
Oct 15, 2013 at 1:59pm UTC
but you still didnt answer: what data type is dict[] and word?
also you can paste whole code here so we can test it.
Oct 15, 2013 at 2:09pm UTC
Oct 15, 2013 at 2:20pm UTC
Wait new problem, I can't seem to initialize lower and upper again after this part. But before it, it's okay. Why is not working?
1 2 3 4 5 6 7 8 9 10
while ( lower <= upper ) {
middle = (lower + upper) / 2;
if (strcmp(word, dict[middle]) == 0){
m = 1;
break ;
}else if (strcmp(word, dict[middle]) > 0) lower = middle+1;
else upper = middle-1;
}
lower = 0;
upper = 178692;
Topic archived. No new replies allowed.