0
public static int getMinor(int[][] mat, int r, int c) {
  int minor = 0;
  int[] min = new int[4];
  int x = 0;
  while (x <= 3) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (i != r && j != c && mat[i][j] != 100) {
          min[x] = mat[i][j];
          x += 1;
          mat[i][j] = 100;
        }
      }
    }
  }
  minor = min[0] * min[3] - min[1] * min[2];
  return minor;
}

public static void main(String[] args) {
  int[][] mat = getMatrix();
  System.out.println(getMinor(mat, 0, 0));
  System.out.println(getMinor(mat, 1, 1));
}

for some reason when I call getMinor function again the code stops working. It prints the minor the first time but does not work when I call it again in the next line. getMatrix function just gets the matrix.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
  • 1
    You are altering `mat` in `getMinor`. Is that intentional? This means the second call to `getMinor` will have the altered `mat` from the first call. – forgivenson Oct 19 '20 at 02:53

2 Answers2

0

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!.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0
  1. You've changed the value of "mat" in "getMinor", so 2ed call dose not have same input as the 1st one . (You may read this to figure out java's "pass by reference" VS "pass by value")

  2. It's not a good practice to modify input within a function named as "getXXX"

Wilson.F
  • 49
  • 3