# helpers.c passes check50 but fails ./generate 1000 50 | ./find 128

check50 gives me all smiles, but when I run the test code ./generate 1000 50 | ./find 127 or 128 they both return "Found needle in haystack". How is that possible? And also, can I be sure my code is right based on the results from check50 even though the debugging test they suggested themselves fails in one of the scenarios? Please help me.

Here is my basic implementation for bool search(int value, int values[], int n), considering that min is 0, max is n - 1, and middle is min + max / 2:

``````//pseudo code
for loop int i is 0; i less than n; i++
{
if max less than min just return 1;

else
if values[i] less than value
min is middle + 1;

else if values[i] greater than value
max is middle - 1;

else
just return middle;
}
return 0;
``````

And here is my basic implementation of void sort(int values[], int n):

``````for loop int i is 0; i less than n; i++
{
for loop int j is i; j less than *values; j++
{
if values[j] less than values[i]
{
swapper is values[j];
values[j] is values[i];
values[i] is swapper;
}
}
}
``````

Looking at your pseudocode gives me some thoughts, but also reduces confidence on how accurate my analysis is. I'd prefer to see actual code. Not sure if your sort works because I'm unsure what the for loop limit on j really is. However, I'm pretty sure that you have some serious issues with your search function.

It is always dangerous to return numbers when a function calls for the return of a bool. Instead, it's always best to return `true` or `false` when returning a bool. When numbers are returned, a 0 is always interpreted as false, and a non-zero is interpreted as true. The first return should be a false, but it returns 1. I'm not entirely sure what you wish to return in later statements. This could account for why your test is returning the results you see.

Note that there's also a common practice of returning 0 when a program or function completes, or a non-zero to indicate a specific error. This is directly opposite from how numbers are interpreted when a bool is supposed to be returned. Don't confuse the two standards.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Thank you for responding my question. I didn't want to put the raw code here (they warn us not to). I will try your suggestions and see if the outcome of my code changes. Thank you for the information, very enlightening.
– Lele
Commented Oct 5, 2016 at 14:19
• I changed a few things, based on your suggestions, and now ./generate 1000 50 | ./find 127 and 128 works accordingly. But check50 now gives me an error: `:( finds 42 in {42,43,44} \ expected an exit code of 0, not 1`. I changed the `return 1` of the line `if max less than min just return 1;` to `break` instead. And the `return 0;` outside the for loop in search() to `return false;`. I did not change anything in sort(). Why doesn't it find 42 in {42, 43, 44} but does find 42 in all the others? I'm confused.
– Lele
Commented Oct 5, 2016 at 15:58
• Again, without seeing code, it's difficult to diagnose your problems. Changing that return 1(true) to a break, may or may not work, depending on the rest of the code. return false would have been the better choice. Have you verified that your sort is working correctly? Maybe print out the list? I'm suspicious that the only test that fails is the one where the needle is the smallest element in the list. Commented Oct 5, 2016 at 20:17
• I tried everything I could think of. I can't understand why only the smallest value of the array wouldn't be sorted. Please help! I put the code in GitHub on a secret gist. This is the link: gist.github.com/lelecarabina/a4b9f68e9941ccd51f2bba2a6d191620
– Lele
Commented Oct 7, 2016 at 13:11
• OMG I figured it out! in the inner loop of sort() I had this line `for (int j = i; j < *values; j++)`. I then changed it to `for (int j = i; j < n; j++)` and everything worked, check50, 127 and 128. Feeling good :)
– Lele
Commented Oct 7, 2016 at 14:00