0

Working on pset3 and tried to write a separate binary search function in helpers.c so I can just call it later in the main search function. Here's my code for the binary search:

bool binarysearch(int target, int array[], int start, int end);
{
    //make sure array exists
    if (array != NULL)
    {
        //makes sure array has remaining values to search
        if (start > end)
            return false;

        else
        {
            //define midpoint
            int mid = (start + end)/2;

            //if midpoint is target, return true
            if (array[mid] == target)
            {
                return true;
            }

            //if midpoint is greater than target, search left    
            else if (array[mid] > target)
            {
                return binarysearch(target, array, start, mid - 1);
            }

            //if midpoint is less than target, search right
            else if (array[mid] < value)
            {
                return binarysearch(target, array, mid + 1, end);
            }
    }
}

And here's the call in the same file:

bool search(int value, int values[], int n)
{
    if (binarysearch(value, values, 0, n) == true)
        return true;

    else
        return false;
}

When I try to make find in my terminal, it returns a top error of "error: control reaches end of non-void function." Where am I going wrong? Is the binary search itself broken or am I calling it wrong in search? Thanks in advance!

0

A return statement is required at the end of main, the compiler does not know if you are going to return true or false because of IF-ELSE conditions. The solution is to return false at the end of the main function

| improve this answer | |
  • I added a return false; just above the last curly bracket but now when I run find it says the needle wasn't found no matter what, even if I enter a value I know for sure is in the generated numbers. Any idea why this might be? – A. H. Nov 17 '16 at 19:48
  • 1
    @MARS Since c99 main is not required to return 0, it is the default behaviour when nothing else is returned. stackoverflow.com/a/4138710/6091017 – Martin Kleiven Nov 17 '16 at 20:07
  • This is not applicable in this case. We have a function that has to return a value and the error is clear – MARS Nov 17 '16 at 21:43
  • 1
    It is still applicable, it is part of the of the C standard. That said I now see you we're actually referring to binarysearch and not specifically to main, given that main was written by staff and is functional and that binarysearch is clearly missing a return. – Martin Kleiven Nov 17 '16 at 21:55
  • @A.H. Now that you have your errors sorted out, you're back to figuring out how to make it work. Debugging a binary search function is tedious though, so I recommend paying close attention to the spec and also look up some resources; I'd start with stackoverflow.com/questions/504335/… and then I also found an interesting article on the topic cprogramming.com/tutorial/binarysearchchallenge.html, skip over any code you see though, as implementing binary search yourself is half the fun! Also please accept one of our answers :) – Martin Kleiven Nov 17 '16 at 22:07
1

Your binarysearch function follows this logic:

if (something)
{
    if (something_else)
    {
    else
    {
        /* ... */
    }

    /* **ERR:NOTHING_IS_RETURNED** */
    // Here you must add a return false statement
}

As you can see you're not returning anything in case the first if condition fails.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .