I am making a method that finds the variance of three numbers. The variance of a list of numbers is the average of the squares of the differences of the numbers from the mean. When I debug, the program crashes and has an error after the first loop. Why?
Code:
static double variance (double [] a){
int index = 0; //the # we are processing
double total = 0; //running total
double mean;
double difference;
double square;
double sum = 0;
double variance;
// loop to find sum of values
while (index <= a[index]) {
total = total + a[index];
index = index + 1; //advance to next
}
//calculates mean
mean = total / a.length;
//loop that calculates difference from mean, square of difference, sums of squares
while (index <= a[index]) {
difference = mean - a[index];
square = difference * difference;
sum = sum + square;
}
//calculates and returns variance
variance = sum / a.length;
return variance;
}