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 ?
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.
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 ?
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.
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!!
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[BIGNUMBER];
int* it = array - 1;
while( it = std::find_if( it + 1, array + BIGNUMBER, std::bind1st( std::bind1st( in_range, lower ), higher ) )
std::cout << *it;
Bookmarks