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
> 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
ya only one time
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).
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/>
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.