# Pset 3 error - Binary search

I'm having some difficulty implementing binary search. I wrote linear search with no problems but when I compile my code for binary search, I get an error. I decided to take the iterative approach just because it wasn't comfortable for me to nest a recursive function inside of the search function.

``````bool search(int value, int values[], int n)
{
// implement a searching algorithm (binary)
int min = 0;
int max = n;
int middle = (min + max) / 2;
while(min <= max)
{
if(values[middle] > values)
{
max = middle - 1;
}
else if(values[middle] < values)
{
min = middle + 1;
}
else if(values[middle] == values)
{
return true;
}
}
return false;
}
``````

and the error that I'm getting is: It's what you are comparing that is giving you issues. You are comparing an array element to the array itself, `values[x] > values`. Or, to put it another way, you have the same typo (or a cut and paste error) in three different places.
Try using `value` instead of `values` as specified in the function signature `bool search(int value, int values[], int n)`
• I don't understand , how `(min+max)/2` gives the middle number? Sep 10, 2015 at 9:13