I\'m not exactly sure on how to start this as I\'m only given one parameter to w
ID: 3552615 • Letter: I
Question
I'm not exactly sure on how to start this as I'm only given one parameter to work with in this function that I'm supposed to implement a linear search code.
The instructions ask:
The idea of this function is to search for its parameter in the words array in the most elementary manner.
This means going through all the words from our words list, comparing w to each of them, and returning true as soon as we found a match.
If we go over all elements of words without finding a match, we return false. Make sure that the comparison between something like
Explanation / Answer
int wordsFindSlow(const char const* w){
/*
ROLE Determines whether a given word is in our words array
Implements slow linear algorithm
PARAMETERS w word to look for in the words array
RETURN VALUE 1 true if the word was found
0 false if the word was not found
*/
for (each word i in the word list in sequential order) {
if (length of word_i not equal to length of w) {
continue;
}
int found = 1;
for(k = 0; k < length of w; k++) {
char a, b;
// Converting all letters to lower case so that they can be compared easily
if ((word_i[k] >= 'A') && (word_i[k] <= 'Z')) {
a = word_i[k] - 'A' + 'a';
}
else {
a = word_i[k];
}
if ((w[k] >= 'A') && (w[k] <= 'Z')) {
b = w[k] - 'A' + 'a'
}
else {
b = w[k];
}
if (a != b) {
found = 0;
break;
}
}
if (found == 1) {
return 1;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.