Go Down

### Topic: Removing elements from array (Read 8733 times)previous topic - next topic

##### Apr 22, 2013, 01:10 pm
Hello everyone

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

I have this array:

Code: [Select]
`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 :/

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

Many thanks for your time!

#### PaulS

#1
##### Apr 22, 2013, 01:35 pm
Quote
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.
The art of getting good answers lies in asking good questions.

#2
##### Apr 22, 2013, 01:39 pm

Quote
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

#### marco_c

#3
##### Apr 22, 2013, 01:48 pm
Post your code because there must be something wrong somewhere.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

#### UKHeliBob

#4
##### Apr 22, 2013, 01:58 pm
Quote
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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### AWOL

#5
##### Apr 22, 2013, 02:08 pm
Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#6
##### Apr 22, 2013, 02:09 pm

Post your code because there must be something wrong somewhere.

uhm..

What I have is:

Code: [Select]
`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:

Code: [Select]
`8910----------------------------`

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

I will get the output

Code: [Select]
`8010----------------------------`

#### AWOL

#7
##### Apr 22, 2013, 02:14 pmLast Edit: Apr 22, 2013, 02:32 pm by AWOL Reason: 1
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#8
##### Apr 22, 2013, 02:26 pmLast Edit: Apr 22, 2013, 02:32 pm by AWOL Reason: 1

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?

Quote
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

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

#### AWOL

#9
##### Apr 22, 2013, 02:31 pm
Quote
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".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### PaulS

#10
##### Apr 22, 2013, 02:36 pm
Quote
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.
The art of getting good answers lies in asking good questions.

#### billroy

#11
##### Apr 22, 2013, 02:41 pm
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

#12
##### Apr 22, 2013, 03:39 pm

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

#13
##### Apr 22, 2013, 03:41 pm

Quote
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

#### AWOL

#14
##### Apr 22, 2013, 03:54 pmLast Edit: Apr 22, 2013, 03:58 pm by AWOL Reason: 1
Quote
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up