I have a programme that calls a function, which in turn calls a recursive function.
How do you pass the result of the recursive function back to the main programme via the middle function?
Currently it will return either found or didn't find depending on how I structure the middle function. In this instance it would return 'found' every time. I know that the binary search function is working although there may be an error in what it is returning to the search function.
Call from main programme:
if (search(value, array, size))
{
printf("\nFound!\n\n");
return 0;
}
else
{
printf("\nDidn't find.\n\n");
return 1;
}
Search function:
bool search(int value, int values[], int n)
{
if(binary_search(value, values, ((n - n) + 1), n))
{
return true;
}
else
{
return false;
}
}
Recursive function:
bool binary_search (int key, int array[], int min, int max)
{
if (max < min)
{
return -1;
}
int midpoint = (min + max)/2;
// {
if (array[midpoint] > key)
{
return binary_search (key, array, min, midpoint-1);
}
else if (array[midpoint] < key)
{
return binary_search (key, array, midpoint+1, max);
}
else if (array[midpoint] == key)
{
return midpoint;
}
else
{
return false;
}
// }
}
return binary_search(value, values, ((n - n) + 1), n)?