Q:

Calculate standard deviation of an array of integers in one for cycle without knowing average in advance

I have an array of numbers that I need to calculate standard deviation of. I want to do it in a single for loop cycle and I don't know the average of the array beforehand:
int[] numbers = new int[] {1, 2, 3, 5};
int sum = 0;
double sumOfSquares = 0;
for (int number: numbers)  {
  sum += number;
  sumOfSquares += number * number; //don't know average yet
}
int average = sum / numbers.length;
double standardDeviation = //?

I know standard deviation can be calculated by the following formula:
Formula1: sqrt((each element - average)^2)

But it requires me to already know the value of average as I loop through the elements in the array.
Is there another way to do it? I heard there's another formula that involves first finding the sum of squares of each element and then once the loop is over applying the average, but I can't find it.

A:

According to Wikipedia you can use a formula that requires only one loop if the number of values are finite and has equal probability which seems to be the case here.
int[] numbers = new int[] {1, 2, 3, 5};
double squareSum = 0;
double sum = 0;

for (int value : numbers) {
    squareSum += value * value;
    sum += value;
}

double average = sum / numbers.length;
double standDev = Math.sqrt(squareSum / numbers.length - average * average);

