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?
Printable View
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?
Assuming n is the number of elements in array, and that you are using numbers, not chars or another data structure.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
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.
Loop through the elements if your array compare them to zero and if that element is than one is the winner.
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)
{
std::cout << "Not Found!" << std::endl;
}
else
{
std::cout << "Found! Value is " << *result << std::endl;
std::cout << "Position is " << result - test << std::endl;
}
std::cin.get();
}
Not very useful considering on most compilers i will (and SHOULD)lose scope after you break the for loop.Quote:
Originally Posted by neandrake
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.
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
}
}
You all seem to have missed one little word...Quote:
Originally Posted by titleist_03
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:
Yeah, that pretty much gives it to you. But I'll leave the actual code to you.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
Quzah.
OK, take two with the STL using count_if.Quote:
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...
I'm starting to like the STL, even if it does take a friggin genius to figure out how it works.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();
}
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
Hehe, just to weird people out, here's the fully Boosted version ;)
boost::end doesn't work in all situations.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;
}
}
I was just reading up on Lambda last night - it's pretty damn cool!
http://www.boost.org/doc/html/lambda.html
gg
Yeah. Actually the line following my if and else should be swapped:Quote:
Originally Posted by jlou
Quzah.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
Wasn't this suposed to beQuote:
Originally Posted by The Brain
?Code:int Classyclass::winner(const int* array) const
oh yeah :rolleyes: