简体   繁体   中英

My program is giving me a StackOverflowError

I made a little program to calculate the determinant of a matrix using some recursion. I tried a 5x5 matrix and got a stack overflow error. I understand that the recursion is probably too deep but I'm lost on how to remedy it. Any solutions?

Here is my code:

/**
 * @requires matrix is at least 2x2 and has all real entries
 * @param matrix[][] a matrix
 * @param row is the row to be omitted
 * @param col is the column to be omitted
 * @requires @code col and @code row are 
 * @returns the @code matrix without column
 */
private static int[][] subMatrix(int[][] matrix, int row, int col){
    int[][] newMatrix = new int[matrix.length][matrix[0].length];
    int newRow = 0;
    for ( int i = 0; i < matrix.length; i++){
        int newCol = 0;
        if ( i != row ){
            for( int j = 0; j < matrix[i].length; j++){
                if ( j != 0){
                    newMatrix[i][j] = matrix[newRow][newCol];
                    newCol++;
                }
            }
            newRow++;
        }

    }
    return newMatrix;
}
/**
 * @requires matrix is at least 2x2 and has all real entries
 * @param matrix[][] a matrix
 * @returns the determinant of @code matrix
 */
private static int det(int[][] matrix){
    int det = 0;
    int rows = matrix.length;
    int cols = matrix[0].length;

    //handling base case
    if(rows == 2 & cols == 2){
        det = matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0];
    } else {
        //expanding a row
        if(rows > cols)
        {
            for(int i = 0; i < rows; i++){
                det += matrix[0][i]*det(subMatrix(matrix, i, 0));
            }
        }
        //expanding a column
        else {
            for(int i = 0; i < rows; i++){
                det += matrix[i][0]*det(subMatrix(matrix, 0, i));
            }
        }
    }
    return det;
}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the number of rows: ");
    int rows = in.nextInt();
    System.out.print("Enter the number of columns: ");
    int cols = in.nextInt();

    //reading in matrix
    int[][] matrix = new int[rows][cols];
    for(int i = 0; i < rows; i++){
        System.out.print("Enter the entries of row " + i + ": ");
        for (int j = 0; j < cols; j++){
            matrix[i][j] = in.nextInt();
        }
    }
    System.out.println("The determinant of the matrix is: " + det(matrix));
}

This causes issues (and next rows are related):

int[][] newMatrix = new int[matrix.length][matrix[0].length];

You actually create the submatrix of exactly the same size like the original and then apply recursion.

I assume you want to create submatrix with row and col completely excluded, therefore of size one smaller in each dimension and move the content to the left and top to specified row and col .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM