# Pset 3 Recursive Binary Search problems - Always returns true

I have tried almost everything and edited my code a hundred times in search of an answer. It seems that whenever I run the program (find.c) the result always resolves to true. Not sure where the problem lies. I tried declaring the variables of min and max before I used them but that created a whole new set of problems that I also couldn't solve. Any help is appreciated!

``````bool binary_search(int value, int array[], int min, int max);

bool search(int value, int values[], int n)
{
//int min = 0;
//int max = n-1;

return &binary_search;
}

bool binary_search(int value, int array[], int min, int max)
{

int mid = (min + max) / 2;

if (array != NULL && min >= 0 )
{

if (value == array[mid])
return true;

else if (value < array[mid])
return binary_search(value, array, min, mid - 1);

else
return binary_search(value, array, mid + 1, max);
}
return false;
}
``````

I have also included the sort code in case it's an issue with the sort.

``````void sort(int values[], int n)
{

// repeat until no swaps

for (int i = 0; i < n - 1; i++) // n -2?
{
int counter = 0;

if (values[i] > values[i +1])
{
int swap = values[i];
values[i] = values[i +1];
values[i +1] = swap;
counter++;
}

if (counter > 0)
{
counter = 0;
return;
}
}
``````

``````return &binary_search;
• If you want to implement a second function, then you would call it from search the same way you called it from inside binary_search(). `return binary_search(value, array, 0,n - 1);` On the other hand, it isn't that difficult to implement as a single function. You just need to calculate both the starting point and the number of elements before making the call. Apr 27, 2017 at 16:53
• Sweet. Managed to solve search just needed to add `if (lower > upper) return false`. Sort needed more work, removed the `return` that was just idling there and put the whole thing inside a 'do while' loop. Again, thank you for your help, probably would've stuck for a lot longer otherwise! :) Apr 30, 2017 at 16:08