I have written the following recursive code for binary search here. The errors I am getting, i think suggests I am not using int min and int max.So the question is how do I initialize or set the values for min and max in the binary search function?
#include <cs50.h>
#include "helpers.h"
bool binarysearch (int values[], int value, int min, int max)
{
if (min > max)
{
return false;
}
int middle = (min + max)/2 ;
// recursive to go left of the list.
if (value < values[middle])
{
return binarysearch ( values, value, min, middle -1);
}
// recursive to go right of the list.
else if (value > values[middle])
{
return binarysearch ( values, value, middle + 1, max);
}
else if (value == values[middle])
{
return true;
}
}
/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
int min = values[0] ;
int max = values[n - 1];
bool binarysearch (int values[], int value, int min, int max);
}
here is the error that i get when i make find
helpers.c:39:4: error: control may reach end of non-void function
[-Werror,-Wreturn-type]
}
^
helpers.c:46:8: error: unused variable 'min' [-Werror,-Wunused-variable]
int min = values[0] ;
^
helpers.c:47:8: error: unused variable 'max' [-Werror,-Wunused-variable]
int max = values[n - 1];
^
helpers.c:53:1: error: control reaches end of non-void function
[-Werror,-Wreturn-type]
}
^