Add function for filling matrix with zeros
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 2 Apr 2024 09:29:44 +0000 (11:29 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 2 Apr 2024 09:29:44 +0000 (11:29 +0200)
It has to be handed an empty (just initialized) matrix as I did not
think about non-empty matrices.

Makefile
ft_arr/ft_mat_zeros.c [new file with mode: 0644]
inc/ft_arr.h

index c49039e6029c24e295422d32b60df3f1a4eaaf0c..b5f287f5806268007be6977ca1936ddd7acde6db 100644 (file)
--- 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 (file)
index 0000000..ca96300
--- /dev/null
@@ -0,0 +1,29 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_mat_zeros.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/02 11:00:49 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/04/02 11:23:05 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stddef.h>
+
+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);
+}
index 4b69c9509e62a707829fbb6d0d948f54172b52ae..bc9bcb1abb1f523285bc2bfec89ee1e1c2dff2f9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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