Can anyone tell me how to stop segmentation fault from happening in my binary search? I am trying to implement a recursive binary search and it works when it finds the needle in the haystack but if the needle is not there it accesses bad memory somehow. I unfortunately can't get GDB to work on my computer since I'm using Mac OS X and I installed the Homebrew version which is incompatible so I can't think of any other way to debug the program. here is the function that is called in find after giving lo 0 and hi n
bool look (int value, int values[], int hi, int lo)
{
int mid = round(hi / 2);
if (hi > 1)
{
for (int i = lo; i < hi; i++)
{
if (values[mid] == value || values[lo] == value || values[hi - 1] == value)
{
return true;
break;
}
else if (values[mid] > value)
{
hi = mid;
return look ( value, values, hi, lo);
break;
}
else if (values[mid] < value)
{
lo = mid;
return look (value, values, hi, lo);
break;
}
}
}
else if (values[hi - 1] == value)
{
return true;
}
return false;
}