Having an assignment where I have to do a binary search of an array using recursive functions and pointers (Yes, its easier without pointers, but that's what the assignment is)
I can't seem to get it to work, and I'm at a loss. It seems to default to false all the time
bool containedInSortedarray(int x, const int* pBegin, const int* pEnd)
{
assert(pEnd-pBegin != 0); //Checks if array is empty
if (pEnd-pBegin == 1 && *pBegin == x) //Base case
return true;
if (pBegin < pEnd){
int midPoint = (*pBegin + *(pEnd-1) / 2);
if(midPoint == x)
return true;
if(*pBegin > x)
return containedInSortedarray(x, pBegin, pEnd - midPoint);
else if(*pBegin < x)
return containedInSortedarray(x, pBegin + midPoint, pEnd);
}
return false;
}
int main(){
int x = 2;
const int size = 9;
int sampleArray[size] = {1,2,3,4,5,6,7,8,9};
assert(containedInSortedarray(x, &sampleArray[0], &sampleArray[size]));
return 0