Search code examples
javamatrixruntime-errorindexoutofboundsexceptiondeterminants

ArrayIndexOutOfBoundsException when trying to find the determinant of a 2D Array/Matrix


this is my first Stack Overflow post, and I am fairly new to java, so I may not initially comprehend some of the feedback you give me.

With this program, I am supposed to find the determinant of a matrix recursively with a size determined by the user. When I do so, however, I get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at Determinant.Copy<Determinant.java:55>
at Determinant.det<Determinant.java:31>
at Determinant.main<Determinant.java:15>

I understand what this error means, but I don't understand why it's happening.

Here are the classes I am using (both printmatrix and the main method were written by my teacher, I had to complete the Copy and det methods):

import javax.swing.JOptionPane;

public class Determinant
{
    public static void main(String args[])
    {
    String sizeStr = JOptionPane.showInputDialog("What size?");
    int size = Integer.parseInt(sizeStr);
    int[][] matrix = new int[size][size];
    for(int i=0; i<size; i++)
        for(int j=0; j<size; j++)
            matrix[i][j] = (int)(Math.random()*40)-20;

    printArray(matrix);
    System.out.println("\nThe determinant = "+det(matrix));

    }


public static int det(int[][] A)
{
    int answer = 0;
    int place = 0;
    int[][] temp;
    int[][] temp1;
    if(A.length==1){
        return(A[0][0]);
    }
    for(int i = 0; i<A.length; i++){
        temp = new int[A.length-1][A[0].length-1];
        temp1 = Copy(temp, i);
        if(i%2==0){
            place = 1;
        }
        else{
            place = -1;
        }
        answer = answer + place * A[0][i] * det(temp1);
    }
    return answer;
}



public static int[][] Copy(int[][] B, int i)
{
    int[][] C = new int[B.length-1][B.length-1];

    for(int j = 1; j<B.length; j++){
        for(int k = 0; k<B[0].length; k++){
            if(k>i){
                C[j-1][k-1]=B[j][k];
            }
            else{
                C[j-1][k]=B[j][k];
            }
        }
    }
    return C;
}


public static void printArray(int[][] A)
{
    for(int i=0; i<A.length; i++)
    {
        for(int j=0; j<A.length; j++)
        {
            int num = A[i][j];
            if(num<-9)
                System.out.print(" ");
            else if(num<0||num>9)
                System.out.print("  ");
            else
                System.out.print("   ");
            System.out.print(A[i][j]);
        }
        System.out.println();
    }
}


}

The error occurs at the else statement in Copy and temp1 = Copy(temp, i).

I am confused, as if either j or k = 1, shouldn't that be a position in the array? What am I missing?


Solution

  • The size of C array in method copy, should be same as B array. Reason: You are copying B array into C array, they should have same size. Try the following:

    import javax.swing.JOptionPane;
    
    public class Determinant
    {
      public static void main(String args[])
      {
        String sizeStr = JOptionPane.showInputDialog("What size?");
        int size = Integer.parseInt(sizeStr);
        int[][] matrix = new int[size][size];
    
        for(int i=0; i<size; i++) {
            for(int j=0; j<size; j++) {
                matrix[i][j] = (int) (Math.random() * 40) - 20;
            }
        }
    
        printArray(matrix);
        System.out.println("\nThe determinant = "+det(matrix));
    
      }
    
    
    public static int det(int[][] A)
    {
        int answer = 0;
        int place = 0;
        int[][] temp;
        int[][] temp1;
        if(A.length==1){
            return(A[0][0]);
        }
        for(int i = 0; i<A.length; i++){
            temp = new int[A.length-1][A[0].length-1];
            temp1 = Copy(temp, i);
            if(i%2==0){
                place = 1;
            }
            else{
                place = -1;
            }
            answer = answer + place * A[0][i] * det(temp1);
        }
        return answer;
      }
    
    
    
    public static int[][] Copy(int[][] B, int i)
    {
        //The C array size should be same as B
        int[][] C = new int[B.length][B[0].length];
    
        for(int j = 1; j<B.length; j++){
            for(int k = 0; k<B[0].length; k++){
                if(k>i){
                    C[j-1][k-1]=B[j][k];
                }
                else{
                    C[j-1][k]=B[j][k];
                }
            }
        }
        return C;
    }
    
    
    public static void printArray(int[][] A)
    {
        for(int i=0; i<A.length; i++)
        {
            for(int j=0; j<A.length; j++)
            {
                int num = A[i][j];
                if(num<-9)
                    System.out.print(" ");
                else if(num<0||num>9)
                    System.out.print("  ");
                else
                    System.out.print("   ");
                System.out.print(A[i][j]);
            }
            System.out.println();
        }
    }
    

    }

    Hope this explains, enjoy!