From 4585e84ca7f988b520925803aeaf43373942a6f8 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 2 Apr 2024 11:29:44 +0200 Subject: [PATCH] Add function for filling matrix with zeros It has to be handed an empty (just initialized) matrix as I did not think about non-empty matrices. --- Makefile | 1 + ft_arr/ft_mat_zeros.c | 29 +++++++++++++++++++++++++++++ inc/ft_arr.h | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 ft_arr/ft_mat_zeros.c diff --git a/Makefile b/Makefile index c49039e..b5f287f 100644 --- a/Makefile +++ b/Makefile @@ -105,6 +105,7 @@ SRCarr := ft_vec_init.c \ ft_mat_append.c \ ft_mat_insert_col.c \ ft_mat_insert_row.c \ + ft_mat_zeros.c \ ft_mat_access.c \ ft_mat_free.c \ diff --git a/ft_arr/ft_mat_zeros.c b/ft_arr/ft_mat_zeros.c new file mode 100644 index 0000000..ca96300 --- /dev/null +++ b/ft_arr/ft_mat_zeros.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_mat_zeros.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/02 11:00:49 by ljiriste #+# #+# */ +/* Updated: 2024/04/02 11:23:05 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +t_arr_stat ft_mat_zeros(t_mat *matrix, size_t rows, size_t cols) +{ + t_arr_stat reserve_res; + + if ((rows * cols) / cols != rows) + return (invalid_size); + reserve_res = ft_vec_reserve(&(matrix->vec), rows * cols); + if (reserve_res != success) + return (reserve_res); + matrix->rows = rows; + matrix->cols = cols; + matrix->vec.size = rows * cols; + return (success); +} diff --git a/inc/ft_arr.h b/inc/ft_arr.h index 4b69c95..bc9bcb1 100644 --- a/inc/ft_arr.h +++ b/inc/ft_arr.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 13:58:15 by ljiriste #+# #+# */ -/* Updated: 2024/01/17 14:52:46 by ljiriste ### ########.fr */ +/* Updated: 2024/04/02 11:13:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -92,5 +92,6 @@ t_arr_stat ft_mat_erase_rows(t_mat *mat, size_t count, t_arr_stat ft_mat_erase_cols(t_mat *mat, size_t count, size_t index, void (*free_el)(void *)); void *ft_mat_access(t_mat *mat, size_t row, size_t col); +t_arr_stat ft_mat_zeros(t_mat *matrix, size_t rows, size_t cols); #endif -- 2.30.2