I thought I got everything right but when I checked my code with check50 it shows the following errors:
:( finds 42 in {42,43,44}
\ killed by server
:( finds 42 in {40,41,42}
\ expected an exit code of 0, not 1
:( finds 42 in {39,40,41,42}
\ expected an exit code of 0, not 1
I see that there's a problem when the value we are searching for is in the beginning or in the end of the array. I don't have a clue what this can mean for my code. At what part of my code should I look to fix this problem? I hope someone of you can push me in the right direction.
bool search(int value, int values[], int n)
{
int low = 0;
int high = n;
while (low < high)
{
int middle = (high + low) / 2;
//base case
if (value == values[middle])
return true;
else if (value > values[middle]) //Value is bigger then midpoint.
low = values[middle + 1];
else if (value < values[middle]) //Value is smaller then midpoint.
high = values[middle - 1];
}
return false;
}