1
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.

Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
Origomar
  • 11
  • 1
  • I'd recommend, if not debugging, then at least printing values of more variables. This will hopefully make it clear quicker what is wring. For example, I don't think anybody will count the size of List array, it's tedious. And split the code in smaller methods. – rtybase Jun 21 '17 at 20:22
  • Can you explain a bit more what your `MovingAvgArray` is going to hold? Using a small example data, perhaps? – juanpa.arrivillaga Jun 21 '17 at 20:26
  • List of numbers in the array "List"= 1, 2, 3, 4, 5, 6 MovingAvgArray would hold the values on the first column of (1+2)/2, (2+3)/2, (3+4)/2, (4+5)/2, (5+6)/2 Second column of the array would hold - (1+2+3)/3,. (2+3+4)/3, (3+4+5)/3, (4+5+6)/3 Third column would hold- (1+2+3+4)/4, (2+3+4+5)/4, (3+4+5+6)/4 – Origomar Jun 21 '17 at 20:30

1 Answers1

0

I have allowed myself to add extra println's to the code, like

            v--;
            System.out.println(" ------------------ v=" + v);
            System.out.println(" ------------------ z=" + z);
            System.out.println(" ------------------ x=" + x);
            System.out.println(" ------------------ a=" + a);

        } while (z < 100 - (x + 1));
        System.out.println(" HERE ");
        v = 0;

just to observe that:

19.060000000000002
 ------------------ v=99
 ------------------ z=99
 ------------------ x=0
 ------------------ a=99
  HERE 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
    at Prices.main(Prices.java:24)

this means that the loop controlled by

while (z < 100 - (x + 1))

ends. a is still 99, x is increased by 1, making a + x + 2=102

rtybase
  • 101
  • 4
  • 1
    thank you i believe i solved the problem it had to do with what you were talking about. i changed those values to 0 and changed it to a while loop. Im not 100% sure if its working correctly because the program is still running nad i have to recheck the numbers but it finished compiling so i assume its not going through an infinite loop. – Origomar Jun 21 '17 at 21:05
  • Great, I'd recommend writing a few unit tests, to the test the class. It will also help you to avoid adding regression issues if you decide to improve the code later. – rtybase Jun 21 '17 at 21:10