I wrote the following recursive binary search function. No matter which exit codes I use (e.g., true, false, 1, 0, -1, "Swedish Chef"), I fail at least two of the checks I need to pass. The code looks right, however, I'm out of ideas as to fixing the return values. Note, the accompanying sort function works perfectly with arrays of all sizes.
Here is the code: bool binary(int value, int values[], int min, int max) {
if (max < min)
{
return false;
}
int midpoint = (min + (max + min)/2);
if (values[midpoint] == value)
{
return midpoint;
}
else if (values[midpoint] < value)
{
return binary(value, values, (midpoint + 1), max);
}
else if (values[midpoint] > value)
{
return binary(value, values, min, (midpoint - 1));
}
return false; }
And here are the error messages.
:) helpers.c exists
:) helpers.c compiles
:) finds 42 in {42,43,44}
:) finds 42 in {41,42,43}
:( finds 42 in {40,41,42} \ expected an exit code of 0, not 1
:( finds 42 in {41,42,43,44} \ expected an exit code of 0, not 1
:( finds 42 in {40,41,42,43} \ expected an exit code of 0, not 1
:( finds 42 in {39,40,41,42} \ expected an exit code of 0, not 1
:) doesn't find 42 in {39,40,41}
:) doesn't find 42 in {39,40,41,43}