Given a dynamic two-dimensional array. Delete all lines and columns at the intersection of which is "0". Normally deletes only the lines. Columns are not normal (deletes only from the end, even if the first one contains "0"). Help

#include <iostream> #include <time.h> #include <cstdlib> using namespace std; int** Memory(int col, int row) { int **arr = new int*[row]; for (int i = 0; i < row; i++) { arr[i] = new int[col]; } return arr; } void Rand(int col, int row, int **arr) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = rand() % 20; } } } void RowColDel(int **arr, int row, int col); void main() { setlocale(LC_ALL, "ukr"); srand(time(NULL)); int col = 5; int row = 5; int **arr = Memory(col, row); Rand(col, row, arr); Print(col, row, arr); cout << endl; RowColDel(arr, row, col); //Delete(arr, col, row); } void RowColDel(int **arr, int row, int col) { int row1 = row, col1 = col; int **tmp = Memory(col1, row1); for (int i = 0, i1 = 0; i < row; i++) { bool rows = false; for (int j = 0; j < col; j++) { if (arr[i][j] == 0) { rows = true; break; } else rows = false; } if (rows == false) { tmp[i1] = arr[i]; i1++; } else row1--; } for (int i = 0, i1 = 0; i < col; i++) { bool cols = false; for (int j = 0; j < row; j++) { if (arr[i][j] == 0) { cols = true; break; } else cols = false; } if (cols == false) { tmp[i1] = arr[i]; i1++; } else col1--; } Print(col1, row1, tmp); // Delete(tmp, col1, row1); } 
  • Delete function (tmp, col1, row1); please bring - Senior Pomidor

1 answer 1

 #include <iostream> #include <cstring> using namespace std; int main(void){ const int ROWS = 5; const int COLS = 4; int mat[ROWS][COLS] = { { 1, 1, 0, 4 }, { 2, 0, 3, 4 }, { 3, 3, 3, 4 }, { 4, 4, 3, 4 }, { 5, 5, 3, 0 } }; int i, j; int rows = ROWS; int cols = COLS; //вывод исходной матрицы for(i = 0; i < rows; ++i){ for(j = 0; j < cols; ++j) cout << mat[i][j] << ' '; cout.put('\n'); } cout.put('\n'); //удаление for(i = 0; i < rows; ++i){ for(j = 0; j < cols; ++j){ if(mat[i][j] != 0) continue; for(int r = 0; r < rows; ++r) memcpy(&mat[r][j], &mat[r][j + 1],(cols-(j+1))*sizeof(int)); //смещение строк for(int c = 0; c < cols; ++c){ for(int r = i; r < (rows - 1); ++r) mat[r][c] = mat[r + 1][c]; } --cols; --rows; --i; j = 0; } } //вывод обработаной матрицы for(i = 0; i < rows; ++i){ for(j = 0; j < cols; ++j) cout << mat[i][j] << ' '; cout.put('\n'); } return 0; } 

conclusion

 1 1 0 4 2 0 3 4 3 3 3 4 4 4 3 4 5 5 3 0 3 4 

http://ideone.com/fdYtY1 here is the whole code with the output