Thread: binary search

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    72

    binary search

    Code:
    #include<iostream.h>
    #include<conio.h>
    
    int binarysearch(int[], int, int);
    
    void main(void)
    {
         clrscr();
         int a[10]={6,4,89,23,45,65,88,22,11,56};
         int loc, item, n=10;
         cout<<"Enter Item for Search: ";
         cin>>item;
         loc=binarysearch(a, item, n);
         if(loc == -1)
         cout<<"Not Found\n";
         else
         cout<<"Found At Location: "<<loc;
         getch();
         }
         int binarysearch(int a[], int item, int n)
         {
             int mid, end, beg;
             beg=0;
             end=n-1;
             while(beg <= end)
             {
                       mid=int((beg+end)/2);
                       if(item == a[mid])
                       return mid;
                       if(item < a[mid])
                       end=mid-1;
                       else
                       beg=mid+1;
                       }
                       return -1;
                       }
    what's wrong with this? ;\ please let me know

    thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Hmm, what is wrong.....

    • iostream.h
    • conio.h
    • void main()
    • Horrible indenting


    This list is not necessarily conclusive.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    HRM

    Indenting Has nothing to do with compilation
    Iostream.h Is used for Input/output else cin/cout are not recognized
    void main() <- even If I change that to int main still gives an error
    conio.h <- clearscreen = clrscr(); wont work without it neither will getch(); but instead of getch(); we can use cin.get(); however we cant use clrscr

    now If you can fix the above logicaly please reply
    thank you

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    72
    Code:
    #include<iostream.h>
    #include<conio.h>
    int binarysearch(int[], int, int);
    
    int main(void)
    {
        
        int a[10]={6,4,89,23,45,65,88,22,11,56};
        int loc, item, n=10;
        cout<<"Enter Item for Search: ";
        cin>>item;
        loc=binarysearch(a, item, n);
        if(loc == -1)
        cout<<"Not Found\n";
        else
        cout<<"Found At Location: "<<loc;
        getch();
          }
         int binarysearch(int a[], int item, int n)
         {
             int mid, end, beg;
             beg=0;
             end=n-1;
             while(beg <= end)
             {
                       mid=int((beg+end)/2);
                       if(item == a[mid])
                       return mid;
                       if(item < a[mid])
                       end=mid-1;
                       else
                       beg=mid+1;
                       }
                       return -1;
                       }
    fixed one, works fine
    thanks anyways UNCLE Mac :P

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by salmansalman View Post
    HRM

    Indenting Has nothing to do with compilation
    It has all to do with people reading it.

    For example, I might have pointed out that binarysearch appears to be defined inside main() (as it appears at first glance).

    Iostream.h Is used for Input/output else cin/cout are not recognized
    void main() <- even If I change that to int main still gives an error
    conio.h <- clearscreen = clrscr(); wont work without it neither will getch(); but instead of getch(); we can use cin.get(); however we cant use clrscr
    You are probably using some ancient compiler, writing non- and prestandard code. Unfortunately yes, there is no standard way to clear the screen (but what's the big deal - if the program is run by double-clicking on the exe/from the IDE, the screen will be empty anyhow?).

    As for what is wrong - did you know that binary search is only supposed to work on sorted data?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by salmansalman View Post
    thanks anyways UNCLE Mac :P
    Apparently I need to have a talk with a sibling of mine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  2. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 06:19 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM