binary_search

A

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;
}
 
A

Alf P. Steinbach

* 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
 
T

Thomas Matthews

arunix said:
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
 
J

Juha Nieminen

arunix said:
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).
 
D

Daniel Pitts

arunix said:
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)
 
J

Jerry Coffin

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top