Pages: [1] 2 3 4   Go Down
Author Topic: Removing elements from array  (Read 2605 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone

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

I have this array:

Code:
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 smiley

Many thanks for your time! smiley-grin
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1268
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code because there must be something wrong somewhere.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4225
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post your code because there must be something wrong somewhere.

uhm..

What I have is:

Code:
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:
8
9
10
----------------------------


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

I will get the output

Code:
8
0
10
----------------------------
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 22, 2013, 07:32:05 am by AWOL » Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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 smiley
« Last Edit: April 22, 2013, 07:32:33 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 299
Posts: 26196
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 22, 2013, 08:58:14 am by AWOL » Logged

"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.

Pages: [1] 2 3 4   Go Up
Jump to: