Printing an array while saving the previous data

So I've been working on this for quite some time and just never really got to figure out how to "save" the data that I am getting for example a Accelerometer(MMA845X). Because I want to make an array where I put all of the data that I am getting from the accelerometer (so which X,Y and Z values it is on) and serialprint them out 5 seconds later, but it needs to keep the other values that it got so he can print it out later, so something like this:

Time(s) input(Accelerometer) output(Serial Monitor)
0 measurement 1 0
1 measurement 2 0
2 measurement 3 0
3 measurement 4 0
4 measurement 5 0
5 measurement 6 measurement 1
6 measurement 7 measurement 2
7 measurement 8 measurement 3
8 measurement 9 measurement 4
9 measurement 10 measurement 5
10 measurement 11 measurement 6

I used the Adafruit example code which I edited a little bit, but I don't know how to go further now.
Libraries:

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();

void setup(void) {
  Serial.begin(9600);
  
  Serial.println("Adafruit MMA8451 test!");
  

  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");
  
  mma.setRange(MMA8451_RANGE_2_G);
  
  Serial.print("Range = "); Serial.print(2 << mma.getRange());  
  Serial.println("G");
  
}

void loop() {

  int MX = mma.x;
  int MY = mma.y;
  int MZ = mma.z;
  int newArray[3]{MX, MY, MZ};
  
  // Read the 'raw' data in 14-bit counts
  mma.read();
  Serial.print("X:\t"); Serial.print(newArray[0]); 
  Serial.print("\tY:\t"); Serial.print(newArray[1]); 
  Serial.print("\tZ:\t"); Serial.print(newArray[2]); 
  Serial.println();

  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: \t"); Serial.print(event.acceleration.x); Serial.print("\t");
  Serial.print("Y: \t"); Serial.print(event.acceleration.y); Serial.print("\t");
  Serial.print("Z: \t"); Serial.print(event.acceleration.z); Serial.print("\t");
  Serial.println("m/s^2 ");
  
  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();
 
  
  Serial.println();
  delay(5000);

  
  
}

You may or may not be looking for global or static variables as well. There is a similar question about global and static variables.

I looked into the global and static variables but it looks like they just save it until you do something with it, in which case I want it to be an continious loop of saving the measurements until after 5 seconds where they will post the first measurement and then the second one, etc. In the post they just save the variables for a while until they make it the same variable again. (correct me if I am wrong though, since I am still very new to this).

First off, welcome to the forums and THANK YOU for reading the how to post information. Code inside code tags, links to libraries used!

If you are measuring ever second, but want to delay the output by 5 seconds, you will need to create an array big enough to hold all those measurement. This array will also have to be global, not declared inside loop() since you want to keep those values around. You will also need a global variable to keep track of where to place the new reading and a global variable to keep track of what to print.

#include <Wire.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();

const int arraySize = 5;
float values[arraySize][3];
int currentIndex = 0;  // where to store the current measurement
int printIndex = arraySize-1;  // index of values to print

void setup(void) {
  Serial.begin(9600);
  
  Serial.println("Adafruit MMA8451 test!");
  

  if (! mma.begin()) {
    Serial.println("Couldnt start");
    while (1);
  }
  Serial.println("MMA8451 found!");
  
  mma.setRange(MMA8451_RANGE_2_G);
  
  Serial.print("Range = "); Serial.print(2 << mma.getRange());  
  Serial.println("G");

  for( int i=0; i<arraySize; i++ ) {
    values[i][0] = values[i][1] = value[i][2] = 0.0;
  }
}

void loop() {

  /* Get a new sensor event */ 
  sensors_event_t event; 
  mma.getEvent(&event);

  values[currentIndex][0] = event.acceleration.x;
  values[currentIndex][1] = event.acceleration.y;
  values[currentIndex][2] = event.acceleration.z;
  currentIndex++;
  if ( currentIndex == arraySize ) currentIndex = 0;  // rollover

  Serial.print( millis() / 1000 );
  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("\tX: \t"); Serial.print(values[printIndex][X_AXIS], 2);
  Serial.print("\tY: \t"); Serial.print(values[printIndex][Y_AXIS], 2);
  Serial.print("\tZ: \t"); Serial.print(values[printIndex][Z_AXIS], 2);
  Serial.println("m/s^2");
  printIndex++;
  if ( printIndex == arraySize ) printIndex = 0;
  
  delay(1000);
}

It sounds like you're looking for a 'circular buffer'. The input action happens every x unit of time. When the input pointer reaches the end of the array it resets and starts over at the beginning, ad infinitum. The print action performs similarly five, or whatever, units behind.