0

For this question, I implemented this:

public void setZeros (int[][] matrix) {
    firstScan(matrix);
    secondScan(matrix);
}

public void firstScan(int[][] matrix) {
    int i,  j;
    for (i =0; i < matrix.length; i++) {
        for (j = 0; j < matrix[0].length; j++)
        {if (matrix[i][j] == 0) {markRowsAndColums(matrix ,i,j);
        }}}}

public void secondScan(int[][] matrix) {
    int i,  j;
    for (i =0; i < matrix.length; i++) {
        for (j = 0; j < matrix[0].length; j++) { if (matrix[i][j] == -1)  {
            matrix[i][j]= 0;
        }}}}

public void markRowsAndColums (int[][] matrix, int i,int j) {
    for ( int x = 0; x< matrix[0].length; x++ ) {
        if(matrix[i][x] !=0) {matrix[i][x] = -1;}}
    for ( int x = 0; x< matrix.length; x++ ) {
        if(matrix[x][j] !=0) {matrix[x][j] = -1;}
    }}

My first scan aims to change all rows and columns that have zeros into -1s (but not the zeros).

My second scan aims to convert all the -1s into zeros.

Do you think it is a good solution? Does it work? And I don't know what the runtime of the algorithm is. It would be great if you could give me a hint cuz I really have no idea.

justin
  • 61
  • 4
  • 1
    The above question should help you with the runtime analysis. Other than that, grading people's homework assignments isn't something that we do here, and we're definitely not here to debug your code. If you want to know if it works, try it on some examples. If it seems to work on all the examples you try, then try to prove that its correct for all inputs, by giving a suitably convincing argument. – David Richerby Nov 17 '17 at 15:24
  • 1
    Wait a second. Suppose the matrix has a zero in it. Then you have to rewrite so that, in particular, that whole row is zeroes. But then each column has a zero in it, so don't you have to then rewrite the whole matrix to be zeroes? – David Richerby Nov 17 '17 at 16:49
  • @DavidRicherby I usually know how to calculate runtime. But in this case I am just not sure. I thought it was O(2n) (thus O(n) cuz I scanned it twice. But in my first scan I was rewriting some rows and columns as well. That is why I am confused. The runtime might depend on the number of zeros in the matrix. – justin Nov 17 '17 at 17:05
  • @DavidRicherby That is why I used a two-scan approach. I didn't make them into zeros the first time, only into -1s. And the second time I will turn them into zeros. If I turn them into zeros the first time then the whole matrix will become all zeros immediately. This is not a homework assignment, and I am not asking anyone to debug my code :). I am trying to improve the code and the runtime and also looking for alternatives. – justin Nov 17 '17 at 17:07
  • Makes sense -- I was questioning the question itself, rather than your approach to solving it. As for the running time, think about how long those nested for loops will take to run, how many times you run them and so on. And what's $n$? The dimension of the matrix? The number of elements in it? – David Richerby Nov 17 '17 at 17:13

0 Answers0