# pset 3 binary search

I know the logic behind binary search is simple but my code isn't compiling. I went along the lines of week 3's section. I am still getting compilation errors.

``````int beginning = 0;
int ending = n - 1;
int middle = (beginning + ending) /2;

while (ending - beginning >= 0)
{

// ERROR: ordered comparison error between pointer and integer
if (values[middle] > values)
{
ending = middle - 1;
}

// ERROR: comparison between pointer and integer
else if (values[middle] == values)
{
return true;
}

//ERROR: expression result unused && expected ;
else (values[middle] < values)
{
beginning = middle + 1;
}

middle = (beginning + ending) /2;

}
``````

You have declared an array called values.

When you say 'values[middle]', it refers to the middle element of array. On the other hand, 'values' refers to the address of the beginning of that array. You are actually comparing address of array with the one of the element of the same array.

I assume that you wanted to compare 'values[middle]' to the number you are looking for, but by mistake compared with 'values' or you have assigned your search element to variable named 'values'. If so then just store the search element in some other variable.

Third error is about 'if else' condition. When you say 'else' it becomes default case for the else if statements. Therefore, the comparison after 'else' won't have any significance. In your case just else would've been suffice, no need to put comparison. If you want to put comparison use 'else if' instead

The values object seems to be a pointer to an array containing some kind of data. The 'basic logic' seems fine, but it seems like you're not comparing with the value you're looking for, you're comparing with the pointer to the entire array.

Imagine your values array contains integers: ``` values[] = {2,5,7,11,56,88,100} ``` Then values represents the array. `Values[middle]` represents the 11 in the array.

How much sense does it make to compare 11 to pointer to array? Not much right? You need to compare with the value you are looking for.

So you need to replace `values` in your if / else statements with a variable containing the value you are looking for. `values[middle]` is fine though, don't touch that.

Now I can't see all your code, but I sure hope you didn't actually put the value you were looking for in the values variable somehow. If you did, you should keep it in a seperate variable called `searchValue` or something.

This would result in changing:

`if (values[middle] > values)` to `if (values[middle] > searchValue)`
`else if (values[middle] == values)` to `else if (values[middle] == searchValue) `
and `else (values[middle] < values) ` to `else (values[middle] < searchValue) `

Where `searchValue` should be a variable of the same type as the stuff that you are storing in `values[]`, and should not be NULL of course.

edit: Also, the else statement activates by default if the other two don't. Either turn that into an `else if` as well, or remove the condition. (Preferably the former)
Hope this helps!

values is an array not a single value that has to be checked. Here it seems that you are comparing a pointer to an array with the middle value.

Also the third statement should be inside the while loop as you want to repeatedly calculate middle not just once.