Remove all elements from array greater than n

7.8k Views Asked by At

I'm beginner in programming. Something is giving me trouble to code. Suppose, I've an array.

int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};

I want to remove all elements which are greater than 9. How can I do this?

3

There are 3 best solutions below

3
On BEST ANSWER

You can do this if you use vector. First initialize vector with your array. Then use remove_if() function. Hope this will help.

#include <algorithm>
#include <vector>

int main()
{
int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};

vector<int> V(Array, Array+20);
vector<int> :: iterator it;

it = remove_if(V.begin(), V.end(), bind2nd(greater<int>(), 9));


V.erase (it, V.end());  // This is your required vector if you wish to use vector

}
2
On

You cannot remove items from an array, since they are fixed in size.

If you used std::vector, then the solution would look like this:

  #include <vector>
  #include <algorithm>
  #include <iostream>
  #include <iterator>

  using namespace std;

  int main()
  {
     std::vector<int> Array = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};

     Array.erase(remove_if(Array.begin(), Array.end(), [](int n) { return n > 9; }),
                 Array.end());
     copy(Array.begin(), Array.end(), ostream_iterator<int>(cout, " "));

  }

Live example: http://ideone.com/UjdJ5h

If you want to stick with your array, but mark the items that are greater than 10, you can use the same algorithm std::remove_if.

  #include <algorithm>
  #include <iostream>
  #include <iterator>

  using namespace std;

  int main()
  {
     int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
     int *overwrite_start = remove_if(std::begin(Array), std::end(Array), [](int n){ return n>9; });
     fill(overwrite_start, std::end(Array), -1);
     copy(std::begin(Array), std::end(Array), ostream_iterator<int>(cout, " "));
  }

The above will move the "erased" items to the end of the array, and mark them with -1.

Live example: http://ideone.com/7rwaXy

Note the usage in both examples of the STL algorithm functions. The second example with the array uses the same remove_if algorithm function. The remove_if returns the start of the "erased" data, as remove_if doesn't actually remove, but moves the data to the end of the sequence.

0
On

i am try swap concept without using vector

int Array[] = {3,6,9,5,10,21,3,25,14,12,32,41,3,24,15,26,7,8,11,4};
int n;
int arr_len = sizeof(Array)/sizeof(int);
void print_array_value() {
int i;
cout << "\n";
for (i = 0; i < arr_len; i++) {
    cout << Array[i] << ", ";
}
cout << " : " << arr_len << "\n";
}
void swap_array_value(int start) {
int i;
for ( ; (start+1) < arr_len; start++) {
    Array[start] = Array[start+1];
}
}
void remove_array_value() {
int i;
for (i = 0; i < arr_len; i++) {
    if (Array[i] > n) {
        swap_array_value(i);
        arr_len--;
        i--;
    }
}
}
void main () {
clrscr();
cout << "Enter the N value : ";
cin >> n;
print_array_value();
remove_array_value();
print_array_value();
cout << "Array Length : " << arr_len;
getch();
}