-1

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?

Hongbin Wang
  • 1,186
  • 2
  • 14
  • 34
rospendan
  • 11
  • 3
  • http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Tunaki May 06 '16 at 17:35
  • First line of copy you made the new array one element smaller than the old one (not the only problem) – joe pelletier May 06 '16 at 17:41
  • 1
    I believe you are trying to copy the 2D Array `B`, if you're setting the the bounds of `C` equivalently you should correct your Line `int[][] C = new int[B.length-1][B.length-1];` to `int[][] C = new int[B.length][B[0].length];` – Mad Matts May 06 '16 at 17:46

2 Answers2

0

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!

MSameer
  • 413
  • 2
  • 9
  • 1
    Only if the matrix B is square If it isn't it should be `int[][] C = new int[B.length][B[0].length];` Or you could use the Built-in `.clone()` method. – Mad Matts May 06 '16 at 17:48
  • @MadMatts, Thanks for pointing that out. Looks like the matrix is square, as program is accepting one input named "size" & performing `new int[size][size]`. I have none the less, edited the post as it supports non-square matrices. – MSameer May 06 '16 at 17:52
  • Yep, that seemed to work. Thank you for the feedback! – rospendan May 06 '16 at 18:00
0

You should declare your matrix in Copy function in this way:

int[][] C = new int[B.length][B[0].length];

Otherwise you are declaring a matrix without a row and column. The fact that you start to use from 0 doesn't mean you should declare one row in less!

granmirupa
  • 2,780
  • 16
  • 27