# Cant get binary search to return found numbers

• 10-09-2009
erekose
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!

Code:

```#include <stdio.h> #define MAXNUM 10 int search (int x, int v[], int n); void main() {         int a[MAXNUM];         int j=0;         int x=0;         int found=0;         int cnt=0;         printf("Enter a number to input into array or -1 to quit.\n");                 scanf("%d",&x);         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 */ printf("Numbers entered %d\n", cnt); /* Echo array */ for (j=0; j<cnt;j++)         printf("%d %d\n", j, a[j]); printf("Enter a number to search for in the array or -1 to quit\n"); /* Get queries */         scanf("%d", &x);         while (x != -1)         {                 found = search (x,a, cnt); /* Query array count */                 if(found=-1)                         printf("Number not found.\n");                 else                         printf("Number found at %d\n", found);         printf("Enter a number to search for or -1 to quit.\n");                 scanf("%d", &x);         } /*End while */ } /* End main */ int search (int x, int v[], int n) {         int cnt=0;         int found=0;         while(cnt<n && !found)         if (x==v[cnt])                 found=1;             else                 cnt++;         if(found)                 return cnt; /*Where found */         else                 return -1; } /* End search */```
Thanks so much!!!
• 10-09-2009
Epy
Code:

```int search (int x, int v[], int n) {         int cnt=0;         while(cnt<n)         if (x==v[cnt])                 return cnt++;             else                 cnt++;         return -1; } /* End search */```
for starters
• 10-09-2009
Epy
haha

if(found=-1)

that's an assignment bro
• 10-09-2009
whiteflags
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.
• 10-09-2009
```        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 */```