Cant get binary search to return found numbers

This is a discussion on Cant get binary search to return found numbers within the C Programming forums, part of the General Programming Boards category; I put the following code in, compiled the program and ran it, but it doesnt find and return any numbers ...

Cant get binary search to return found numbers

I put the following code in, compiled the program and ran it, but it doesnt find and return any numbers to me! Can anyone point me in the right directions here? I've tried other boards but all they said was initialize my ints...I did and it didnt make any difference. Any help would be greatly appreciated!

Neither of these examples are binary search. Binary search works by eliminating half of the possibilities each step, by examining the midpoint. You either find X when it is the midpoint, or you run out of search items. In fact, many implementations return either X's place or -1, so try writing a real binary search.

In this part of the code, you're assigning a[0] = x, and x is still 0, since the assignment is done before the scanf() for a new x line of code, has been reached.

Code:

while (x != -1 && cnt<MAXNUM)
{
a[cnt++]=x;
{if(cnt==MAXNUM)
printf("The array is full.\nEnter -1 to begin searching the array for a number.\n");
else
printf("Enter a number to input into array or -1 to quit.\n");
scanf("%d", &x);
} /* End of if */
} /* End of while */

Also, you need to say if you need a binary search (which you don't have at all, right now), or you really want to do a sequential search.