binary_search

Discussion in 'C++' started by arunix, Sep 1, 2009.

  1. arunix

    arunix Guest

    hello dear all its mine first post here i am newbie with "C++" i am
    learning "C++ Algorithms" here is mine problem that i am ussing
    "binary_search" into the code its given me "false value" 0 boz when i
    gave the number which is in the list it gives me the False Part "Your
    Number is not in the List" when condition is true. plz tell me whats
    wrong with mine code

    here is mine code.


    #include<iostream>
    #include<algorithm>

    int main()
    {
    int num[]={2,6,25,14,86,52,64,75,27,52,96,58,52.0};
    const int start=1, end=2;
    int check;
    std :: cout <<"please Enter Your Number :- ";
    std :: cin >> check;

    if(std ::binary_search((num+start),(num+end),check))
    {
    std :: cout<<" Your Value is in the List " << check <<std ::
    endl;
    }
    else
    {
    std :: cout <<"Your Number is not in the List "
    <<std :: endl;
    }
    return 0;
    }
     
    arunix, Sep 1, 2009
    #1
    1. Advertising

  2. * arunix:
    > hello dear all its mine first post here i am newbie with "C++" i am
    > learning "C++ Algorithms" here is mine problem that i am ussing
    > "binary_search" into the code its given me "false value" 0 boz when i
    > gave the number which is in the list it gives me the False Part "Your
    > Number is not in the List" when condition is true. plz tell me whats
    > wrong with mine code
    >
    > here is mine code.
    >
    >
    > #include<iostream>
    > #include<algorithm>
    >
    > int main()
    > {
    > int num[]={2,6,25,14,86,52,64,75,27,52,96,58,52.0};
    > const int start=1, end=2;
    > int check;
    > std :: cout <<"please Enter Your Number :- ";
    > std :: cin >> check;
    >
    > if(std ::binary_search((num+start),(num+end),check))
    > {
    > std :: cout<<" Your Value is in the List " << check <<std ::
    > endl;
    > }
    > else
    > {
    > std :: cout <<"Your Number is not in the List "
    > <<std :: endl;
    > }
    > return 0;
    > }
    >


    Are you sure that you want to search only from element 1 to (but not including)
    element 2?


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Sep 1, 2009
    #2
    1. Advertising

  3. arunix wrote:
    > hello dear all its mine first post here i am newbie with "C++" i am
    > learning "C++ Algorithms" here is mine problem that i am ussing
    > "binary_search" into the code its given me "false value" 0 boz when i
    > gave the number which is in the list it gives me the False Part "Your
    > Number is not in the List" when condition is true. plz tell me whats
    > wrong with mine code
    >
    > here is mine code.
    >
    >
    > #include<iostream>
    > #include<algorithm>
    >
    > int main()
    > {
    > int num[]={2,6,25,14,86,52,64,75,27,52,96,58,52.0};

    Notice that the last number is floating point.

    > const int start=1, end=2;

    This is not necessary.

    > int check;
    > std :: cout <<"please Enter Your Number :- ";
    > std :: cin >> check;
    >
    > if(std ::binary_search((num+start),(num+end),check))

    [snip]
    You need to have the compiler work more to earn its keep:
    int num[] = {2,6,25,14,86,52,64,75,27,52,96,58,52};
    const unsigned int QUANTITY = sizeof(num) / sizeof(num[0]);
    //...
    if (std::binary_search(num, num + QUANTITY, check))
    //...

    Stating the name of the array decomposes (sp?) to the location of the
    first element (per the language standard). The identifier QUANTITY
    is calculated by the compiler for you. This allows you to adjust
    the quantity in the array without having to retype the value for
    QUANTITY each time.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    http://www.sgi.com/tech/stl -- Standard Template Library
     
    Thomas Matthews, Sep 1, 2009
    #3
  4. arunix

    arunix Guest


    > Are you sure that you want to search only from element 1 to (but not including)
    > element 2?


    ya only one time
     
    arunix, Sep 1, 2009
    #4
  5. arunix wrote:
    > int num[]={2,6,25,14,86,52,64,75,27,52,96,58,52.0};


    Two responses, and neither one saw the most prominent flaw in this code.

    For std::binary_search() to work the array must be sorted. It won't
    work if the elements are in random order.

    Either put the elements in increasing order, or sort the array using
    std::sort().

    > const int start=1, end=2;
    > if(std ::binary_search((num+start),(num+end),check))


    You are here performing a binary search for the second element in the
    array only. "num+1" points to the second element in the array, and
    "num+2" points to the third element (which is not included in the search).

    What you want to do is std::binary_search(num, num+13, check).
     
    Juha Nieminen, Sep 1, 2009
    #5
  6. arunix

    Daniel Pitts Guest

    arunix wrote:
    > hello dear all its mine first post here i am newbie with "C++" i am
    > learning "C++ Algorithms" here is mine problem that i am ussing
    > "binary_search" into the code its given me "false value" 0 boz when i
    > gave the number which is in the list it gives me the False Part "Your
    > Number is not in the List" when condition is true. plz tell me whats
    > wrong with mine code
    >
    > here is mine code.
    >
    >
    > #include<iostream>
    > #include<algorithm>
    >
    > int main()
    > {
    > int num[]={2,6,25,14,86,52,64,75,27,52,96,58,52.0};


    binary_search only works if your collection is sorted!

    > const int start=1, end=2;

    why are start and end 1 and 2?
    maybe you should have start = 0, and end = sizeof(num)/sizeof(num[0])
    > int check;
    > std :: cout <<"please Enter Your Number :- ";
    > std :: cin >> check;
    >
    > if(std ::binary_search((num+start),(num+end),check))

    why not binary_search(num, num + end)
    > {
    > std :: cout<<" Your Value is in the List " << check <<std ::
    > endl;
    > }
    > else
    > {
    > std :: cout <<"Your Number is not in the List "
    > <<std :: endl;
    > }
    > return 0;
    > }
    >



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Sep 1, 2009
    #6
  7. arunix

    Jerry Coffin Guest

    In article <5a4aa309-c255-40b8-a2e7-
    >, says...
    >
    > hello dear all its mine first post here i am newbie with "C++" i am
    > learning "C++ Algorithms" here is mine problem that i am ussing
    > "binary_search" into the code its given me "false value" 0 boz when i
    > gave the number which is in the list it gives me the False Part "Your
    > Number is not in the List" when condition is true. plz tell me whats
    > wrong with mine code
    >
    > here is mine code.
    >
    >
    > #include<iostream>
    > #include<algorithm>
    >
    > int main()
    > {
    > int num[]={2,6,25,14,86,52,64,75,27,52,96,58,52.0};
    > const int start=1, end=2;
    > int check;
    > std :: cout <<"please Enter Your Number :- ";
    > std :: cin >> check;
    >
    > if(std ::binary_search((num+start),(num+end),check))


    The first iterator should point to the beginning of the range you
    want to search, and the second to one past the end of the range you
    want to search. Since you're giving num+1 and num+2, you're only ever
    searching the second element. In other words, as written, this is a
    very roundabout way of saying:
    if (check==6)

    --
    Later,
    Jerry.
     
    Jerry Coffin, Sep 1, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    2
    Views:
    1,257
    Maxim Yegorushkin
    Jan 7, 2009
  2. Angus
    Replies:
    10
    Views:
    954
    Jonathan Lee
    Mar 11, 2011
  3. Stefan Ram
    Replies:
    0
    Views:
    107
    Stefan Ram
    Dec 31, 2013
  4. Stefan Ram
    Replies:
    0
    Views:
    103
    Stefan Ram
    Dec 31, 2013
  5. Alf P. Steinbach
    Replies:
    5
    Views:
    145
    Jorgen Grahn
    Jan 1, 2014
Loading...

Share This Page