0

I am getting the following error below. It has to do with my binarySearch function.

helpers.c:149:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]
}
^ 

However I have a return value in the if and else statements. Can someone explain why I get that error even though I have return statements?

   #include <cs50.h>
    #include <stdio.h>

    #include "helpers.h"

    #define     MIN     0

    bool search(int value, int values[], int n)
    {
        int p;
        p = binarySearch(value, values, MIN, n-1);

        if (p == -1)
            return 0;
        else
            return 1;

    }

    int findMidPoint(int min, int max)
    {
        int midpoint = (max + min)/2;

        return midpoint;

    }

    int binarySearch(int value, int values[], int min, int max)
    {
        //if (max < min):
        int midpoint, max_i, min_i;
            //return -1
        max_i = max;
        min_i = min;


        if (max_i < min_i)
        {
            return -1;

        }

        else
        {
            midpoint = findMidPoint(min, max);

            if (values[midpoint] < value)
            {
               min_i = midpoint + 1;
               binarySearch(value, values, min_i, max_i);           
            }
            else if(values[midpoint] > value)
            {
                max_i = midpoint - 1;
                binarySearch(value, values, min_i, max_i);  
            }
            else
            {
                return midpoint;
            }

        }

    }
1

Currently, if binarySearch does not score a direct hit (values[midpoint] == value), it does not return anything. In those cases, however, you should return the result of the recursive call, which you currently ignore.

To fix this, in binarySearch, place a return in front of your calls to binarySearch, like

            if (values[midpoint] < value)
            {
                min_i = midpoint + 1;
                return binarySearch(value, values, min_i, max_i);           
            }
            else if (values[midpoint] > value)
            {
                max_i = midpoint - 1;
                return binarySearch(value, values, min_i, max_i);  
            }
            else
3
  • Blauelf is correct. You're missing critical returns in the code. You might find the following explanation helpful: cs50.stackexchange.com/questions/9691/… Blauelf, while you're correct, this is a learning and teaching forum, so instead of simply providing a correct solution, please try to give explanation, or better yet, guidance without the solution, so that the questioner can work it out and learn from it. ;-)
    – Cliff B
    Jan 4 '17 at 11:09
  • Thank you. I am truly humbled. :)
    – Salamit
    Jan 4 '17 at 11:09
  • @CliffB Would have provided more information, but was a bit in a hurry, thanks for providing some link with context. Tried to add some explanation to my answer.
    – Blauelf
    Jan 4 '17 at 12:40

You must log in to answer this question.

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