update
This commit is contained in:
73
ex4/ft_is_sort.c
Normal file
73
ex4/ft_is_sort.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_is_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/13 14:01:47 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/13 14:30:30 by lclerel- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_is_sort(int *tab, int length, int (*f)(int, int))
|
||||
{
|
||||
int i;
|
||||
int cs;
|
||||
int ds;
|
||||
|
||||
i = 0;
|
||||
cs = 1;
|
||||
ds = 1;
|
||||
if (length <= 1)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
while (i < length - 1)
|
||||
{
|
||||
if (f(tab[i], tab[i + 1]) > 0)
|
||||
{
|
||||
cs = 0;
|
||||
}
|
||||
if (f(tab[i], tab[i + 1]) < 0)
|
||||
{
|
||||
ds = 0;
|
||||
i++;
|
||||
}
|
||||
return (cs || ds);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_putnbr(int nb)
|
||||
{
|
||||
char value;
|
||||
|
||||
if (nb == -2147483648)
|
||||
{
|
||||
write(1, "-2147483648", 11);
|
||||
return ;
|
||||
}
|
||||
if (nb < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb > 9)
|
||||
{
|
||||
ft_putnbr(nb / 10);
|
||||
}
|
||||
value = nb % 10 + '0';
|
||||
write(1, &value, 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int tab[] = {1, 2, 3, 4, 5, 6, 9, 7};
|
||||
|
||||
if (ft_is_sort(tab, 8, &ft_putnbr == 1))
|
||||
{
|
||||
write(1, "Ok :3", 5);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user