Thread: Cant get binary search to return found numbers

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Unhappy 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!!!

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    how about:
    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

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    haha

    if(found=-1)

    that's an assignment bro

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

    So, what kind of a search do you want or need?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Finding a word in a string.
    By esbo in forum C Programming
    Replies: 15
    Last Post: 08-28-2006, 07:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM