Searching an array

This is a discussion on Searching an array within the C++ Programming forums, part of the General Programming Boards category; I'm wanting to search an array. If there is only one value greater than zero then that position in the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    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. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Loop through the elements if your array compare them to zero and if that element is than one is the winner.
    Woop?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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();
    }

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Quote 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. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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 07: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

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    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

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,674
    I was just reading up on Lambda last night - it's pretty damn cool!

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

    gg

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  13. #13
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Quote 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. #14
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. searching thru a c++ class array
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 05-29-2002, 09:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21