I'm trying to delete all elements of an array that match a particular case. for example..
if(ar[i]==0)
delete all elements which are 0 in the array
print out the number of elements of the remaining array after deletion
what i tried:
if (ar[i]==0)
{ x++; } b=N-x; cout<<b<<endl;
this works only if i want to delete a single element every time and i can't figure out how to delete in my required case. Im assuming that i need to traverse the array and select All instances of the element found and delete All instances of occurrences. Instead of incrementing the 'x' variable only once for one occurence, is it possible to increment it a certain number of times for a certain number of occurrences?
edit(someone requested that i paste all of my code):
int N; cin>>N; int ar[N]; int i=0; while (i<N) { cin>>ar[i]; i++; }//array was created and we looped through the array, inputting each element. int a=0; int b=N; cout<<b; //this is for the first case (no element is deleted) int x=0; i=0; //now we need to subtract every other element from the array from this selected element. while (i<N) { if (a>ar[i]) { //we selected the smallest element. a=ar[i]; } i=0; while (i<N) { ar[i]=ar[i]-a; i++; //this is applied to every single element. } if (ar[i]==0) //in this particular case, we need to delete the ith element. fix this step. { x++; } b=N-x; cout<<b<<endl; i++; } return 0; }
the entire question is found here: Cut-the-sticks
std::remove_if().