0

I've spent hours on this, have tried multiple configurations based on code I can find online, but am getting no where. Any input would be appreciated. I'm trying to make a recursive, binary search function (extra curricula activity, but linked with pset3). My code:

bool recur_search(int key, int values[], int lower, int upper)
{
    // base case 1: not in haystack
    if (upper < lower)
    {
        return false;
    }

    int mid = (lower + upper) / 2;

    // base case 2: found
    if (key == values[mid])
    {
        return true;
    }

    // recursive cases
    else if (key < values[mid])
    {
        recur_search(key, values, lower, mid - 1);
    }        
    else // (key > values[mid])
    {
        recur_search(key, values, mid + 1, upper);
    }

    // to get rid of "error: control may reach end of non-void function"
    return false;
}

It finds the middle value of a sorted array but nothing else. Valgrind has no complaints, but I can't get GDB to run with it - it just skips over my breakpoints no matter where I put them in the main function.

The inputs are: key - search key; values[] - array to be search; lower and upper - the indices of the first and last array value (I've entered 0 and size - 1 respectively).

0
0

classic mistake! what are you doing with the value returned by a recursive call?

2
  • GMTA! I flagged it and referenced the explanation. – Cliff B Jun 8 '16 at 1:52
  • Thanks. I read through so many similar posts, can't believe I missed that single word - "return". Much appreciated! – user11665 Jun 8 '16 at 2:21