basic structure of program n mem allocation

This commit is contained in:
Steffen Matthes 2023-03-25 23:31:12 +01:00
parent 0365fe15c4
commit d2326c9d34
3 changed files with 118 additions and 11 deletions

50
ex00/gen_util.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gen_util.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:41:44 by smatthes #+# #+# */
/* Updated: 2023/03/25 22:52:55 by smatthes ### ########.fr */
/* */
/* ************************************************************************** */
#include<unistd.h>
int str_len(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
void put_error()
{
write(1,"Error\n",6);
}
int *find_position(int **board, int n, int *pos)
{
int i;
int j;
i = -1;
j = -1;
while(++i < n)
{
while(++j < n)
if(board[i][j] == 0)
{
pos[0] = i;
pos[1] = j;
return pos;
}
}
return (0);
}

View File

@ -6,19 +6,22 @@
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 18:58:21 by smatthes #+# #+# */
/* Updated: 2023/03/25 19:12:23 by smatthes ### ########.fr */
/* Updated: 2023/03/25 23:29:41 by smatthes ### ########.fr */
/* */
/* ************************************************************************** */
int check_inp_valid(int argc, char **argv, int n);
int str_len(char *str);
void atoi_arr(char **argv, int *borders, int n);
int check_opposite_sum(int *borders, int n);
int handle_input(int argc, char **argv, int *borders, int n)
{
if (check_inp_valid(argc, argv, n) == 0)
return (0);
atoi_arr(argv, borders, n);
if (check_opposite_sum(borders, n) == 0)
return (0);
return (1);
}
@ -48,16 +51,6 @@ int check_inp_valid(int argc, char **argv, int n)
return (1);
}
int str_len(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
void atoi_arr(char **argv, int *borders, int n)
{
int i;
@ -69,3 +62,26 @@ void atoi_arr(char **argv, int *borders, int n)
i = i + 2;
}
}
int check_opposite_sum(int *borders, int n)
{
int i;
int val;
int opp_val;
i = 0;
while (i < 3 * n)
{
val = borders[1];
opp_val = borders[i + n];
if ((val + opp_val) > (n + 1))
{
return (0);
}
i++;
if (i == n)
i = 2 * n;
}
return (1);
}

41
ex00/handle_mem.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smatthes <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/25 19:45:42 by smatthes #+# #+# */
/* Updated: 2023/03/25 22:41:35 by smatthes ### ########.fr */
/* */
/* ************************************************************************** */
# include<stdlib.h>
# include <stdio.h>
int **allo_mem(int **board, int n)
{
int i;
board = malloc(n * 8);
i = -1;
while(++i < n)
{
board[i] = malloc(4);
}
return board;
}
void free_mem(int *borders, int **board, int n)
{
int i;
i = -1;
while(++i < n)
{
free(board[i]);
}
free(board);
free(borders);
}