how to find a set of numbers in an array?

let say i have 20 numbers in an array, i need to find 8 numbers that is smaller than the first element. how do i find those 8 numbers in the array?

Thanks
One way is this:

1
2
3
4
5
6
7
8
9
// pseudo-code
void findElements( int array[], size_t numElems ) {
    for( elements 1 through numElems - 1 )
          if( array[ element ] < array[ 0 ] )
               copy array[ element ] to a new array

    // Now the new array will have only the elements smaller
    // than array[ 0 ].
}


Another way to do it (in one line of code):
1
2
3
4
5
6
  #include <algorithm>

  int array[ ARRAY_SIZE ];   // Assume this is the array of elements

  // This will make elements [1] - [9] the smallest values in the array.
  std::partial_sort( &array[ 1 ], &array[ 9], &array[ ARRAY_SIZE ] );


See http://www.sgi.com/tech/stl/partial_sort.html for details on
partial_sort.
well, depends on what you want. do you want the 8 SMALLEST numbers that is smaller than the first element?

for example, if you array is {10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ....} do you want the smallest 8 number smaller than 10, meaning (0, 1, 2, 3, 4, 5, 6, 7) or is something like ( 0, 1, 2, 3, 4, 5, 8, 9) also ok?
lets say the first element is 15, i need to find 8 numbers that is smaller than 15 in the array.

To jsmith:
Thanks for your help, but i am new in C++, so i dont quiet get the codes u posted above ;), but thanks anyway =D
Topic archived. No new replies allowed.