# Pset3 How do I initialize minimum and maximum in binary search?

I have written the following recursive code for binary search here. The errors I am getting, i think suggests I am not using int min and int max.So the question is how do I initialize or set the values for min and max in the binary search function?

``````#include <cs50.h>

#include "helpers.h"
bool binarysearch (int values[], int value, int min, int max)
{

if (min > max)
{
return false;
}

int middle = (min + max)/2 ;
// recursive to go left of the list.
if (value < values[middle])
{
return binarysearch ( values, value, min, middle -1);

}
// recursive to go right of the list.
else if (value > values[middle])
{
return binarysearch ( values, value, middle + 1, max);

}
else if (value == values[middle])
{
return true;
}

}

/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
int min = values[0] ;
int max = values[n - 1];

bool binarysearch (int values[], int value, int min, int max);

}
``````

here is the error that i get when i make find

``````helpers.c:39:4: error: control may reach end of non-void function
[-Werror,-Wreturn-type]
}
^
helpers.c:46:8: error: unused variable 'min' [-Werror,-Wunused-variable]
int min = values[0] ;
^
helpers.c:47:8: error: unused variable 'max' [-Werror,-Wunused-variable]
int max = values[n - 1];
^
helpers.c:53:1: error: control reaches end of non-void function
[-Werror,-Wreturn-type]
}
^
``````

I think maybe the issue is with your `search()` function.

It looks like maybe your line 50 is the issue.

The way you have written this line, it looks like it's a prototype, to be included at the top of a file, to let the compiler know what's coming. Is it possible you were thinking you were calling the function?

Somewhere in the `search()` function you presumably want to call the `binarysearch()` function.

You also need to return something in your `search()` function, since you declared it should have a bool output in line 44.

Since your `boolsearch()` function also returns a bool, you can probably just return its value, like so:

``````return binarysearch( values, value, min, max);
``````

I'm thinking maybe you meant that instead of what you have in line 50, but I could be off target.

UPDATE: Change:

`````` /**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
int min = values[0] ;
int max = values[n - 1];
bool binarysearch (int values[], int value, int min, int max);
}
``````

to:

``````/**
* Returns true if value is in array of n values, else false.
*/
bool search(int value, int values[], int n)
{
int min = values[0] ;
int max = values[n - 1];
return binarysearch (values, value, min, max);
}
``````

That last line of code needs to call the function binarysearch. The way you had it written, it was declaring a prototype of binarysearch.

HTH

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark

• If I remove the ";" in the bool search function then the compiler gives the error of undeclared function Sep 16 '15 at 15:03
• Huh? Which semi-colon? Sep 16 '15 at 16:11
• bool search(int value, int values[], int n) { int min = values[0] ; int max = values[n - 1]; bool binarysearch (int values[], int value, int min, int max); <<<< this semi colon, though i have made my binaray search now with a while loop but still i wanna know why this code was not working for me Sep 17 '15 at 12:42
• Furukh, I added more to the answer above Sep 17 '15 at 17:03