Removing elements from array

Hello everyone

I have a problem with removing an element from an array.

I have this array:

int array[100] = {8, 9, 10};

and I want to remove 9 so that array is equal to {8, 10}

I have been looking on google for an answer, but I can't find anything :confused:

So I come to you in the hope of some one here have a answer for me :slight_smile:

Many thanks for your time! :smiley:

I have this array...and I want to remove 9 so that array is equal to {8, 10}

That assumes that array starts equal to {8, 9, 10}. It does not. It starts equal to {8, 9, 10, 0, ... 0} (where I left out 95 more 0s).

Changing {8, 9, 10, 0, ... 0} to {8, 10, 0, 0, ... 0} is simple, isn't it? Create a for loop that iterates from the position to replace to the end of the array (minus 1), and copy the element in [n+1] to [n]. Assign 0 to [99]. You're done.

PaulS:

I have this array...and I want to remove 9 so that array is equal to {8, 10}

That assumes that array starts equal to {8, 9, 10}. It does not. It starts equal to {8, 9, 10, 0, ... 0} (where I left out 95 more 0s).

Changing {8, 9, 10, 0, ... 0} to {8, 10, 0, 0, ... 0} is simple, isn't it? Create a for loop that iterates from the position to replace to the end of the array (minus 1), and copy the element in [n+1] to [n]. Assign 0 to [99]. You're done.

Yea, but when I write out all the elements in my array I only get 8, 9, 10 and not all the 0s you are talking about, but if I change 9 to 0 it will write out 9, 0, 10, how can that be? :S

Post your code because there must be something wrong somewhere.

if I change 9 to 0 it will write out 9, 0, 10, how can that be?

You don't change 9 to 0 you change 9 to the next value (which is 10), then 10 to the next value (which is 0) and so on.

Do you need the array to have 100 elements as it is currently defined ?

Yea, but when I write out all the elements in my array I only get 8, 9, 10 and not all the 0s you are talking about

Whether you write them out or not, the zeroes are still there.

Imagine array elements as pigeon holes; if you take a letter out of the second pigeon hole, the other pigeon holes don't automagically move along to fill the gap.

marco_c:
Post your code because there must be something wrong somewhere.

uhm..

What I have is:

int array[100] = {8, 9, 10};
int count = 0;

void setup(){
Serial.begin;
while(array[count] != '\0'){
count++;
}
}

void loop(){
for(int x = 0; x < count; x++){
Serial.print(array[x]);
Serial.print("\n");
}
Serial.print("----------------------------\n")
}

this will give me:

8
9
10
----------------------------

but If I change the 9 (array[1]) to 0 in the loop with array[1] = 0;

I will get the output

8
0
10
----------------------------

You counted the non-zero elements in "setup".
Actually, you didn't - you stopped counting elements when you found the first zero element, which isn't the same thing.

Writing zero to an array element is just the same as writing any other value to an array element - it doesn't (CANNOT) affect the number of elements in the array.

'\0' applies really only to "char" datatypes.

AWOL:
You counted the non-zero elements in "setup".
Actually, you didn't - you stopped counting elements when you found the first zero element, which isn't the same thing.

Writing zero to an array element is just the same as writing any other value to an array element - it doesn't (CANNOT) affect the number of elements in the array.

'\0' applies really only to "char" datatypes.

Okay, so what you say is that I only counts the elements that are not zero?

UKHeliBob:

if I change 9 to 0 it will write out 9, 0, 10, how can that be?

You don't change 9 to 0 you change 9 to the next value (which is 10), then 10 to the next value (which is 0) and so on.

Do you need the array to have 100 elements as it is currently defined ?

No I don't need the 100, I would have used "array[]" instead of "array[100]" but if I use "array[]" the whole sketch would loop..
So I have it at "array[100]" to be sure I have enought elements for my inputs :slight_smile:

Tough I could make it a smaller number, if I can't use some function to remove an element and have to move all the elements everytime :slight_smile:

Okay, so what you say is that I only counts the elements that are not zero?

No, I said you stopped counting when you found the first zero element.
That's not the same thing.

If you consider a zero elements as a "removed" element, then simply don't print it - all it takes is a simple "if".

Tough I could make it a smaller number, if I can't use some function to remove an element and have to move all the elements everytime

