Piscine C 01
Piscine C 01 ex00 (ft_ft)
Задание:
• Create a function that takes a pointer to int as a parameter, and sets the value "42" to that int. • Here’s how it should be prototyped : void ft_ft(int *nbr);
• Создайте функцию, которая принимает указатель на 'int' в качестве параметра и задает значение "42" для этого 'int'. • Вот как это должно быть объявлено: void ft_ft(int *nbr);
Разрешенные функции: отсутствуют
Решение 1
void ft_ft(int *nbr)
{
*nbr = 42;
}Решение 2
void ft_ft(int *nbr)
{
*nbr = 42;
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex01 (ft_ultimate_ft)
Задание:
• Create a function that takes a pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to pointer to int as a parameter and sets the value "42" to that int. • Here’s how it should be prototyped :
void ft_ultimate_ft(int *********nbr);
• Создайте функцию, которая принимает "указатель на указатель на указатель на указатель на указатель на указатель на указатель на указатель на указатель на указатель на указатель" на 'int' в качестве параметра и устанавливает значение " 42 " для этого 'int'. • Вот как это должно быть объявлено:
void ft_ultimate_ft(int *********nbr);
void ft_ultimate_ft(int *********nbr)
{
*********nbr = 42;
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex02 (ft_swap)
Задание:
• Create a function that swaps the value of two integers whose addresses are entered as parameters. • Here’s how it should be prototyped :
void ft_swap(int *a, int *b);
• Создайте функцию, которая меняет местами значения двух целых чисел, адреса которых вводятся в качестве параметров. • Вот как это должно быть объявлено:
oid ft_swap(int *a, int *b);
Решение 1
void ft_swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}Решение 2
void ft_swap(int *a, int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}Решение 3
void ft_swap(int *a, int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
}
Решение 4
void ft_swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex03 (ft_div_mod)
Задание:
• Create a function ft_div_mod prototyped like this :
void ft_div_mod(int a, int b, int *div, int *mod);
• This function divides parameters a by b and stores the result in the int pointed by div. It also stores the remainder of the division of a by b in the int pointed by mod.
• Эта функция делит параметров 'а', 'b' и сохраняет результат в переменной типа 'int', на который указывает элемент 'div'. Он также хранит остаток деления 'a' на 'b' в 'int', указанном 'mod'. • Вот как это должно быть объявлено.
void ft_div_mod(int a, int b, int *div, int *mod);
Решение 1
void ft_div_mod(int a, int b, int *div, int *mod)
{
*div = a / b;
*mod = a % b;
}Решение 2
void ft_div_mod(int a, int b, int *div, int *mod)
{
if (b != 0)
{
*div = a / b;
*mod = a % b;
}
}Решение 3
void ft_div_mod(int a, int b, int *div, int *mod)
{
*div = a / b;
*mod = a % b;
}Решение 4
void ft_div_mod(int a, int b, int *div, int *mod)
{
*div = a / b;
*mod = a % b;
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex04 (ft_ultimate_div_mod)
Задание:
• Create a function ft_ultimate_div_mod with the following prototype :
void ft_ultimate_div_mod(int *a, int *b);
• This function divides parameters a by b. The result of this division is stored in the int pointed by a. The remainder of the division is stored in the int pointed by b.
• Эта функция делит параметры 'a' на 'b'. результат этого деления сохраняется в 'int', указанном 'a'. Остаток от деления хранится в переменной типа 'int' указывает на 'b'. • Вот как это должно быть объявлено:
void ft_ultimate_div_mod(int *a, int *b);
Решение 1
void ft_ultimate_div_mod(int *a, int *b)
{
int temp;
temp = *a;
*a = *a / *b;
*b = temp % *b;
}Решение 2
void ft_ultimate_div_mod(int *a, int *b)
{
int t;
if (b != 0)
{
t = *a;
*a = *a / *b;
*b = t % *b;
}
}Решение 3
void ft_ultimate_div_mod(int *a, int *b)
{
int c;
int d;
c = *a;
d = *b;
*a = c / d;
*b = c % d;
}Решение 4
void ft_ultimate_div_mod(int *a, int *b)
{
int div;
int mod;
div = *a / *b;
mod = *a % *b;
*a = div;
*b = mod;
}Решение 5
void ft_ultimate_div_mod(int *a, int *b)
{
int tmp;
tmp = *a;
*a /= *b;
*b = tmp % *b;
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex05 (ft_putstr)
Задание:
• Create a function that displays a string of characters on the standard output. • Here’s how it should be prototyped :
void ft_putstr(char *str);
• Создайте функцию, которая отображает строку символов в стандартном выводе. • Вот как это должно быть объявлено:
void ft_putstr(char *str);
Разрешенные функции: write
Решение 1
void ft_putstr(char *str)
{
unsigned int i;
i = 0;
while (*(str + i))
i++;
write(1, str, i);
}
/* ИЛИ, тоже самое но, ТАК :
void ft_putstr(char *str)
{
unsigned int i;
i = 0;
while (str[i])
i++;
write(1, str, i);
}
*/Решение 2
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
unsigned int i;
i = 0;
while (str[i] != '\0')
{
ft_putchar(str[i]);
i++;
}
}
/* ИЛИ, тоже самое но, ТАК :
void ft_putstr(char *str)
{
unsigned int i;
i = 0;
while (str[i])
ft_putchar(str[i++]);
}
*/
Решение 3
void ft_putchar(char c)
{
write(1, &c, 1);
}
void ft_putstr(char *str)
{
while (*str != '\0')
{
ft_putchar(*str);
str++;
}
}
/* ИЛИ, тоже самое но, ТАК :
void ft_putstr(char *str)
{
while (*str)
ft_putchar(*str++);
}
*/
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex06 (ft_strlen)
Задание:
• Create a function that counts and returns the number of characters in a string. • Here’s how it should be prototyped :
int ft_strlen(char *str);
• Создайте функцию, которая подсчитывает и возвращает количество символов в строке. • Вот как это должно быть обьявлено:
int ft_strlen(char *str);
Решение 1
int ft_strlen(char *str)
{
int n;
n = 0;
while (str[n] != '\0')
{
n++;
}
return (n);
}Решение 2
int ft_strlen(char *str)
{
int n;
n = 0;
while (1)
{
if (str[n] == '\0')
{
return (n);
}
n++;
}
}Решение 3
int ft_strlen(char *str)
{
int count;
count = 0;
while (*str != '\0')
{
count++;
str++;
}
return (count);
}Решение 4
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i += 1;
return (i);
}Решение 5
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}Решение 6
int ft_strlen(char *str)
{
int l;
l = 0;
while (*str++ != '\0')
l++;
return (l);
}Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex07 (ft_rev_int_tab)
Задание:
• Create a function which reverses a given array of integer (first goes last, etc). • The arguments are a pointer to int and the number of ints in the array. • Here’s how it should be prototyped :
void ft_rev_int_tab(int *tab, int size);
• Создайте функцию, которая переворачивает данный массив целых чисел (первый элемент массива станет последним, и т.д.). • Аргументами будут указатель на адрес первого элемента массива целых чисел типа 'int' и количество целых чисел в этом массиве. • Вот как это должно быть объявлено:
void ft_rev_int_tab(int *tab, int size);
Решение 1
void ft_rev_int_tab(int *tab, int size)
{
int i;
char temp;
i = -1;
while (++i < --size)
{
temp = tab[i];
tab[i] = tab[size];
tab[size] = temp;
}
}Решение 2
void ft_rev_int_tab(int *tab, int size)
{
int counter;
char temp;
counter = 0;
while (counter < size - 1)
{
temp = tab[size];
tab[size] = tab[counter];
tab[counter] = temp;
counter++;
size--;
}
}
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out
Piscine C 01 ex08 (ft_sort_int_tab)
Задание:
• Create a function which sorts an array of integers by ascending order. • The arguments are a pointer to int and the number of ints in the array. • Here’s how it should be prototyped :
void ft_sort_int_tab(int *tab, int size);
• Создайте функцию, которая сортирует массив (таблицу) целых чисел по возрастанию. • Аргументами являются указатель на int и число ints в поле массив. • Вот как это должно быть объявлено:
void ft_sort_int_tab(int *tab, int size);
Решение 1
void ft_sort_int_tab(int *tab, int size)
{
int i;
int j;
int temp;
i = 0;
while (i < size - 1)
{
j = 0;
while (j < size - i - 1)
{
if (tab[j] > tab[j + 1])
{
temp = tab[j];
tab[j] = tab[j + 1];
tab[j + 1] = temp;
}
j++;
}
i++;
}
}Решение 2
void ft_sort_int_tab(int *tab, int size)
{
int i;
int temp;
i = 0;
while (i < size - 1)
{
if (tab[i] > tab[i + 1])
{
temp = tab[i];
tab[i] = tab[i + 1];
tab[i + 1] = temp;
i = -1;
}
i++;
}
}Решение 3
void ft_sort_int_tab(int *tab, int size)
{
int temp;
int i;
int j;
i = 0;
while (i < size - 1)
{
j = i;
while (j < size)
{
if (tab[i] > tab[j])
{
temp = tab[i];
tab[i] = tab[j];
tab[j] = temp;
}
j++;
}
i++;
}
}
Объяснения + проверка int main
Команда для компиляции и одновременного запуска:
gcc -Wall -Werror -Wextra названиефайла.c && chmod +x ./a.out && ./a.out