A simple way is shown below - you get an updated running everage every sample.
int aveLen = 50;
float altitude,ave2,ave3;
ave2 += altitude;
ave2 *= (1-1/aveLen);
ave3 = ave2/(aveLen-1);
aveLen is the number of samples over which to average.
ave3 is the result.
As you may guess, this was for an altimeter (using a BME280)
Allan