diff --git a/0x00-hello_world/TASKS.md b/0x00-hello_world/TASKS.md new file mode 100644 index 00000000..b63c2368 --- /dev/null +++ b/0x00-hello_world/TASKS.md @@ -0,0 +1,243 @@ +# 0x00-hello_world +>Project Done in 1 Day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* Why __C__ programming is awesome +* Who invented C +* Who are [Dennis Ritchie](https://en.wikipedia.org/wiki/Dennis_Ritchie), [Brian Kernighan](https://www.youtube.com/watch?v=de2Hsvxaf8M) and Linus Torvalds +* What happens when you type `gcc main.c` +* What is an entry point +* What is `main` +* How to print text using `printf`, `puts` and `putchar` +* How to get the size of a specific type using the unary operator `sizeof` +* How to compile using `gcc` +* What is the default program name when compiling with `gcc` +* What is the official __C__ coding style and how to check your code with [betty-style](https://github.com/holbertonschool/Betty/wiki) +* How to find the right header to include in your source code when using a standard library function +* How does the `main` function influence the return value of the program + +## Resourcess Used + +* [0x00. C - Hello, World](https://www.youtube.com/watch?v=co0b0xLEuRM&t=11s) +* [Why __c__ Programming is Awesome](https://www.youtube.com/watch?v=smGalmxPVYc) +* [Learning to Program in __c__ Part |](https://www.youtube.com/watch?v=rk2fK2IIiiQ) +* [Learning to Program in __c__ Part ||](https://www.youtube.com/watch?v=FwpP_MsZWnU) +* [Understanding __c__ Program Compilation Process](https://www.youtube.com/watch?v=VDslRumKvRA) +* [Hash-Bang Under the Hood](https://twitter.com/unix_byte/status/1024147947393495040?s=21) +* [Linus Torvalds on __c__ vs __c++__](http://harmful.cat-v.org/software/c++/linus) +* man `gcc` +* man 3 `printf` +* man `puts` +* man `putchar` + +## Betty linter + +To run the Betty linter just with command betty : + + Go to the [Betty](https://github.com/holbertonschool/Betty) repository + + Clone the repo to your local machine + + cd into the Betty directory + + Install the linter with sudo ./install.sh + + Once saved, exit file and change permissions to apply to all users with `chmod a+x betty` + + Move the `betty` file into `/bin/` directory or somewhere else in your `$PATH` with `sudo mv betty /bin/` + + You can now type `betty ` to run the Betty linter! + +## Tasks Completed + ++ [x] 0\. Preprocessor
_**[0-preprocessor](0-preprocessor)**_ runs a C file through the preprocessor and save the result into another file. The C file name will be saved in the variable `$CFILE`. The output would be saved in the file `c`.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ export CFILE=main.c +bekalue@BEKALU-PC:~/c/0x00$ ./0-preprocessor +bekalue@BEKALU-PC:~/c/0x00$ tail c +# 942 "/usr/include/stdio.h" 3 4 + +# 2 "main.c" 2 + + +# 3 "main.c" +int main(void) +{ + return (0); +} +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 1\. Compiler
_**[1-compiler](1-compiler)**_ compiles a C file but does not link. The C file name will be saved in the variable `$CFILE`. The output file would be named the same as the C file, but with the extension `.o` instead of `.c`.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ export CFILE=main.c +bekalue@BEKALU-PC:~/c/0x00$ cat main.c +#include + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + return (0); +} +bekalue@BEKALU-PC:~/c/0x00$ ./1-compiler +bekalue@BEKALU-PC:~/c/0x00$ ls +0-preprocessor 1-compiler c main.o +Makefile 100-intel main.c main.s +bekalue@BEKALU-PC:~/c/0x00$ cat -v main.o | head +^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^A^@>^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^P^B^@^@^@^@^@^@^@^@^@^@@^@^@^@^@^@@^@^K^@^H^@UHM-^IM-eM-8^@^@^@^@]M-C^@GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609^@^T^@^@^@^@^@^@^@^AzR^@^Ax^P^A^[^L^G^HM-^P^A^@^@^\^@^@^@^\^@^@^@^@^@^@^@^K^@^@^@^@A^N^PM-^F^BC^M^FF^L^G^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^D^@M-qM-^?^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^E^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^F^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^D^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^H^@^@^@^R^@^A^@^@^@^@^@^@^@^@^@^K^@^@^@^@^@^@^@^@main.c^@main^@^@^@^@ ^@^@^@^@^@^@^@^B^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@.symtab^@.strtab^@.shstrtab^@.text^@.data^@.bss^@.comment^@.note.GNU-stack^@.rela.eh_frame^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^[^@^@^@^A^@^@^@^F^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@@^@^@^@^@^@^@^@^K^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@!^@^@^@^A^@^@^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@K^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@'^@^@^@^H^@^@^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@K^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@,^@^@^@^A^@^@^@0^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@K^@^@^@^@^@^@^@5^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@5^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@J^@^@^@^A^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-^@^@^@^@^@^@^@^@8^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^H^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@E^@^@^@^D^@^@^@@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M- ^A^@^@^@^@^@^@^X^@^@^@^@^@^@^@ ^@^@^@^F^@^@^@^H^@^@^@^@^@^@^@^X^@^@^@^@^@^@^@^Q^@^@^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-8^A^@^@^@^@^@^@T^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-8^@^@^@^@^@^@^@M-X^@^@^@^@^@^@^@ +^@^@^@^H^@^@^@^H^@^@^@^@^@^@^@^X^@^@^@^@^@^@^@ ^@^@^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@M-^P^A^@^@^@^@^@^@^M^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 2\. Assembler
_**[2-assembler](2-assembler)**_ generates the assembly code of a C code and save it in an output file. The C file name will be saved in the variable `$CFILE`. The output file would be named the same as the C file, but with the extension `.s` instead of `.c`.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ export CFILE=main.c +bekalue@BEKALU-PC:~/c/0x00$ cat main.c +#include + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + return (0); +} +bekalue@BEKALU-PC:~/c/0x00$ ./2-assembler +bekalue@BEKALU-PC:~/c/0x00$ ls +0-preprocessor 1-compiler 2-assembler c main.c main.s Makefile +bekalue@BEKALU-PC:~/c/0x00$ cat main.s + .file "main.c" + .text + .globl main + .type main, @function +main: +.LFB0: + .cfi_startproc + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + movl $0, %eax + popq %rbp + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE0: + .size main, .-main + .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609" + .section .note.GNU-stack,"",@progbits +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 3\. Name
_**[3-name](3-name)**_ compiles a C file and creates an executable named `cisfun`. The C file name will be saved in the variable `$CFILE`.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ export CFILE=main.c +bekalue@BEKALU-PC:~/c/0x00$ cat main.c +#include + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + return (0); +} +bekalue@BEKALU-PC:~/c/0x00$ ./3-name +bekalue@BEKALU-PC:~/c/0x00$ ls +0-preprocessor 1-compiler 3-name cisfun main.o Makefile +100-intel 2-assembler c main.c main.s +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 4\. Hello, puts
_**[4-puts.c](4-puts)**_ prints exactly `"Programming is like building a multilingual puzzle`, followed by a new line using the function `puts` rather than `printf`.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 4-puts.c && ./a.out +"Programming is like building a multilingual puzzle +bekalue@BEKALU-PC:~/c/0x00$ echo $? +0 +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 5\. Hello, printf
_**[5-printf.c](5-printf.c)**_ prints exactly `with proper grammar, but the outcome is a piece of art,`, followed by a new line using the function `printf` rather than the function `puts`. The program should compile without warning when using the `-Wall` `gcc` option.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 5-printf.c +bekalue@BEKALU-PC:~/c/0x00$ ./a.out +with proper grammar, but the outcome is a piece of art, +bekalue@BEKALU-PC:~/c/0x00$ echo $? +0 +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 6\. Size is not grandeur, and territory does not make a nation
_**[6-size.c](6-size.c)**_ prints the size of various types on the computer it is compiled and run on. You might have to install the package `libc6-dev-i386` on your Linux system to test the `-m32` `gcc` option.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ gcc 6-size.c -m32 -o size32 2> /tmp/32 +bekalue@BEKALU-PC:~/c/0x00$ gcc 6-size.c -m64 -o size64 2> /tmp/64 +bekalue@BEKALU-PC:~/c/0x00$ ./size32 +Size of a char: 1 byte(s) +Size of an int: 4 byte(s) +Size of a long int: 4 byte(s) +Size of a long long int: 8 byte(s) +Size of a float: 4 byte(s) +bekalue@BEKALU-PC:~/c/0x00$ ./size64 +Size of a char: 1 byte(s) +Size of an int: 4 byte(s) +Size of a long int: 8 byte(s) +Size of a long long int: 8 byte(s) +Size of a float: 4 byte(s) +bekalue@BEKALU-PC:~/c/0x00$ echo $? +0 +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 7\. Intel
_**[100-intel](100-intel)**_ generates the assembly code (Intel syntax) of a C code and save it in an output file. The C file name will be saved in the variable `$CFILE`. The output file would be named the same as the C file, but with the extension `.s` instead of `.c`.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ export CFILE=main.c +bekalue@BEKALU-PC:~/c/0x00$ cat main.c +#include + +/** + * main - Entry point + * + * Return: Always 0 (Success) + */ +int main(void) +{ + return (0); +} +bekalue@BEKALU-PC:~/c/0x00$ ./100-intel +bekalue@BEKALU-PC:~/c/0x00$ cat main.s + .file "main.c" + .intel_syntax noprefix + .text + .globl main + .type main, @function +main: +.LFB0: + .cfi_startproc + push rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + mov rbp, rsp + .cfi_def_cfa_register 6 + mov eax, 0 + pop rbp + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE0: + .size main, .-main + .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609" + .section .note.GNU-stack,"",@progbits +bekalue@BEKALU-PC:~/c/0x00$ +``` ++ [x] 8\. UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity
_**[101-quote.c](101-quote.c)**_ prints exactly `and that piece of art is useful" - Dora Korpar, 2015-10-19`, followed by a new line, to the standard error.The functions listed in the NAME section of the man (3) `printf` or man (3) `puts` would not be used. The return value of the program would be 1 and would compile without any warnings when using the `-Wall` `gcc` option.
__Example__: +```c +bekalue@BEKALU-PC:~/c/0x00$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -o quote 101-quote.c +bekalue@BEKALU-PC:~/c/0x00$ ./quote +and that piece of art is useful" - Dora Korpar, 2015-10-19 +bekalue@BEKALU-PC:~/c/0x00$ echo $? +1 +bekalue@BEKALU-PC:~/c/0x00$ ./quote 2> q +bekalue@BEKALU-PC:~/c/0x00$ cat q +and that piece of art is useful" - Dora Korpar, 2015-10-19 +bekalue@BEKALU-PC:~/c/0x00$ grep printf < 101-quote.c +bekalue@BEKALU-PC:~/c/0x00$ grep put < 101-quote.c +bekalue@BEKALU-PC:~/c/0x00$ +``` diff --git a/0x01-variables_if_else_while/TASKS.md b/0x01-variables_if_else_while/TASKS.md new file mode 100644 index 00000000..8611a264 --- /dev/null +++ b/0x01-variables_if_else_while/TASKS.md @@ -0,0 +1,168 @@ +# 0x01-variables_if_else_while +> project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Arithmetic Operators in C](https://www.tutorialspoint.com/cprogramming/c_arithmetic_operators.htm) +* What are the arithmetic operators and how to use them + +[Logical Operators](https://fresh2refresh.com/c-programming/c-operators-expressions/c-logical-operators/) +* What are the logical operators (sometimes called boolean operators) and how to use them + +[Relational Operators](https://www.tutorialspoint.com/cprogramming/c_relational_operators.htm) +* What the the relational operators and how to use them +* What values are considered `TRUE` and `FALSE` in C (0 vs any other value) +* What are the boolean operators and how to use them + +[If](https://www.cprogramming.com/tutorial/c/lesson2.html) [If Else](https://www.tutorialspoint.com/cprogramming/if_else_statement_in_c.htm) +* How to use the `if`, `if ... else` statements +* How to use comments + +[Variables](http://publications.gbdirect.co.uk/c_book/chapter2/keywords_and_identifiers.html) +* How to declare variables of types `char`, `int`, `unsigned int` +* How to assign values to variables +* How to print the values of variables of type `char`, `int`, `unsigned int` with `printf` + +[While](https://www.youtube.com/watch?v=Ju1LYO9pkaI) +* How to use the `while` loop +* How to use variables with the `while` loop +* How to print variables using `printf` + +[ASCII](https://man7.org/linux/man-pages/man7/ascii.7.html) +* What is the `ASCII` character set +* What are the purpose of the `gcc` flags `-m32` and `-m64` + +## Tasks Completed + ++ [x] 0\. Positive anything is better than negative nothing
_**[0-positive_or_negative.c](0-positive_or_negative.c)**_ assigns a random number to the variable `n` each time it is executed and prints whether the number stored in the variable `n` is positive or negative.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-positive_or_negative.c -o 0-positive_or_negative +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +-520693284 is negative +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +-973398895 is negative +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +-199220452 is negative +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +561319348 is positive +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +561319348 is positive +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +266853958 is positive +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +-48147767 is negative +bekalue@BEKALU-PC:~/0x01$ ./0-positive_or_negative +0 is zero +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 1\. The last digit
_**[1-last_digit.c](1-last_digit.c)**_ assigns a random number to the variable `n` each time it is executed and prints the last digit of the number stored in the variable `n`.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-last_digit.c -o 1-last_digit +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 629438752 is 2 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of -748255693 is -3 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of -1052791662 is -2 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of -284805734 is -4 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of -284805734 is -4 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 491506926 is 6 and is greater than 5 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 954249937 is 7 and is greater than 5 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 652334952 is 2 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of -729688197 is -7 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of -729688197 is -7 and is less than 6 and not 0 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 45528266 is 6 and is greater than 5 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 45528266 is 6 and is greater than 5 +bekalue@BEKALU-PC:~/0x01$ ./1-last_digit +Last digit of 809065140 is 0 and is 0 +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 2\. I sometimes suffer from insomnia. And when I can't fall asleep, I play what I call the alphabet game
_**[2-print_alphabet.c](2-print_alphabet.c)**_ prints the alphabet in lowercase, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-print_alphabet.c -o 2-print_alphabet +bekalue@BEKALU-PC:~/0x01$ ./2-print_alphabet +abcdefghijklmnopqrstuvwxyz +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 3\. alphABET
_**[3-print_alphabets.c](3-print_alphabets.c)**_ prints the alphabet in lowercase, and then in uppercase, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-print_alphabets.c -o 3-print_alphabets +bekalue@BEKALU-PC:~/0x01$ ./3-print_alphabets | cat -e +abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$ +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 4\. When I was having that alphabet soup, I never thought that it would pay off
_**[4-print_alphabt.c](4-print_alphabt.c)**_ prints the alphabet in lowercase, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-print_alphabt.c -o 4-print_alphabt +bekalue@BEKALU-PC:~/0x01$ ./4-print_alphabt +abcdfghijklmnoprstuvwxyz +bekalue@BEKALU-PC:~/0x01$ ./4-print_alphabt | grep [eq] +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 5\. Numbers
_**[5-print_numbers.c](5-print_numbers.c)**_ prints all single digit numbers of base 10 starting from `0`, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-print_numbers.c -o 5-print_numbers +bekalue@BEKALU-PC:~/0x01$ ./5-print_numbers +0123456789 +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 6\. Numberz
_**[6-print_numberz.c](6-print_numberz.c)**_ prints all single digit numbers of base 10 starting from `0`, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 6-print_numberz.c -o 6-print_numberz +bekalue@BEKALU-PC:~/0x01$ ./6-print_numberz +0123456789 +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 7\. Smile in the mirror
_**[7-print_tebahpla.c](7-print_tebahpla.c)**_ prints the lowercase alphabet in reverse, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 7-print_tebahpla.c -o 7-print_tebahpla +bekalue@BEKALU-PC:~/0x01$ ./7-print_tebahpla +zyxwvutsrqponmlkjihgfedcba +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 8\. Hexadecimal
_**[8-print_base16.c](8-print_base16.c)**_ prints all the numbers of base 16 in lowercase, followed by a new line.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-print_base16.c -o 8-print_base16 +bekalue@BEKALU-PC:~/0x01$ ./8-print_base16 +0123456789abcdef +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 9\. Patience, persistence and perspiration make an unbeatable combination for success
_**[9-print_comb.c](9-print_comb.c)**_ prints all possible combinations of single-digit numbers.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 9-print_comb.c -o 9-print_comb +bekalue@BEKALU-PC:~/0x01$ ./9-print_comb | cat -e +0, 1, 2, 3, 4, 5, 6, 7, 8, 9$ +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 10\. Inventing is a combination of brains and materials. The more brains you use, the less material you need
_**[100-print_comb3.c](100-print_comb3.c)**_ prints all possible different combinations of two digits.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-print_comb3.c -o 100-print_comb3 +bekalue@BEKALU-PC:~/0x01$ ./100-print_comb3 +01, 02, 03, 04, 05, 06, 07, 08, 09, 12, 13, 14, 15, 16, 17, 18, 19, 23, 24, 25, 26, 27, 28, 29, 34, 35, 36, 37, 38, 39, 45, 46, 47, 48, 49, 56, 57, 58, 59, 67, 68, 69, 78, 79, 89 +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 11\. The success combination in business is: Do what you do better... and: do more of what you do...
_**[101-print_comb4.c](101-print_comb4.c)**_ prints all possible different combinations of three digits.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-print_comb4.c -o 101-print_comb4 +bekalue@BEKALU-PC:~/0x01$ ./101-print_comb4 +012, 013, 014, 015, 016, 017, 018, 019, 023, 024, 025, 026, 027, 028, 029, 034, 035, 036, 037, 038, 039, 045, 046, 047, 048, 049, 056, 057, 058, 059, 067, 068, 069, 078, 079, 089, 123, 124, 125, 126, 127, 128, 129, 134, 135, 136, 137, 138, 139, 145, 146, 147, 148, 149, 156, 157, 158, 159, 167, 168, 169, 178, 179, 189, 234, 235, 236, 237, 238, 239, 245, 246, 247, 248, 249, 256, 257, 258, 259, 267, 268, 269, 278, 279, 289, 345, 346, 347, 348, 349, 356, 357, 358, 359, 367, 368, 369, 378, 379, 389, 456, 457, 458, 459, 467, 468, 469, 478, 479, 489, 567, 568, 569, 578, 579, 589, 678, 679, 689, 789 +bekalue@BEKALU-PC:~/0x01$ +``` ++ [x] 12\. Software is eating the World
_**[102-print_comb5.c](102-print_comb5.c)**_ prints all possible combinations of two two-digit numbers.
__Example__: +```c +bekalue@BEKALU-PC:~/0x01$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 102-print_comb5.c -o 102-print_comb5 +bekalue@BEKALU-PC:~/0x01$ ./102-print_comb5 +00 01, 00 02, 00 03, 00 04, 00 05, 00 06, 00 07, 00 08, 00 09, 00 10, 00 11, [...] 40 91, 40 92, 40 93, 40 94, 40 95, 40 96, 40 97, 40 98, 40 99, 41 42, 41 43, 41 44, 41 45, 41 46, 41 47, 41 48, 41 49, 41 50, 41 51, 41 52, 41 53 [...] 93 95, 93 96, 93 97, 93 98, 93 99, 94 95, 94 96, 94 97, 94 98, 94 99, 95 96, 95 97, 95 98, 95 99, 96 97, 96 98, 96 99, 97 98, 97 99, 98 99 +``` diff --git a/0x02-functions_nested_loops/TASKS.md b/0x02-functions_nested_loops/TASKS.md new file mode 100644 index 00000000..38d4cf57 --- /dev/null +++ b/0x02-functions_nested_loops/TASKS.md @@ -0,0 +1,235 @@ +# 0x02. C - Functions, nested loops +>Project Done in 1 Day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Nested Loops](https://www.youtube.com/watch?v=Z3iGeQ1gIss) +* What are nested loops and how to use them + +[Functions](http://www.tutorialspoint.com/cprogramming/c_functions.htm), [Helpful Video](https://www.youtube.com/watch?v=qMlnFwYdqIw) +* What is a function and how do you use a function? +* What is the difference between a declaration and a defintion of a function? +* Scope of variables + +[Function Prototype](https://www.geeksforgeeks.org/what-is-the-purpose-of-a-function-prototype/) +* What is a prototype + +[GCC](https://linux.die.net/man/1/gcc) +* What are the `gcc` flags `-Wall` `-Werror` `-pedantic` `-Wextra` + +[Headers](https://www.tutorialspoint.com/cprogramming/c_header_files.htm) +* What are header files and how to use them with `#include` + +## Tasks Completed + ++ [x] 0\. _putchar_
_**[0-putchar.c](0-putchar.c)**_ prints `_putchar`, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 0-putchar.c -o 0-putchar +julien@ubuntu:~/0x02$ ./0-putchar +_putchar +julien@ubuntu:~/0x02$ +``` ++ [x] 1\. I sometimes suffer from insomnia. And when I can't fall asleep, I play what I call the alphabet game
_**[1-alphabet.c](1-alphabet.c)**_ prints the alphabet, in lowercase, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 1-main.c 1-alphabet.c -o 1-alphabet +julien@ubuntu:~/0x02$ ./1-alphabet +abcdefghijklmnopqrstuvwxyz +julien@ubuntu:~/0x02$ +``` ++ [x] 2\. 10 x alphabet
_**[2-print_alphabet_x10.c](2-print_alphabet_x10.c)**_ prints 10 times the alphabet, in lowercase, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 2-main.c 2-print_alphabet_x10.c -o 2-alphabet_x10 +julien@ubuntu:~/0x02$ ./2-alphabet_x10 +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +abcdefghijklmnopqrstuvwxyz +julien@ubuntu:~/0x02$ +``` ++ [x] 3\. islower
_**[3-islower.c](3-islower.c)**_ checks for lowercase character.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 3-main.c 3-islower.c -o 3-islower +julien@ubuntu:~/0x02$ ./3-islower +011 +julien@ubuntu:~/0x02$ +``` ++ [x] 4\. isalpha
_**[4-isalpha.c](4-isalpha.c)**_ checks for alphabetic character.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 4-main.c 4-isalpha.c -o 4-isalpha +julien@ubuntu:~/0x02$ ./4-isalpha +1110 +julien@ubuntu:~/0x02$ +``` ++ [x] 5\. Sign
_**[5-sign.c](5-sign.c)**_ prints the sign of a number.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 5-main.c 5-sign.c -o 5-sign +julien@ubuntu:~/0x02$ ./5-sign ++, 1 +0, 0 ++, 1 +-, / +julien@ubuntu:~/0x02$ +``` ++ [x] 6\. There is no such thing as absolute value in this world. You can only estimate what a thing is worth to you
_**[6-abs.c](6-abs.c)**_ computes the absolute value of an integer.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 6-main.c 6-abs.c -o 6-abs +julien@ubuntu:~/0x02$ ./6-abs +1 +0 +1 +98 +julien@ubuntu:~/0x02$ +``` ++ [x] 7\. There are only 3 colors, 10 digits, and 7 notes; it's what we do with them that's important
_**[7-print_last_digit.c](7-print_last_digit.c)**_ prints the last digit of a number.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 7-main.c 7-print_last_digit.c -o 7-last_digit +julien@ubuntu:~/0x02$ ./7-last_digit +8044 +julien@ubuntu:~/0x02$ +``` ++ [x] 8\. I'm federal agent Jack Bauer, and today is the longest day of my life
_**[8-24_hours.c](8-24_hours.c)**_ prints every minute of the day of Jack Bauer, starting from 00:00 to 23:59.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 8-main.c 8-24_hours.c -o 8-24 +julien@ubuntu:~/0x02$ ./8-24 | head +00:00 +00:01 +00:02 +00:03 +00:04 +00:05 +00:06 +00:07 +00:08 +00:09 +julien@ubuntu:~/0x02$ ./8-24 | tail +23:50 +23:51 +23:52 +23:53 +23:54 +23:55 +23:56 +23:57 +23:58 +23:59 +julien@ubuntu:~/0x02$ ./8-24 | wc -l +1440 +julien@ubuntu:~/0x02$ +``` ++ [x] 9\. Learn your times table
_**[9-times_table.c](9-times_table.c)**_ prints the 9 times table, starting with 0.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 9-main.c 9-times_table.c -o 9-times_table +ulien@ubuntu:~/0x02$ ./9-times_table | cat -e +0, 0, 0, 0, 0, 0, 0, 0, 0, 0$ +0, 1, 2, 3, 4, 5, 6, 7, 8, 9$ +0, 2, 4, 6, 8, 10, 12, 14, 16, 18$ +0, 3, 6, 9, 12, 15, 18, 21, 24, 27$ +0, 4, 8, 12, 16, 20, 24, 28, 32, 36$ +0, 5, 10, 15, 20, 25, 30, 35, 40, 45$ +0, 6, 12, 18, 24, 30, 36, 42, 48, 54$ +0, 7, 14, 21, 28, 35, 42, 49, 56, 63$ +0, 8, 16, 24, 32, 40, 48, 56, 64, 72$ +0, 9, 18, 27, 36, 45, 54, 63, 72, 81$ +julien@ubuntu:~/0x02$ ./9-times_table | tr ' ' . | cat -e +0,..0,..0,..0,..0,..0,..0,..0,..0,..0$ +0,..1,..2,..3,..4,..5,..6,..7,..8,..9$ +0,..2,..4,..6,..8,.10,.12,.14,.16,.18$ +0,..3,..6,..9,.12,.15,.18,.21,.24,.27$ +0,..4,..8,.12,.16,.20,.24,.28,.32,.36$ +0,..5,.10,.15,.20,.25,.30,.35,.40,.45$ +0,..6,.12,.18,.24,.30,.36,.42,.48,.54$ +0,..7,.14,.21,.28,.35,.42,.49,.56,.63$ +0,..8,.16,.24,.32,.40,.48,.56,.64,.72$ +0,..9,.18,.27,.36,.45,.54,.63,.72,.81$ +julien@ubuntu:~/0x02$ +``` ++ [x] 10\. a + b
_**[10-add.c](10-add.c)**_ adds two integers and returns the result.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 10-main.c 10-add.c -o 10-add +julien@ubuntu:~/0x02$ ./10-add +98 +julien@ubuntu:~/0x02$ +``` ++ [x] 11\. 98 Battery Street, the OG
_**[11-print_to_98.c](11-print_to_98.c)**_ prints all natural numbers from `n` to `98`, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 11-main.c 11-print_to_98.c -o 11-98 +julien@ubuntu:~/0x02$ ./11-98 +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98 +98 +111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, 99, 98 +81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98 +-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98 +julien@ubuntu:~/0x02$ +``` ++ [x] 12\. The World looks like a multiplication-table, or a mathematical equation, which, turn it how you will, balances itself
_**100-times_table.c**_ prints the `n` times table, starting with 0.
__Example__: +```c +julien@ubuntu:~/0x02$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 100-main.c 100-times_table.c -o 100-times_table +julien@ubuntu:~/0x02$ ./100-times_table +0, 0, 0, 0 +0, 1, 2, 3 +0, 2, 4, 6 +0, 3, 6, 9 + +0, 0, 0, 0, 0, 0 +0, 1, 2, 3, 4, 5 +0, 2, 4, 6, 8, 10 +0, 3, 6, 9, 12, 15 +0, 4, 8, 12, 16, 20 +0, 5, 10, 15, 20, 25 + + +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 +0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24 +0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36 +0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48 +0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 +0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72 +0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84 +0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96 +0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108 +0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120 +0, 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132 +0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144 +julien@ubuntu:~/0x02$ ./100-times_table | tr ' ' . | cat -e +0,...0,...0,...0$ +0,...1,...2,...3$ +0,...2,...4,...6$ +0,...3,...6,...9$ +$ +0,...0,...0,...0,...0,...0$ +0,...1,...2,...3,...4,...5$ +0,...2,...4,...6,...8,..10$ +0,...3,...6,...9,..12,..15$ +0,...4,...8,..12,..16,..20$ +0,...5,..10,..15,..20,..25$ +$ +$ +0,...0,...0,...0,...0,...0,...0,...0,...0,...0,...0,...0,...0$ +0,...1,...2,...3,...4,...5,...6,...7,...8,...9,..10,..11,..12$ +0,...2,...4,...6,...8,..10,..12,..14,..16,..18,..20,..22,..24$ +0,...3,...6,...9,..12,..15,..18,..21,..24,..27,..30,..33,..36$ +0,...4,...8,..12,..16,..20,..24,..28,..32,..36,..40,..44,..48$ +0,...5,..10,..15,..20,..25,..30,..35,..40,..45,..50,..55,..60$ +0,...6,..12,..18,..24,..30,..36,..42,..48,..54,..60,..66,..72$ +0,...7,..14,..21,..28,..35,..42,..49,..56,..63,..70,..77,..84$ +0,...8,..16,..24,..32,..40,..48,..56,..64,..72,..80,..88,..96$ +0,...9,..18,..27,..36,..45,..54,..63,..72,..81,..90,..99,.108$ +0,..10,..20,..30,..40,..50,..60,..70,..80,..90,.100,.110,.120$ +0,..11,..22,..33,..44,..55,..66,..77,..88,..99,.110,.121,.132$ +0,..12,..24,..36,..48,..60,..72,..84,..96,.108,.120,.132,.144$ +julien@ubuntu:~/0x02$ +``` ++ [x] 13\. Nature made the natural numbers; All else is the work of women
_**[101-natural.c](101-natural.c)**_ prints the sum of all the multiples of `3` or `5` below `1024` (excluded). ++ [x] 14\. In computer class, the first assignment was to write a program to print the first 100 Fibonacci numbers. Instead, I wrote a program that would steal passwords of students. My teacher gave me an A
_**[102-fibonacci.c](102-fibonacci.c)**_ prints the first 50 Fibonacci numbers, starting with `1` and `2`. ++ [x] 15\. Even Liber Abbaci
_**[103-fibonacci.c](103-fibonacci.c)**_ finds and prints the sum of the even-valued terms by considering the terms in the Fibonacci sequence whose values do not exceed 4,000,000. ++ [x] 16\. In computer class, the first assignment was to write a program to print the first 100 Fibonacci numbers. Instead, I wrote a program that would steal passwords of students. My teacher gave me an A+
_**[104-fibonacci.c](104-fibonacci.c)**_ finds and prints the first 98 Fibonacci numbers. diff --git a/0x02-functions_nested_loops/test/TASKS.md b/0x02-functions_nested_loops/test/TASKS.md new file mode 100644 index 00000000..55abe088 --- /dev/null +++ b/0x02-functions_nested_loops/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +> test file for each tasks which contain main function (entry point) diff --git a/0x03-debugging/TASKS.md b/0x03-debugging/TASKS.md new file mode 100644 index 00000000..45fd710e --- /dev/null +++ b/0x03-debugging/TASKS.md @@ -0,0 +1,21 @@ +# 0x03. C - Debugging +>project Done in 3 days + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objective +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What is [debugging](https://en.wikipedia.org/wiki/Debugging) +* What are some [methods of debugging](https://www.thoughtfulcode.com/rubber-duck-debugging-psychology/) manually +* How to read the error messages + +## Compilation + +`gcc`, using the options `-Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. Multiple mains
_**[0-main.c](0-main.c)**_ tests that the function `positive_or_negative()` gives the correct output when given a case of `0`. a function similar to the one we worked with in an [earlier C project](https://github.com/bekalue/alx-low_level_programming/blob/master/0x01-variables_if_else_while/0-positive_or_negative.c) ++ [x] 1\. Like, comment, subscribe
_**[1-main.c](1-main.c)**_ shows a commented out code section that is causing the output to go into an infinite loop. ++ [x] 2\. 0 > 972?
_**[2-largest_number.c](2-largest_number.c)**_ prints the largest of three integers. ++ [x] 3\. Leap year
_**[3-print_remaining_days.c](3-print_remaining_days.c)**_ converts a date to the day of year and determines how many days are left in the year, taking leap year into consideration. diff --git a/0x03-debugging/test/TASKS.md b/0x03-debugging/test/TASKS.md new file mode 100644 index 00000000..36074c64 --- /dev/null +++ b/0x03-debugging/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +> test files for each task which contains main function which is entry point. diff --git a/0x04-more_functions_nested_loops/TASKS.md b/0x04-more_functions_nested_loops/TASKS.md new file mode 100644 index 00000000..3c0c8964 --- /dev/null +++ b/0x04-more_functions_nested_loops/TASKS.md @@ -0,0 +1,178 @@ +# 0x04. C - More functions, more nested loops +> project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are [nested loops](https://www.youtube.com/watch?v=Z3iGeQ1gIss) and how to use them +* What is a [function](https://www.tutorialspoint.com/cprogramming/c_functions.htm) and how do you use [functions](https://www.youtube.com/watch?v=qMlnFwYdqIw) +* What is the difference between a declaration and a definition of a function +* What is a [prototype](https://www.geeksforgeeks.org/what-is-the-purpose-of-a-function-prototype/) +* Scope of variables +* What are the `gcc` flags `-Wall -Werror -pedantic -Wextra -std=gnu89` +* What are [header files](https://www.tutorialspoint.com/cprogramming/c_header_files.htm) and how to to use them with `#include` + +## Compilation + +`gcc -Wall -Werror -pedantic -Wextra -std=gnu89` + +## Tasks Completed + ++ [x] 0\. isupper
_**[0-isupper.c](0-isupper.c)**_ checks for uppercase character.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-isupper.c -o 0-isuper +julien@ubuntu:~/0x04$ ./0-isuper +A: 1 +a: 0 +julien@ubuntu:~/0x04$ +``` ++ [x] 1\. isdigit
_**[1-isdigit.c](1-isdigit.c)**_ checks for a base 10 digit.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-isdigit.c -o 1-isdigit +julien@ubuntu:~/0x04$ ./1-isdigit +0: 1 +a: 0 +julien@ubuntu:~/0x04$ +``` ++ [x] 2\. Collaboration is multiplication
_**[2-mul.c](2-mul.c)**_ multiplies two integers.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-mul.c -o 2-mul +julien@ubuntu:~/0x04$ ./2-mul +100352 +-1646592 +julien@ubuntu:~/0x04$ +``` ++ [x] 3\. The numbers speak for themselves
_**[3-print_numbers.c](3-print_numbers.c)**_ prints the numbers, from `0` to `9`.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 3-main.c 3-print_numbers.c -o 3-print_numbers +julien@ubuntu:~/0x04$ ./3-print_numbers | cat -e +0123456789$ +julien@ubuntu:~/0x04$ +``` ++ [x] 4\. I believe in numbers and signs
_**[4-print_most_numbers.c](4-print_most_numbers.c)**_ prints the numbers, from `0` to `9` except `2` and `4`.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 4-main.c 4-print_most_numbers.c -o 4-print_most_numbers +julien@ubuntu:~/0x04$ ./4-print_most_numbers +01356789 +julien@ubuntu:~/0x04$ +``` ++ [x] 5\. Numbers constitute the only universal language
_**[5-more_numbers.c](5-more_numbers.c)**_ prints 10 times the numbers, from `0` to `14`.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 5-main.c 5-more_numbers.c -o 5-more_numbers +julien@ubuntu:~/0x04$ ./5-more_numbers +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +01234567891011121314 +julien@ubuntu:~/0x04 +``` ++ [x] 6\. The shortest distance between two points is a straight line
_**[6-print_line.c](6-print_line.c)**_ draws a straight line in the terminal.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 6-main.c 6-print_line.c -o 6-lines +julien@ubuntu:~/0x04$ ./6-lines | cat -e +$ +__$ +__________$ +$ +julien@ubuntu:~/0x04$ +``` ++ [x] 7\. I feel like I am diagonally parked in a parallel universe
_**[7-print_diagonal.c](7-print_diagonal.c)**_ draws a diagonal line on the terminal.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 7-main.c 7-print_diagonal.c -o 7-diagonals +julien@ubuntu:~/0x04$ ./7-diagonals | cat -e +$ +\$ + \$ +\$ + \$ + \$ + \$ + \$ + \$ + \$ + \$ + \$ + \$ +$ +julien@ubuntu:~/0x04$ +``` ++ [x] 8\. You are so much sunshine in every square inch
_**[8-print_square.c](8-print_square.c)**_ prints a square.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 8-main.c 8-print_square.c -o 8-squares +julien@ubuntu:~/0x04$ ./8-squares +## +## +########## +########## +########## +########## +########## +########## +########## +########## +########## +########## + +julien@ubuntu:~/0x04$ +``` ++ [x] 9\. Fizz-Buzz
_**[9-fizz_buzz.c](9-fizz_buzz.c)**_ prints the numbers from `1` to `100`, followed by a new line. But for multiples of three print `Fizz` instead of the number and for the multiples of five print `Buzz`. For numbers which are multiples of both three and five print `FizzBuzz`.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 9-fizz_buzz.c -o 9-fizz_buzz +julien@ubuntu:~/0x04$ ./9-fizz_buzz +1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz +julien@ubuntu:~/0x04$ +``` ++ [x] 10\. Triangles
_**[10-print_triangle.c](10-print_triangle.c)**_ prints a triangle.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 10-main.c 10-print_triangle.c -o 10-triangles +julien@ubuntu:~/0x04$ ./10-triangles + # +## + # + ## + ### + #### + ##### + ###### + ####### + ######## + ######### +########## +# + +julien@ubuntu:~/0x04$ ./10-triangles | tr ' ' . | cat -e +.#$ +##$ +.........#$ +........##$ +.......###$ +......####$ +.....#####$ +....######$ +...#######$ +..########$ +.#########$ +##########$ +#$ +$ +julien@ubuntu:~/0x04$ +``` ++ [x] 11\. The problem of distinguishing prime numbers from composite numbers and of resolving the latter into their prime factors is known to be one of the most important and useful in arithmetic
_**[100-prime_factor.c](100-prime_factor.c)**_ prints the largest prime factor of the number `612852475143`. ++ [x] 12\. Numbers have life; they're not just symbols on paper
_**[101-print_number.c](101-print_number.c)**_ prints an integer.
__Example__: +```c +julien@ubuntu:~/0x04$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 101-main.c 101-print_number.c -o 101-print_numbers +julien@ubuntu:~/0x04$ ./101-print_numbers +98 +402 +1024 +0 +-98 +julien@ubuntu:~/0x04$ +``` diff --git a/0x04-more_functions_nested_loops/test/TASKS.md b/0x04-more_functions_nested_loops/test/TASKS.md new file mode 100644 index 00000000..9fd658dd --- /dev/null +++ b/0x04-more_functions_nested_loops/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +This repo contains main function for each task files to test. diff --git a/0x05-pointers_arrays_strings/TASKS.md b/0x05-pointers_arrays_strings/TASKS.md new file mode 100644 index 00000000..deebb756 --- /dev/null +++ b/0x05-pointers_arrays_strings/TASKS.md @@ -0,0 +1,117 @@ +# 0x05-pointers arrays strings +> project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are pointers and how to use them +* What are arrays and how to use them +* What are the differences between [pointers](https://www.tutorialspoint.com/cprogramming/c_pointers.htm) and [arrays](https://www.tutorialspoint.com/cprogramming/c_arrays.htm) +* How to use [strings](https://www.tutorialspoint.com/cprogramming/c_strings.htm) and how to manipulate them +* Scope of variables +* [Memory Layout](https://aticleworld.com/memory-layout-of-c-program/) + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. 98 Battery st.
_**[0-reset_to_98.c](0-reset_to_98.c)**_ contains a function that takes a pointer to an `int` as parameter and updates the value it points to to `98`.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-reset_to_98.c -o 0-98 +julien@ubuntu:~/0x05$ ./0-98 +n=402 +n=98 +julien@ubuntu:~/0x05$ +``` ++ [x] 1\. Don't swap horses in crossing a stream
_**[1-swap.c](1-swap.c)**_ contains a function that swaps the values of two integers.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-swap.c -o 1-swap +julien@ubuntu:~/0x05$ ./1-swap +a=98, b=42 +a=42, b=98 +julien@ubuntu:~/0x05$ +``` ++ [x] 2\. This report, by its very length, defends itself against the risk of being read
_**[2-strlen.c](2-strlen.c)**_ contains a function that returns the length of a string.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-strlen.c -o 2-strlen +julien@ubuntu:~/0x05$ ./2-strlen +16 +julien@ubuntu:~/0x05$ +``` ++ [x] 3\. I do not fear computers. I fear the lack of them
_**[3-puts.c](3-puts.c)**_ contains a function that prints a string, followed by a new line, to `stdout`.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 3-main.c 3-puts.c -o 3-puts +julien@ubuntu:~/0x05$ ./3-puts +I do not fear computers. I fear the lack of them - Isaac Asimov +julien@ubuntu:~/0x05$ +``` ++ [x] 4\. I can only go one way. I've not got a reverse gear
_**[4-print_rev.c](4-print_rev.c)**_ a function that prints a string, in reverse, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 4-main.c 4-print_rev.c -o 4-print_rev +julien@ubuntu:~/0x05$ ./4-print_rev +vomisA caasI - meht fo kcal eht raef I .sretupmoc raef ton od I +julien@ubuntu:~/0x05$ +``` ++ [x] 5\. A good engineer thinks in reverse and asks himself about the stylistic consequences of the components and systems he proposes
_**[5-rev_string.c](5-rev_string.c)**_ contains a function that reverses a string.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-rev_string.c -o 5-rev_string +julien@ubuntu:~/0x05$ ./5-rev_string +My School +loohcS yM +julien@ubuntu:~/0x05$ +``` ++ [x] 6\. Half the lies they tell about me aren't true
_**[6-puts2.c](6-puts2.c)**_ contains a function that prints every other character of a string, starting with the first character, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 6-main.c 6-puts2.c -o 6-puts2 +julien@ubuntu:~/0x05$ ./6-puts2 +02468 +julien@ubuntu:~/0x05$ +``` ++ [x] 7\. Winning is only half of it. Having fun is the other half
_**[7-puts_half.c](7-puts_half.c)**_ contains a function that prints half of a string, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 7-main.c 7-puts_half.c -o 7-puts_half +julien@ubuntu:~/0x05$ ./7-puts_half +56789 +julien@ubuntu:~/0x05$ +``` ++ [x] 8\. Arrays are not pointers
_**[8-print_array.c](8-print_array.c)**_ contains a function that prints `n` elements of an array of integers, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-main.c 8-print_array.c -o 8-print_array +julien@ubuntu:~/0x05$ ./8-print_array +98, 402, -198, 298, -1024 +julien@ubuntu:~/0x05$ +``` ++ [x] 9\. strcpy
_**[9-strcpy.c](9-strcpy.c)**_ contains a function that copies the string pointed to by `src`, including the terminating null byte (`\0`), to the buffer pointed to by `dest`.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 9-main.c 9-strcpy.c -o 9-strcpy +julien@ubuntu:~/0x05$ ./9-strcpy +First, solve the problem. Then, write the code +First, solve the problem. Then, write the code +julien@ubuntu:~/0x05$ +``` ++ [x] 10\. Great leaders are willing to sacrifice the numbers to save the people. Poor leaders sacrifice the people to save the numbers
_**[100-atoi.c](100-atoi.c)**_ contains a function that converts a string to an integer.
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 -fsanitize=signed-integer-overflow 100-main.c 100-atoi.c -o 100-atoi +julien@ubuntu:~/0x05$ ./100-atoi +98 +-402 +-98 +214748364 +0 +402 +98 +402 +julien@ubuntu:~/0x05$ +``` ++ [x] 11\. Don't hate the hacker, hate the code
_**[101-keygen.c](101-keygen.c)**_ generates random valid passwords for the program [101-crackme](https://github.com/holbertonschool/0x04.c). + + man `srand`, `rand`, `time` + + `gdb` and `objdump` can help
__Example__: +```c +julien@ubuntu:~/0x05$ gcc -Wall -pedantic -Werror -Wextra 101-keygen.c -o 101-keygen +julien@ubuntu:~/0x05$ ./101-crackme "`./101-keygen`" +Tada! Congrats +julien@ubuntu:~/0x05$ +``` diff --git a/0x06-pointers_arrays_strings/TASKS.md b/0x06-pointers_arrays_strings/TASKS.md new file mode 100644 index 00000000..3e719dfa --- /dev/null +++ b/0x06-pointers_arrays_strings/TASKS.md @@ -0,0 +1,509 @@ +# 0x06-pointers arrays strings +> project done in 2 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objective +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are pointers and how to use them +* What are arrays and how to use them +* What are the differences between pointers and arrays +* How to use strings and how to manipulate them +* Scope of variables + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. strcat
_**[0-strcat.c](0-strcat.c)**_ contains a function that concatenates two strings.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 0-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char s1[98] = "Hello "; + char s2[] = "World!\n"; + char *ptr; + + printf("%s\n", s1); + printf("%s", s2); + ptr = _strcat(s1, s2); + printf("%s", s1); + printf("%s", s2); + printf("%s", ptr); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-strcat.c -o 0-strcat +julien@ubuntu:~/0x06$ ./0-strcat +Hello +World! +Hello World! +World! +Hello World! +julien@ubuntu:~/0x06$ +``` ++ [x] 1\. strncat
_**[1-strncat.c](1-strncat.c)**_ contains a function that concatenates two strings.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 1-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char s1[98] = "Hello "; + char s2[] = "World!\n"; + char *ptr; + + printf("%s\n", s1); + printf("%s", s2); + ptr = _strncat(s1, s2, 1); + printf("%s\n", s1); + printf("%s", s2); + printf("%s\n", ptr); + ptr = _strncat(s1, s2, 1024); + printf("%s", s1); + printf("%s", s2); + printf("%s", ptr); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-strncat.c -o 1-strncat +julien@ubuntu:~/0x06$ ./1-strncat +Hello +World! +Hello W +World! +Hello W +Hello WWorld! +World! +Hello WWorld! +julien@ubuntu:~/0x06$ +``` ++ [x] 2\. strncpy
_**[2-strncpy.c](2-strncpy.c)**_ contains a function that copies a string.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 2-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char s1[98]; + char *ptr; + int i; + + for (i = 0; i < 98 - 1; i++) + { + s1[i] = '*'; + } + s1[i] = '\0'; + printf("%s\n", s1); + ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 5); + printf("%s\n", s1); + printf("%s\n", ptr); + ptr = _strncpy(s1, "First, solve the problem. Then, write the code\n", 90); + printf("%s", s1); + printf("%s", ptr); + for (i = 0; i < 98; i++) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", s1[i]); + } + printf("\n"); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-strncpy.c -o 2-strncpy +julien@ubuntu:~/0x06$ ./2-strncpy +************************************************************************************************* +First******************************************************************************************** +First******************************************************************************************** +First, solve the problem. Then, write the code +First, solve the problem. Then, write the code +0x46 0x69 0x72 0x73 0x74 0x2c 0x20 0x73 0x6f 0x6c +0x76 0x65 0x20 0x74 0x68 0x65 0x20 0x70 0x72 0x6f +0x62 0x6c 0x65 0x6d 0x2e 0x20 0x54 0x68 0x65 0x6e +0x2c 0x20 0x77 0x72 0x69 0x74 0x65 0x20 0x74 0x68 +0x65 0x20 0x63 0x6f 0x64 0x65 0x0a 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x2a 0x00 +julien@ubuntu:~/0x06$ +``` ++ [x] 3\. strcmp
_**[3-strcmp.c](3-strcmp.c)**_ contains a function that compares two strings.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 3-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char s1[] = "Hello"; + char s2[] = "World!"; + + printf("%d\n", _strcmp(s1, s2)); + printf("%d\n", _strcmp(s2, s1)); + printf("%d\n", _strcmp(s1, s1)); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-strcmp.c -o 3-strcmp +julien@ubuntu:~/0x06$ ./3-strcmp +-15 +15 +0 +julien@ubuntu:~/0x06$ +``` ++ [x] 4\. I am a kind of paranoid in reverse. I suspect people of plotting to make me happy
_**[4-rev_array.c](4-rev_array.c)**_ contains a function that reverses the content of an array of integers.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 4-main.c +#include "main.h" +#include + +/** + * main - check the code + * @a: an array of integers + * @n: the number of elements to swap + * + * Return: nothing. + */ +void print_array(int *a, int n) +{ + int i; + + i = 0; + while (i < n) + { + if (i != 0) + { + printf(", "); + } + printf("%d", a[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 1024, 1337}; + + print_array(a, sizeof(a) / sizeof(int)); + reverse_array(a, sizeof(a) / sizeof(int)); + print_array(a, sizeof(a) / sizeof(int)); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-rev_array.c -o 4-rev_array +julien@ubuntu:~/0x06$ ./4-rev_array +0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 98, 1024, 1337 +1337, 1024, 98, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +julien@ubuntu:~/0x06$ +``` ++ [x] 5\. Always look up
_**[5-string_toupper.c](5-string_toupper.c)**_ contains a function that changes all lowercase letters of a string to uppercase.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 5-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char str[] = "Look up!\n"; + char *ptr; + + ptr = string_toupper(str); + printf("%s", ptr); + printf("%s", str); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-string_toupper.c -o 5-string_toupper +julien@ubuntu:~/0x06$ ./5-string_toupper +LOOK UP! +LOOK UP! +julien@ubuntu:~/0x06$ +``` ++ [x] 6\. Expect the best. Prepare for the worst. Capitalize on what comes
_**[6-cap_string.c](6-cap_string.c)**_ contains a function that capitalizes all words of a string.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 6-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char str[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n"; + char *ptr; + + ptr = cap_string(str); + printf("%s", ptr); + printf("%s", str); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 6-main.c 6-cap_string.c -o 6-cap +julien@ubuntu:~/0x06$ ./6-cap +Expect The Best. Prepare For The Worst. Capitalize On What Comes. +Hello World! Hello-world 0123456hello World Hello World.Hello World +Expect The Best. Prepare For The Worst. Capitalize On What Comes. +Hello World! Hello-world 0123456hello World Hello World.Hello World +julien@ubuntu:~/0x06$ +``` ++ [x] 7\. Mozart composed his music not for the elite, but for everybody
_**[7-leet.c](7-leet.c)**_ contains a function that encodes a string into `1337`.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 7-main.c +#include "main.h" +#include + +/** + * main - check the code for + * + * Return: Always 0. + */ +int main(void) +{ + char s[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\n"; + char *p; + + p = leet(s); + printf("%s", p); + printf("%s", s); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 7-main.c 7-leet.c -o 7-1337 +julien@ubuntu:~/0x06$ ./7-1337 +3xp3c7 7h3 b3s7. Pr3p4r3 f0r 7h3 w0rs7. C4pi741iz3 0n wh47 c0m3s. +3xp3c7 7h3 b3s7. Pr3p4r3 f0r 7h3 w0rs7. C4pi741iz3 0n wh47 c0m3s. +julien@ubuntu:~/0x06$ +``` ++ [x] 8\. rot13
_**[100-rot13.c](100-rot13.c)**_ contains a function that encodes a string using [`rot13`](https://en.wikipedia.org/wiki/ROT13).
__Example__: +```c +julien@ubuntu:~/0x06$ cat 100-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char s[] = "ROT13 (\"rotate by 13 places\", sometimes hyphenated ROT-13) is a simple letter substitution cipher.\n"; + char *p; + + p = rot13(s); + printf("%s", p); + printf("------------------------------------\n"); + printf("%s", s); + printf("------------------------------------\n"); + p = rot13(s); + printf("%s", p); + printf("------------------------------------\n"); + printf("%s", s); + printf("------------------------------------\n"); + p = rot13(s); + printf("%s", p); + printf("------------------------------------\n"); + printf("%s", s); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-rot13.c -o 100-rot13 +julien@ubuntu:~/0x06$ ./100-rot13 +EBG13 ("ebgngr ol 13 cynprf", fbzrgvzrf ulcurangrq EBG-13) vf n fvzcyr yrggre fhofgvghgvba pvcure. +------------------------------------ +EBG13 ("ebgngr ol 13 cynprf", fbzrgvzrf ulcurangrq EBG-13) vf n fvzcyr yrggre fhofgvghgvba pvcure. +------------------------------------ +ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher. +------------------------------------ +ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher. +------------------------------------ +EBG13 ("ebgngr ol 13 cynprf", fbzrgvzrf ulcurangrq EBG-13) vf n fvzcyr yrggre fhofgvghgvba pvcure. +------------------------------------ +EBG13 ("ebgngr ol 13 cynprf", fbzrgvzrf ulcurangrq EBG-13) vf n fvzcyr yrggre fhofgvghgvba pvcure. +julien@ubuntu:~/0x06$ +``` ++ [x] 9\. Numbers have life; they're not just symbols on paper
_**[101-print_number.c](101-print_number.c)**_ contains a function that prints an integer.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 101-main.c +#include "main.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + print_number(98); + _putchar('\n'); + print_number(402); + _putchar('\n'); + print_number(1024); + _putchar('\n'); + print_number(0); + _putchar('\n'); + print_number(-98); + _putchar('\n'); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 101-main.c 101-print_number.c -o 101-print_numbers +julien@ubuntu:~/0x06$ ./101-print_numbers +98 +402 +1024 +0 +-98 +julien@ubuntu:~/0x06$ +``` ++ [x] 10\. A dream doesn't become reality through magic; it takes sweat, determination and hard work
_**[102-magic.c](102-magic.c)**_ adds one line to this [code](https://github.com/holbertonschool/make_magic_happen/blob/master/magic.c), so that the program prints `a[2] = 98`, followed by a new line. ++ [x] 11\. It is the addition of strangeness to beauty that constitutes the romantic character in art
_**[103-infinite_add.c](103-infinite_add.c)**_ contains a function that adds two numbers.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 103-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *n = "1234567892434574367823574575678477685785645685876876774586734734563456453743756756784458"; + char *m = "9034790663470697234682914569346259634958693246597324659762347956349265983465962349569346"; + char r[100]; + char r2[10]; + char r3[11]; + char *res; + + res = infinite_add(n, m, r, 100); + if (res == 0) + { + printf("Error\n"); + } + else + { + printf("%s + %s = %s\n", n, m, res); + } + n = "1234567890"; + m = "1"; + res = infinite_add(n, m, r2, 10); + if (res == 0) + { + printf("Error\n"); + } + else + { + printf("%s + %s = %s\n", n, m, res); + } + n = "999999999"; + m = "1"; + res = infinite_add(n, m, r2, 10); + if (res == 0) + { + printf("Error\n"); + } + else + { + printf("%s + %s = %s\n", n, m, res); + } + res = infinite_add(n, m, r3, 11); + if (res == 0) + { + printf("Error\n"); + } + else + { + printf("%s + %s = %s\n", n, m, res); + } + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 103-main.c 103-infinite_add.c -o 103-add +julien@ubuntu:~/0x06$ ./103-add +1234567892434574367823574575678477685785645685876876774586734734563456453743756756784458 + 9034790663470697234682914569346259634958693246597324659762347956349265983465962349569346 = 10269358555905271602506489145024737320744338932474201434349082690912722437209719106353804 +Error +Error +999999999 + 1 = 1000000000 +julien@ubuntu:~/0x06$ +``` ++ [x] 12\. Noise is a buffer, more effective than cubicles or booth walls
_**[104-print_buffer.c](104-print_buffer.c)**_ contains a function that prints a buffer.
__Example__: +```c +julien@ubuntu:~/0x06$ cat 104-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char buffer[] = "This is a string!\0And this is the rest of the #buffer :)\1\2\3\4\5\6\7#cisfun\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x20\x21\x34\x56#pointersarefun #infernumisfun\n"; + + printf("%s\n", buffer); + printf("---------------------------------\n"); + print_buffer(buffer, sizeof(buffer)); + return (0); +} +julien@ubuntu:~/0x06$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 104-main.c 104-print_buffer.c -o 104-buffer +julien@ubuntu:~/0x06$ ./104-buffer +This is a string! +--------------------------------- +00000000: 5468 6973 2069 7320 6120 This is a +0000000a: 7374 7269 6e67 2100 416e string!.An +00000014: 6420 7468 6973 2069 7320 d this is +0000001e: 7468 6520 7265 7374 206f the rest o +00000028: 6620 7468 6520 2362 7566 f the #buf +00000032: 6665 7220 3a29 0102 0304 fer :).... +0000003c: 0506 0723 6369 7366 756e ...#cisfun +00000046: 0a00 0000 0000 0000 0000 .......... +00000050: 0000 0000 0000 0000 0000 .......... +0000005a: 2021 3456 2370 6f69 6e74 !4V#point +00000064: 6572 7361 7265 6675 6e20 ersarefun +0000006e: 2369 6e66 6572 6e75 6d69 #infernumi +00000078: 7366 756e 0a00 sfun.. +julien@ubuntu:~/0x06$ +``` + diff --git a/0x07-pointers_arrays_strings/TASKS.md b/0x07-pointers_arrays_strings/TASKS.md new file mode 100644 index 00000000..1f666d7d --- /dev/null +++ b/0x07-pointers_arrays_strings/TASKS.md @@ -0,0 +1,379 @@ +# 0x07-pointers arrays strings project +> project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are [pointers to pointers](https://www.tutorialspoint.com/cprogramming/c_pointer_to_pointer.htm) and how to use [them](https://beginnersbook.com/2014/01/c-pointer-to-pointer/) +* What are [multidimensional arrays](https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm) and how to use [them](https://beginnersbook.com/2014/01/2d-arrays-in-c-example/) +* What are the most common C standard library functions to manipulate strings + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. memset
_**[0-memset.c](0-memset.c)**_ contains a function that fills memory with a constant byte.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 0-main.c +#include "main.h" +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(char *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char buffer[98] = {0x00}; + + simple_print_buffer(buffer, 98); + _memset(buffer, 0x01, 95); + printf("-------------------------------------------------\n"); + simple_print_buffer(buffer, 98); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-memset.c -o 0-memset +julien@ubuntu:~/0x07$ ./0-memset +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +------------------------------------------------- +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 0x01 +0x01 0x01 0x01 0x01 0x01 0x00 0x00 0x00 +julien@ubuntu:~/0x07$ +``` ++ [x] 1\. memcpy
_**[1-memcpy.c](1-memcpy.c)**_ contains a function that copies memory area.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 1-main.c +#include "main.h" +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(char *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char buffer[98] = {0}; + char buffer2[98] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; + + simple_print_buffer(buffer, 98); + _memcpy(buffer + 50, buffer2, 10); + printf("-------------------------------------------------\n"); + simple_print_buffer(buffer, 98); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-memcpy.c -o 1-memcpy +julien@ubuntu:~/0x07$ ./1-memcpy +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +------------------------------------------------- +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x01 0x02 0x03 0x04 0x05 0x07 0x07 0x08 0x09 0x0a +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +julien@ubuntu:~/0x07$ +``` ++ [x] 2\. strchr
_**[2-strchr.c](2-strchr.c)**_ contains a function that locates a character in a string.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 2-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *s = "hello"; + char *f; + + f = _strchr(s, 'l'); + + if (f != NULL) + { + printf("%s\n", f); + } + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-strchr.c -o 2-strchr +julien@ubuntu:~/0x07$ ./2-strchr +llo +julien@ubuntu:~/0x07$ +``` ++ [x] 3\. strspn
_**[3-strspn.c](3-strspn.c)**_ contains a function that gets the length of a prefix substring.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 3-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *s = "hello, world"; + char *f = "oleh"; + unsigned int n; + + n = _strspn(s, f); + printf("%u\n", n); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-strspn.c -o 3-strspn +julien@ubuntu:~/0x07$ ./3-strspn +5 +julien@ubuntu:~/0x07$ +``` ++ [x] 4\. strpbrk
_**[4-strpbrk.c](4-strpbrk.c)**_ contains a function that searches a string for any of a set of bytes.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 4-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *s = "hello, world"; + char *f = "world"; + char *t; + + t = _strpbrk(s, f); + printf("%s\n", t); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-strpbrk.c -o 4-strpbrk +julien@ubuntu:~/0x07$ ./4-strpbrk +llo, world +julien@ubuntu:~/0x07$ +``` ++ [x] 5\. strstr
_**[5-strstr.c](5-strstr.c)**_ contains a function that locates a substring.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 5-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *s = "hello, world"; + char *f = "world"; + char *t; + + t = _strstr(s, f); + printf("%s\n", t); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-strstr.c -o 5-strstr +julien@ubuntu:~/0x07$ ./5-strstr +world +julien@ubuntu:~/0x07$ +``` ++ [x] 6\. Chess is mental torture
_**[7-print_chessboard.c](7-print_chessboard.c)**_ contains a function that prints the chessboard.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 7-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char board[8][8] = { + {'r', 'k', 'b', 'q', 'k', 'b', 'k', 'r'}, + {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'}, + {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, + {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, + {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, + {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, + {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'}, + {'R', 'K', 'B', 'Q', 'K', 'B', 'K', 'R'}, + }; + print_chessboard(board); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 7-main.c 7-print_chessboard.c -o 7-print_chessboard +julien@ubuntu:~/0x07$ ./7-print_chessboard +rkbqkbkr +pppppppp + + + + +PPPPPPPP +RKBQKBKR +julien@ubuntu:~/0x07$ +``` ++ [x] 7\. The line of life is a ragged diagonal between duty and desire
_**[8-print_diagsums.c](8-print_diagsums.c)**_ contains a function that prints the sum of the two diagonals of a square matrix of integers.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 8-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int c3[3][3] = { + {0, 1, 5}, + {10, 11, 12}, + {1000, 101, 102}, + }; + int c5[5][5] = { + {0, 1, 5, 12124, 1234}, + {10, 11, 12, 123521, 12512}, + {1000, 101, 102, 12545, 214543435}, + {100, 1012451, 11102, 12545, 214543435}, + {10, 12401, 10452, 11542545, 1214543435}, + }; + print_diagsums((int *)c3, 3); + print_diagsums((int *)c5, 5); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-main.c 8-print_diagsums.c -o 8-print_diagsums +julien@ubuntu:~/0x07$ ./8-print_diagsums +113, 1016 +1214556093, 1137318 +julien@ubuntu:~/0x07$ +``` ++ [x] 8\. Double pointer, double fun
_**[100-set_string.c](100-set_string.c)**_ contains a function that sets the value of a pointer to a char.
__Example__: +```c +julien@ubuntu:~/0x07$ cat 100-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *s0 = "Bob Dylan"; + char *s1 = "Robert Allen"; + + printf("%s, %s\n", s0, s1); + set_string(&s1, s0); + printf("%s, %s\n", s0, s1); + return (0); +} +julien@ubuntu:~/0x07$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-set_string.c -o 100-set_string +julien@ubuntu:~/0x07$ ./100-set_string +Bob Dylan, Robert Allen +Bob Dylan, Bob Dylan +julien@ubuntu:~/0x07$ +``` ++ [x] 9\. My primary goal of hacking was the intellectual curiosity, the seduction of adventure
_**[101-crackme_password](101-crackme_password)**_ contains the password for the [crackme2](https://github.com/holbertonschool/0x06.c) executable. + + Your file should contain the exact password, no new line, no extra space + + `ltrace`, `ldd`, `gdb` and `objdump` can help + + You may need to install the `openssl` library to run the `crakme2` program: `sudo apt install libssl-dev` + + Edit the source list `sudo nano /etc/apt/sources.list` to add the following line: `deb http://security.ubuntu.com/ubuntu xenial-security main` Then `sudo apt update` and `sudo apt install libssl1.0.0` + diff --git a/0x08-recursion/TASKS.md b/0x08-recursion/TASKS.md new file mode 100644 index 00000000..48059050 --- /dev/null +++ b/0x08-recursion/TASKS.md @@ -0,0 +1,336 @@ +# 0x08-recursion +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Recursion](https://www.tutorialspoint.com/cprogramming/c_recursion.htm) + +[What on Earth is Recursion](https://www.youtube.com/watch?v=Mv9NEXX1VHc) + +[C Programming Tutorial 85, Recursion pt.1](https://www.youtube.com/watch?v=XGxbXMP6k8k) + +[C Programming Tutorial 86, Recursion pt.2](https://www.youtube.com/watch?v=7XiIS6HobNs) +* What is recursion +* How to implement recursion +* In what situations you should implement recursion +* In what situations you shouldn’t implement recursion + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. She locked away a secret, deep inside herself, something she once knew to be true... but chose to forget
_**[0-puts_recursion.c](0-puts_recursion.c)**_ contains a function that prints a string, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 0-main.c +#include "main.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + _puts_recursion("Puts with recursion"); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 0-main.c 0-puts_recursion.c -o 0-puts_recursion +julien@ubuntu:~/0x08. Recursion$ ./0-puts_recursion +Puts with recursion +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 1\. Why is it so important to dream? Because, in my dreams we are together
_**[1-print_rev_recursion.c](1-print_rev_recursion.c)**_ contains a function that prints a string in reverse.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 1-main.c +#include "main.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + _print_rev_recursion("\nColton Walker"); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 1-main.c 1-print_rev_recursion.c -o 1-print_rev_recursion +julien@ubuntu:~/0x08. Recursion$ ./1-print_rev_recursion +reklaW notloC +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 2\. Dreams feel real while we're in them. It's only when we wake up that we realize something was actually strange
_**[2-strlen_recursion.c](2-strlen_recursion.c)**_ contains a function that returns the length of a string.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 2-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int n; + + n = _strlen_recursion("Corbin Coleman"); + printf("%d\n", n); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-strlen_recursion.c -o 2-strlen_recursion +julien@ubuntu:~/0x08. Recursion$ ./2-strlen_recursion +14 +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 3\. You mustn't be afraid to dream a little bigger, darling
_**[3-factorial.c](3-factorial.c)**_ contains a function that returns the factorial of a given number.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 3-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int r; + + r = factorial(1); + printf("%d\n", r); + r = factorial(5); + printf("%d\n", r); + r = factorial(10); + printf("%d\n", r); + r = factorial(-1024); + printf("%d\n", r); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-factorial.c -o 3-factorial +julien@ubuntu:~/0x08. Recursion$ ./3-factorial +1 +120 +3628800 +-1 +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 4\. Once an idea has taken hold of the brain it's almost impossible to eradicate
_**[4-pow_recursion.c](4-pow_recursion.c)**_ contains a function that returns the value of `x` raised to the power of `y`.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 4-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int r; + + r = _pow_recursion(1, 10); + printf("%d\n", r); + r = _pow_recursion(1024, 0); + printf("%d\n", r); + r = _pow_recursion(2, 16); + printf("%d\n", r); + r = _pow_recursion(5, 2); + printf("%d\n", r); + r = _pow_recursion(5, -2); + printf("%d\n", r); + r = _pow_recursion(-5, 3); + printf("%d\n", r); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-pow_recursion.c -o 4-pow +julien@ubuntu:~/0x08. Recursion$ ./4-pow +1 +1 +65536 +25 +-1 +-125 +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 5\. Your subconscious is looking for the dreamer
_**[5-sqrt_recursion.c](5-sqrt_recursion.c)**_ contains a function that returns the natural square root of a number.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 5-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int r; + + r = _sqrt_recursion(1); + printf("%d\n", r); + r = _sqrt_recursion(1024); + printf("%d\n", r); + r = _sqrt_recursion(16); + printf("%d\n", r); + r = _sqrt_recursion(17); + printf("%d\n", r); + r = _sqrt_recursion(25); + printf("%d\n", r); + r = _sqrt_recursion(-1); + printf("%d\n", r); + return (0); +} +julien@ubuntu:~/0x08. gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-sqrt_recursion.c -o 5-sqrt +julien@ubuntu:~/0x08. Recursion$ ./5-sqrt +1 +32 +4 +-1 +5 +-1 +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 6\. Inception. Is it possible?
_**[6-is_prime_number.c](6-is_prime_number.c)**_ contains a function that returns `1` if the input integer is a [prime number](https://en.wikipedia.org/wiki/Prime_number), otherwise return `0`.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 6-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int r; + + r = is_prime_number(1); + printf("%d\n", r); + r = is_prime_number(1024); + printf("%d\n", r); + r = is_prime_number(16); + printf("%d\n", r); + r = is_prime_number(17); + printf("%d\n", r); + r = is_prime_number(25); + printf("%d\n", r); + r = is_prime_number(-1); + printf("%d\n", r); + r = is_prime_number(113); + printf("%d\n", r); + r = is_prime_number(7919); + printf("%d\n", r); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 6-main.c 6-is_prime_number.c -o 6-prime +julien@ubuntu:~/0x08. Recursion$ ./6-prime +0 +0 +0 +1 +0 +0 +1 +1 +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 7\. They say we only use a fraction of our brain's true potential. Now that's when we're awake. When we're asleep, we can do almost anything
_**[100-is_palindrome.c](100-is_palindrome.c)**_ contains a function that returns `1` if a string is a palindrome and `0` if not.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 100-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int r; + + r = is_palindrome("level"); + printf("%d\n", r); + r = is_palindrome("redder"); + printf("%d\n", r); + r = is_palindrome("test"); + printf("%d\n", r); + r = is_palindrome("step on no pets"); + printf("%d\n", r); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-is_palindrome.c -o 100-palindrome +julien@ubuntu:~/0x08. Recursion$ ./100-palindrome +1 +1 +0 +1 +julien@ubuntu:~/0x08. Recursion$ +``` ++ [x] 8\. Inception. Now, before you bother telling me it's impossible...
_**[101-wildcmp.c](101-wildcmp.c)**_ contains a function that compares two strings and returns `1` if the strings can be considered identical, otherwise return `0`.
__Example__: +```c +julien@ubuntu:~/0x08. Recursion$ cat 101-main.c +#include "main.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int r; + + r = wildcmp("main.c", "*.c"); + printf("%d\n", r); + r = wildcmp("main.c", "m*a*i*n*.*c*"); + printf("%d\n", r); + r = wildcmp("main.c", "main.c"); + printf("%d\n", r); + r = wildcmp("main.c", "m*c"); + printf("%d\n", r); + r = wildcmp("main.c", "ma********************************c"); + printf("%d\n", r); + r = wildcmp("main.c", "*"); + printf("%d\n", r); + r = wildcmp("main.c", "***"); + printf("%d\n", r); + r = wildcmp("main.c", "m.*c"); + printf("%d\n", r); + r = wildcmp("main.c", "**.*c"); + printf("%d\n", r); + r = wildcmp("main-main.c", "ma*in.c"); + printf("%d\n", r); + r = wildcmp("main", "main*d"); + printf("%d\n", r); + r = wildcmp("abc", "*b"); + printf("%d\n", r); + return (0); +} +julien@ubuntu:~/0x08. Recursion$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-main.c 101-wildcmp.c -o 101-wildcmp +julien@ubuntu:~/0x08. Recursion$ ./101-wildcmp +1 +1 +1 +1 +1 +1 +1 +0 +1 +1 +0 +0 +julien@ubuntu:~/0x08. Recursion$ +``` diff --git a/0x09-static_libraries/TASKS.md b/0x09-static_libraries/TASKS.md new file mode 100644 index 00000000..e8811453 --- /dev/null +++ b/0x09-static_libraries/TASKS.md @@ -0,0 +1,171 @@ +# 0x09-static_libraries +> project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Static Library](https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html) +* What is a static library, how does it work, how to create one, and how to use it +* Basic usage of `ar`, `ranlib`, `nm` + +[Difference b/n Dynamic and Static Library](https://www.youtube.com/watch?v=eW5he5uFBNM) + +[concept](./CONCEPT.md) +## Tasks Completed + ++ [x] 0\. A library is not a luxury but one of the necessities of life
_**[libmy.a](libmy.a)**_ is a static library containing the functions below. +```c +int _putchar(char c); +int _islower(int c); +int _isalpha(int c); +int _abs(int n); +int _isupper(int c); +int _isdigit(int c); +int _strlen(char *s); +void _puts(char *s); +char *_strcpy(char *dest, char *src); +int _atoi(char *s); +char *_strcat(char *dest, char *src); +char *_strncat(char *dest, char *src, int n); +char *_strncpy(char *dest, char *src, int n); +int _strcmp(char *s1, char *s2); +char *_memset(char *s, char b, unsigned int n); +char *_memcpy(char *dest, char *src, unsigned int n); +char *_strchr(char *s, char c); +unsigned int _strspn(char *s, char *accept); +char *_strpbrk(char *s, char *accept); +char *_strstr(char *haystack, char *needle); +``` +__Example__: +```c +julien@ubuntu:~/0x09. Static Librairies$ ar -t libmy.a +0-isupper.o +0-memset.o +0-strcat.o +1-isdigit.o +1-memcpy.o +1-strncat.o +100-atoi.o +2-strchr.o +2-strlen.o +2-strncpy.o +3-islower.o +3-puts.o +3-strcmp.o +3-strspn.o +4-isalpha.o +4-strpbrk.o +5-strstr.o +6-abs.o +9-strcpy.o +_putchar.o +julien@ubuntu:~/0x09. Static Librairies$ nm libmy.a + +0-isupper.o: +0000000000000000 T _isupper + +0-memset.o: +0000000000000000 T _memset + +0-strcat.o: +0000000000000000 T _strcat + +1-isdigit.o: +0000000000000000 T _isdigit + +1-memcpy.o: +0000000000000000 T _memcpy + +1-strncat.o: +0000000000000000 T _strncat + +100-atoi.o: +0000000000000000 T _atoi + +2-strchr.o: +0000000000000000 T _strchr + +2-strlen.o: +0000000000000000 T _strlen + +2-strncpy.o: +0000000000000000 T _strncpy + +3-islower.o: +0000000000000000 T _islower + +3-puts.o: + U _putchar +0000000000000000 T _puts + +3-strcmp.o: +0000000000000000 T _strcmp + +3-strspn.o: +0000000000000000 T _strspn + +4-isalpha.o: +0000000000000000 T _isalpha + +4-strpbrk.o: +0000000000000000 T _strpbrk + +5-strstr.o: +0000000000000000 T _strstr + +6-abs.o: +0000000000000000 T _abs + +9-strcpy.o: +0000000000000000 T _strcpy + +_putchar.o: +0000000000000000 T _putchar + U write +julien@ubuntu:~/0x09. Static Librairies$ cat main.c +#include "main.h" + +int main(void) +{ + _puts("\"At the end of the day, my goal was to be the best hacker\"\n\t- Kevin Mitnick"); + return (0); +} +julien@ubuntu:~/0x09. Static Librairies$ gcc -std=gnu89 main.c -L. -lmy -o quote +julien@ubuntu:~/0x09. Static Librairies$ ./quote +"At the end of the day, my goal was to be the best hacker" + - Kevin Mitnick +julien@ubuntu:~/0x09. Static Librairies$ +``` ++ [x] 1\. Without libraries what have we? We have no past and no future
_**[create_static_lib.sh](create_static_lib.sh)**_ creates a static library called `liball.a` from all the `.c` files that are in the current directory.
__Example__: +```c +julien@ubuntu:~/0x09. Static Librairies$ ls *.c +0-isupper.c 0-strcat.c 1-isdigit.c 1-strncat.c 2-strlen.c 3-islower.c 3-strcmp.c 4-isalpha.c 5-strstr.c 9-strcpy.c _putchar.c +0-memset.c 100-atoi.c 1-memcpy.c 2-strchr.c 2-strncpy.c 3-puts.c 3-strspn.c 4-strpbrk.c 6-abs.c +julien@ubuntu:~/0x09. Static Librairies$ ./create_static_lib.sh +julien@ubuntu:~/0x09. Static Librairies$ ls *.a +liball.a +julien@ubuntu:~/0x09. Static Librairies$ ar -t liball.a +0-isupper.o +0-memset.o +0-strcat.o +100-atoi.o +1-isdigit.o +1-memcpy.o +1-strncat.o +2-strchr.o +2-strlen.o +2-strncpy.o +3-islower.o +3-puts.o +3-strcmp.o +3-strspn.o +4-isalpha.o +4-strpbrk.o +5-strstr.o +6-abs.o +9-strcpy.o +_putchar.o +julien@ubuntu:~/0x09. Static Librairies$ +``` diff --git a/0x09-static_libraries/test/TASKS.md b/0x09-static_libraries/test/TASKS.md new file mode 100644 index 00000000..bb843122 --- /dev/null +++ b/0x09-static_libraries/test/TASKS.md @@ -0,0 +1,18 @@ +# Test file + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +
_**[main.c](main.c)**_ is a test file for static library. + +## Usage + +`gcc -std=gnu89 main.c -L../ -lmy -o quote` + +`./quote` + +## Output + +```c +"At the end of the day, my goal was to be the best hacker" + - Kevin Mitnick +``` diff --git a/0x0A-argc_argv/TASKS.md b/0x0A-argc_argv/TASKS.md new file mode 100644 index 00000000..f3674c36 --- /dev/null +++ b/0x0A-argc_argv/TASKS.md @@ -0,0 +1,104 @@ +# 0x0A-argc_argv +> project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* How to use arguments passed to your program +* What are two prototypes of main that you know of, and in which case do you use one or the other +* How to use `__attribute__((unused))` or `(void)` to compile functions with unused variables or parameters + +## Resources Used +* [Arguments to main](https://publications.gbdirect.co.uk//c_book/chapter10/arguments_to_main.html) +* [argc and argv](http://crasseux.com/books/ctutorial/argc-and-argv.html) +* [What does `int argc`, `char* argv[]` mean?](https://www.youtube.com/watch?v=aP1ijjeZc24) + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. It ain't what they call you, it's what you answer to
_**[0-whatsmyname.c](0-whatsmyname.c)**_ is a program that prints its name, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x0A. argc, argv$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-whatsmyname.c -o mynameis +julien@ubuntu:~/0x0A. argc, argv$ ./mynameis +./mynameis +julien@ubuntu:~/0x0A. argc, argv$ mv mynameis mynewnameis +julien@ubuntu:~/0x0A. argc, argv$ ./mynewnameis +./mynewnameis +julien@ubuntu:~/0x0A. argc, argv$ +``` ++ [x] 1\. Silence is argument carried out by other means
_**[1-args.c](1-args.c)**_ is a program that prints the number of arguments passed into it.
__Example__: +```c +julien@ubuntu:~/0x0A. argc, argv$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-args.c -o nargs +julien@ubuntu:~/0x0A. argc, argv$ ./nargs +0 +julien@ubuntu:~/0x0A. argc, argv$ ./nargs hello +1 +julien@ubuntu:~/0x0A. argc, argv$ ./nargs "hello, world" +1 +julien@ubuntu:~/0x0A. argc, argv$ ./nargs hello, world +2 +julien@ubuntu:~/0x0A. argc, argv$ +``` ++ [x] 2\. The best argument against democracy is a five-minute conversation with the average voter
_**[2-args.c](2-args.c)**_ is a program that prints all arguments it receives.
__Example__: +```c +julien@ubuntu:~/0x0A. argc, argv$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-args.c -o args +julien@ubuntu:~/0x0A. argc, argv$ ./args +./args +julien@ubuntu:~/0x0A. argc, argv$ ./args You can do anything, but not everything. +./args +You +can +do +anything, +but +not +everything. +julien@ubuntu:~/0x0A. argc, argv$ +``` ++ [x] 3\. Neither irony nor sarcasm is argument
_**[3-mul.c](3-mul.c)**_ is a program that multiplies two numbers passed to it as command line arguments.
__Example__: +```c +julien@ubuntu:~/0x0A. argc, argv$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-mul.c -o mul +julien@ubuntu:~/0x0A. argc, argv$ ./mul 2 3 +6 +julien@ubuntu:~/0x0A. argc, argv$ ./mul 2 -3 +-6 +julien@ubuntu:~/0x0A. argc, argv$ ./mul 2 0 +0 +julien@ubuntu:~/0x0A. argc, argv$ ./mul 245 3245342 +795108790 +julien@ubuntu:~/0x0A. argc, argv$ ./mul +Error +julien@ubuntu:~/0x0A. argc, argv$ +``` ++ [x] 4\. To infinity and beyond
_**[4-add.c](4-add.c)**_ is a program that adds positive numbers passed to it as command line arguments.
__Example__: +```c +julien@ubuntu:~/0x0A. argc, argv$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-add.c -o add +julien@ubuntu:~/0x0A. argc, argv$ ./add 1 1 +2 +julien@ubuntu:~/0x0A. argc, argv$ ./add 1 10 100 1000 +1111 +julien@ubuntu:~/0x0A. argc, argv$ ./add 1 2 3 e 4 5 +Error +julien@ubuntu:~/0x0A. argc, argv$ ./add +0 +julien@ubuntu:~/0x0A. argc, argv$ +``` ++ [x] 5\. Minimal Number of Coins for Change
_**[100-change.c](100-change.c)**_ is a program that prints the minimum number of coins to make change for an amount of money passed as a command line argument.
The coins have values 25, 10, 5, 2, and 1 cent.
__Example__: +```c +julien@ubuntu:~/0x0A. argc, argv$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-change.c -o change +julien@ubuntu:~/0x0A. argc, argv$ ./change +Error +julien@ubuntu:~/0x0A. argc, argv$ ./change 10 +1 +julien@ubuntu:~/0x0A. argc, argv$ ./change 100 +4 +julien@ubuntu:~/0x0A. argc, argv$ ./change 101 +5 +julien@ubuntu:~/0x0A. argc, argv$ ./change 13 +3 +julien@ubuntu:~/0x0A. argc, argv$ +``` diff --git a/0x0B-malloc_free/TASKS.md b/0x0B-malloc_free/TASKS.md new file mode 100644 index 00000000..ecd88a5c --- /dev/null +++ b/0x0B-malloc_free/TASKS.md @@ -0,0 +1,386 @@ +# 0x0B. C - malloc, free +>project done in 2 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Dynamic Memory allocation in c](https://www.youtube.com/watch?v=xDVC3wKjS64) +* What is the difference between automatic and dynamic allocation +* What is malloc and free and how to use them +* Why and when use malloc +* How to use valgrind to check for memory leak + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. Float like a butterfly, sting like a bee
_**[0-create_array.c](0-create_array.c)**_ contains a function that creates an array of chars, and initializes it with a specific char.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 0-main.c +#include "main.h" +#include +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(char *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code for ALX School students. + * + * Return: Always 0. + */ +int main(void) +{ + char *buffer; + + buffer = create_array(98, 'H'); + if (buffer == NULL) + { + printf("failed to allocate memory\n"); + return (1); + } + simple_print_buffer(buffer, 98); + free(buffer); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-create_array.c -o a +julien@ubuntu:~/0x0a. malloc, free$ ./a +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +0x48 0x48 0x48 0x48 0x48 0x48 0x48 0x48 +julien@ubuntu:~/0x0a. malloc, free$ +``` ++ [x] 1\. The woman who has no imagination has no wings
_**[1-strdup.c](1-strdup.c)**_ contains a function that returns a pointer to a newly allocated space in memory, which contains a copy of the string given as a parameter.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 1-main.c +#include "main.h" +#include +#include + +/** + * main - check the code for ALX School students. + * + * Return: Always 0. + */ +int main(void) +{ + char *s; + + s = _strdup("ALX SE"); + if (s == NULL) + { + printf("failed to allocate memory\n"); + return (1); + } + printf("%s\n", s); + free(s); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-strdup.c -o s +julien@ubuntu:~/0x0a. malloc, free$ ./s +ALX SE +julien@ubuntu:~/0x0a. malloc, free$ +``` ++ [x] 2\. He who is not courageous enough to take risks will accomplish nothing in life
_**[2-str_concat.c](2-str_concat.c)**_ contains a function that concatenates two strings.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 2-main.c +#include "main.h" +#include +#include + +/** + * main - check the code for ALX School students. + * + * Return: Always 0. + */ +int main(void) +{ + char *s; + + s = str_concat("Betty ", "Holberton"); + if (s == NULL) + { + printf("failed\n"); + return (1); + } + printf("%s\n", s); + free(s); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-str_concat.c -o c +julien@ubuntu:~/c/curriculum_by_julien/holbertonschool-low_level_programming/0x0a. malloc, free$ ./c | cat -e +Betty Holberton$ +julien@ubuntu:~/c/curriculum_by_julien/holbertonschool-low_level_programming/0x0a. malloc, free$ +``` ++ [x] 3\. If you even dream of beating me you'd better wake up and apologize
_**[3-alloc_grid.c](3-alloc_grid.c)**_ contains a function that returns a pointer to a 2 dimensional array of integers.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 3-main.c +#include "main.h" +#include +#include + +/** + * print_grid - prints a grid of integers + * @grid: the address of the two dimensional grid + * @width: width of the grid + * @height: height of the grid + * + * Return: Nothing. + */ +void print_grid(int **grid, int width, int height) +{ + int w; + int h; + + h = 0; + while (h < height) + { + w = 0; + while (w < width) + { + printf("%d ", grid[h][w]); + w++; + } + printf("\n"); + h++; + } +} + +/** + * main - check the code for ALX School students. + * + * Return: Always 0. + */ +int main(void) +{ + int **grid; + + grid = alloc_grid(6, 4); + if (grid == NULL) + { + return (1); + } + print_grid(grid, 6, 4); + printf("\n"); + grid[0][3] = 98; + grid[3][4] = 402; + print_grid(grid, 6, 4); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-alloc_grid.c -o g +julien@ubuntu:~/0x0a. malloc, free$ ./g +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 + +0 0 0 98 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 402 0 +julien@ubuntu:~/0x0a. malloc, free$ +``` ++ [x] 4\. It's not bragging if you can back it up
_**[4-free_grid.c](4-free_grid.c)**_ contains a function that frees a 2 dimensional grid previously created by my `[alloc_grid](3-alloc_grid.c)` function.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 4-main.c +#include "main.h" +#include +#include + +/** + * print_grid - prints a grid of integers + * @grid: the address of the two dimensional grid + * @width: width of the grid + * @height: height of the grid + * + * Return: Nothing. + */ +void print_grid(int **grid, int width, int height) +{ + int w; + int h; + + h = 0; + while (h < height) + { + w = 0; + while (w < width) + { + printf("%d ", grid[h][w]); + w++; + } + printf("\n"); + h++; + } +} + +/** + * main - check the code for ALX School students. + * + * Return: Always 0. + */ +int main(void) +{ + int **grid; + + grid = alloc_grid(6, 4); + if (grid == NULL) + { + return (1); + } + print_grid(grid, 6, 4); + printf("\n"); + grid[0][3] = 98; + grid[3][4] = 402; + print_grid(grid, 6, 4); + free_grid(grid, 4); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 3-alloc_grid.c 4-free_grid.c -o f +julien@ubuntu:~/0x0a. malloc, free$ valgrind ./f +==5013== Memcheck, a memory error detector +==5013== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==5013== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==5013== Command: ./f +==5013== +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 + +0 0 0 98 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 402 0 +==5013== +==5013== HEAP SUMMARY: +==5013== in use at exit: 0 bytes in 0 blocks +==5013== total heap usage: 6 allocs, 6 frees, 1,248 bytes allocated +==5013== +==5013== All heap blocks were freed -- no leaks are possible +==5013== +==5013== For counts of detected and suppressed errors, rerun with: -v +==5013== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x0a. malloc, free$ +``` ++ [x] 5\. It isn't the mountains ahead to climb that wear you out; it's the pebble in your shoe
_**[100-argstostr.c](100-argstostr.c)**_ contains a function that concatenates all the arguments of your program.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 100-main.c +#include "main.h" +#include +#include + +/** + * main - check the code for ALX School students. + * + * Return: Always 0. + */ +int main(int ac, char *av[]) +{ + char *s; + + s = argstostr(ac, av); + if (s == NULL) + { + return (1); + } + printf("%s", s); + free(s); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-argstostr.c -o args +julien@ubuntu:~/0x0a. malloc, free$ ./args I will "show you" how great I am +./args +I +will +show you +how +great +I +am +julien@ubuntu:~/0x0a. malloc, free$ +``` ++ [x] 6\. I will show you how great I am
_**[101-strtow.c](101-strtow.c)**_ contains a function that splits a string into words.
__Example__: +```c +julien@ubuntu:~/0x0a. malloc, free$ cat 101-main.c +#include "main.h" +#include +#include + +/** + * print_tab - Prints an array of string + * @tab: The array to print + * + * Return: nothing + */ +void print_tab(char **tab) +{ + int i; + + for (i = 0; tab[i] != NULL; ++i) + { + printf("%s\n", tab[i]); + } +} + +/** + * main - check the code for ALX School students. + * + * Return: 1 if an error occurred, 0 otherwise + */ +int main(void) +{ + char **tab; + + tab = strtow(" ALX School #cisfun "); + if (tab == NULL) + { + printf("Failed\n"); + return (1); + } + print_tab(tab); + return (0); +} +julien@ubuntu:~/0x0a. malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-main.c 101-strtow.c -o strtow +julien@ubuntu:~/0x0a. malloc, free$ ./strtow | cat -e +ALX$ +School$ +#cisfun$ +julien@ubuntu:~/0x0a. malloc, free$ +``` diff --git a/0x0C-more_malloc_free/TASKS.md b/0x0C-more_malloc_free/TASKS.md new file mode 100644 index 00000000..664614b6 --- /dev/null +++ b/0x0C-more_malloc_free/TASKS.md @@ -0,0 +1,289 @@ +# 0x0C-more_malloc_free +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* How to use the `exit` function +* What are the functions `calloc` and `realloc` from the standard library and how to use them +* Q/A:
[casting in malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. Trust no one
_**[0-malloc_checked.c](0-malloc_checked.c)**_ contains a function that allocates memory using `malloc`.
__Example__: +```c +julien@ubuntu:~/0x0b. more malloc, free$ cat 0-main.c +#include "main.h" +#include +#include +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *c; + int *i; + float *f; + double *d; + + c = malloc_checked(sizeof(char) * 1024); + printf("%p\n", (void *)c); + i = malloc_checked(sizeof(int) * 402); + printf("%p\n", (void *)i); + f = malloc_checked(sizeof(float) * 100000000); + printf("%p\n", (void *)f); + d = malloc_checked(INT_MAX); + printf("%p\n", (void *)d); + free(c); + free(i); + free(f); + free(d); + return (0); +} +julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-malloc_checked.c -o a +julien@ubuntu:~/0x0b. more malloc, free$ ./a +0x1e39010 +0x1e39830 +0x7f31f6c19010 +julien@ubuntu:~/0x0b. more malloc, free$ echo $? +98 +julien@ubuntu:~/0x0b. more malloc, free$ +``` ++ [x] 1\. string_nconcat
_**[1-string_nconcat.c](1-string_nconcat.c)**_ contains a function that concatenates two strings.
__Example__: +```c +julien@ubuntu:~/0x0b. more malloc, free$ cat 1-main.c +#include "main.h" +#include +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *concat; + + concat = string_nconcat("Best ", "School !!!", 6); + printf("%s\n", concat); + free(concat); + return (0); +} +julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-string_nconcat.c -o 1-string_nconcat +julien@ubuntu:~/0x0b. more malloc, free$ ./1-string_nconcat +Best School +julien@ubuntu:~/0x0b. more malloc, free$ +``` ++ [x] 2\. \_calloc
_**[2-calloc.c](2-calloc.c)**_ contains a function that allocates memory for an array, using `malloc`.
__Example__: +```c +julien@ubuntu:~/0x0b. more malloc, free$ cat 2-main.c +#include "main.h" +#include +#include +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(char *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + char *a; + + a = _calloc(98, sizeof(char)); + strcpy(a, "Best"); + strcpy(a + 4, " School! :)\n"); + a[97] = '!'; + simple_print_buffer(a, 98); + free(a); + return (0); +} +julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-calloc.c -o 2-calloc +julien@ubuntu:~/0x0b. more malloc, free$ ./2-calloc +0x42 0x65 0x73 0x74 0x20 0x53 0x63 0x68 0x6f 0x6f +0x6c 0x21 0x20 0x3a 0x29 0x0a 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 +0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x21 +julien@ubuntu:~/0x0b. more malloc, free$ +``` ++ [x] 3\. array_range
_**[3-array_range.c](3-array_range.c)**_ contains a function that creates an array of integers.
__Example__: +```c +julien@ubuntu:~/0x0b. more malloc, free$ cat 3-main.c +#include "main.h" +#include +#include +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(int *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int *a; + + a = array_range(0, 10); + simple_print_buffer(a, 11); + free(a); + return (0); +} +julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-array_range.c -o 3-array_range +julien@ubuntu:~/0x0b. more malloc, free$ ./3-array_range +0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 +0x0a +julien@ubuntu:~/0x0b. more malloc, free$ +``` ++ [x] 4\. \_realloc
_**[100-realloc.c](100-realloc.c)**_ contains a function that reallocates a memory block using `malloc` and `free`.
__Example__: +```c +julien@ubuntu:~/0x0b. more malloc, free$ cat 100-main.c +#include "main.h" +#include +#include +#include + +/** + * simple_print_buffer - prints buffer in hexa + * @buffer: the address of memory to print + * @size: the size of the memory to print + * + * Return: Nothing. + */ +void simple_print_buffer(char *buffer, unsigned int size) +{ + unsigned int i; + + i = 0; + while (i < size) + { + if (i % 10) + { + printf(" "); + } + if (!(i % 10) && i) + { + printf("\n"); + } + printf("0x%02x", buffer[i]); + i++; + } + printf("\n"); +} + +/** + * main - check the code for + * + * Return: Always 0. + */ +int main(void) +{ + char *p; + int i; + + p = malloc(sizeof(char) * 10); + p = _realloc(p, sizeof(char) * 10, sizeof(char) * 98); + i = 0; + while (i < 98) + { + p[i++] = 98; + } + simple_print_buffer(p, 98); + free(p); + return (0); +} +julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-realloc.c -o 100-realloc +julien@ubuntu:~/0x0b. more malloc, free$ ./100-realloc +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +0x62 0x62 0x62 0x62 0x62 0x62 0x62 0x62 +julien@ubuntu:~/0x0b. more malloc, free$ +``` ++ [x] 5\. We must accept finite disappointment, but never lose infinite hope
_**[101-mul.c](101-mul.c)**_ is a program that multiplies two positive numbers passed as command line arguments.
__Example__: +```c +julien@ubuntu:~/0x0b. more malloc, free$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-mul.c _putchar.c -o 101-mul +julien@ubuntu:~/0x0b. more malloc, free$ ./101-mul 10 98 +980 +julien@ubuntu:~/0x0b. more malloc, free$ ./101-mul 235234693269436436223446526546334576437634765378653875874687649698659586695898579 28658034365084365083426083109679137608216408631430814308651084650816406134060831608310853086103769013709675067130586570832760732096730978014607369739567864508634086304807450973045703428580934825098342095832409850394285098342509834209583425345267413639235755891879970464524226159074760914989935413350556875770807019893069201247121855122836389417022552166316010013074258781583143870461182707893577849408672040555089482160343085482612348145322689883025225988799452329290281169927532160590651993511788518550547570284574715925006962738262888617840435389140329668772644708 +6741363923575589187997046452422615907476091498993541335055687577080701989306920124712185512283638941702255216631601001307425878158314387046118270789357784940867204055508948216034308548261234814532268988302522598879945232929028116992753216059081057377926651337612618248332113256902485974371969385156015068813868274000683912187818601667058605418678284322237297213673482412392922068159291496274311170208689056585352782844484721140846367741649962638649229509281867896067208474178402156294978940712959518351846413859141792380853313812015295333546716634344284086426775480775747808150030732119704867805688704303461042373101473485092019906795014369069932 +julien@ubuntu:~/0x0b. more malloc, free$ +``` diff --git a/0x0D-preprocessor/TASKS.md b/0x0D-preprocessor/TASKS.md new file mode 100644 index 00000000..95999ecb --- /dev/null +++ b/0x0D-preprocessor/TASKS.md @@ -0,0 +1,142 @@ +# 0x0D-preprocessor +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are macros and how to use them +* What are the most common predefined macros +* How to include guard your header files + +## Resources Used +* [Understanding C program Compilation process](https://www.youtube.com/watch?v=eW5he5uFBNM) +* [Object-like Macros](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Object-like-Macros.html#Object-like-Macros) +* [Macro-arguments](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Macro-Arguments.html#Macro-Arguments) +* [Pre-Processor Directives](https://www.youtube.com/watch?v=X6HiYbY3Uak) +* [The C pre-processor](https://www.cprogramming.com/tutorial/cpreprocessor.html) +* [Standard pre-defined Macros](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Standard-Predefined-Macros.html#Standard-Predefined-Macros) +* [include guards](https://en.wikipedia.org/wiki/Include_guard) +* [Common Pre-defined Macros](https://gcc.gnu.org/onlinedocs/gcc-5.1.0/cpp/Common-Predefined-Macros.html#Common-Predefined-Macros) + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. Object-like Macro
_**[0-object_like_macro.h](0-object_like_macro.h)**_ is a header file that defines a macro named `SIZE` as an abbreviation for the token `1024`.
__Example__: +```c +julien@ubuntu:~/0x0c. macro, structures$ cat 0-main.c +#include "0-object_like_macro.h" +#include "0-object_like_macro.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int s; + + s = 98 + SIZE; + printf("%d\n", s); + return (0); +} +julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c -o a +julien@ubuntu:~/0x0c. macro, structures$ ./a +1122 +julien@ubuntu:~/0x0c. macro, structures$ +``` ++ [x] 1\. Pi
_**[1-pi.h](1-pi.h)**_ is a header file that defines a macro named `PI` as an abbreviation for the token `3.14159265359`.
__Example__: +```c +julien@ubuntu:~/0x0c. macro, structures$ cat 1-main.c +#include "1-pi.h" +#include "1-pi.h" +#include + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + float a; + float r; + + r = 98; + a = PI * r * r; + printf("%.3f\n", a); + return (0); +} +julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c -o b +julien@ubuntu:~/0x0c. macro, structures$ ./b +30171.855 +julien@ubuntu:~/0x0c. macro, structures$ +``` ++ [x] 2\. File name
_**[2-main.c](2-main.c)**_ is a program that prints the name of the file it was compiled from, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c -o c +julien@ubuntu:~/0x0c. macro, structures$ ./c +2-main.c +julien@ubuntu:~/0x0c. macro, structures$ cp 2-main.c 02-main.c +julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 02-main.c -o cc +julien@ubuntu:~/0x0c. macro, structures$ ./cc +02-main.c +julien@ubuntu:~/0x0c. macro, structures$ +``` ++ [x] 3\. Function-like macro
_**[3-function_like_macro.c](3-function_like_macro.h)**_ contains a function-like macro `ABS(x)` that computes the absolute value of a number `x`.
__Example__: +```c +julien@ubuntu:~/0x0c. macro, structures$ cat 3-main.c +#include +#include "3-function_like_macro.h" +#include "3-function_like_macro.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int i; + int j; + + i = ABS(-98) * 10; + j = ABS(98) * 10; + printf("%d, %d\n", i, j); + return (0); +} +julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c -o d +julien@ubuntu:~/0x0c. macro, structures$ ./d +980, 980 +julien@ubuntu:~/0x0c. macro, structures$ +``` ++ [x] 4\. SUM
_**[4-sum.h](4-sum.h)**_ contains a function-like macro `SUM(x, y)` that computes the sum of the numbers `x` and `y`.
__Example__: +```c +julien@ubuntu:~/0x0c. macro, structures$ cat 4-main.c +#include +#include "4-sum.h" +#include "4-sum.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int s; + + s = SUM(98, 1024); + printf("%d\n", s); + return (0); +} +julien@ubuntu:~/0x0c. macro, structures$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c -o e +julien@ubuntu:~/0x0c. macro, structures$ ./e +1122 +julien@ubuntu:~/0x0c. macro, structures$ +``` diff --git a/0x0E-structures_typedef/TASKS.md b/0x0E-structures_typedef/TASKS.md new file mode 100644 index 00000000..aae1d5fe --- /dev/null +++ b/0x0E-structures_typedef/TASKS.md @@ -0,0 +1,194 @@ +# 0x0E-structures_typedef +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Struct](https://en.wikipedia.org/wiki/Struct_(C_programming_language)) +* What are `structures`, when, why and how to use them + +[Typedef](https://publications.gbdirect.co.uk//c_book/chapter8/typedef.html) +* How to use `typedef`. + +Advanced Topic +* [The lost art of C structure Packing](http://www.catb.org/esr/structure-packing/) + +## Compilation + +`gcc -Wall -Werror -Wextra -pedantic -std=gnu89` + +## Tasks Completed + ++ [x] 0\. Poppy
_**[dog.h](dog.h)**_ contains a new type `struct dog with the following elements. + + `name`, type = `char *` + + `age`, type = `float *` + + `owner`, type = `char *` + + __Example__: + ```c + julien@ubuntu:~/0x0d. structures, typedef$ cat 0-main.c + #include + #include "dog.h" + /** + * main - check the code + * + * Return: Always 0. + */ + int main(void) + { + struct dog my_dog; + + my_dog.name = "Poppy"; + my_dog.age = 3.5; + my_dog.owner = "Bob"; + printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); + return (0); + } + julien@ubuntu:~/0x0d. structures, typedef$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c -o a + julien@ubuntu:~/0x0d. structures, typedef$ ./a + My name is Poppy, and I am 3.5 :) - Woof! + julien@ubuntu:~/0x0d. structures, typedef$ + ``` ++ [x] 1\. A dog is the only thing on earth that loves you more than you love yourself
_**[1-init_dog.c](1-init_dog.c)**_ contains a function that initializes a variable of type `struct dog`.
__Example__: +```c +julien@ubuntu:~/0x0d. structures, typedef$ cat 1-main.c +#include +#include "dog.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + struct dog my_dog; + + init_dog(&my_dog, "Poppy", 3.5, "Bob"); + printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); + return (0); +} +julien@ubuntu:~/0x0d. structures, typedef$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-init_dog.c -o b +julien@ubuntu:~/0x0d. structures, typedef$ ./b +My name is Poppy, and I am 3.5 :) - Woof! +julien@ubuntu:~/0x0d. structures, typedef$ +``` ++ [x] 2\. A dog will teach you unconditional love. If you can have that in your life, things won't be too bad
_**[2-print_dog.c](2-print_dog.c)**_ contains a function that prints a `struct dog`.
__Example__: +```c +julien@ubuntu:~/0x0d. structures, typedef$ cat 2-main.c +#include +#include "dog.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + struct dog my_dog; + + my_dog.name = "Poppy"; + my_dog.age = 3.5; + my_dog.owner = "Bob"; + print_dog(&my_dog); + return (0); +} +julien@ubuntu:~/0x0d. structures, typedef$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-print_dog.c -o c +julien@ubuntu:~/0x0d. structures, typedef$ ./c +Name: Poppy +Age: 3.500000 +Owner: Bob +julien@ubuntu:~/0x0d. structures, typedef$ +``` ++ [x] 3\. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to read
_**[dog.h](dog.h)**_ defines a new type `dog_t` as a new name for the type `struct dog`.
__Example__: +```c +julien@ubuntu:~/0x0d. structures, typedef$ cat 3-main.c +#include +#include "dog.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + dog_t my_dog; + + my_dog.name = "Poppy"; + my_dog.age = 3.5; + my_dog.owner = "Bob"; + printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog.name, my_dog.age); + return (0); +} +julien@ubuntu:~/0x0d. structures, typedef$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c -o d +julien@ubuntu:~/0x0d. structures, typedef$ ./d +My name is Poppy, and I am 3.5 :) - Woof! +julien@ubuntu:~/0x0d. structures, typedef$ +``` ++ [x] 4\. A door is what a dog is perpetually on the wrong side of
_**[4-new_dog.c](4-new_dog.c)**_ contains a function that creates a new dog.
__Example__: +```c +julien@ubuntu:~/0x0d. structures, typedef$ cat 4-main.c +#include +#include "dog.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + dog_t *my_dog; + + my_dog = new_dog("Poppy", 3.5, "Bob"); + printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); + return (0); +} +julien@ubuntu:~/0x0d. structures, typedef$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-new_dog.c -o e +julien@ubuntu:~/0x0d. structures, typedef$ ./e +My name is Poppy, and I am 3.5 :) - Woof! +julien@ubuntu:~/0x0d. structures, typedef$ +``` ++ [x] 5\. How many legs does a dog have if you call his tail a leg? Four. Saying that a tail is a leg doesn't make it a leg
_**[5-free_dog.c](5-free_dog.c)**_ contains a function that frees dogs.
__Example__: +```c +julien@ubuntu:~/0x0d. structures, typedef$ cat 5-main.c +#include +#include "dog.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + dog_t *my_dog; + + my_dog = new_dog("Poppy", 3.5, "Bob"); + printf("My name is %s, and I am %.1f :) - Woof!\n", my_dog->name, my_dog->age); + free_dog(my_dog); + return (0); +} +julien@ubuntu:~/0x0d. structures, typedef$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-free_dog.c 4-new_dog.c -o f +julien@ubuntu:~/0x0d. structures, typedef$ valgrind ./f +==22840== Memcheck, a memory error detector +==22840== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==22840== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==22840== Command: ./f +==22840== +My name is Poppy, and I am 3.5 :) - Woof! +==22840== +==22840== HEAP SUMMARY: +==22840== in use at exit: 0 bytes in 0 blocks +==22840== total heap usage: 4 allocs, 4 frees, 1,059 bytes allocated +==22840== +==22840== All heap blocks were freed -- no leaks are possible +==22840== +==22840== For counts of detected and suppressed errors, rerun with: -v +==22840== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x0d. structures, typedef$ +``` diff --git a/0x0F-function_pointers/TASKS.md b/0x0F-function_pointers/TASKS.md new file mode 100644 index 00000000..c7dd844c --- /dev/null +++ b/0x0F-function_pointers/TASKS.md @@ -0,0 +1,266 @@ +# 0x0F-function_pointers +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are function pointers and how to use them +* What does a function pointer exactly hold +* Where does a function pointer point to in the virtual memory + +## Resources Used +* [Function Pointer in C](https://www.geeksforgeeks.org/function-pointer-in-c/) +* [Pointers to Function](https://publications.gbdirect.co.uk//c_book/chapter5/function_pointers.html) +* [Function Pointers in C/C++](https://www.youtube.com/watch?v=ynYtgGUNelE) +* [Why Pointers to functions](https://www.youtube.com/watch?v=sxTFSDAZM8s) +* [Everything we need to know about pointers in C](https://boredzo.org/pointers/) + +## Compilation + +`gcc Wall -Werror -Wextra -pedantic -std=gnu89` + +## Task Completed + ++ [x] 0\. What's my name
_**[0-print_name.c](0-print_name.c)**_ contains a function that prints a name.
__Example__: +```c +julien@ubuntu:~/0x0e. Function pointers$ cat 0-main.c +#include +#include "function_pointers.h" + +/** + * print_name_as_is - prints a name as is + * @name: name of the person + * + * Return: Nothing. + */ +void print_name_as_is(char *name) +{ + printf("Hello, my name is %s\n", name); +} + +/** + * print_name_uppercase - print a name in uppercase + * @name: name of the person + * + * Return: Nothing. + */ +void print_name_uppercase(char *name) +{ + unsigned int i; + + printf("Hello, my uppercase name is "); + i = 0; + while (name[i]) + { + if (name[i] >= 'a' && name[i] <= 'z') + { + putchar(name[i] + 'A' - 'a'); + } + else + { + putchar(name[i]); + } + i++; + } +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + print_name("Bob", print_name_as_is); + print_name("Bob Dylan", print_name_uppercase); + printf("\n"); + return (0); +} +julien@ubuntu:~/0x0e. Function pointers$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-print_name.c -o a +julien@ubuntu:~/0x0e. Function pointers$ ./a +Hello, my name is Bob +Hello, my uppercase name is BOB DYLAN +julien@ubuntu:~/0x0e. Function pointers$ +``` ++ [x] 1\. If you spend too much time thinking about a thing, you'll never get it done
_**[1-array_iterator.c](1-array_iterator.c)**_ contains a function that executes a function given as a parameter on each element of an array.
__Example__: +```c +julien@ubuntu:~/0x0e. Function pointers$ cat 1-main.c +#include +#include "function_pointers.h" + +/** + * print_elem - prints an integer + * @elem: the integer to print + * + * Return: Nothing. + */ +void print_elem(int elem) +{ + printf("%d\n", elem); +} + +/** + * print_elem_hex - prints an integer, in hexadecimal + * @elem: the integer to print + * + * Return: Nothing. + */ +void print_elem_hex(int elem) +{ + printf("0x%x\n", elem); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int array[5] = {0, 98, 402, 1024, 4096}; + + array_iterator(array, 5, &print_elem); + array_iterator(array, 5, &print_elem_hex); + return (0); +} +julien@ubuntu:~/0x0e. Function pointers$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-array_iterator.c -o b +julien@ubuntu:~/0x0e. Function pointers$ ./b +0 +98 +402 +1024 +4096 +0x0 +0x62 +0x192 +0x400 +0x1000 +julien@ubuntu:~//0x0e. Function pointers$ +``` ++ [x] 2\. To hell with circumstances; I create opportunities
_**[2-int_index.c](2-int_index.c)**_ contains a function that searches for an integer.
__Example__: +```c +julien@ubuntu:~/0x0e. Function pointers$ cat 2-main.c +#include +#include "function_pointers.h" + +/** + * is_98 - check if a number is equal to 98 + * @elem: the integer to check + * + * Return: 0 if false, something else otherwise. + */ +int is_98(int elem) +{ + return (98 == elem); +} + +/** + * is_strictly_positive - check if a number is greater than 0 + * @elem: the integer to check + * + * Return: 0 if false, something else otherwise. + */ +int is_strictly_positive(int elem) +{ + return (elem > 0); +} + + +/** + * abs_is_98 - check if the absolute value of a number is 98 + * @elem: the integer to check + * + * Return: 0 if false, something else otherwise. + */ +int abs_is_98(int elem) +{ + return (elem == 98 || -elem == 98); +} + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(void) +{ + int array[20] = {0, -98, 98, 402, 1024, 4096, -1024, -98, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 98}; + int index; + + index = int_index(array, 20, is_98); + printf("%d\n", index); + index = int_index(array, 20, abs_is_98); + printf("%d\n", index); + index = int_index(array, 20, is_strictly_positive); + printf("%d\n", index); + return (0); +} +julien@ubuntu:~/0x0e. Function pointers$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-int_index.c -o c +julien@ubuntu:~/0x0e. Function pointers$ ./c +2 +1 +2 +julien@ubuntu:~/0x0e. Function pointers$ +``` ++ [x] 3\. A goal is not always meant to be reached, it often serves simply as something to aim at
_**[3-main.c](3-main.c)**_ contains a program that performs simple operations.
__Example__: +```c +julien@ubuntu:~/0x0e. Function pointers$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-op_functions.c 3-get_op_func.c -o calc +julien@ubuntu:~/0x0e. Function pointers$ ./calc 1 + 1 +2 +julien@ubuntu:~/0x0e. Function pointers$ ./calc 97 + 1 +98 +julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 / 10 +102 +julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 '*' 98 +100352 +julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 '\*' 98 +Error +julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 - 98 +926 +julien@ubuntu:~/0x0e. Function pointers$ ./calc 1024 '%' 98 +44 +julien@ubuntu:~/0x0e. Function pointers$ +``` ++ [x] 4\. Most hackers are young because young people tend to be adaptable. As long as you remain adaptable, you can always be a good hacker
_**[100-main_opcodes.c](100-main_opcodes.c)**_ contains a program that prints the [opcodes](https://en.wikipedia.org/wiki/Opcode) of its own main function. + + Usage: `./main number_of_bytes` + + Output format: + + the opcodes should be printed in hexadecimal, lowercase + + each opcode is two char long + + listing ends with a new line + + see example + + You are allowed to use `printf` and `atoi` + + You have to use atoi to convert the argument to an `int` + + If the number of argument is not the correct one, print `Error`, followed by a new line, and exit with the status `1` + + If the number of bytes is negative, print `Error`, followed by a new line, and exit with the status `2` + + You do not have to compile with any flags + + Note: if you want to translate your opcodes to assembly instructions, you can use, for instance [udcli](https://sourceforge.net/projects/udis86/). + + `je` is equivalent to `jz` + + depending on how you write your `main` function, and on which machine you compile your program, the opcodes (and by extension the assembly code) might be different than the example + +__Example__: +```c +julien@ubuntu:~/0x0e. Function pointers$ gcc -std=gnu89 100-main_opcodes.c -o main +julien@ubuntu:~/0x0e. Function pointers$ ./main 21 +55 48 89 e5 48 83 ec 30 89 7d dc 48 89 75 d0 83 7d dc 02 74 14 +julien@ubuntu:~/0x0e. Function pointers$ objdump -d -j.text -M intel main +[...] +00000000004005f6
: + 4005f6: 55 push rbp + 4005f7: 48 89 e5 mov rbp,rsp + 4005fa: 48 83 ec 30 sub rsp,0x30 + 4005fe: 89 7d dc mov DWORD PTR [rbp-0x24],edi + 400601: 48 89 75 d0 mov QWORD PTR [rbp-0x30],rsi + 400605: 83 7d dc 02 cmp DWORD PTR [rbp-0x24],0x2 + 400609: 74 14 je 40061f +[...] +julien@ubuntu:~/0x0e. Function pointers$ ./main 21 | udcli -64 -x -o 4005f6 +00000000004005f6 55 push rbp +00000000004005f7 4889e5 mov rbp, rsp +00000000004005fa 4883ec30 sub rsp, 0x30 +00000000004005fe 897ddc mov [rbp-0x24], edi +0000000000400601 488975d0 mov [rbp-0x30], rsi +0000000000400605 837ddc02 cmp dword [rbp-0x24], 0x2 +0000000000400609 7414 jz 0x40061f +julien@ubuntu:~/0x0e. Function pointers$ +``` diff --git a/0x0F-function_pointers/test/TASKS.md b/0x0F-function_pointers/test/TASKS.md new file mode 100644 index 00000000..84fc2ff8 --- /dev/null +++ b/0x0F-function_pointers/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +This repo contains main function for each task to test. diff --git a/0x10-variadic_functions/TASKS.md b/0x10-variadic_functions/TASKS.md new file mode 100644 index 00000000..48e71f73 --- /dev/null +++ b/0x10-variadic_functions/TASKS.md @@ -0,0 +1,47 @@ +# 0x10-variadic_functions +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are variadic functions +* How to use `va_start`, `va_arg` and `va_end` macros +* Why and how to use the `const` type qualifier + +## Resources Used +* [stdarg.h](https://en.wikipedia.org/wiki/Stdarg.h) +* [Vardiac Functions](https://www.gnu.org/software/libc/manual/html_node/Variadic-Functions.html) +* [Const Keyword](https://www.youtube.com/watch?v=1W4oyuOdXv8) + +## Tasks Completed + ++ [x] 0\. Beauty is variable, ugliness is constant
_**[0-sum_them_all.c](0-sum_them_all.c)**_ contains a function that returns the sum of all its parameters.
__Example__: +```c +julien@ubuntu:~/0x0f. variadic functions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-sum_them_all.c -o a +julien@ubuntu:~/0x0f. variadic functions$ ./a +1122 +500 +julien@ubuntu:~/0x0f. variadic functions$ +``` ++ [x] 1\. To be is to be the value of a variable
_**[1-print_numbers.c](1-print_numbers.c)**_ contains a function that prints numbers, followed by a new line.
__Example__: +```c +jjulien@ubuntu:~/0x0f. variadic functions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-print_numbers.c -o b +julien@ubuntu:~/0x0f. variadic functions$ ./b +0, 98, -1024, 402 +julien@ubuntu:~/0x0f. variadic functions$ +``` ++ [x] 2\. One woman's constant is another woman's variable
_**[2-print_strings.c](2-print_strings.c)**_ contains a function that prints strings, followed by a new line.
__Example__: +```c +julien@ubuntu:~/0x0f. Variadic functions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-print_strings.c -o c +julien@ubuntu:~/0x0f. Variadic functions$ ./c +Jay, Django +julien@ubuntu:~/0x0f. Variadic functions$ +``` ++ [x] 3\. To be is a to be the value of a variable
_**[3-print_all.c](3-print_all.c)**_ contains a function that prints anything.
__Example__: +```c +julien@ubuntu:~/0x0f. Variadic functions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-print_all.c -o d +julien@ubuntu:~/0x0f. Variadic functions$ ./d +B, 3, stSchool +julien@ubuntu:~/0x0f. Variadic functions$ +``` diff --git a/0x10-variadic_functions/test/TASKS.md b/0x10-variadic_functions/test/TASKS.md new file mode 100644 index 00000000..732a524a --- /dev/null +++ b/0x10-variadic_functions/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +This repo contains main function for each task to test diff --git a/0x12-singly_linked_lists/TASKS.md b/0x12-singly_linked_lists/TASKS.md new file mode 100644 index 00000000..32e2ee9a --- /dev/null +++ b/0x12-singly_linked_lists/TASKS.md @@ -0,0 +1,143 @@ +# 0x12-singly_linked_lists +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Linked List](https://www.geeksforgeeks.org/what-is-linked-list/) +* When and why using linked lists vs arrays +* How to build and use linked lists + +## Tasks Completed + ++ [x] 0\. Print list
_**[0-print_list.c](0-print_list.c)**_ contains a function that prints all the elements of a `list_t` list.
__Example__: +```c +julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-print_list.c -o a +julien@ubuntu:~/0x12. Singly linked lists$ ./a +[5] Hello +[5] World +-> 2 elements + +[0] (nil) +[5] World +-> 2 elements +julien@ubuntu:~/0x12. Singly linked lists$ +``` ++ [x] 1\. List length
_**[1-list_len.c](1-list_len.c)**_ contains a function that returns the number of elements in a linked `list_t` list.
__Example__: +```c +julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-list_len.c -o b +julien@ubuntu:~/0x12. Singly linked lists$ ./b +-> 2 elements +julien@ubuntu:~/0x12. Singly linked lists$ +``` ++ [x] 2\. Add node
_**[2-add_node.c](2-add_node.c)**_ contains a function that adds a new node at the beginning of a `list_t` list.
__Example__: +```c +julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-add_node.c 0-print_list.c -o c +julien@ubuntu:~/0x12. Singly linked lists$ ./c +[3] Zee +[7] William +[7] Tasneem +[6] Steven +[9] Sravanthi +[6] Siphan +[4] Rona +[4] Rick +[4] Rick +[7] Praylin +[5] Mason +[6] Marine +[4] Kris +[7] Josquin +[4] John +[4] John +[3] Joe +[6] Gloria +[7] Electra +[4] Dora +[6] Daniel +[6] Damian +[8] Chandler +[5] Bilal +[7] Bennett +[8] Augustin +[5] Asaia +[9] Alexandro +julien@ubuntu:~/0x12. Singly linked lists$ +``` ++ [x] 3\. Add node at the end
_**[3-add_node_end.c](3-add_node_end.c)**_ contains a function that adds a new node at the end of a `list_t` list.
__Example__: +```c +julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-add_node_end.c 0-print_list.c -o d +julien@ubuntu:~/0x12. Singly linked lists$ ./d +[4] Anne +[6] Colton +[6] Corbin +[6] Daniel +[6] Danton +[5] David +[4] Gary +[6] Holden +[3] Ian +[3] Ian +[3] Jay +[6] Jennie +[5] Jimmy +[6] Justin +[6] Kalson +[4] Kina +[7] Matthew +[3] Max +[7] Michael +[4] Ntuj +[6] Philip +[7] Richard +[8] Samantha +[6] Stuart +[5] Swati +[7] Timothy +[6] Victor +[6] Walton +julien@ubuntu:~/0x12. Singly linked lists$ +``` ++ [x] 4\. Free list
_**[4-free_list.c](4-free_list.c)**_ contains a function that frees a `list_t` list.
__Example__: +```c +julien@ubuntu:~/0x12. Singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-free_list.c 3-add_node_end.c 0-print_list.c -o e +julien@ubuntu:~/0x12. Singly linked lists$ valgrind ./e +==3598== Memcheck, a memory error detector +==3598== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==3598== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==3598== Command: ./e +==3598== +[6] Bob +[1] & +[3] Kris +[4] love +[3] asm +==3598== +==3598== HEAP SUMMARY: +==3598== in use at exit: 0 bytes in 0 blocks +==3598== total heap usage: 11 allocs, 11 frees, 1,166 bytes allocated +==3598== +==3598== All heap blocks were freed -- no leaks are possible +==3598== +==3598== For counts of detected and suppressed errors, rerun with: -v +==3598== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x12. Singly linked lists$ +``` ++ [x] 5\. The Hare and the Tortoise
_**[100-first.c](100-first.c)**_ contains a function that prints `You're beat! and yet, you must allow,\nI bore my house upon my back!\n` before the `main` function is executed.
__Example__: +```c +julien@ubuntu:~/$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-first.c -o first +julien@ubuntu:~/$ ./first +You're beat! and yet, you must allow, +I bore my house upon my back! +(A tortoise, having pretty good sense of a hare's nature, challenges one to a race.) +julien@ubuntu:~/$ +``` ++ [x] 6\. Real programmers can write assembly code in any language
_**[101-hello_holberton.asm](101-hello_holberton.asm)**_ is a 64-bit program in assembly that prints `Hello, Holberton`, followed by a new line.
__Example__: +```c +julien@ubuntu:~/$ nasm -f elf64 101-hello_holberton.asm && gcc -no-pie -std=gnu89 101-hello_holberton.o -o hello +julien@ubuntu:~/$ ./hello +Hello, Holberton +julien@ubuntu:~/$ +``` diff --git a/0x12-singly_linked_lists/test/TASKS.md b/0x12-singly_linked_lists/test/TASKS.md new file mode 100644 index 00000000..4a0f894e --- /dev/null +++ b/0x12-singly_linked_lists/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +This repo contains main function for each task. diff --git a/0x13-more_singly_linked_lists/TASKS.md b/0x13-more_singly_linked_lists/TASKS.md new file mode 100644 index 00000000..372795fd --- /dev/null +++ b/0x13-more_singly_linked_lists/TASKS.md @@ -0,0 +1,393 @@ +# 0x13. C - More singly linked lists +>project done in 2 days + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* How to use linked lists + +## Tasks Completed + ++ [x] 0\. Print list
_**[0-print_listint.c](0-print_listint.c)**_ contains a function that prints all the elements of a `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-print_listint.c -o a +julien@ubuntu:~/0x13. More singly linked lists$ ./a +9 +8 +-> 2 elements +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 1\. List length
_**[1-listint_len.c](1-listint_len.c)**_ contains a function that returns the number of elements in a linked `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-listint_len.c -o b +julien@ubuntu:~/0x13. More singly linked lists$ ./b +-> 2 elements +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 2\. Add node
_**[2-add_nodeint.c](2-add_nodeint.c)**_ contains a function that adds a new node at the beginning of a `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-add_nodeint.c 0-print_listint.c -o c +julien@ubuntu:~/0x13. More singly linked lists$ ./c +1024 +402 +98 +4 +3 +2 +1 +0 +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 3\. Add node at the end
_**[3-add_nodeint_end.c](3-add_nodeint_end.c)**_ contains a function that adds a new node at the end of a `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-add_nodeint_end.c 0-print_listint.c -o d +julien@ubuntu:~/0x13. More singly linked lists$ ./d +0 +1 +2 +3 +4 +98 +402 +1024 +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 4\. Free list
_**[4-free_listint.c](4-free_listint.c)**_ contains a function that frees a `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 3-add_nodeint_end.c 0-print_listint.c 4-free_listint.c -o e +julien@ubuntu:~/0x13. More singly linked lists$ valgrind ./e +==3643== Memcheck, a memory error detector +==3643== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==3643== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==3643== Command: ./e +==3643== +0 +1 +2 +3 +4 +98 +402 +1024 +==3643== +==3643== HEAP SUMMARY: +==3643== in use at exit: 0 bytes in 0 blocks +==3643== total heap usage: 9 allocs, 9 frees, 1,152 bytes allocated +==3643== +==3643== All heap blocks were freed -- no leaks are possible +==3643== +==3643== For counts of detected and suppressed errors, rerun with: -v +==3643== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 5\. Free
_**[5-free_listint2.c](5-free_listint2.c)**_ contains a function that frees a `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 3-add_nodeint_end.c 0-print_listint.c 5-free_listint2.c -o f +julien@ubuntu:~/0x13. More singly linked lists$ valgrind ./f +==3843== Memcheck, a memory error detector +==3843== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==3843== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==3843== Command: ./f +==3843== +0 +1 +2 +3 +4 +98 +402 +1024 +(nil) +==3843== +==3843== HEAP SUMMARY: +==3843== in use at exit: 0 bytes in 0 blocks +==3843== total heap usage: 9 allocs, 9 frees, 1,152 bytes allocated +==3843== +==3843== All heap blocks were freed -- no leaks are possible +==3843== +==3843== For counts of detected and suppressed errors, rerun with: -v +==3843== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 6\. Pop
_**[6-pop_listint.c](6-pop_listint.c)**_ contains a function that deletes the head node of a `listint_t` linked list, and returns the head node’s data (n).
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 6-main.c 3-add_nodeint_end.c 0-print_listint.c 5-free_listint2.c 6-pop_listint.c -o g +julien@ubuntu:~/0x13. More singly linked lists$ valgrind ./g +==4369== Memcheck, a memory error detector +==4369== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==4369== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==4369== Command: ./g +==4369== +0 +1 +2 +3 +4 +98 +402 +1024 +- 0 +1 +2 +3 +4 +98 +402 +1024 +- 1 +2 +3 +4 +98 +402 +1024 +(nil) +==4369== +==4369== HEAP SUMMARY: +==4369== in use at exit: 0 bytes in 0 blocks +==4369== total heap usage: 9 allocs, 9 frees, 1,152 bytes allocated +==4369== +==4369== All heap blocks were freed -- no leaks are possible +==4369== +==4369== For counts of detected and suppressed errors, rerun with: -v +==4369== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 7\. Get node at index
_**[7-get_nodeint.c](7-get_nodeint.c)**_ contains a function that returns the nth node of a `listint_t` linked list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 7-main.c 3-add_nodeint_end.c 0-print_listint.c 5-free_listint2.c 7-get_nodeint.c -o h +julien@ubuntu:~/0x13. More singly linked lists$ ./h +0 +1 +2 +3 +4 +98 +402 +1024 +98 +0 +1 +2 +3 +4 +98 +402 +1024 +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 8\. Sum list
_**[8-sum_listint.c](8-sum_listint.c)**_ contains a function that returns the sum of all the data (n) of a `listint_t` linked list.
__Example__: +```c +julien@ubuntu:~/c0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-main.c 3-add_nodeint_end.c 5-free_listint2.c 8-sum_listint.c -o i +julien@ubuntu:~/0x13. More singly linked lists$ ./i +sum = 1534 +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 9\. Insert
_**[9-insert_nodeint.c](9-insert_nodeint.c)**_ contains a function that inserts a new node at a given position.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 9-main.c 3-add_nodeint_end.c 0-print_listint.c 5-free_listint2.c 9-insert_nodeint.c -o j +julien@ubuntu:~/0x13. More singly linked lists$ ./j +0 +1 +2 +3 +4 +98 +402 +1024 +----------------- +0 +1 +2 +3 +4 +4096 +98 +402 +1024 +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 10\. Delete at index
_**[10-delete_nodeint.c](10-delete_nodeint.c)**_ contains a function that deletes the node at index `index` of a `listint_t` linked list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 10-main.c 3-add_nodeint_end.c 0-print_listint.c 5-free_listint2.c 10-delete_nodeint.c -o k +julien@ubuntu:~/0x13. More singly linked lists$ valgrind ./k +==5571== Memcheck, a memory error detector +==5571== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==5571== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==5571== Command: ./k +==5571== +0 +1 +2 +3 +4 +98 +402 +1024 +----------------- +0 +1 +2 +3 +4 +402 +1024 +----------------- +1 +2 +3 +4 +402 +1024 +----------------- +2 +3 +4 +402 +1024 +----------------- +3 +4 +402 +1024 +----------------- +4 +402 +1024 +----------------- +402 +1024 +----------------- +1024 +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +==5571== +==5571== HEAP SUMMARY: +==5571== in use at exit: 0 bytes in 0 blocks +==5571== total heap usage: 9 allocs, 9 frees, 1,152 bytes allocated +==5571== +==5571== All heap blocks were freed -- no leaks are possible +==5571== +==5571== For counts of detected and suppressed errors, rerun with: -v +==5571== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 11\. Reverse list
_**[100-reverse_listint.c](100-reverse_listint.c)**_ contains a function that reverses a `listint_t` linked list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 3-add_nodeint_end.c 0-print_listint.c 5-free_listint2.c 100-reverse_listint.c -o l +julien@ubuntu:~/0x13. More singly linked lists$ valgrind ./l +==3117== Memcheck, a memory error detector +==3117== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==3117== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==3117== Command: ./l +==3117== +0 +1 +2 +3 +4 +98 +402 +1024 +1024 +402 +98 +4 +3 +2 +1 +0 +==3117== +==3117== HEAP SUMMARY: +==3117== in use at exit: 0 bytes in 0 blocks +==3117== total heap usage: 9 allocs, 9 frees, 1,152 bytes allocated +==3117== +==3117== All heap blocks were freed -- no leaks are possible +==3117== +==3117== For counts of detected and suppressed errors, rerun with: -v +==3117== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 12\. Print (safe version)
_**[101-print_listint_safe.c](101-print_listint_safe.c)**_ contains a function that prints a `listint_t` linked list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-main.c 2-add_nodeint.c 101-print_listint_safe.c -o m +julien@ubuntu:~/0x13. More singly linked lists$ ./m +[0x1b500f0] 1024 +[0x1b500d0] 402 +[0x1b500b0] 98 +[0x1b50090] 4 +[0x1b50070] 3 +[0x1b50050] 2 +[0x1b50030] 1 +[0x1b50010] 0 +[0x1b50600] 1024 +[0x1b505e0] 402 +[0x1b505c0] 98 +[0x1b505a0] 4 +[0x1b50580] 3 +[0x1b50560] 2 +[0x1b50540] 1 +[0x1b50110] 0 +-> [0x1b505c0] 98 +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 13\. Free (safe version)
_**[102-free_listint_safe.c](102-free_listint_safe.c)**_ contains a function that frees a `listint_t` list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 102-main.c 2-add_nodeint.c 101-print_listint_safe.c 102-free_listint_safe.c -o n +julien@ubuntu:~/0x13. More singly linked lists$ ./n +[0x11260f0] 1024 +[0x11260d0] 402 +[0x11260b0] 98 +[0x1126090] 4 +[0x1126070] 3 +[0x1126050] 2 +[0x1126030] 1 +[0x1126010] 0 +[0x1126600] 1024 +[0x11265e0] 402 +[0x11265c0] 98 +[0x11265a0] 4 +[0x1126580] 3 +[0x1126560] 2 +[0x1126540] 1 +[0x1126110] 0 +-> [0x11265c0] 98 +(nil), (nil) +julien@ubuntu:~/0x13. More singly linked lists$ +``` ++ [x] 14\. Find the loop
_**[103-find_loop.c](103-find_loop.c)**_ contains a function that finds the loop in a linked list.
__Example__: +```c +julien@ubuntu:~/0x13. More singly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 103-main.c 2-add_nodeint.c 101-print_listint_safe.c 102-free_listint_safe.c 103-find_loop.c -o o +julien@ubuntu:~/0x13. More singly linked lists$ ./o +[0x13700f0] 1024 +[0x13700d0] 402 +[0x13700b0] 98 +[0x1370090] 4 +[0x1370070] 3 +[0x1370050] 2 +[0x1370030] 1 +[0x1370010] 0 +[0x1370560] 1024 +[0x1370540] 402 +[0x1370010] 98 +[0x1370030] 7 +[0x1370050] 6 +[0x1370070] 5 +[0x1370090] 4 +[0x13700b0] 3 +[0x13700d0] 2 +[0x13700f0] 1 +[0x1370110] 0 +-> [0x1370030] 7 +Loop starts at [0x1370030] 7 +julien@ubuntu:~/0x13. More singly linked lists$ +``` diff --git a/0x13-more_singly_linked_lists/failed test/TASKS.md b/0x13-more_singly_linked_lists/failed test/TASKS.md new file mode 100644 index 00000000..521b48b6 --- /dev/null +++ b/0x13-more_singly_linked_lists/failed test/TASKS.md @@ -0,0 +1,31 @@ +# File main_5.c + +it was a test file for [101-print_listint_safe.c](https://github.com/bekalue/alx-low_level_programming/blob/master/0x13-more_singly_linked_lists/101-print_listint_safe.c) but showed error. + +## Compilation + +`gcc -Wall -pedantic -Werror -Wextra -std=gnu89 main_5.c 2-add_nodeint.c 101-print_listint_safe.c -o m` + +`./m` + +output: + +``` +1 +4 +-> 3 +2 +``` + +Expected: + +``` +1 +4 +3 +2 +5 +-> 3 +5 +``` + diff --git a/0x13-more_singly_linked_lists/test/TASKS.md b/0x13-more_singly_linked_lists/test/TASKS.md new file mode 100644 index 00000000..9ce244ac --- /dev/null +++ b/0x13-more_singly_linked_lists/test/TASKS.md @@ -0,0 +1,3 @@ +# Test Files + +This repo contains main functions for each task which are used to test those functions. diff --git a/0x14-bit_manipulation/TASKS.md b/0x14-bit_manipulation/TASKS.md new file mode 100644 index 00000000..6495e15d --- /dev/null +++ b/0x14-bit_manipulation/TASKS.md @@ -0,0 +1,96 @@ +# 0x14. C - Bit manipulation +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[Bit Manipulation](https://medium.com/@peejayhack/bitwise-operators-the-hows-and-the-whys-5ccea3abafa3) +* How to manipulate bits and use bitwise operators. + +## Tasks Completed + ++ [x] 0\. 0
_**[0-binary_to_uint.c](0-binary_to_uint.c)**_ contains a function that converts a binary number to an `unsigned int`.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-binary_to_uint.c -o a +julien@ubuntu:~/0x14. Binary$ ./a +1 +5 +0 +98 +402 +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 1\. 1
_**[1-print_binary.c](1-print_binary.c)**_ contains a function that prints the binary representation of a number.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-print_binary.c _putchar.c -o b +julien@ubuntu:~/0x14. Binary$ ./b +0 +1 +1100010 +10000000000 +10000000001 +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 2\. 10
_**[2-get_bit.c](2-get_bit.c)**_ contains a function that returns the value of a bit at a given index.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-get_bit.c -o c +julien@ubuntu:~/0x14. Binary$ ./c +1 +1 +0 +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 3\. 11
_**[3-set_bit.c](3-set_bit.c)**_ contains a function that sets the value of a bit to `1` at a given index.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-set_bit.c -o d +julien@ubuntu:~/0x14. Binary$ ./d +1056 +1024 +99 +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 4\. 100
_**[4-clear_bit.c](4-clear_bit.c)**_ contains a function that sets the value of a bit to `0` at a given index.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 4-clear_bit.c -o e +julien@ubuntu:~/0x14. Binary$ ./e +0 +0 +96 +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 5\. 101
_**[5-flip_bits.c](5-flip_bits.c)**_ contains a function that returns the number of bits you would need to flip to get from one number to another.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 5-flip_bits.c -o f +julien@ubuntu:~/0x14. Binary$ ./f +2 +5 +3 +1 +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 6\. Endianness
_**[100-get_endianness.c](100-get_endianness.c)**_ contains a function that checks the endianness.
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-get_endianness.c -o h +julien@ubuntu:~/0x14. Binary$ ./h +Little Endian +julien@ubuntu:~/0x14. Binary$ lscpu | head +Architecture: x86_64 +CPU op-mode(s): 32-bit, 64-bit +Byte Order: Little Endian +CPU(s): 1 +On-line CPU(s) list: 0 +Thread(s) per core: 1 +Core(s) per socket: 1 +Socket(s): 1 +NUMA node(s): 1 +Vendor ID: GenuineIntel +julien@ubuntu:~/0x14. Binary$ +``` ++ [x] 7\. Crackme3
_**[101-password](101-password)**_ contains the password for [this program](https://github.com/holbertonschool/0x13.c/blob/b2dce6991a499f451ffd5b9522ac5d94ecb7bb56/crackme3).
__Example__: +```c +julien@ubuntu:~/0x14. Binary$ ./crackme3 `cat 101-password` +Congratulations! +julien@ubuntu:~/0x14. Binary$ +``` diff --git a/0x14-bit_manipulation/test/TASKS.md b/0x14-bit_manipulation/test/TASKS.md new file mode 100644 index 00000000..fdc8ca2c --- /dev/null +++ b/0x14-bit_manipulation/test/TASKS.md @@ -0,0 +1,5 @@ +# Test files + +This Repo contains main function for each tasks. + +and crackme source file of [this program](https://github.com/holbertonschool/0x13.c/blob/b2dce6991a499f451ffd5b9522ac5d94ecb7bb56/crackme3). diff --git a/0x15-file_io/TASKS.md b/0x15-file_io/TASKS.md new file mode 100644 index 00000000..f61bd613 --- /dev/null +++ b/0x15-file_io/TASKS.md @@ -0,0 +1,348 @@ +# 0x15. C - File I/O +>project done in 1 day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +>needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. + +[File Descriptor](https://en.wikipedia.org/wiki/File_descriptor) +* What are file descriptors +* What are the 3 standard file descriptors, purpose, and `POSIX`names +* What is a system call +* What is the the difference between a function and a system call + +[open() read() write()](https://www.youtube.com/watch?v=dP3N8g7h8gY) +* How to create, open, close, read and write files +* How to use I/O system calls `open`, `close`, `read` and `write` +* What are and how to use flags `O_RDONLY` `O_WRONLY` `O_RDWR` +* What are file permissions, and how to set them when creating a new file with the `open` system call + +## Tasks Completed + ++ [x] 0\. Tread lightly, she is near
_**[0-read_textfile.c](0-read_textfile.c)**_ contains a function that reads a text file and prints it to the `POSIX` standard output.
__Example__: +```c +julien@ubuntu:~/0x15. File descriptors and permissions$ cat Requiescat +Requiescat +by Oscar Wilde + +Tread lightly, she is near +Under the snow, +Speak gently, she can hear +The daisies grow. + +All her bright golden hair +Tarnished with rust, +She that was young and fair +Fallen to dust. + +Lily-like, white as snow, +She hardly knew +She was a woman, so +Sweetly she grew. + +Coffin-board, heavy stone, +Lie on her breast, +I vex my heart alone, +She is at rest. + +Peace, Peace, she cannot hear +Lyre or sonnet, +All my life's buried here, +Heap earth upon it. +julien@ubuntu:~/0x15. File descriptors and permissions$ cat 0-main.c +#include +#include +#include "main.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(int ac, char **av) +{ + ssize_t n; + + if (ac != 2) + { + dprintf(2, "Usage: %s filename\n", av[0]); + exit(1); + } + n = read_textfile(av[1], 114); + printf("\n(printed chars: %li)\n", n); + n = read_textfile(av[1], 1024); + printf("\n(printed chars: %li)\n", n); + return (0); +} +julien@ubuntu:~/0x15. File descriptors and permissions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-read_textfile.c -o a +julien@ubuntu:~/0x15. File descriptors and permissions$ ./a Requiescat +Requiescat +by Oscar Wilde + +Tread lightly, she is near +Under the snow, +Speak gently, she can hear +The daisies grow. +(printed chars: 114) +Requiescat +by Oscar Wilde + +Tread lightly, she is near +Under the snow, +Speak gently, she can hear +The daisies grow. + +All her bright golden hair +Tarnished with rust, +She that was young and fair +Fallen to dust. + +Lily-like, white as snow, +She hardly knew +She was a woman, so +Sweetly she grew. + +Coffin-board, heavy stone, +Lie on her breast, +I vex my heart alone, +She is at rest. + +Peace, Peace, she cannot hear +Lyre or sonnet, +All my life's buried here, +Heap earth upon it. + +(printed chars: 468) +julien@ubuntu:~/0x15. File descriptors and permissions$ +``` ++ [x] 1\. Under the snow
_**[1-create_file.c](1-create_file.c)**_ contains a function that creates a file.
__Example__: +```c +julien@ubuntu:~/0x15. File descriptors and permissions$ cat 1-main.c +#include +#include +#include "main.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(int ac, char **av) +{ + int res; + + if (ac != 3) + { + dprintf(2, "Usage: %s filename text\n", av[0]); + exit(1); + } + res = create_file(av[1], av[2]); + printf("-> %i)\n", res); + return (0); +} +julien@ubuntu:~/0x15. File descriptors and permissions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-create_file.c -o b +julien@ubuntu:~/0x15. File descriptors and permissions$ ./b hello world +-> 1) +julien@ubuntu:~/0x15. File descriptors and permissions$ ls -l hello +-rw------- 1 julien julien 5 Dec 3 14:28 hello +julien@ubuntu:~/0x15. File descriptors and permissions$ cat hello +worldjulien@ubuntu:~/0x15. File descriptors and permis$ +``` ++ [x] 2\. Speak gently, she can hear
_**[2-append_text_to_file.c](2-append_text_to_file.c)**_ contains a function that appends text at the end of a file.
__Example__: +```c +julien@ubuntu:~/0x15. File descriptors and permissions$ cat 2-main.c +#include +#include +#include "main.h" + +/** + * main - check the code + * + * Return: Always 0. + */ +int main(int ac, char **av) +{ + int res; + + if (ac != 3) + { + dprintf(2, "Usage: %s filename text\n", av[0]); + exit(1); + } + res = append_text_to_file(av[1], av[2]); + printf("-> %i)\n", res); + return (0); +} +julien@ubuntu:~/0x15. File descriptors and permissions$ echo -n Hello > hello +julien@ubuntu:~/0x15. File descriptors and permissions$ ls -l hello +-rw-rw-r-- 1 julien julien 5 Dec 3 14:48 hello +julien@ubuntu:~/0x15. File descriptors and permissions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-append_text_to_file.c -o c +julien@ubuntu:~/0x15. File descriptors and permissions$ ./c hello " World! +> " +-> 1) +julien@ubuntu:~/0x15. File descriptors and permissions$ cat hello +Hello World! +julien@ubuntu:~/0x15. File descriptors and permissions$ +``` ++ [x] 3\. cp
_**[3-cp.c](3-cp.c)**_ contains a program that copies the content of a file to another file. + + Usage: `cp file_from file_to` + + if the number of argument is not the correct one, exit with code `97` and print `Usage: cp file_from file_t`o, followed by a new line, on the `POSIX` standard error + + if `file_to` already exists, truncate it + + if `file_from` does not exist, or if you can not read it, exit with code `98` and print `Error: Can't read from file NAME_OF_THE_FILE`, followed by a new line, on the `POSIX` standard error + + where `NAME_OF_THE_FILE` is the first argument passed to your program + + if you can not create or if `write` to `file_to` fails, exit with code `99` and print `Error: Can't write to NAME_OF_THE_FILE`, followed by a new line, on the `POSIX` standard error + + where `NAME_OF_THE_FILE` is the second argument passed to your program + + if you can not close a file descriptor , exit with code `100` and print `Error: Can't close fd FD_VALUE`, followed by a new line, on the `POSIX` standard error + + where `FD_VALUE` is the value of the file descriptor + + Permissions of the created file: `rw-rw-r--`. If the file already exists, do not change the permissions + + You must read `1,024` bytes at a time from the `file_from` to make less system calls. Use a buffer + + You are allowed to use `dprintf` + +__Example__: +```c +julien@ubuntu:~/0x15. File descriptors and permissions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-cp.c -o cp +julien@ubuntu:~/0x15. File descriptors and permissions$ cat incitatous +Why you should think twice before putting pictures on social media. +(What you always wanted to know about @Incitatous) +#PrivacyAware +http://imgur.com/a/Mq1tc +julien@ubuntu:~/0x15. File descriptors and permissions$ ./cp incitatous Incitatous +julien@ubuntu:~/0x15. File descriptors and permissions$ ls -l Incitatous +-rw-rw-r-- 1 julien julien 158 Dec 3 15:39 Incitatous +julien@ubuntu:~/0x15. File descriptors and permissions$ cat Incitatous +Why you should think twice before putting pictures on social media. +(What you always wanted to know about @Incitatous) +#PrivacyAware +http://imgur.com/a/Mq1tc +julien@ubuntu:~/0x15. File descriptors and permissions$ +``` ++ [x] 4\. elf
_**[100-elf_header.c](100-elf_header.c)**_ contains a program that displays the information contained in the `ELF` header at the start of an `ELF` file. + + Usage: `elf_header elf_filename` + + Displayed information: (no less, no more, do not include trailing whitespace) + + Magic + + Class + + Data + + Version + + OS/ABI + + ABI Version + + Type + + Entry point address + + Format: the same as `readelf -h` (version `2.26.1`) + + If the file is not an `ELF` file, or on error, exit with status code `98` and display a comprehensive error message to `stderr` + + You are allowed to use `lseek` once + + You are allowed to use `read` a maximum of 2 times at runtime + + man `elf`, `readelf` +__Example__: +```c +julien@ubuntu:~/0x15. File descriptors and permissions$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-elf_header.c -o elf_header +julien@ubuntu:~/0x15. File descriptors and permissions$ ./elf_header ubuntu64 +ELF Header: + Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 + Class: ELF64 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: EXEC (Executable file) + Entry point address: 0x400600 +julien@ubuntu:~/0x15. File descriptors and permissions$ readelf --version +GNU readelf (GNU Binutils for Ubuntu) 2.26.1 +Copyright (C) 2015 Free Software Foundation, Inc. +This program is free software; you may redistribute it under the terms of +the GNU General Public License version 3 or (at your option) any later version. +This program has absolutely no warranty. +julien@ubuntu:~/0x15. File descriptors and permissions$ readelf -h ubuntu64 +ELF Header: + Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 + Class: ELF64 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: EXEC (Executable file) + Machine: Advanced Micro Devices X86-64 + Version: 0x1 + Entry point address: 0x400600 + Start of program headers: 64 (bytes into file) + Start of section headers: 6936 (bytes into file) + Flags: 0x0 + Size of this header: 64 (bytes) + Size of program headers: 56 (bytes) + Number of program headers: 9 + Size of section headers: 64 (bytes) + Number of section headers: 31 + Section header string table index: 28 +julien@ubuntu:~/0x15. File descriptors and permissions$ ./elf_header /lib/ld-linux.so.2 +ELF Header: + Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: DYN (Shared object file) + Entry point address: 0xac0 +julien@ubuntu:~/0x15. File descriptors and permissions$ readelf -h /lib/ld-linux.so.2 +ELF Header: + Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: DYN (Shared object file) + Machine: Intel 80386 + Version: 0x1 + Entry point address: 0xac0 + Start of program headers: 52 (bytes into file) + Start of section headers: 145756 (bytes into file) + Flags: 0x0 + Size of this header: 52 (bytes) + Size of program headers: 32 (bytes) + Number of program headers: 7 + Size of section headers: 40 (bytes) + Number of section headers: 24 + Section header string table index: 23 +julien@ubuntu:~/0x15. File descriptors and permissions$ ./elf_header netbsd32 +ELF Header: + Magic: 7f 45 4c 46 01 01 01 02 00 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - NetBSD + ABI Version: 0 + Type: EXEC (Executable file) + Entry point address: 0x80484c0 +julien@ubuntu:~/0x15. File descriptors and permissions$ ./elf_header sortix32 +ELF Header: + Magic: 7f 45 4c 46 01 01 01 53 00 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: + ABI Version: 0 + Type: EXEC (Executable file) + Entry point address: 0x80484c0 +julien@ubuntu:~/0x15. File descriptors and permissions$ ./elf_header solaris32 +ELF Header: + Magic: 7f 45 4c 46 01 01 01 06 01 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - Solaris + ABI Version: 1 + Type: EXEC (Executable file) + Entry point address: 0x8052400 +julien@ubuntu:~/0x15. File descriptors and permissions$ ./elf_header sparc32 +ELF Header: + Magic: 7f 45 4c 46 01 02 01 00 00 00 00 00 00 00 00 00 + Class: ELF32 + Data: 2's complement, big endian + Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: EXEC (Executable file) + Entry point address: 0x10d20 +julien@ubuntu:~/0x15. File descriptors and permissions$ +``` diff --git a/0x17-doubly_linked_lists/TASKS.md b/0x17-doubly_linked_lists/TASKS.md new file mode 100644 index 00000000..2172b2f6 --- /dev/null +++ b/0x17-doubly_linked_lists/TASKS.md @@ -0,0 +1,195 @@ +# 0x17. C - Doubly linked lists +> Project done in 1 day. + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) +![Pycode style](https://img.shields.io/badge/-pycodestyle-brightgreen) + +## Learning Objectives +> needed to be [explained](https://fs.blog/feynman-learning-technique/) after project + +[Doubly Linked List](https://www.youtube.com/watch?v=k0pjD12bzP0) +* What is a doubly linked list +* How to use doubly linked lists + +## Tasks Completed ⚓ + ++ [x] 0\. Print list
_**[0-print_dlistint.c](0-print_dlistint.c)**_ contains a function that prints all the elements of a `dlistint_t` list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-print_dlistint.c -o a +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./a +9 +8 +-> 2 elements +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 1\. List length
_**[1-dlistint_len.c](1-dlistint_len.c)**_ contains a function that returns the number of elements in a linked `dlistint_t` list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-dlistint_len.c -o b +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./b +-> 2 elements +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 2\. Add node
_**[2-add_dnodeint.c](2-add_dnodeint.c)**_ contains a function that adds a new node at the beginning of a `dlistint_t` list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 2-add_dnodeint.c 0-print_dlistint.c -o c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./c +1024 +402 +98 +4 +3 +2 +1 +0 +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 3\. Add node at the end
_**[3-add_dnodeint_end.c](3-add_dnodeint_end.c)**_ contains a function that adds a new node at the end of a `dlistint_t` list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 3-add_dnodeint_end.c 0-print_dlistint.c -o d +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./d +0 +1 +2 +3 +4 +98 +402 +1024 +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 4\. Free list
_**[4-free_dlistint.c](4-free_dlistint.c)**_ contains a function that frees a `dlistint_t` list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 3-add_dnodeint_end.c 0-print_dlistint.c 4-free_dlistint.c -o e +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ valgrind ./e +==4197== Memcheck, a memory error detector +==4197== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. +==4197== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info +==4197== Command: ./e +==4197== +0 +1 +2 +3 +4 +98 +402 +1024 +==4197== +==4197== HEAP SUMMARY: +==4197== in use at exit: 0 bytes in 0 blocks +==4197== total heap usage: 9 allocs, 9 frees, 1,216 bytes allocated +==4197== +==4197== All heap blocks were freed -- no leaks are possible +==4197== +==4197== For counts of detected and suppressed errors, rerun with: -v +==4197== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 5\. Get node at index
_**[5-get_dnodeint.c](5-get_dnodeint.c)**_ contains a function that returns the nth node of a `dlistint_t` linked list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 3-add_dnodeint_end.c 0-print_dlistint.c 4-free_dlistint.c 5-get_dnodeint.c -o h +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./h +0 +1 +2 +3 +4 +98 +402 +1024 +98 +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 6\. Sum list
_**[6-sum_dlistint.c](6-sum_dlistint.c)**_ contains a function that returns the sum of all the data (n) of a `dlistint_t` linked list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra 6-main.c -std=gnu89 3-add_dnodeint_end.c 4-free_dlistint.c 6-sum_dlistint.c -o i +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./i +sum = 1534 +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 7\. Insert at index
_**[7-insert_dnodeint.c](7-insert_dnodeint.c)**_ contains a function that inserts a new node at a given position.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 7-main.c 2-add_dnodeint.c 3-add_dnodeint_end.c 0-print_dlistint.c 4-free_dlistint.c 7-insert_dnodeint.c -o j +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./j +0 +1 +2 +3 +4 +98 +402 +1024 +----------------- +0 +1 +2 +3 +4 +4096 +98 +402 +1024 +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 8\. Delete at index
_**[8-delete_dnodeint.c](8-delete_dnodeint.c)**_ contains a function that deletes the node at `index` index of a `dlistint_t` linked list.
__Example__: +```c +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 8-main.c 3-add_dnodeint_end.c 0-print_dlistint.c 4-free_dlistint.c 8-delete_dnodeint.c -o k +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ ./k +0 +1 +2 +3 +4 +98 +402 +1024 +----------------- +0 +1 +2 +3 +4 +402 +1024 +----------------- +1 +2 +3 +4 +402 +1024 +----------------- +2 +3 +4 +402 +1024 +----------------- +3 +4 +402 +1024 +----------------- +4 +402 +1024 +----------------- +402 +1024 +----------------- +1024 +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +----------------- +bekalue@BEKALU-PC:~/0x17. Doubly linked lists$ +``` ++ [x] 9\. Crackme4
_**[100-password](100-password)**_ contains the password for [crackme4](https://github.com/holbertonschool/0x17.c). ++ [x] 10\. Palindromes
_**[102-result](102-result)**_ contains he largest palindrome made from the product of two 3-digit numbers. ++ [x] 11\. crackme5
_**[103-keygen.c](103-keygen.c)**_ contains a keygen for [crackme5](https://github.com/holbertonschool/0x17.c). diff --git a/0x18-dynamic_libraries/TASKS.md b/0x18-dynamic_libraries/TASKS.md new file mode 100644 index 00000000..d489ee75 --- /dev/null +++ b/0x18-dynamic_libraries/TASKS.md @@ -0,0 +1,217 @@ +# 0x18. C - Dynamic libraries +>project Done in 1 Day + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What is a dynamic library, how does it work, how to create one, and how to use it +* What is the environment variable `$LD_LIBRARY_PATH` and how to use it +* What are the differences between static and shared libraries +* Basic usage `nm`, `ldd`, `ldconfig` + +## Resources +* [What is difference between Dynamic and Static library (Static and Dynamic linking)](https://www.youtube.com/watch?v=eW5he5uFBNM) +* [create dynamic libraries on Linux](https://medium.com/@The_Mad_Zaafa/creating-and-using-dynamic-libraries-c-a9d344822ed0) +* [Technical Writing](./TECH.md) + +## Tasks + ++ [x] 0\. A library is not a luxury but one of the necessities of life
_**[libdynamic.so](libdynamic.so)**_ contains the functions with the following prototypes from previous projects in this repository:
+ ```c + int _putchar(char c); + int _islower(int c); + int _isalpha(int c); + int _abs(int n); + int _isupper(int c); + int _isdigit(int c); + int _strlen(char *s); + void _puts(char *s); + char *_strcpy(char *dest, char *src); + int _atoi(char *s); + char *_strcat(char *dest, char *src); + char *_strncat(char *dest, char *src, int n); + char *_strncpy(char *dest, char *src, int n); + int _strcmp(char *s1, char *s2); + char *_memset(char *s, char b, unsigned int n); + char *_memcpy(char *dest, char *src, unsigned int n); + char *_strchr(char *s, char c); + unsigned int _strspn(char *s, char *accept); + char *_strpbrk(char *s, char *accept); + char *_strstr(char *haystack, char *needle); + ``` +
__Example__:
+ ```c + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ls -la lib* + -rwxrwxr-x 1 julien julien 13632 Jan 7 06:25 libdynamic.so + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ nm -D libdynamic.so + 0000000000000a90 T _abs + 0000000000000aa9 T _atoi + 0000000000202048 B __bss_start + w __cxa_finalize + 0000000000202048 D _edata + 0000000000202050 B _end + 00000000000011f8 T _fini + w __gmon_start__ + 0000000000000900 T _init + 0000000000000bd7 T _isalpha + 0000000000000c04 T _isdigit + 0000000000000c25 T _islower + 0000000000000c46 T _isupper + w _ITM_deregisterTMCloneTable + w _ITM_registerTMCloneTable + w _Jv_RegisterClasses + 0000000000000c67 T _memcpy + 0000000000000caa T _memset + 0000000000000ce9 T _putchar + 0000000000000d0e T _puts + 0000000000000d4a T _strcat + 0000000000000dcf T _strchr + 0000000000000e21 T _strcmp + 0000000000000e89 T _strcpy + 0000000000000eeb T _strlen + 0000000000000f15 T _strncat + 0000000000000fa5 T _strncpy + 0000000000001029 T _strpbrk + 000000000000109d T _strspn + 0000000000001176 T _strstr + U write + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ cat 0-main.c + #include "main.h" + #include + + /** + * main - check the code + * + * Return: Always EXIT_SUCCESS. + */ + int main(void) + { + printf("%d\n", _strlen("My Dyn Lib")); + return (EXIT_SUCCESS); + } + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ gcc -Wall -pedantic -Werror -Wextra -L. 0-main.c -ldynamic -o len + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ldd len + linux-vdso.so.1 => (0x00007fff5d1d2000) + libdynamic.so => not found + libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f74c6bb9000) + /lib64/ld-linux-x86-64.so.2 (0x0000556be5b82000) + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ldd len + linux-vdso.so.1 => (0x00007fff41ae9000) + libdynamic.so => ./libdynamic.so (0x00007fd4bf2d9000) + libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd4beef6000) + /lib64/ld-linux-x86-64.so.2 (0x0000557566402000) + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ./len + 10 + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ++ [x] 1\. Without libraries what have we? We have no past and no future
_**[1-create_dynamic_lib.sh](1-create_dynamic_lib.sh)**_ creates a dynamic library called `liball.so` from all the `.c` files that are in the current directory.
__Example__:
+ ```c + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ls *.c + abs.c isalpha.c islower.c memcpy.c putchar.c strcat.c strcmp.c strlen.c strncpy.c strspn.c + atoi.c isdigit.c isupper.c memset.c puts.c strchr.c strcpy.c strncat.c strpbrk.c strstr.c + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ ./1-create_dynamic_lib.sh + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ nm -D --defined-only liball.so + 0000000000000a90 T _abs + 0000000000000aa9 T _atoi + 0000000000202048 B __bss_start + 0000000000202048 D _edata + 0000000000202050 B _end + 00000000000011f8 T _fini + 0000000000000900 T _init + 0000000000000bd7 T _isalpha + 0000000000000c04 T _isdigit + 0000000000000c25 T _islower + 0000000000000c46 T _isupper + 0000000000000c67 T _memcpy + 0000000000000caa T _memset + 0000000000000ce9 T _putchar + 0000000000000d0e T _puts + 0000000000000d4a T _strcat + 0000000000000dcf T _strchr + 0000000000000e21 T _strcmp + 0000000000000e89 T _strcpy + 0000000000000eeb T _strlen + 0000000000000f15 T _strncat + 0000000000000fa5 T _strncpy + 0000000000001029 T _strpbrk + 000000000000109d T _strspn + 0000000000001176 T _strstr + bekalue@BEKALU-PC:~/0x18. Dynamic libraries$ + ``` ++ [x] 2\. Let's call C functions from Python
_**[100-operations.so](100-operations.so)**_ contains C functions that can be called from Python.
The functions have the following prototypes:
+ ```c + int add(int a, int b); /* Addition */ + int sub(int a, int b); /* Subtraction */ + int mul(int a, int b); /* Multiplication */ + int div(int a, int b); /* Division */ + int mod(int a, int b); /* Modulo */ + ``` + __Example__: + ```c + bekalue@BEKALU-PC:~/0x18$ cat 100-tests.py + import random + import ctypes + + cops = ctypes.CDLL('./100-operations.so') + a = random.randint(-111, 111) + b = random.randint(-111, 111) + print("{} + {} = {}".format(a, b, cops.add(a, b))) + print("{} - {} = {}".format(a, b, cops.sub(a, b))) + print("{} x {} = {}".format(a, b, cops.mul(a, b))) + print("{} / {} = {}".format(a, b, cops.div(a, b))) + print("{} % {} = {}".format(a, b, cops.mod(a, b))) + bekalue@BEKALU-PC:~/0x16. Doubly linked lists$ python3 100-tests.py + 66 + -76 = -10 + 66 - -76 = 142 + 66 x -76 = -5016 + 66 / -76 = 0 + 66 % -76 = 66 + bekalue@BEKALU-PC:~/0x18$ python3 100-tests.py + -34 + -57 = -91 + -34 - -57 = 23 + -34 x -57 = 1938 + -34 / -57 = 0 + -34 % -57 = -34 + bekalue@BEKALU-PC:~/0x18$ python3 100-tests.py + -5 + -72 = -77 + -5 - -72 = 67 + -5 x -72 = 360 + -5 / -72 = 0 + -5 % -72 = -5 + bekalue@BEKALU-PC:~/0x18$ python3 100-tests.py + 39 + -62 = -23 + 39 - -62 = 101 + 39 x -62 = -2418 + 39 / -62 = 0 + 39 % -62 = 39 + bekalue@BEKALU-PC:~/0x18$ ++ [x] 3\. Code injection: Win the Giga Millions!
_**[101-make_me_win.sh](101-make_me_win.sh)**_ runs two commands on the same server where the [Giga Millions](https://github.com/holbertonschool/0x18.c) program runs so that the numbers `9, 8, 10, 24, 75 + 9` would win the Jackpot. + + You can’t modify the program [gm](https://github.com/holbertonschool/0x18.c/blob/master/gm) itself as Master Sysadmin Sylvain (MSS) always checks its `[MD5](https://github.com/holbertonschool/0x18.c/blob/master/101-md5_gm) before running it + + The system is an `Linux Ubuntu 16.04` + + The server has internet access + + the mole will be only able to run two commands from a shell script, without being detected by MSS + + the shell script should be maximum 3 lines long. You are not allowed to use `;`, `&&`, `||`, `|`, ` (it would be detected by MSS), and have a maximum of two commands + + The mole has only the authorization to upload one file on the server. It will be your shell script + + The mole will run your shell script this way: `mss@gm_server$ . ./101-make_me_win.sh` + + The mole will run your shell script from the same directory containing the program `gm`, exactly 98 seconds before MSS runs `gm` with my numbers: `./gm 9 8 10 24 75 9` + + MSS will use the same terminal and session than mole + + Before running the `gm` program, MSS always check the content of the directory + + MSS always exit after running the program `gm` + + TL;DR; This is what is going to happen + ```bash + mss@gm_server$ . ./101-make_me_win.sh + mss@gm_server$ rm 101-make_me_win.sh + mss@gm_server$ ls -la + . .. gm + mss@gm_server$ history -c + mss@gm_server$ clear + mss@gm_server$ ls -la + . .. gm + mss@gm_server$ md5sum gm + d52e6c18e0723f5b025a75dea19ef365 gm + mss@gm_server$ ./gm 9 8 10 24 75 9 + --> Please make me win! + mss@gm_server$ exit + ``` + Tip: `LD_PRELOAD` diff --git a/0x1A-hash_tables/TASKS.md b/0x1A-hash_tables/TASKS.md new file mode 100644 index 00000000..ec266bc1 --- /dev/null +++ b/0x1A-hash_tables/TASKS.md @@ -0,0 +1,147 @@ +# 0x1A. C - Hash tables +> Project done in 2 days + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning objectives +> to be [explained](https://fs.blog/feynman-learning-technique/) after project +* What is a hash function +* What makes a good hash function +* What is a hash table, how do they work and how to use them +* What is a collision and what are the main ways of dealing with collisions in the context of a hash table +* What are the advantages and drawbacks of using hash tables +* What are the most common use cases of hash tables +## Resources Used +* [What is a HashTable Data Structure - Introduction to Hash Tables , Part 0](https://www.youtube.com/watch?v=MfhjkfocRR0) +* [Hash function](https://en.wikipedia.org/wiki/Hash_function) +* [Hash table](https://en.wikipedia.org/wiki/Hash_table) +* [All about hash tables](https://www.digitalocean.com/community/tutorials/hash-table-in-c-plus-plus) +* [why hash tables and not arrays](https://stackoverflow.com/questions/31930046/what-is-a-hash-table-and-how-do-you-make-it-in-c) +* [how dictionaries are implemented in Python 2.7](http://www.laurentluce.com/posts/python-dictionary-implementation/) +* [difference between a dictionary and a hash table](https://stackoverflow.com/questions/2061222/what-is-the-true-difference-between-a-dictionary-and-a-hash-table) + +## Tasks To Complete + ++ [x] 0\. >>> ht = {}
_**[0-hash_table_create.c](0-hash_table_create.c)**_ contains a function that creates a hash table.
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 0-main.c 0-hash_table_create.c -o a +bekalue@BEKALU-PC:~/0x1A. Hash tables$ ./a +0x238a010 +bekalue@BEKALU-PC:~/0x1A. Hash tables$ valgrind ./a +==7602== Memcheck, a memory error detector +==7602== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. +==7602== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info +==7602== Command: ./a +==7602== +0x51fc040 +==7602== +==7602== HEAP SUMMARY: +==7602== in use at exit: 8,208 bytes in 2 blocks +==7602== total heap usage: 2 allocs, 0 frees, 8,208 bytes allocated +==7602== +==7602== LEAK SUMMARY: +==7602== definitely lost: 16 bytes in 1 blocks +==7602== indirectly lost: 8,192 bytes in 1 blocks +==7602== possibly lost: 0 bytes in 0 blocks +==7602== still reachable: 0 bytes in 0 blocks +==7602== suppressed: 0 bytes in 0 blocks +==7602== Rerun with --leak-check=full to see details of leaked memory +==7602== +==7602== For counts of detected and suppressed errors, rerun with: -v +==7602== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 1\. djb2
_**[1-djb2.c](1-djb2.c)**_ contains a hash function implementing the [djb2 algorithm](https://gist.github.com/papamuziko/7bb52dfbb859fdffc4bd0f95b76f71e8).
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 1-main.c 1-djb2.c -o b +bekalue@BEKALU-PC:~/0x1A. Hash tables$ ./b +6953392314605 +3749890792216096085 +5861846 +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 2\. key -> index
_**[2-key_index.c](2-key_index.c)**_ contains a function that gives you the index of a key.
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 2-main.c 1-djb2.c 2-key_index.c -o c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ ./c +6953392314605 +237 +3749890792216096085 +341 +5861846 +470 +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 3\. >>> ht['betty'] = 'cool'
_**[3-hash_table_set.c](3-hash_table_set.c)**_ contains a function that adds an element to the hash table. + * To test for collisions, here are some strings that collide using the djb2 algorithm: + * `hetairas` collides with `mentioner` + * `heliotropes` collides with `neurospora` + * `depravement` collides with `serafins` + * `stylist` collides with `subgenera` + * `joyful` collides with `synaphea` + * `redescribed` collides with `urites` + * `dram` collides with `vivency`
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 3-main.c 0-hash_table_create.c 1-djb2.c 2-key_index.c 3-hash_table_set.c -o d +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 4\. >>> ht['betty']
_**[4-hash_table_get.c](4-hash_table_get.c)**_ contains a function that retrieves a value associated with a key.
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 4-main.c 0-hash_table_create.c 1-djb2.c 2-key_index.c 3-hash_table_set.c 4-hash_table_get.c -o e +bekalue@BEKALU-PC:~/0x1A. Hash tables$ ./e +python:awesome +Bob:and Kris love asm +N:queens +Asterix:Obelix +Betty:Cool +98:Battery Street +c:isfun +javascript:(null) +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 5\. >>> print(ht)
_**[5-hash_table_print.c](5-hash_table_print.c)**_ contains a function that prints a hash table.
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 5-main.c 0-hash_table_create.c 1-djb2.c 2-key_index.c 3-hash_table_set.c 4-hash_table_get.c 5-hash_table_print.c -o f +bekalue@BEKALU-PC:~/0x1A. Hash tables$ ./f +{} +{'Betty': 'Cool', 'python': 'awesome', 'Bob': 'and Kris love asm', '98': 'Battery Street', 'N': 'queens', 'c': 'fun', 'Asterix': 'Obelix'} +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 6\. >>> del ht
_**[6-hash_table_delete.c](6-hash_table_delete.c)**_ contains a function that deletes a hash table.
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra 6-main.c 0-hash_table_create.c 1-djb2.c 2-key_index.c 3-hash_table_set.c 4-hash_table_get.c 5-hash_table_print.c 6-hash_table_delete.c -o g +bekalue@BEKALU-PC:~/0x1A. Hash tables$ valgrind ./g +==6621== Memcheck, a memory error detector +==6621== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. +==6621== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info +==6621== Command: ./g +==6621== +{'Betty': 'Cool', 'mentioner': 'Bob', 'hetairas': 'Bob Z Chu', 'python': 'awesome', 'Bob': 'and Kris love asm', '98': 'Battery Street', 'N': 'queens', 'c': 'fun', 'Tim': 'Britton', 'Asterix': 'Obelix'} +==6621== +==6621== HEAP SUMMARY: +==6621== in use at exit: 0 bytes in 0 blocks +==6621== total heap usage: 37 allocs, 37 frees, 8,646 bytes allocated +==6621== +==6621== All heap blocks were freed -- no leaks are possible +==6621== +==6621== For counts of detected and suppressed errors, rerun with: -v +==6621== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` ++ [x] 7\. $ht['Betty'] = 'Cool'
_**[100-sorted_hash_table.c](100-sorted_hash_table.c)**_ contains rewrites of the previous functions that create a sorted hash table. + * [PHP Internals Book: HashTable](https://www.phpinternalsbook.com/php5/hashtables/basic_structure.html) + * [PHP](https://www.php.net/manual/en/intro-whatis.php)
__Example__: +```c +bekalue@BEKALU-PC:~/0x1A. Hash tables$ gcc -Wall -pedantic -Werror -Wextra -std=gnu89 100-main.c 100-sorted_hash_table.c 1-djb2.c 2-key_index.c -o sht +bekalue@BEKALU-PC:~/0x1A. Hash tables$ ./sht +{'y': '0'} +{'j': '1', 'y': '0'} +{'c': '2', 'j': '1', 'y': '0'} +{'b': '3', 'c': '2', 'j': '1', 'y': '0'} +{'b': '3', 'c': '2', 'j': '1', 'y': '0', 'z': '4'} +{'b': '3', 'c': '2', 'j': '1', 'n': '5', 'y': '0', 'z': '4'} +{'a': '6', 'b': '3', 'c': '2', 'j': '1', 'n': '5', 'y': '0', 'z': '4'} +{'a': '6', 'b': '3', 'c': '2', 'j': '1', 'm': '7', 'n': '5', 'y': '0', 'z': '4'} +{'z': '4', 'y': '0', 'n': '5', 'm': '7', 'j': '1', 'c': '2', 'b': '3', 'a': '6'} +bekalue@BEKALU-PC:~/0x1A. Hash tables$ +``` diff --git a/0x1C-makefiles/TASKS.md b/0x1C-makefiles/TASKS.md new file mode 100644 index 00000000..b1cb2f87 --- /dev/null +++ b/0x1C-makefiles/TASKS.md @@ -0,0 +1,230 @@ +# 0x1C. C - Makefiles +>Project Done in 3 Day + +![Pep8 style](https://img.shields.io/badge/PEP8-style%20guide-green?style=round-square) + +

+ +

+ +## Learning objectives +> needed to be [explaied](https://fs.blog/feynman-learning-technique/) after the project. +* What are `make`, Makefiles +* When, why and how to use Makefiles +* What are rules and how to set and use them +* What are explicit and implicit rules +* What are the most common / useful rules +* What are variables and how to set and use them + +## Resourcess +* [Makefile](https://www.google.com/search?q=makefile) +* [Installing the make utility](https://www.geeksforgeeks.org/how-to-install-make-on-ubuntu/) +* [make-official documentation](https://www.gnu.org/software/make/manual/html_node/) +* [Learn Makefiles](https://makefiletutorial.com/#:~:text=A%20Makefile%20consists%20of%20a,make%20the%20target(s)) +* [Tutorial](https://www.math.colostate.edu/~yzhou/computer/writemakefile.html) +* [Using make and writing Makefiles](https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html) + +## Requirements +* Allowed editors: `vi`, `vim`, `emacs` +* OS: `Ubuntu 20.04 LTS` +* Version of `gcc`: 9.3.0 +* Version of `make`: GNU Make 4.2.1 +* All your files should end with a new line +* A `README.md` file, at the root of the folder of the project, is mandatory + +## Tasks ++ [x] 0\. make -f 0-Makefile
_**[0-Makefile](0-Makefile)**_ Makefile that creates an executable `school` based on + [school.c](./school.c) and [main.c](./main.c). Includes: + + Rule `all` that builds the executable.
__Example__: +```c +julien@ubuntu:~/0x1C. Makefiles$ make -f 0-Makefile +gcc main.c school.c -o school +julien@ubuntu:~/0x1C. Makefiles$ ./school +j#0000000000000000000000000000000000000 +j#000000000000000000@Q**g00000000000000 +j#0000000000000000*]++]4000000000000000 +j#000000000000000k]++]++*N#000000000000 +j#0000000000000*C+++]++]++]J*0000000000 +j#00000000000@+]++qwwwp=]++++]*00000000 +j#0000000000*+++]q#0000k+]+]++]4#000000 +j#00000000*C+]+]w#0000*]+++]+]++0000000 +j#0000we+]wW000***C++]++]+]++++40000000 +j#000000000*C+]+]]+]++]++]++]+q#0000000 +j#0000000*]+]+++++++]++]+++]+++J0000000 +j#000000C++]=]+]+]+]++]++]+]+]+]=000000 +j#00000k+]++]+++]+]++qwW0000000AgW00000 +j#00000k++]++]+]+++qW#00000000000000000 +j#00000A]++]++]++]++J**0000000000000000 +j#000000e]++]+++]++]++]J000000000000000 +j#0000000A]++]+]++]++]++000000000000000 +j#000000000w]++]+]++]+qW#00000000000000 +j#00000000000w]++++]*0##000000000000000 +j#0000000000000Ag]+]++*0000000000000000 +j#00000000000000000we]+]Q00000000000000 +j#0000000000000@@+wgdA]+J00000000000000 +j#0000000000000k?qwgdC=]4#0000000000000 +j#00000000000000w]+]++qw#00000000000000 +"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +julien@ubuntu:~/0x1C. Makefiles$ +``` ++ [x] 1\. make -f 1-Makefile
_**[1-Makefile](1-Makefile)**_ Makefile that creates an executable `school` based on + [school.c](./school.c) and [main.c](./main.c). Builds on [0-Makefile](./0-Makefile) + with: + + Variable `CC` that defines the compiler to be used. + + Variable `SRC` that defines the `.c` files to compile. + + The `all` rule only recompiles updated source files.
__Example__: +```c +julien@ubuntu:~/0x1C. Makefiles$ make -f 1-Makefile +gcc main.c school.c -o school +julien@ubuntu:~/0x1C. Makefiles$ make -f 1-Makefile +gcc main.c school.c -o school +julien@ubuntu:~/0x1C. Makefiles$ +``` ++ [x] 2\. make -f 2-Makefile
_**[2-Makefile](2-Makefile)**_ Makefile that creates an executable `school` based on + [school.c](./school.c) and [main.c](./main.c). Builds on [1-Makefile](./1-Makefile) + with: + + Variable `OBJ` that defines the `.o` files to compile. + + Variable `NAME` that defines the name of the executable.
__Example__: +```c +julien@ubuntu:~/0x1C. Makefiles$ make -f 2-Makefile +gcc -c -o main.o main.c +gcc -c -o school.o school.c +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ make -f 2-Makefile +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ echo "/* School */" >> main.c +julien@ubuntu:~/0x1C. Makefiles$ make -f 2-Makefile +gcc -c -o main.o main.c +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ +``` ++ [x] 3\. make -f 3-Makefile
_**[3-Makefile](3-Makefile)**_ Makefile that creates an executable `school` based on + [school.c](./school.c) and [main.c](./main.c). Builds on [2-Makefile](./2-Makefile) + with: + + Rule `clean` that deletes all Emacs/Vim temporary files as well as the + executable. + + Rule `oclean` that deletes the object files. + + Rule `fclean` that deltes all of the temporary files, executable, and + object files. + + Rule `re` that forces recompilation of all source files. + + Variable `RM` that defines the command to delete files.
__Example__: +```c +julien@ubuntu:~//0x1C. Makefiles$ ls -1 +0-Makefile +1-Makefile +2-Makefile +3-Makefile +school.c +main.c +main.c~ +m.h +julien@ubuntu:~/0x1C. Makefiles$ make -f 3-Makefile +gcc -c -o main.o main.c +gcc -c -o school.o school.c +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ make all -f 3-Makefile +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ ls -1 +0-Makefile +1-Makefile +2-Makefile +3-Makefile +school +school.c +school.o +main.c +main.c~ +main.o +m.h +julien@ubuntu:~/0x1C. Makefiles$ make clean -f 3-Makefile +rm -f *~ school +julien@ubuntu:~/0x1C. Makefiles$ make oclean -f 3-Makefile +rm -f main.o school.o +julien@ubuntu:~/0x1C. Makefiles$ make fclean -f 3-Makefile +rm -f *~ school +rm -f main.o school.o +julien@ubuntu:~/0x1C. Makefiles$ make all -f 3-Makefile +gcc -c -o main.o main.c +gcc -c -o school.o school.c +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ make all -f 3-Makefile +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ make re -f 3-Makefile +rm -f main.o school.o +gcc -c -o main.o main.c +gcc -c -o school.o school.c +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ +``` ++ [x] 4\. A complete Makefile
_**[4-Makefile](4-Makefile)**_ Makefile that creates an executable `school` based on + [school.c](./school.c) and [main.c](./main.c). Builds on [3-Makefile](./3-Makefile) + with: + + Variable `CFLAGS` that defines the compiler flags `-Wall -Werror -Wextra + -pedantic`.
__Example__: +```c +julien@ubuntu:~/0x1C. Makefiles$ make all -f 4-Makefile +gcc -Wall -Werror -Wextra -pedantic -c -o main.o main.c +gcc -Wall -Werror -Wextra -pedantic -c -o school.o school.c +gcc main.o school.o -o school +julien@ubuntu:~/0x1C. Makefiles$ +``` ++ [x] 5\. Island Perimeter
_**[5-island_perimeter.py](5-island_perimeter.py)**_ contains a function `def island_perimeter(grid):` that returns the perimeter of the island described in `grid`: + + `grid` is a list of list of integers: + + 0 represents a water zone. + + 1 represents a land zone. + + One cell is a square with side length 1. + + Grid cells are connected horizontally/vertically (not diagonally). + + Grid is rectangular, width and height don't exceed 100. + + Grid is completely surrounded by water, and there is one island (or nothing). + + The island doesn't have "lakes" (water inside that isn't connected to the water around the island).
__Example__: +```c +guillaume@ubuntu:~/0x1C$ cat 5-main.py +#!/usr/bin/python3 +""" +5-main +""" +island_perimeter = __import__('5-island_perimeter').island_perimeter + +if __name__ == "__main__": + grid = [ + [0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [0, 1, 1, 1, 0, 0], + [0, 0, 0, 0, 0, 0] + ] + print(island_perimeter(grid)) + +guillaume@ubuntu:~/0x1C$ +guillaume@ubuntu:~/0x1C$ ./5-main.py +12 +guillaume@ubuntu:~/0x1C$ +``` ++ [x] 6\. make -f 100-Makefile
_**[100-Makefile](100-Makefile)**_ Makefile that creates an executable `school` based on + [school.c](./school.c) and [main.c](./main.c). Builds on [4-Makefile](./4-Makefile) + with: + + name of the executable: `school` + + rules: `all`, `clean`, `fclean`, `oclean`, `re` + + `all`: builds your executable + + `clean`: deletes all Emacs and Vim temporary files along with the executable + + `oclean`: deletes the object files + + `fclean`: deletes all Emacs and Vim temporary files, the executable, and the object files + + `re`: forces recompilation of all source files + + variables: `CC`, `SRC`, `OBJ`, `NAME`, `RM`, `CFLAGS` + + `CC`: the compiler to be used + + `SRC`: the `.c` files + + `OBJ`: the `.o` files + + `NAME`: the name of the executable + + `RM`: the program to delete files + + `CFLAGS`: your favorite compiler flags: `-Wall -Werror -Wextra -pedantic` + + The `all` rule should recompile only the updated source files + + The `clean`, `oclean`, `fclean`, `re` rules should never fail + + You are not allowed to have a list of all the `.o` files + + You have to use `$(RM)` for the cleaning up rules, but you are not allowed to set the `RM` variable + + You are not allowed to use the string `$(CC)` more than once in your Makefile + + You are only allowed to use the string `$(RM)` twice in your Makefile + + You are not allowed to use the string `$(CFLAGS)` (but the compiler should still use the flags you set in this variable) + + You are not allowed to have an `$(OBJ)` rule + + You are not allowed to use the `%.o: %.c` rule + + Your Makefile should work even if there is a file in the folder that has the same name as one of your rule + + Your Makefile should not compile if the header file `m.h` is missing diff --git a/0x1E-search_algorithms/TASKS.md b/0x1E-search_algorithms/TASKS.md new file mode 100644 index 00000000..2a162a72 --- /dev/null +++ b/0x1E-search_algorithms/TASKS.md @@ -0,0 +1,617 @@ +# 0x1E. C - Search Algorithms +> project done in 3 days + +![Betty style](https://img.shields.io/badge/betty-style%20guide-purple?style=round-square) + +## Learning Objective +> needed to be [explained](https://fs.blog/feynman-learning-technique/) after the project. +* What is a search algorithm +* What is a linear search +* What is a binary search +* What is the best search algorithm to use depending on your needs + +## Resources +* [Search algorithm](https://en.wikipedia.org/wiki/Search_algorithm) +* [Space complexity (1)](https://www.geeksforgeeks.org/g-fact-86/) + +## Tasks To Complete + ++ [x] 0. Linear search
_**[0-linear.c](0-linear.c)**_ contains a function that searches for a value in an array of integers using the [Linear search algorithm](https://en.wikipedia.org/wiki/Linear_search). + + Prototype: `int linear_search(int *array, size_t size, int value);`. + + Where `array` is a pointer to the first element of the array to search in. + + `size` is the number of elements in `array`. + + And `value` is the value to search for. + + Your function must return the first index where `value` is located. + + If `value` is not present in `array` or if `array` is `NULL`, your function must return `-1`. + + Every time you compare a value in the array to the value you are searching, you have to print this value (see the example below). + ```powershell + wilfried@0x1E-search_algorithms$ cat 0-main.c + #include + #include + #include "search_algos.h" + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + int array[] = { + 10, 1, 42, 3, 4, 42, 6, 7, -1, 9 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + printf("Found %d at index: %d\n\n", 3, linear_search(array, size, 3)); + printf("Found %d at index: %d\n\n", 42, linear_search(array, size, 42)); + printf("Found %d at index: %d\n", 999, linear_search(array, size, 999)); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 0-main.c 0-linear.c -o 0-linear + wilfried@0x1E-search_algorithms$ ./0-linear + Value checked array[0] = [10] + Value checked array[1] = [1] + Value checked array[2] = [42] + Value checked array[3] = [3] + Found 3 at index: 3 + + Value checked array[0] = [10] + Value checked array[1] = [1] + Value checked array[2] = [42] + Found 42 at index: 2 + + Value checked array[0] = [10] + Value checked array[1] = [1] + Value checked array[2] = [42] + Value checked array[3] = [3] + Value checked array[4] = [4] + Value checked array[5] = [42] + Value checked array[6] = [6] + Value checked array[7] = [7] + Value checked array[8] = [-1] + Value checked array[9] = [9] + Found 999 at index: -1 + ``` + ++ [x] 1. Binary search
_**[1-binary.c](1-binary.c)**_ contains a function that searches for a value in a sorted array of integers using the [Binary search algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm). + + Prototype: `int binary_search(int *array, size_t size, int value);`. + + Where `array` is a pointer to the first element of the array to search in. + + `size` is the number of elements in array. + + And `value` is the value to search for. + + Your function must return the index where value is located. + + You can assume that array will be sorted in ascending order. + + You can assume that value won’t appear more than once in array. + + If value is not present in array or if array is NULL, your function must return -1. + + You must print the array being searched every time it changes. (e.g. at the beginning and when you search a subarray) (see example below). + ```powershell + wilfried@0x1E-search_algorithms$ cat 1-main.c + #include + #include + #include "search_algos.h" + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + int array[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + printf("Found %d at index: %d\n\n", 2, binary_search(array, size, 2)); + printf("Found %d at index: %d\n\n", 5, binary_search(array, 5, 5)); + printf("Found %d at index: %d\n", 999, binary_search(array, size, 999)); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 1-main.c 1-binary.c -o 1-binary + wilfried@0x1E-search_algorithms$ ./1-binary + Searching in array: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + Searching in array: 0, 1, 2, 3 + Searching in array: 2, 3 + Found 2 at index: 2 + + Searching in array: 0, 1, 2, 3, 4 + Searching in array: 3, 4 + Searching in array: 4 + Found 5 at index: -1 + + Searching in array: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + Searching in array: 5, 6, 7, 8, 9 + Searching in array: 8, 9 + Searching in array: 9 + Found 999 at index: -1 + ``` + ++ [x] 2. Big 0 #0
_**[2-O](2-O)**_ contains the `time complexity` (worst case) of a linear search in an array of size `n`. + ++ [x] 3. Big 0 #1
_**[3-O](3-O)**_ contains the `space complexity` (worst case) of an iterative linear search algorithm in an array of size `n`. + ++ [x] 4. Big 0 #2
_**[4-O](4-O)**_ contains the `time complexity` (worst case) of a binary search in an array of size `n`. + ++ [x] 5. Big 0 #3
_**[5-O](5-O)**_ contains the `space complexity` (worst case) of a binary search in an array of size `n`. + ++ [x] 6. Big 0 #4
_**[6-O](6-O)**_ contains the space complexity of the following function/algorithm. + ```c + int **allocate_map(int n, int m) + { + int **map; + + map = malloc(sizeof(int *) * n); + for (size_t i = 0; i < n; i++) + { + map[i] = malloc(sizeof(int) * m); + } + return (map); + } + ``` + ++ [x] 7. Jump search
_**[100-jump.c](100-jump.c)**_ contains a function that searches for a value in a sorted array of integers using the [Jump search algorithm](https://en.wikipedia.org/wiki/Jump_search). + + Prototype: `int jump_search(int *array, size_t size, int value);`. + + Where `array` is a pointer to the first element of the array to search in. + + `size` is the number of elements in `array`. + + And `value` is the value to search for. + + Your function must return the first index where `value` is located. + + You can assume that `array` will be sorted in ascending order. + + If `value` is not present in `array` or if `array` is `NULL`, your function must return `-1`. + + You have to use the square root of the size of the array as the jump step. + + You can use the `sqrt()` function included in `` (don’t forget to compile with -lm). + + Every time you compare a value in the array to the value you are searching for, you have to print this value (see example below). + ```powershell + wilfried@0x1E-search_algorithms$ cat 100-main.c + #include + #include + #include "search_algos.h" + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + int array[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + printf("Found %d at index: %d\n\n", 6, jump_search(array, size, 6)); + printf("Found %d at index: %d\n\n", 1, jump_search(array, size, 1)); + printf("Found %d at index: %d\n", 999, jump_search(array, size, 999)); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 100-main.c 100-jump.c -lm -o 100-jump + wilfried@0x1E-search_algorithms$ ./100-jump + Value checked array[0] = [0] + Value checked array[3] = [3] + Value found between indexes [3] and [6] + Value checked array[3] = [3] + Value checked array[4] = [4] + Value checked array[5] = [5] + Value checked array[6] = [6] + Found 6 at index: 6 + + Value checked array[0] = [0] + Value found between indexes [0] and [3] + Value checked array[0] = [0] + Value checked array[1] = [1] + Found 1 at index: 1 + + Value checked array[0] = [0] + Value checked array[3] = [3] + Value checked array[6] = [6] + Value checked array[9] = [9] + Value found between indexes [9] and [12] + Value checked array[9] = [9] + Found 999 at index: -1 + ``` + ++ [x] 8. Big 0 #5
_**[101-O](101-O)**_ contains the `time complexity` (average case) of a jump search in an array of size `n`, using `step = sqrt(n)`. + ++ [x] 9. Interpolation search
_**[102-interpolation.c](102-interpolation.c)**_ contains a function that searches for a value in a sorted array of integers using the [Interpolation search algorithm](https://en.wikipedia.org/wiki/Interpolation_search). + + Prototype: `int interpolation_search(int *array, size_t size, int value);`. + + Where `array` is a pointer to the first element of the array to search in. + + `size` is the number of elements in `array`. + + And `value` is the value to search for. + + Your function must return the first index where `value` is located. + + You can assume that `array` will be sorted in ascending order. + + If `value` is not present in `array` or if `array` is `NULL`, your function must return `-1`. + + To determine the probe position, you can use: `size_t pos = low + (((double)(high - low) / (array[high] - array[low])) * (value - array[low]))`. + + Every time you compare a value in the array to the value you are searching, you have to print this value (see example below). + ```powershell + wilfried@0x1E-search_algorithms$ cat 102-main.c + #include + #include + #include "search_algos.h" + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + int array[] = { + 0, 0, 1, 2, 2, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8, 9, 9 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + printf("Found %d at index: %d\n\n", 3, interpolation_search(array, size, 3)); + printf("Found %d at index: %d\n\n", 7, interpolation_search(array, size, 7)); + printf("Found %d at index: %d\n", 999, interpolation_search(array, size, 999)); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 102-main.c 102-interpolation.c -o 102-interpolation + wilfried@0x1E-search_algorithms$ ./102-interpolation + Value checked array[6] = [2] + Value checked array[7] = [3] + Found 3 at index: 7 + + Value checked array[14] = [7] + Found 7 at index: 14 + + Value checked array[2109] is out of range + Found 999 at index: -1 + ``` + ++ [x] 10. Exponential search
_**[103-exponential.c](103-exponential.c)**_ contains a function that searches for a value in a sorted array of integers using the [Exponential search algorithm](https://en.wikipedia.org/wiki/Exponential_search). + + Prototype: `int exponential_search(int *array, size_t size, int value);`. + + Where `array` is a pointer to the first element of the array to search in. + + `size` is the number of elements in `array`. + + And `value` is the value to search for. + + Your function must return the first index where `value` is located. + + You can assume that `array` will be sorted in ascending order. + + If `value` is not present in `array` or if `array` is `NULL`, your function must return `-1`. + + You have to use powers of 2 as exponential ranges to search in your array. + + Every time you compare a value in the array to the value you are searching for, you have to print this value (see example below). + + Once you've found the good range, you need to use a binary search:. + + Every time you split the array, you have to print the new array (or subarray) you’re searching in (see example below). + ```powershell + wilfried@0x1E-search_algorithms$ cat 103-main.c + #include + #include + #include "search_algos.h" + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + int array[] = { + 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 54, 61, 62, 76, 99 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + printf("Found %d at index: %d\n\n", 62, exponential_search(array, size, 62)); + printf("Found %d at index: %d\n\n", 3, exponential_search(array, size, 3)); + printf("Found %d at index: %d\n", 999, exponential_search(array, size, 999)); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 103-main.c 103-exponential.c -o 103-exponential + wilfried@0x1E-search_algorithms$ ./103-exponential + Value checked array[1] = [1] + Value checked array[2] = [2] + Value checked array[4] = [4] + Value checked array[8] = [18] + Value found between indexes [8] and [15] + Searching in array: 18, 19, 23, 54, 61, 62, 76, 99 + Searching in array: 61, 62, 76, 99 + Found 62 at index: 13 + + Value checked array[1] = [1] + Value checked array[2] = [2] + Value found between indexes [2] and [4] + Searching in array: 2, 3, 4 + Found 3 at index: 3 + + Value checked array[1] = [1] + Value checked array[2] = [2] + Value checked array[4] = [4] + Value checked array[8] = [18] + Value found between indexes [8] and [15] + Searching in array: 18, 19, 23, 54, 61, 62, 76, 99 + Searching in array: 61, 62, 76, 99 + Searching in array: 76, 99 + Searching in array: 99 + Found 999 at index: -1 + ``` + ++ [x] 11. Advanced binary search
_**[104-advanced_binary.c](104-advanced_binary.c)**_ contains a function that searches for a value in a sorted array of integers using the [Binary search algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm). + + **Info:** You may have noticed that the basic binary search does not necessarily return the index of the first value in the array (if this value appears more than once in the array). In this exercise, you’ll have to solve this problem. + + Prototype: `int advanced_binary(int *array, size_t size, int value);`. + + Where `array` is a pointer to the first element of the array to search in. + + `size` is the number of elements in `array`. + + And `value` is the value to search for. + + Your function must return the index where `value` is located. + + You can assume that `array` will be sorted in ascending order. + + If `value` is not present in `array` or if `array` is `NULL`, your function must return `-1`. + + Every time you split the array, you have to print the new array (or subarray) you’re searching in (see example below). + + You have to use recursion. You may only use one loop (`while`, `for`, `do while`, etc.) in order to print the array. + ```powershell + wilfried@0x1E-search_algorithms$ cat 104-main.c + #include + #include + #include "search_algos.h" + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + int array[] = { + 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + printf("Found %d at index: %d\n\n", 8, advanced_binary(array, size, 8)); + printf("Found %d at index: %d\n\n", 5, advanced_binary(array, size, 5)); + printf("Found %d at index: %d\n", 999, advanced_binary(array, size, 999)); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 104-main.c 104-advanced_binary.c -o 104-advanced_binary + wilfried@0x1E-search_algorithms$ ./104-advanced_binary + Searching in array: 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 + Searching in array: 6, 6, 7, 8, 9 + Searching in array: 8, 9 + Found 8 at index: 8 + + Searching in array: 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 + Searching in array: 0, 1, 2, 5, 5 + Searching in array: 5, 5 + Found 5 at index: 3 + + Searching in array: 0, 1, 2, 5, 5, 6, 6, 7, 8, 9 + Searching in array: 6, 6, 7, 8, 9 + Searching in array: 8, 9 + Searching in array: 9 + Found 999 at index: -1 + ``` + ++ [x] 12. Jump search in a singly linked list
_**[105-jump_list.c](105-jump_list.c)**_ contains a function that searches for a value in a sorted list of integers using the [Jump search algorithm](https://en.wikipedia.org/wiki/Jump_search). + + **Info:** + + You might think that linear search is not as effective as any other algorithm, right? Well, we should see what happens with a singly linked list. + + Please define the following data structure in your [search_algos.h](search_algos.h) header file. + ```h + /** + * struct listint_s - singly linked list + * + * @n: Integer + * @index: Index of the node in the list + * @next: Pointer to the next node + * + * Description: singly linked list node structure + */ + typedef struct listint_s + { + int n; + size_t index; + struct listint_s *next; + } listint_t; + ``` + + Prototype: `listint_t *jump_list(listint_t *list, size_t size, int value);`. + + Where `list` is a pointer to the head of the list to search in. + + `size` is the number of nodes in `list`. + + And `value` is the value to search for. + + Your function must return a pointer to the first node where `value` is located. + + You can assume that `list` will be sorted in ascending order. + + If `value` is not present in `head` or if `head` is `NULL`, your function must return `NULL`. + + You have to use the square root of the size of the list as the jump step. + + You can use the `sqrt()` function included in `` (don’t forget to compile with -lm). + + Every time you compare a value in the list to the value you are searching, you have to print this value (see example below). + + **NOTE:** The functions used in the example can be found at [create_list.c](listint/create_list.c), [free_list.c](listint/free_list.c), and [print_list.c](listint/print_list.c). + ```powershell + wilfried@0x1E-search_algorithms$ cat 105-main.c + #include + #include + #include "search_algos.h" + + listint_t *create_list(int *array, size_t size); + void print_list(const listint_t *list); + void free_list(listint_t *list); + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + listint_t *list, *res; + int array[] = { + 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + list = create_list(array, size); + print_list(list); + + res = jump_list(list, size, 53); + printf("Found %d at index: %lu\n\n", 53, res->index); + res = jump_list(list, size, 2); + printf("Found %d at index: %lu\n\n", 2, res->index); + res = jump_list(list, size, 999); + printf("Found %d at index: %p\n", 999, (void *) res); + + free_list(list); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 105-main.c 105-jump_list.c listint/*.c -lm -o 105-jump + wilfried@0x1E-search_algorithms$ ./105-jump + List: + Index[0] = [0] + Index[1] = [1] + Index[2] = [2] + Index[3] = [3] + Index[4] = [4] + Index[5] = [7] + Index[6] = [12] + Index[7] = [15] + Index[8] = [18] + Index[9] = [19] + Index[10] = [23] + Index[11] = [53] + Index[12] = [61] + Index[13] = [62] + Index[14] = [76] + Index[15] = [99] + + Value checked at index [4] = [4] + Value checked at index [8] = [18] + Value checked at index [12] = [61] + Value found between indexes [8] and [12] + Value checked at index [8] = [18] + Value checked at index [9] = [19] + Value checked at index [10] = [23] + Value checked at index [11] = [53] + Found 53 at index: 11 + + Value checked at index [4] = [4] + Value found between indexes [0] and [4] + Value checked at index [0] = [0] + Value checked at index [1] = [1] + Value checked at index [2] = [2] + Found 2 at index: 2 + + Value checked at index [4] = [4] + Value checked at index [8] = [18] + Value checked at index [12] = [61] + Value checked at index [15] = [99] + Value found between indexes [12] and [15] + Value checked at index [12] = [61] + Value checked at index [13] = [62] + Value checked at index [14] = [76] + Value checked at index [15] = [99] + Found 999 at index: (nil) + ``` + ++ [x] 13. Linear search in a skip list
_**[106-linear_skip.c](106-linear_skip.c)**_ contains a function that searches for a value in a sorted skip list of integers. + + **Info:** + + As you see now, looking for a specific value in a singly linked list always leads to browse every element of the list. A common way to optimize the time complexity of a search in a singly linked list is to modify the list itself by adding an “express lane” to browse it. A linked list with an express lane is called a [skip list](https://en.wikipedia.org/wiki/Skip_list). This change does not come without consequences. Indeed, the space complexity of a search in this kind of list will grow as `sizeof(skiplist_t) > sizeof(listint_t)` (see example below). + + Please define the following data structure in your [search_algos.h](search_algos.h) header file. + ```h + /** + * struct skiplist_s - Singly linked list with an express lane + * + * @n: Integer + * @index: Index of the node in the list + * @next: Pointer to the next node + * @express: Pointer to the next node in the express lane + * + * Description: singly linked list node structure with an express lane + */ + typedef struct skiplist_s + { + int n; + size_t index; + struct skiplist_s *next; + struct skiplist_s *express; + } skiplist_t; + ``` + + Prototype: `skiplist_t *linear_skip(skiplist_t *list, int value);`. + + Where `list` is a pointer to the head of the skip list to search in + + A node of the express lane is placed every index which is a multiple of the square root of the size of the list (see example below). + + And `value` is the value to search for. + + You can assume that `list` will be sorted in ascending order. + + Your function must return a pointer on the first node where `value` is located. + + If `value` is not present in `list` or if `head` is `NULL`, your function must return `NULL`. + + Every time you compare a value in the list to the value you are searching, you have to print this value (see example below). + + **NOTE:** The functions used in the example can be found at [create_skiplist.c](skiplist/create_skiplist.c), [free_skiplist.c](skiplist/free_skiplist.c), and [print_skiplist.c](skiplist/print_skiplist.c). + ```powershell + wilfried@0x1E-search_algorithms$ cat 106-main.c + #include + #include + #include "search_algos.h" + + skiplist_t *create_skiplist(int *array, size_t size); + void print_skiplist(const skiplist_t *list); + void free_skiplist(skiplist_t *list); + + /** + * main - Entry point + * + * Return: Always EXIT_SUCCESS + */ + int main(void) + { + skiplist_t *list, *res; + int array[] = { + 0, 1, 2, 3, 4, 7, 12, 15, 18, 19, 23, 53, 61, 62, 76, 99 + }; + size_t size = sizeof(array) / sizeof(array[0]); + + list = create_skiplist(array, size); + print_skiplist(list); + + res = linear_skip(list, 53); + printf("Found %d at index: %lu\n\n", 53, res->index); + res = linear_skip(list, 2); + printf("Found %d at index: %lu\n\n", 2, res->index); + res = linear_skip(list, 999); + printf("Found %d at index: %p\n", 999, (void *) res); + + free_skiplist(list); + return (EXIT_SUCCESS); + } + wilfried@0x1E-search_algorithms$ gcc -Wall -Wextra -Werror -pedantic -std=gnu89 106-main.c 106-linear_skip.c skiplist/*.c -lm -o 106-linear + wilfried@0x1E-search_algorithms$ ./106-linear + List: + Index[0] = [0] + Index[1] = [1] + Index[2] = [2] + Index[3] = [3] + Index[4] = [4] + Index[5] = [7] + Index[6] = [12] + Index[7] = [15] + Index[8] = [18] + Index[9] = [19] + Index[10] = [23] + Index[11] = [53] + Index[12] = [61] + Index[13] = [62] + Index[14] = [76] + Index[15] = [99] + + Express lane: + Index[0] = [0] + Index[4] = [4] + Index[8] = [18] + Index[12] = [61] + + Value checked at index [4] = [4] + Value checked at index [8] = [18] + Value checked at index [12] = [61] + Value found between indexes [8] and [12] + Value checked at index [8] = [18] + Value checked at index [9] = [19] + Value checked at index [10] = [23] + Value checked at index [11] = [53] + Found 53 at index: 11 + + Value checked at index [4] = [4] + Value found between indexes [0] and [4] + Value checked at index [0] = [0] + Value checked at index [1] = [1] + Value checked at index [2] = [2] + Found 2 at index: 2 + + Value checked at index [4] = [4] + Value checked at index [8] = [18] + Value checked at index [12] = [61] + Value found between indexes [12] and [15] + Value checked at index [12] = [61] + Value checked at index [13] = [62] + Value checked at index [14] = [76] + Value checked at index [15] = [99] + Found 999 at index: (nil) + ``` + ++ [x] 14. Big 0 #6
_**[107-O](107-O)**_ contains the `time complexity` (average case) of a jump search in a singly linked list of size `n`, using `step = sqrt(n)`. + ++ [x] 15. Big 0 #7
_**[108-O](108-O)**_ contains the `time complexity` (average case) of a jump search in a skip list of size `n`, with an express lane using `step = sqrt(n)`.