diff --git a/main.c b/main.c index fb6e61b6002c5fb66ac295e27583cdf3f20d06d7..e83ba658bb6c014eb5cac34951416c0b8c6c9981 100644 --- a/main.c +++ b/main.c @@ -11,7 +11,7 @@ #include "matrix.h" int main() { - matrix mat; + matrix mat, cloned; int32_t m = 3; int32_t n = 4; int32_t s = m * n; @@ -23,6 +23,12 @@ int main() { matrix_init_from_array(&mat, m, n, data, s); printf("Matrix %dx%d from array\n", m, n); matrix_print(mat); + matrix_clone(&cloned, mat); + printf("Cloned matrix :\n"); + matrix_print(cloned); + // Free the memory + matrix_destroy(&mat); + matrix_destroy(&cloned); return EXIT_SUCCESS; } diff --git a/matrix.c b/matrix.c index 9d8118f2042425fa901a69017677cb22d76fd10f..af26f76afc8e2ab4914ed2663bbb70bf0bc9083f 100644 --- a/matrix.c +++ b/matrix.c @@ -77,9 +77,32 @@ error_code matrix_print(const matrix mat) { return ok; } -void swap(int *x, int *y) -{ - int tmp = *x; - *x = *y; - *y = tmp; +error_code matrix_clone(matrix *cloned, const matrix mat) { + error_code code = matrix_alloc(cloned, mat.m, mat.n); + if (code == err) { + return err; + } + + for (int i = 0; i < mat.m; i++) { + for (int k = 0; k < mat.n; k++) { + cloned->data[i][k] = mat.data[i][k]; + } + } + + return ok; +} + +error_code matrix_destroy(matrix *mat) { + // Free each row + for (int32_t i = 0; i < mat->m; i++) { + free(mat->data[i]); + } + + // Free the data and reset the number of row and col + free(mat->data); + mat->m = -1; + mat->n = -1; + mat->data = NULL; + + return ok; } diff --git a/matrix.h b/matrix.h index a614f6671dd9aaa6ce8616befcb38f7da138b3a1..c4729ea3ef8af525c7cc3ca80394f0c7f914cf42 100644 --- a/matrix.h +++ b/matrix.h @@ -39,6 +39,4 @@ error_code matrix_get(int32_t *elem, const matrix mat, int32_t ix, int32_t iy); error_code matrix_set(matrix mat, int32_t ix, int32_t iy, int32_t elem); -void swap(int *x, int *y); - #endif \ No newline at end of file diff --git a/tests b/tests deleted file mode 100755 index 9fcfc55a0f9140259233a074e457e80538bc020f..0000000000000000000000000000000000000000 Binary files a/tests and /dev/null differ