1. ## Searching an array

I'm wanting to search an array. If there is only one value greater than zero then that position in the array is declared the winner. How can I do that?

2. Code:
```for (int i=0; i<=n; i++)
{
if (array[i] > 0) break;
}
// i is now the position where the element is greater than 0```
Assuming n is the number of elements in array, and that you are using numbers, not chars or another data structure.

If there are two places in the array greater than zero, it breaks on the first one, and you will need more to find other positions that qualify.

3. Loop through the elements if your array compare them to zero and if that element is than one is the winner.

4. Of course, if you want to look professional and make your code totally unreadable to the average mortal you could use the STL std::find_if.
Code:
```#include <iostream>
#include <algorithm>
#include <functional>

int main()
{
int test[] = { -1, -2, 0, 0, 2, -1 };

int* result = std::find_if(test, test + 6, std::bind2nd(std::greater<int>(), 0));

if (result == test + 6)
{
}
else
{
std::cout << "Found! Value is " << *result << std::endl;
std::cout << "Position is " << result - test << std::endl;
}

std::cin.get();
}```

5. Originally Posted by neandrake
Code:
```for (int i=0; i<=n; i++)
{
if (array[i] > 0) break;
}
// i is now the position where the element is greater than 0```
Assuming n is the number of elements in array, and that you are using numbers, not chars or another data structure.

If there are two places in the array greater than zero, it breaks on the first one, and you will need more to find other positions that qualify.
Not very useful considering on most compilers i will (and SHOULD)lose scope after you break the for loop.

Titleist, since you failed to tell us what happens when more than one variable is greater than zero or no variables are greater than zero, its kind of hard to help you with this problem.

6. Here is a 'while' loop version of what you might want to do:

Code:
```int Classyclass::winner(const int& array) const
{

bool flag = false;
int i = 0;

while  (i < sizeof(array) && flag==false)
{
if(array[i]!=1)

++i;

else

flag = true;
}

if(flag)

return i;    //Return position of winning element

else
{
cout << "No elements above zero!";
return -1;   //Return a nonsense negative sentinal value
}

}```

7. Originally Posted by titleist_03
I'm wanting to search an array. If there is only one value greater than zero then that position in the array is declared the winner. How can I do that?
You all seem to have missed one little word...

ONLY one. Only the first reply seems to have noticed that. But then again, we're just here to get you started. Not do everything for you. Here's an idea:
Code:
```int positiveindex = -1;

loop through each element
if this element greater than zero
if positiveindex less than zero
return no winner
else
positiveindex equals this spot
if positiveindex less than zero
return no winner
return positiveindex```
Yeah, that pretty much gives it to you. But I'll leave the actual code to you.

Quzah.

8. You all seem to have missed one little word...

ONLY one. Only the first reply seems to have noticed that.You all seem to have missed one little word...
OK, take two with the STL using count_if.
Code:
```#include <iostream>
#include <algorithm>
#include <functional>

int main()
{
int test[] = { -1, -2, 0, 0, 27, -1 };

if (std::count_if(test, test + 6, std::bind2nd(std::greater<int>(), 0)) == 1)
{
int* winner = std::find_if(test, test + 6, std::bind2nd(std::greater<int>(), 0));

std::cout << "We have a winner!"             << std::endl;
std::cout << "Value is "    << *winner       << std::endl;
std::cout << "Position is " << winner - test << std::endl;
}
else
{
std::cout << "No winner. Please play again." << std::endl;
}

std::cin.get();
}```
I'm starting to like the STL, even if it does take a friggin genius to figure out how it works.

9. pseudo-error:
Code:
```int positiveindex = -1;

loop through each element
if this element greater than zero
if positiveindex greater than or equal to zero
return no winner
else
positiveindex equals this spot
if positiveindex less than zero
return no winner
return positiveindex```

10. Hehe, just to weird people out, here's the fully Boosted version
Code:
```#include <iostream>
#include <boost/lambda/lambda.hpp>
#include <boost/range/end.hpp>
using namespace boost::lambda;

int main()
{
int test[] = { -1, -2, 0, 0, 27, -1 };

if (std::count_if(test, boost::end(test), _1 > 0) == 1)
{
int* winner = std::find_if(test, boost::end(test), _1 > 0);

std::cout << "We have a winner!"             << std::endl;
std::cout << "Value is "    << *winner       << std::endl;
std::cout << "Position is " << winner - test << std::endl;
}
else
{
std::cout << "No winner. Please play again." << std::endl;
}
}```
boost::end doesn't work in all situations.

11. I was just reading up on Lambda last night - it's pretty damn cool!

http://www.boost.org/doc/html/lambda.html

gg

12. Originally Posted by jlou
pseudo-error:
Code:
```int positiveindex = -1;

loop through each element
if this element greater than zero
if positiveindex greater than or equal to zero
return no winner
else
positiveindex equals this spot
if positiveindex less than zero
return no winner
return positiveindex```
Yeah. Actually the line following my if and else should be swapped:
Code:
```    if this element greater than zero
if positiveindex less than zero
positiveindex equals this spot
else
return no winner
if positiveindex less than zero
return no winner
return positiveindex```
Quzah.

13. Originally Posted by The Brain
Code:
`int Classyclass::winner(const int& array) const`
Wasn't this suposed to be
Code:
`int Classyclass::winner(const int* array) const`
?

14. oh yeah