Hi all, is there a way to shift an array the way the first new value i push on the top pops the last value out?
Store the element at index 0
Move all elements down the array
for (i=0; i<length-1; i++)
array = array[i+1];[/li]
- Store the new element at array[length-1]*
You can also employ a rolling pointer system, where you point to the first element (oldest) you added so when you add a new value, it replaces the old and increment the pointer. Then do pointer=(++pointer)%total_element to make sure the pointer doesn't run away.
You might also be able to avoid shifting it and treat it as a ring buffer with a head and tail.
It always depends on your exact needs.
This is a library that implements what the above replies describe :)
thank you all guys for your advices, i'll check what's the best solution for me :) the final goal is to achieve a running window average.
the final goal is to achieve a running window average.
Check my playground article for a running average - http://arduino.cc/playground/Main/RunningAverage
really thanks! just what i was looking for! (i only have to add the window :) )