aboutsummaryrefslogtreecommitdiff
path: root/set_matrix_zeroes.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-27 12:45:33 -0600
committerJack Sangdahl <[email protected]>2025-08-27 12:45:33 -0600
commit4552bcf5a4a356415e248dee3b201f0cea7205bb (patch)
tree9868465918c6a6841b84cd14d66923b993dd9d62 /set_matrix_zeroes.c
parentf549305fb5ca531d29dce6c2c091560f44190443 (diff)
downloadleetcode-4552bcf5a4a356415e248dee3b201f0cea7205bb.tar.gz
leetcode-4552bcf5a4a356415e248dee3b201f0cea7205bb.zip
chore: Refactor two_d_arr_init function
Return a pointer to a new struct, rather than initialise objects in an existing struct
Diffstat (limited to 'set_matrix_zeroes.c')
-rw-r--r--set_matrix_zeroes.c23
1 files changed, 12 insertions, 11 deletions
diff --git a/set_matrix_zeroes.c b/set_matrix_zeroes.c
index be0e332..6bb14e6 100644
--- a/set_matrix_zeroes.c
+++ b/set_matrix_zeroes.c
@@ -24,21 +24,22 @@ void setZeroes(int **matrix, int matrixSize, int *matrixColSize) {
int main() {
int m1i[3][3] = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}},
m2i[3][4] = {{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}};
- struct two_d_arr m1, m2;
- two_d_arr_init(&m1, ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i);
- two_d_arr_init(&m2, ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i);
- setZeroes(m1.arr, m1.row_size, m1.col_size);
- setZeroes(m2.arr, m2.row_size, m2.col_size);
- for (int i = 0; i < m1.row_size; i++) {
- for (int j = 0; j < m1.col_size[i]; j++)
+ struct two_d_arr *m1 =
+ two_d_arr_init(ARRAY_SIZE(m1i), ARRAY_SIZE(m1i[0]), m1i);
+ struct two_d_arr *m2 =
+ two_d_arr_init(ARRAY_SIZE(m2i), ARRAY_SIZE(m2i[0]), m2i);
+ setZeroes(m1->arr, m1->row_size, m1->col_size);
+ setZeroes(m2->arr, m2->row_size, m2->col_size);
+ for (int i = 0; i < m1->row_size; i++) {
+ for (int j = 0; j < m1->col_size[i]; j++)
printf("%d ", m1i[i][j]); // expect: [[1,0,1],[0,0,0],[1,0,1]]
printf("\n");
}
- for (int i = 0; i < m2.row_size; i++) {
- for (int j = 0; j < m2.col_size[i]; j++)
+ for (int i = 0; i < m2->row_size; i++) {
+ for (int j = 0; j < m2->col_size[i]; j++)
printf("%d ", m2i[i][j]); // expect: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
printf("\n");
}
- two_d_arr_free(&m1);
- two_d_arr_free(&m2);
+ two_d_arr_free(m1);
+ two_d_arr_free(m2);
}