Your code contains the potential for an infinite loop. It is possible for (x <= 3)
to never become false
, and so for your while
loop to run forever. You haven't given your input matrix, but I assume that what is happening is that the first call to getMinor
returns and so println
prints the returned result, but then the execution goes into getMinor
the second time and gets stuck...never comes out, and so the second println
never occurs and the program never exits.
Yes, that's what's going on. I just made up an arbitrary matrix, and my run prints a 0
and then locks up in the second call to getMinor
, going around and around in the while
loop forever with if (i != r && j != c && mat[i][j] != 100)
never being true
, and so x
never changing. This occurs because you're operating on the same matrix the second time, and the locations already set to 100
are sufficient to cause the while
loop to never exit.
If you start with a fresh matrix the second time, this doesn't so easily occur. This code, at least with my sample matrix, completes:
int[][] mat = getMatrix();
System.out.println(getMinor(mat, 0, 0));
mat = getMatrix();
System.out.println(getMinor(mat, 1, 1));
Another potential problem with your code (I say potential because maybe you will never use the wrong parameters so that this happens) is if you pass in r
and c
values that are larger than the w/h bounds of the matrix, then the program crashes with an index out of bounds error
because the if
clause succeeds more than 4 times in a run through the matrix, and so you haven't allocated enough slots in the min
array, and min[x] = mat[i][j];
goes bang!.