Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

binary_search

0 views
Skip to first unread message

arunix

unread,
Sep 1, 2009, 1:52:11 AM9/1/09
to
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;
}

Alf P. Steinbach

unread,
Sep 1, 2009, 2:12:32 AM9/1/09
to
* arunix:

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


Cheers & hth.,

- Alf

Thomas Matthews

unread,
Sep 1, 2009, 2:30:11 AM9/1/09
to
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

arunix

unread,
Sep 1, 2009, 2:31:18 AM9/1/09
to

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

ya only one time

Juha Nieminen

unread,
Sep 1, 2009, 10:59:46 AM9/1/09
to
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).

Daniel Pitts

unread,
Sep 1, 2009, 3:57:31 PM9/1/09
to
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/>

Jerry Coffin

unread,
Sep 1, 2009, 4:19:04 PM9/1/09
to
In article <5a4aa309-c255-40b8-a2e7-
e70402...@u38g2000pro.googlegroups.com>, arr...@gmail.com 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.

0 new messages