Dimension of dynamic multi-dimensional mass



  • In the NxM size matrix, rationalize the elements of zero cell columns by increasing by simple insertion.

    My sorting looks like:

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (!matrix[j][i]) {
                for (i = 0; i < m; i++) {
                    int min = i + 1;
                    for (j = i+2; j < n; j++) {
                        if (matrix[j][i] < matrix[min][i]) {
                            min = j;
                            if (matrix[min][i] < matrix[j][i]) {
                                int t = matrix[min][i];
                                matrix[min][i] = matrix[j][i];
                                matrix[j][i] = t;
                            }
                        }
                    }
                }
                break;
            }
        }
    }
    

    But it doesn't work, it doesn't sort anything, tell me how to fix it, thank you in advance!



  • First of all, the boxing is different.

    Second, the external cycle variables i, j are used as internal enumerators and cannot operate. Didn't the compilator warn you of such non-use?

    Like this (not tested):

      if (!matrix[j][i]) {
            for (k = 1; k < n; k++) {
                int temp = matrix[k][i];
                int l = k;
                while (l>0) & (temp<matrix[l-1][i]) { 
                    matrix[l][i] = matrix[l-1][i];      
                    l--;
                }
                matrix[l][i] = temp;
            }
            break;
      }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2