Inserting and Removing Array Elements

I would like to create an array with a yet to be determined set of elements that will need to have elements added and removed dynamically based on events but I don't know how to extend an array or shrink it.

For example, say I want to keep a running total of rainfall in the last 24 hour (86,400,000 millisecond) by keeping track of bucket tips. Since the "last 24 hours" is a sliding period of time, tip events may be added to the end and expired tip events need to be removed from the beginning. (FIFO) Of course there may be no events whatsoever.

My question is since arrays on arduinos aren't objects with insert and delete functions, how can I add elements to the end an array of unknown length and remove elements from the beginning of that array?

https://www.google.com/search?q=ring+buffer

...aka...

https://www.google.com/search?q=circular+buffer

powderburned:
My question is since arrays on arduinos aren't objects with insert and delete functions, how can I add elements to the end an array of unknown length and remove elements from the beginning of that array?

You probably are not going to store an indefinite array of tips, I'm guessing.

In C++ you will have to define your array at compile time with a fixed size...

So you could create an array like this:

size_t rainBucket [120] ; /* 120 hours = 5 days of data */

and add to the first element each time you tip...

if (tipped)
{
  rainBucket[0]++;
  tipped = false;
}

then cascade the elements each hour:

if (millis() - lastMillis >= ONE_HOUR)
{
  for (size_t i = sizeof(rainBucket)/sizeof(rainbucket[0]) - 1; i >= 0; i--)//cascade an hour of values
  {
    rainBucket [i + 1] = rainBucket [i];
  }
  ...

Then getting your values for the past 5 days is somewhat trivial....

BulldogLowell:
You probably are not going to store an indefinite array of tips, I'm guessing.

In C++ you will have to define your array at compile time with a fixed size...

I was afraid of that. I was hoping it could be more dynamic for simplicity and readability.

Thank you all!

I was afraid of that. I was hoping it could be more dynamic for simplicity and readability.

If it were dynamic it would almost certainly be less simple and readable.

Why are you logging tip events? Can't you just count the number of tips in a time interval?

aarg:
Why are you logging tip events? Can't you just count the number of tips in a time interval?

Good question.

Since the "24 hour period" is a rolling period of time (ie, 24 hour from 2 minutes ago may be a different sum than 24 hours from now), I need to be able to take off counts from the last time I checked. If I'm just summing up as I go, I don't know how much to take off to keep that rolling count accurate. If I maintain this in an array of timestamps, I can eliminate the timestamps older than 24 hours ago.

As pointed out in Reply #2, a circular buffer is the way to go.

gfvalvo:
As pointed out in Reply #2, a circular buffer is the way to go.

Agreed.