0

The purpose of the below code snippet is to create an instance of a matrix and then calculate the determinant of an nxn matrix. However, method computeDet won't return because the following error occurs: value cannot be resolved to a variable. Why is that?
** determinant code edited from here (this code does work - no error)

public class MatrixArrayCR {
long[][] matrix;

public MatrixArrayCR(long[] arr) {
    double len = Math.sqrt(arr.length);
    int counter = 0;
    for (int i=0; i==len; i++) {
        for (int j=0; j==len; j++) {
            matrix[i][j] = arr[counter];
            counter++;
        }
    }
}
// determinant method edited from code off http://www.coderanch.com/t/446179/java/java/Learning-Project
    public long determinant() {
        return computeDet(matrix);
    }
    public long computeDet(long[][] matrix) {
        int matrixSize = matrix.length;
        if (matrixSize==2) {
            return matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
        } else {
            long value = 0;
            for (int i=0; i<matrixSize; i++) {
                long[][] minor=generateMinor(matrix,0,i);
                value += (sgn(i)*matrix[0][i]*computeDet(minor));
            }
        } return value; //ERROR HERE
    }
    private int sgn(int n) {
        if (n%2==0) {
            return 1;
        } else {
            return -1;
        }
    }
    private long[][] generateMinor(long[][] matrix, int row, int column) {
        int matrixSize = matrix.length;
        int minorSize = matrixSize -1;
        int counterOne = 0;
        int counterTwo = 0;
        long[][] minor = new long[minorSize][minorSize];

        for (int i=0; i<matrixSize; i++) {
            if (i==row) {
                continue;
            } for (int j=0; j<matrixSize; j++) {
                if (j==column) {
                    continue;
                } minor[counterOne][counterTwo] = matrix[i][j];
                ++ counterTwo;
            } ++counterOne;
            counterTwo = 0;
        } return minor;
    }
}
Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36
Kastrasza
  • 1
  • 1

2 Answers2

2

value is a local variable, i.e it's only visible in the block it has been declared. Since you declared value in the else block and try to return it outside this block, you get this error.

The return statement should be in the else block. I.e :

public long computeDet(long[][] matrix) {
        int matrixSize = matrix.length;
        if (matrixSize==2) {
            return matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
        } else {
            long value = 0;
            for (int i=0; i<matrixSize; i++) {
                long[][] minor=generateMinor(matrix,0,i);
                value += (sgn(i)*matrix[0][i]*computeDet(minor));
            }
            return value; 
        }
    }
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
1

The value variable is declared inside of an if or else block and is only visible within the block it was declared. If you want it visible at the end of the method, declare it at the start of the method, before the if/else blocks.

public long computeDet(long[][] matrix) {
    long value = 0L;
    int matrixSize = matrix.length;
    if (matrixSize==2) {
        value = matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
    } else {
        for (int i=0; i<matrixSize; i++) {
            long[][] minor=generateMinor(matrix,0,i);
            value += (sgn(i)*matrix[0][i]*computeDet(minor));
        }
    } 
    return value; 
}

Either that or return it from inside of the else block:

public long computeDet(long[][] matrix) {
    int matrixSize = matrix.length;
    if (matrixSize==2) {
        return matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
    } else {
        long value = 0;
        for (int i=0; i<matrixSize; i++) {
            long[][] minor=generateMinor(matrix,0,i);
            value += (sgn(i)*matrix[0][i]*computeDet(minor));
        }
        return value; //ERROR HERE
     }  // this guy moved
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373