0

I am trying to test the find function in problem set 3. To get started, I wanted to use a linear search function:

{
// TODO: implement a searching algorithm
for (int value = 0; value < n; value++)
{
    if ( value == values[value])
    return true;
}
return false;

}

I can compile the find.c function without an error. However, when I test with:

./generate 1000 50 | ./find 127

The output is as if I didn't code anything new:

haystack[999] =                                                                                                                             

haystack[1000] =                                                                                                                            


Didn't find needle in haystack. 
0

your implementation of loop is incorrect. The variable value is already defined in the function parameter as the value to be searched in the values[] array. So when you are again declaring value inside the loop you are basically overwriting the data inside variable value. What you need to do is to initialize the loop with some arbitrary variable like int i = 0 and in each loop compare value with values[i].

You must log in to answer this question.

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