Question dealing with utrasonic range sensors

A friend and I are working on something and we’ve got the code working properly to show the distance with 2 ultrasonic range sensors with 4 decimal places using float. Is it possible to only run it x number of times and then get the average of all the distances? We’ve tried dowhile loops in multiple places and it never works. We’re using two $30 ultrasonic range sensors from Radioshack

int UltraSoundSignalPins[] = {7,8}; //Left,Right US Signal Pins//
char *pingString[] = {"Left", "Right"};

void setup() {
  Serial.begin(9600);
}
float ping(int index){
  float echo;
  pinMode(UltraSoundSignalPins[index],OUTPUT); 
  digitalWrite(UltraSoundSignalPins[index], LOW);
  delayMicroseconds(2); 
  digitalWrite(UltraSoundSignalPins[index], HIGH); 
  delayMicroseconds(5); 
  digitalWrite(UltraSoundSignalPins[index], LOW); 
  pinMode(UltraSoundSignalPins[index], INPUT); 
  digitalWrite(UltraSoundSignalPins[index], HIGH); 
  echo = pulseIn(UltraSoundSignalPins[index], HIGH); 
  return (echo / 58.138) * .39;   
}

void loop() {
  float ultrasoundValue;
  for(int i=0; i < 2; i++){
    ultrasoundValue = ping(i); 
    Serial.print(pingString[i]);
    Serial.print(ultrasoundValue);
    Serial.print("in, ");    
    delay(50);
  }
  Serial.println();
  delay(50);
}

I appreciate any help and guidance. Thanks!

add a parameter to the ping-call

float ping(int index, int nbr) //nbr=number of times
{
  float echo = 0;
  pinMode(UltraSoundSignalPins[index], OUTPUT);
  for int(i = 0; i < nbr); i++)
  {
    digitalWrite(UltraSoundSignalPins[index], LOW);
    delayMicroseconds(2);
    digitalWrite(UltraSoundSignalPins[index], HIGH);
    delayMicroseconds(5);
    digitalWrite(UltraSoundSignalPins[index], LOW);
    pinMode(UltraSoundSignalPins[index], INPUT);
    digitalWrite(UltraSoundSignalPins[index], HIGH);
    echo += pulseIn(UltraSoundSignalPins[index], HIGH);
  }
  echo/=nbr;
  return (echo / 58.138) * .39;
}