public class Prices {
public static void main(String[] args) {
int i = 100;
int z = 0;
int x = 0;
int a = 0;
int v = 0;
double sum = 0;
double[][] MovingAvgArray = new double[i][i];
double[] List = {20.19, 21.67, 22.49, 22.26,23.99, 23.75, 23.34, 23.54, 23.25, 23.9, 23.84, 23.19, 22.03, 22.7, 21.14, 22.54, 24.36, 24.85, 23.96, 25.1, 20.79, 19.96, 19.98, 19.54, 19.09, 18.47, 18.59, 18.96, 18.93, 18.79, 19.06, 19.82, 19.5, 19.13, 19.25, 18.9, 19.05, 19.02, 19.09, 18.73, 18.44, 18.65, 18.07, 18.12, 17.92, 18.21, 17.81, 18.57, 18.92, 18.4, 17.83, 17.83, 18.55, 18.25, 18.55, 17.72, 17.28, 17.37, 16.99, 16.28, 17.02, 15.68, 15.73, 16.45, 15.54, 15.15, 15.51, 15.51, 15.54, 15.05, 14.98, 15.88, 16.14, 16.2, 16, 16.82, 17.74, 18.01, 17.99, 18.01, 16.99, 18, 18.3, 19.02, 18.14, 18.95, 19.19, 18.67, 17.63, 17.6, 17.56, 18.59, 18.57, 19.07, 19.57, 19.59, 18.71, 18.71, 18.75, 19.37};
do{
do{
for(;v < a + x +2;)
{
sum = sum + List[v];
v++;
}
MovingAvgArray[x][z] = sum;
MovingAvgArray[x][z] = (MovingAvgArray[x][z])/(x + 2.0);
System.out.println(MovingAvgArray[x][z]);
z++;
a++;
sum = 0;
v--;
}while(z<100-(x+1));
v = 0;
z=0;
x++;
}while(x<i-2);
}
}
The goal of this program is to get the moving average(which is calculated in the for loop) of the List of data, starting at the moving average of the first two numbers, then the first three, ect. and plug it into MovingAvgArray. The moving average of two numbers would be on the array values [0][0 through (i-3)], the average of three numbers would be on values [0][0 through (i-4)], ect ect. Until this is no longer possible.
If i take out the outermost do-while loop the program runs without error, but only calculates the moving average for two numbers(there will be 98 of them).
When i try to loop it to do three numbers which is the above code i get the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100" on the line that says "sum=sum+List[v];
which im pretty sure means that the v in List[v] is a number that is greater than the values listed in List[v]. (its trying to get the 101th value in a array that only has 100 values).
The problem is i have no idea why this is happening because "v" should be set back to 0 before this loop tries to run again.