CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2003
    Posts
    893

    Finding numbers surrounding...

    I have an array of integer, when the user inputs a certain number called NUM and a range for me to search, I will have to output all the numbers within the range around NUM....
    Could you tell me your ideas about how to solve this kind of problem ?

    Any help/ideas are really appreciated...

    -hometown

  2. #2
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189

    Re: Finding numbers surrounding...

    Originally posted by hometown
    I have an array of integer, when the user inputs a certain number called NUM and a range for me to search, I will have to output all the numbers within the range around NUM....
    Could you tell me your ideas about how to solve this kind of problem ?

    Any help/ideas are really appreciated...

    -hometown
    Loop through the array one element at a time, check each element's value to see if it is in range, and output it if it is.

  3. #3
    Join Date
    Apr 2003
    Posts
    893

    Re: Re: Finding numbers surrounding...

    Originally posted by gjs368
    Loop through the array one element at a time, check each element's value to see if it is in range, and output it if it is.
    Thanks GJs386 very much for your idea...
    But do you know any other ways to resolve that problem ?
    Supposing I have an array like array[BIGGESTNUMBER] and the range to search is extremely LARGE and then my small computer's memory will not be able to afford that way to search the whole stuff, right ?

    Thanks again,

    Fiona

  4. #4
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189
    Supposing I have an array like array[BIGGESTNUMBER] and the range to search is extremely LARGE and then my small computer's memory will not be able to afford that way to search the whole stuff, right ?
    If it's already in an array, its already in memory, and you won't have to worry about not having enough. The only overhead to looping would be the integer you use to index the loop, and perhaps another integer or two to hold the comparison value, etc.

    If you need to read a huge number of values in from a file, you can do it a piece at a time, by reading a subset of the values, to stay within the constraints of available memory.

  5. #5
    Join Date
    Apr 2003
    Posts
    893
    Thanks GJs386 a lot...

    Fiona

  6. #6
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    its even easier if you use std::find; search the whole array in 1 line.

  7. #7
    Join Date
    Oct 2002
    Location
    Arkansas, USA
    Posts
    189
    Originally posted by hometown
    when the user inputs a certain number called NUM and a range for me to search, I will have to output all the numbers within the range around NUM
    Originally posted by mwilliamson
    its even easier if you use std::find; search the whole array in 1 line.
    Since Hometown is not looking for a single value, but a range of values, could you really accomplish the task using std::find in 1 line?

    As far as I know, std::find takes three arguments;
    1)where to start,
    2)where to end, and
    3)the exact value to search for,
    and returns a single value which identifies the first element of the value searched for.

    Is there an overloaded version that finds a range and returns an array of all values meeting the criterion? That would certainly be an elegant solution!!

  8. #8
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    2,236
    I took it mean he wanted to search for a single value in a range of the array. ie. find a 6 between elements 3 and 8. There is an overload that can do ranges; find_if. You would use it like this:

    Code:
    void in_range( int lower, int higher, int test )
    {
     return lower < test && higher > test;
    }
    
    int array&#091;BIGNUMBER&#093;;
    
    int* it = array - 1;
    while( it = std::find_if( it + 1, array + BIGNUMBER, std::bind1st( std::bind1st( in_range, lower ), higher ) )
     std::cout << *it;

  9. #9
    Join Date
    Apr 2003
    Posts
    893
    Thanks MWilliamson and GJs386 alot for your help...

    Fiona

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured