[Solved] Code is not giving expected result in sudoku


This line:

        if (mat1[i][j] == mat2[i][j]) 

is testing to see if the corresponding elements of the 2 matrices have the same value.

Looking at it in context:

        if(mat1[i][j] == mat2[i][j]){
            b = b & true;
        }
        else{
            b = b &false;
        }

is a rather cumbersome way of saying this:

        if(mat1[i][j] != mat2[i][j]){
            b = false;
        }

But you could rewrite the checkEqual method to this:

public boolean checkEqual(int[][] mat1,int[][] mat2){
    for(int i = 0;i<mat1.length;i++){
        for(int j=0;j<mat1.length;j++){
            if(mat1[i][j] != mat2[i][j]){
                return false;
            }
        }
    }
    return true;
}

which is a whole lot faster in the case where the matrices are not equal.

Finally, it is worth noting that the code assumes that the two matrices are square, and they have the same size. If either of those assumptions is false, the method will throw an exception.

2

solved Code is not giving expected result in sudoku