diff --git a/073_set_matrix_zeroes/Makefile b/073_set_matrix_zeroes/Makefile new file mode 100644 index 0000000..85d2147 --- /dev/null +++ b/073_set_matrix_zeroes/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test set_zero.c diff --git a/073_set_matrix_zeroes/set_zero.c b/073_set_matrix_zeroes/set_zero.c new file mode 100644 index 0000000..71e54a7 --- /dev/null +++ b/073_set_matrix_zeroes/set_zero.c @@ -0,0 +1,40 @@ +#include +#include + +static void setZeroes(int** matrix, int matrixRowSize, int matrixColSize) { + int row, col, bRow = 0, bCol = 0; + for (row = 0; row < matrixRowSize; row++) { + for (col = 0; col < matrixColSize; col++) { + if (matrix[row][col] == 0) { + if (row == 0) bCol = 1; + if (col == 0) bRow = 1; + matrix[0][col] = matrix[row][0] = 0; + } + } + } + + for (row = 1; row < matrixRowSize; row++) { + for(col = 1; col < matrixColSize; col++){ + if (matrix[0][col] == 0 || matrix[row][0] == 0) { + matrix[row][col] = 0; + } + } + } + + if (bRow) { + for(row = 0; row < matrixRowSize; row++) { + matrix[row][0] = 0; + } + } + + if (bCol) { + for (col = 0; col