0

I have tried almost everything and edited my code a hundred times in search of an answer. It seems that whenever I run the program (find.c) the result always resolves to true. Not sure where the problem lies. I tried declaring the variables of min and max before I used them but that created a whole new set of problems that I also couldn't solve. Any help is appreciated!

bool binary_search(int value, int array[], int min, int max);

bool search(int value, int values[], int n)
{
//int min = 0;
//int max = n-1;

return &binary_search;
}

bool binary_search(int value, int array[], int min, int max) 
{

int mid = (min + max) / 2;

if (array != NULL && min >= 0 )
{

if (value == array[mid])
    return true;

else if (value < array[mid])
    return binary_search(value, array, min, mid - 1);

else 
    return binary_search(value, array, mid + 1, max);
}
return false;
}

I have also included the sort code in case it's an issue with the sort.

void sort(int values[], int n)
{

 // repeat until no swaps

for (int i = 0; i < n - 1; i++) // n -2? 
{
    int counter = 0;

    if (values[i] > values[i +1])
    {
    int swap = values[i];
    values[i] = values[i +1];
    values[i +1] = swap;
    counter++;
    }

    if (counter > 0)
    {
        counter = 0;
        return;
    }
}
0
return &binary_search;

Of course this always returns true. This is not a call to binary_search(). It simply says "return the address of binary_search". It returns a non-zero number. A non-zero number is treated as "TRUE". We won't even discuss that if it were a call to binary_search(), it wouldn't have any parameters. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

5
  • Thank you for answering! But now I've run into the original problem that I thought I solved by putting the "&" in as the compiler suggested. I think trying to get one function to call another as in is causing the problem: Really what is happening is the original function calls for 3 parameters but I want to use 4 parameters without changing the original prototype. I thought the solution was to somehow use the first function to call the second but it just doesn't work. Should I ask this here or should I open a new question? Not sure of the etiquette of the forum haha :). – Mo Amer Apr 27 '17 at 16:01
  • If you want to implement a second function, then you would call it from search the same way you called it from inside binary_search(). return binary_search(value, array, 0,n - 1); On the other hand, it isn't that difficult to implement as a single function. You just need to calculate both the starting point and the number of elements before making the call. – Cliff B Apr 27 '17 at 16:53
  • Think I'm closer now to an answer, it might be my sort function that is causing a bigger issue i.e not doing it's job properly. Just need to stick at it a little longer. Thank you for your help. :) – Mo Amer Apr 29 '17 at 23:41
  • Yes, the sort needs work. It has two major issues. First, it will return after the first pair of numbers is swapped, so no further sorting is being done. Second, even after you remove that problem, it is only making one pass through the list, which will only bubble the largest value to the top. It needs to keep making passes through the remaining unsorted part of the list until it is completely sorted, thus big-O = n*n. Perhaps a review of the lectures and shorts on bubble sorts is in order? ;-) – Cliff B Apr 29 '17 at 23:46
  • Sweet. Managed to solve search just needed to add if (lower > upper) return false. Sort needed more work, removed the return that was just idling there and put the whole thing inside a 'do while' loop. Again, thank you for your help, probably would've stuck for a lot longer otherwise! :) – Mo Amer Apr 30 '17 at 16:08

You must log in to answer this question.

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