You can, as soon as you write it. It's pretty simple, really. You've already been given all the information you need.

Jumping up a level for a moment: shuffling data within an array is a well-known anti-pattern, which is to say it should be avoided if possible in favor of other, better approaches.

What is it that you are trying to do that apparently requires you to shuffle data within an array? Perhaps there is a different approach...

-br

billroy:
Jumping up a level for a moment: shuffling data within an array is a well-known anti-pattern, which is to say it should be avoided if possible in favor of other, better approaches.

What is it that you are trying to do that apparently requires you to shuffle data within an array? Perhaps there is a different approach...

-br

I'm trying to make an elevator, so the array contains the different floors that the elevator are told to go to, so when it have visited a floor I want it to remove the floor (element) from the array, and be able to add it (as the last element) in the array when the button are pressed again.

I hope you understand what I mean :slight_smile:

AWOL:

Okay, so what you say is that I only counts the elements that are not zero?

No, I said you stopped counting when you found the first zero element.
That's not the same thing.

If you consider a zero elements as a "removed" element, then simply don't print it - all it takes is a simple "if".

I don't quite get what you mean, I don't consider a zero element as a "removed" element, I was told that to remove an element from an array I should set the value of it to 0 or '\0', but that didn't work for me :slight_smile:

I don't quite get what you mean, I don't consider a zero element as a "removed" element, I was told that to remove an element from an array I should set the value of it to 0 or '\0', but that didn't work for me

But if this is for an elevator, and you don't want to stop at the third floor, you don't rearrange your building to remove the third floor completely (see also my pigeon hole metaphor earlier).

If you don't want to print elements that contain a zero, then don't print them.
It really is that simple.

AWOL:

I don't quite get what you mean, I don't consider a zero element as a "removed" element, I was told that to remove an element from an array I should set the value of it to 0 or '\0', but that didn't work for me

But if this is for an elevator, and you don't want to stop at the third floor, you don't rearrange your building to remove the third floor completely.

If you don't want to print elements that contain a zero, then don't print them.
It really is that simple.

No not like that..

The array is storing the floors where a person have clicked the button to call the elevator or a person in the elevator have clicked the button to that floor. Then when the elevator comes to the floor it will remove it from the array and continue to the next floor :slight_smile:

cookied:
No not like that..

The array is storing the floors where a person have clicked the button to call the elevator or a person in the elevator have clicked the button to that floor. Then when the elevator comes to the floor it will remove it from the array and continue to the next floor :slight_smile:

The problem you're having it that you're assuming assigning a value of '\0' or 0 to an element in the array "removes it" from the array. That's not what the array thinks. The array thinks you're just updating its value. If a value of 0 in an array slot means that the slot is "empty", then you need to have code that accounts for that, and not rely on the array to assume that's what you mean.

Arrch:

cookied:
No not like that..

The array is storing the floors where a person have clicked the button to call the elevator or a person in the elevator have clicked the button to that floor. Then when the elevator comes to the floor it will remove it from the array and continue to the next floor :slight_smile:

The problem you're having it that you're assuming assigning a value of '\0' or 0 to an element in the array "removes it" from the array. That's not what the array thinks. The array thinks you're just updating its value. If a value of 0 in an array slot means that the slot is "empty", then you need to have code that accounts for that, and not rely on the array to assume that's what you mean.

I know that now, that's why I'm asking if there is a way to remove the element :slight_smile:
Like other languages have something like delete or remove functions, I just wanna know if there is something like that for Arduino :slight_smile:

cookied:
I know that now, that's why I'm asking if there is a way to remove the element :slight_smile:
Like other languages have something like delete or remove functions, I just wanna know if there is something like that for Arduino :slight_smile:

No, there is no way to remove an element from a statically sized array. You can only set the value to something you have defined as empty and account for that when you are looking in the array.

Uncompiled, untested.

const int floors [100] = {12, 3, 0, 4, 5, 0, 7, 8, 9, 0, 10, 1, 2, 0};
void setup ()
{ 
  Serial.begin (9600);
  for (int i = 0; i < sizeof (floors) / sizeof (floors [0]); i++) {
    if (floors [i]) {
      Serial.println (floors [i]);
    }
  }
}
void loop () {}