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?
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.
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
Loop through the elements if your array compare them to zero and if that element is than one is the winner.
Woop?
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.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 } }
Last edited by The Brain; 12-08-2004 at 08:23 AM.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
You all seem to have missed one little word...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.
Hope is the first step on the road to disappointment.
OK, take two with the STL using count_if.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; } }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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: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
Hope is the first step on the road to disappointment.
Wasn't this suposed to beOriginally Posted by The Brain?Code:int Classyclass::winner(const int* array) const
oh yeah
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel