Thread: binary search program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    binary search program

    this is a code for the binary search program in c language.now the program is not showing any error but when i run this program then the output is like:
    enter the total numbers:6
    enter the array elements: 5 4 7 1 3 2
    the sorted numbers are:1 2 3 4 5 7
    enter the number to be searched:5
    then it prints::::the number is not found
    now why does it always print the number is not found no matter what number i enter.it always show "the number is not found"????why?????it is printing the sorted list alright.so why not the number then?plz help me out!!!!!!!!
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a[10],n,i,j,temp;
    int beg,end,mid,target;
    clrscr();
    printf(“enter the total numbers:”);
    scanf(“%d”,&n);
    printf(“enter the array elements:” );
    for(i=0;i<n;i++)
    scanf(“%d”,&a[i]);
    for(i=0;i<n-1;i++)
        {  
         for(j=0;j<n-i-1;j++)
           {
              If(a[j+1]<a[j])
                {
                   temp=a[j];
                    a[j]=a[j+1];
                     a[j+1]=temp;
                }
          }
      }
    printf(“the sorted numbers are:”);
    for(i=0;i<n;i++)
    printf(“%4d”,a[i]);
    beg=a[0];
    end=a[9];
    mid=(beg+end)/2;
    printf(“\nenter the number to be searched:”);
    scanf(“%d”,&target);
    while(beg<=end && a[mid]!=target)
    {
    if(target<a[mid])
     end=mid-1;
    else
    beg=mid+1;
    mid=(beg+end)/2;
    }
    If(a[mid]==target)
      {
    printf(“\nthe number is found at position %2d”,mid);
      }
    else
        {
    printf(“\nthe number is not found:”);
         }
    getch();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's probably because you're using void main. That's undefined, and as a result, anything could happen. Seriously though, you'll have to learn how to indent if you want anyone to wade through all that crap. While you're at it, you should really consider breaking things up into functions.

    Plus I see you've got something called "If" in there...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    beg=a[0];
    end=a[9];
    mid=(beg+end)/2;
    printf(“\nenter the number to be searched:”);
    scanf(“%d”,&target);
    while(beg<=end && a[mid]!=target)
    Your initializing/setting of beg, end and mid are wrong. You should be setting these values to the indicies and not the values stored at those positions in the array. mid for example should be set to index 4 or 5 and not the average of the values at the begining and end of the array. Consider what happens the first time through the loop if a[0] = 20 and a[9] = 100. beg becomes 20, end becomes 100 and mid becomes 60. You then test a[mid] (i.e. a[60]) which is an invalid array element.

    Code:
    void main
    Should always be int instead of void.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    i m not able to understand here....what exactly do u want m,e to try?
    could u plz make some necessary changes in my code....in order to make it run properly and what all i have been lacking there?plz help me out .......?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    before somebody will even consider looking at your code you should indent it. And as quzah said, void main is undefined.
    Last edited by Shakti; 01-24-2005 at 12:02 PM.

  6. #6
    Saravanan S-CH s.saran's Avatar
    Join Date
    Dec 2004
    Location
    India
    Posts
    4
    Hi,

    Code:
    beg=a[0];
    end=a[9];
    mid=(beg+end)/2;
    printf(“\nenter the number to be searched:”);
    In the above code, use n-1 instread of 9. because it will affect the Binary search Logic if you used less or more than 10 input values.
    Code:
    if(a[mid]==target)
      {
    printf("\nthe number is found at position %2d",mid);
      }
    else
        {
    printf("\nthe number is not found:");
    Use (mid+1) while printing the position.

    Note:: Output Verified .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching a binary search tree - doesn't work
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-11-2005, 03:53 PM
  2. Bible program - search functionality
    By ChadJohnson in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-15-2005, 11:33 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM