Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > binary_search

Reply
Thread Tools

binary_search

 
 
arunix
Guest
Posts: n/a
 
      09-01-2009
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;
}

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-01-2009
* 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
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      09-01-2009
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
 
Reply With Quote
 
arunix
Guest
Posts: n/a
 
      09-01-2009

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


ya only one time
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-01-2009
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).
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-01-2009
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/>
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-01-2009
In article <5a4aa309-c255-40b8-a2e7-
http://www.velocityreviews.com/forums/(E-Mail Removed)>, (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find position of element using binary_search Angus C++ 10 03-11-2011 12:38 AM
std::binary_search(), iterator to the found element and timecomplexity Alex Vinokur C++ 2 01-07-2009 10:44 AM



Advertisments