Hello and thanks for responding in such detail, I really appreciate it!
Originally Posted by
cas
You're not giving “i” a value, so it's filled with garbage. A good modern compiler (e.g. gcc or clang) can warn about this:
Code:
t.c: In function 'recursiveLinearSearch':
t.c:50:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
t.c: In function 'main':
t.c:30:16: warning: 'i' is used uninitialized in this function [-Wuninitialized]
result = recursiveLinearSearch(testArr, arrLen, i, lookUpVal);
That's gcc 5.2.0 with the -Wall flag. If you're using gcc or clang,
always build with at least -Wall, which enables a lot of warnings (not all, despite its name).
Thanks for this! I thought I was using the -Wall flag, I tried to make a Makefile that looked like this:
Code:
CFLAGS=-Wall -g
all:
gcc -o p lcthw.c
./p
clean:
rm -r p
which enabled me to just type 'make' ... but it wasn't including the -Wall flag... so I changed it instead to:
Code:
all:
gcc -Wall -g -o p lcthw.c
./p
clean:
rm -r p
... which works now, but would you know how to use the CFLAGS=-Wall correctly?
The other warning is due to the fact that gcc isn't able to determine that you are always returning a value, because the conditions are a bit muddled. I find it a bit unclear, too, compared to how it might look:
Code:
if(i > arrLen) {
return 25;
} else {
if(testArr[i] == searchVal) {
return i;
} else {
return recursiveLinearSearch(testArr, arrLen, i + 1, searchVal);
}
}
That is,
is generally easier to quickly parse than
Code:
if(foo) {} else if(!foo) {}
even though they do the same thing. That's because you don't have to look carefully to compare the two conditions. Once you've seen “foo”, you know that “else” is “not foo”. Especially, as in your code, when the conditions are more complex (e.g. using &&) it becomes more burdensome to read, and you are going to be more likely to introduce a bug if you have to change two conditions instead of one. I had to look carefully to make sure gcc wasn't right.
Yes you are totally right, it's much better that way thanks.
The algorithms book specified these exact conditions in the pseudocode to be precise I guess, but it's not necessary in the final code ...
So it works now, thanks a lot ... the final code is:
Code:
#include <stdio.h>
int recursiveLinearSearch(int testArr[], int arrLen, int i, int searchVal);
int main(int argc, const char *argv[])
{
int testArr[] = {1,2,3,4,5,6};
int arrLen = sizeof(testArr)/sizeof(testArr[0]);
int result;
int lookUpVal;
int i = 0;
printf("%d\n", testArr[0]);
printf("%d\n", arrLen);
printf("Ok lets get started!\n");
printf("Enter an integer to find in the mysterious array:");
scanf(" %d", &lookUpVal);
// printf("%d\n", lookUpVal);
result = recursiveLinearSearch(testArr, arrLen, i, lookUpVal);
printf("%d\n", result);
return(0);
}
int recursiveLinearSearch(int testArr[], int arrLen, int i, int searchVal)
{
if (i > arrLen) {
return 25; // 25 = not found
}
else {
if (testArr[i] == searchVal) {
return i; // found!
}
else {
return recursiveLinearSearch(testArr, arrLen, i+1, searchVal);
}
}
}
"lcthw.c" 316L, 6812C written