diff --git a/main.c b/main.c index b524a783ddc17aa49ec0e739f79203f29ba9f024..0a04670791342aa2b15a9fb285d66a2ef6a7d47e 100644 --- a/main.c +++ b/main.c @@ -1,16 +1,19 @@ /* Author : Dario GENGA - * Date : 13.10.2021 - * Description : Template for a standard c file + * Date : 16.11.2021 + * Description : Manipulate matrix */ #include <stdio.h> #include <stdlib.h> // #include <math.h> // #include <time.h> +// #include <stdbool.h> #include "matrix.h" int main() { - printf("Hello world!"); + matrix mat; + matrix_init(&mat, 3, 4, 0); + matrix_print(mat); return EXIT_SUCCESS; } diff --git a/makefile b/makefile index 30b4034d349004523261f19b8a2d521fa7fcfb4d..07e07fb52c8a2d771c7e8bf0c2f7cf18096a49ca 100644 --- a/makefile +++ b/makefile @@ -1,12 +1,18 @@ LIB=-lm CC=gcc -Wall -Wextra -g -matrix:matrix.o main.o - gcc $^ -fsanitize=address -o $@ $(LIB) +run_tests: tests + ./$< + +matrix: matrix.o main.o + $(CC) $^ -fsanitize=address -fsanitize=leak -o $@ $(LIB) + +tests: test.o matrix.o + $(CC) $^ -fsanitize=address -fsanitize=leak -o $@ $(LIB) matrix.o: matrix.c matrix.h $(CC) -c $< $(LIB) main.o: main.c $(CC) -c $< $(LIB) clean: - rm -f *.o matrix \ No newline at end of file + rm -f *.o matrix tests \ No newline at end of file diff --git a/matrix.c b/matrix.c index 920e761dd3b05dbc9b7c6dd534b23b2fa24656d8..64fe388d222e8fd2ed571dc5942725ed370900cf 100644 --- a/matrix.c +++ b/matrix.c @@ -1,11 +1,58 @@ /* Author : Dario GENGA - * Date : 15.11.2021 - * Description : Manipulate an unidimensional array with dynamic memory allocation + * Date : 16.11.2021 + * Description : Manipulate matrix */ #include "matrix.h" #include <stdio.h> +error_code matrix_alloc(matrix *mat, int32_t m, int32_t n) { + int32_t **data = malloc(m * sizeof(int32_t *)); + mat->m = m; + mat->n = n; + + if (data == NULL) { + return err; + } + mat->data = data; + + for (int32_t i = 0; i < m; i++) { + int32_t *row = malloc(n * sizeof(int32_t *)); + + if (row == NULL) { + return err; + } + + mat->data[i] = row; + } + + return ok; +} + +error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val) { + matrix_alloc(mat, m, n); + + for (int32_t i = 0; i < m; i++) { + for (int32_t k = 0; k < n; k++) { + mat->data[i][k] = val; + } + } + + return ok; +} + +error_code matrix_print(const matrix mat) { + for (int i = 0; i < mat.m; i++) { + printf("["); + for (int k = 0; k < mat.n; k++) { + printf(" %d ", mat.data[i][k]); + } + printf("]\n"); + } + + return ok; +} + void swap(int *x, int *y) { int tmp = *x; diff --git a/matrix.h b/matrix.h index ee830b65601b0ba90f6b078f844b11bb494d8f0c..a614f6671dd9aaa6ce8616befcb38f7da138b3a1 100644 --- a/matrix.h +++ b/matrix.h @@ -1,11 +1,43 @@ /* Author : Dario GENGA - * Date : 15.11.2021 - * Description : Manipulate an unidimensional array with dynamic memory allocation + * Date : 16.11.2021 + * Description : Manipulate matrix */ #ifndef _MATRIX_H #define _MATRIX_H #include <stdio.h> #include <stdlib.h> +#include <stdbool.h> + +typedef struct _matrix { + int32_t m, n; + int32_t ** data; +} matrix; + +typedef enum _error_code { + ok, err +} error_code; + +error_code matrix_alloc(matrix *mat, int32_t m, int32_t n); + +error_code matrix_init(matrix *mat, int32_t m, int32_t n, int32_t val); + +error_code matrix_destroy(matrix *mat); + +error_code matrix_init_from_array(matrix *mat, int32_t m, int32_t n, int32_t data[], int32_t s); + +error_code matrix_clone(matrix *cloned, const matrix mat); + +error_code matrix_transpose(matrix *transposed, const matrix mat); + +error_code matrix_print(const matrix mat); + +error_code matrix_extract_submatrix(matrix *sub, const matrix mat, int32_t m0, int32_t m1, int32_t n0, int32_t n1); + +bool matrix_is_equal(matrix mat1, matrix mat2); + +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); diff --git a/test.c b/test.c new file mode 100644 index 0000000000000000000000000000000000000000..e34b3b44f498110f10eeb65c12ef8b844e1b41ad --- /dev/null +++ b/test.c @@ -0,0 +1,25 @@ +/* Author : Dario GENGA + * Date : 16.11.2021 + * Description : Manipulate matrix + */ +#include "matrix.h" +#include <assert.h> +#include <math.h> +#include <stdbool.h> +#include <stdio.h> + +int main() { + // Initialisation + matrix mat; + int32_t m = 3; + int32_t n = 4; + int32_t val = 1; + printf("Starting the tests...\n"); + // Test allocation + assert(matrix_alloc(&mat, m, n) == ok); + // Test initialisation + assert(matrix_init(&mat, m, n, val) == ok); + + printf("The tests are completed and were successful !"); + return EXIT_SUCCESS; +} diff --git a/tests b/tests new file mode 100755 index 0000000000000000000000000000000000000000..9fcfc55a0f9140259233a074e457e80538bc020f Binary files /dev/null and b/tests differ