indexoutofboundsexception - java matrix chain multiplication array index out of bound exception -


trying chain matrix multiplication code throws out of bound exception @ 2 places, please me eliminate it. exception occurs @ 2 places exception in thread "main" java.lang.arrayindexoutofboundsexception: 7     @ recursive.recursive_matrix(recursive.java:27)     @ recursive.main(recursive.java:15). 

the problem using following recursion repeatedly calling follows:

 mij =  0, if   = j else          min   ( mik + mk+1 j + ri-1 * rk * rj )    if  < j              ≤ k < j 

i think related max value being 99999.or, due other comparisons.

 class recursive{       public static int size = 7;     public static int max = 99999;      static int m[][] = new int[size][size];     static int i, k, q, j;     static int p[] = new int []  { 25,10,15,5,30,10,15};       public static void main(string[] args)     {          recursive_matrix( 1, size);         print_m();         return;     }      static int recursive_matrix(int i, int j)      {         if( == j )             return 0;         else         {             m[i][j] = max;             for(k = i; k <= j-1; k++)             {                 q =  recursive_matrix(i, k) + recursive_matrix( k+1, j) +  ( p[i-1] * p[k] * p[j] );                 if( q < m[i][j])                     m[i][j] = q;             }         }         return m[i][j];      } 

//this function used print elements of matrix //the diagonal elements 0, , other elements computed above recursive function.
static void print_m() { for(int x = 1; x<= size; x++) { for(int y = 1; y <= size; y++) { system.out.println(m[x][y]+" "); } system.out.println("\n"); } } }

just have @ first call recursive_matrix( 1, size);. j=7 , later ...m[j]... , ...p[j]... out of bound indexes start 0. use debugger , step through code!


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -