From ea5a29c918839741e6640f801477c16756e92159 Mon Sep 17 00:00:00 2001 From: Zev Averbach Date: Sun, 27 Jul 2025 19:34:54 +0200 Subject: [PATCH] first --- 2023/Makefile | 2 + 2023/a.out | Bin 0 -> 33024 bytes 2023/algorithmic_thinking/README.md | 1 + 2023/algorithmic_thinking/food_line | Bin 0 -> 33248 bytes 2023/algorithmic_thinking/food_line.c | 30 + 2023/algorithmic_thinking/snowflakes/Makefile | 6 + 2023/algorithmic_thinking/snowflakes/TODO.md | 11 + ...understand_snowflakes_optimized_solution.c | 33 + ...tand_snowflakes_optimized_solution_WRONG.c | 28 + ...and_snowflakes_optimized_solution_WRONG2.c | 29 + ...and_snowflakes_optimized_solution_WRONG3.c | 31 + .../snowflakes/snowflakes | Bin 0 -> 33664 bytes .../snowflakes/snowflakes.c | 147 ++++ .../snowflakes/snowflakes.md | 25 + .../snowflakes/snowflakes.txt | 8 + 2023/arrays_of_structs | Bin 0 -> 33192 bytes 2023/arrays_of_structs.c | 16 + 2023/assert.c | 8 + 2023/beginning_c__from_beginner_to_pro/a.out | Bin 0 -> 8448 bytes .../hello_world.c | 10 + .../hello_world.o | Bin 0 -> 768 bytes .../limits.c | 17 + .../mult_table.c | 46 ++ .../printable_chars.c | 20 + 2023/get_index_of_letter | Bin 0 -> 33024 bytes 2023/get_index_of_letter.c | 9 + 2023/get_largest_number_for_x_bits.c | 10 + 2023/get_num_digits_of_integer | Bin 0 -> 33112 bytes 2023/get_num_digits_of_integer.c | 18 + 2023/leetcode/maximum_subarray_53 | Bin 0 -> 33264 bytes 2023/leetcode/maximum_subarray_53.c | 21 + 2023/muratori/add/.gitignore | 405 +++++++++++ 2023/muratori/add/a.out | Bin 0 -> 16592 bytes 2023/muratori/add/add.cpp | 10 + 2023/muratori/add/how_many_instructions.s | 44 ++ 2023/pointers | Bin 0 -> 33024 bytes 2023/pointers.c | 23 + 2023/shifting | Bin 0 -> 33024 bytes 2023/shifting.c | 11 + 2023/tiny_c_projects/greetings | Bin 0 -> 33144 bytes 2023/tiny_c_projects/greetings.c | 30 + 2023/tiny_c_projects/pithy | Bin 0 -> 33592 bytes 2023/tiny_c_projects/pithy.c | 41 ++ 2023/tiny_c_projects/pithy.txt | 5 + .../tsoding/music_visualizer/music_visualizer | 1 + 2025/a.out | Bin 0 -> 8440 bytes .../ch_4/division_and_signs | Bin 0 -> 8440 bytes .../ch_4/division_and_signs.c | 14 + .../ch_4/increment_a_float | Bin 0 -> 8440 bytes .../ch_4/increment_a_float.c | 8 + .../ch_4/reverse_digits | Bin 0 -> 8568 bytes .../ch_4/reverse_digits.c | 31 + .../ch_5/calc_num_digits | Bin 0 -> 8480 bytes .../ch_5/calc_num_digits.c | 17 + .../ch_5/find_closest_flight | Bin 0 -> 8560 bytes .../ch_5/find_closest_flight.c | 101 +++ .../ch_5/find_closest_flight.md | 15 + .../ch_5/find_smallest_and_largest | Bin 0 -> 8480 bytes .../ch_5/find_smallest_and_largest.c | 33 + .../ch_6/all_even_squares | Bin 0 -> 8480 bytes .../ch_6/all_even_squares.c | 12 + .../ch_6/approximate_e | Bin 0 -> 8480 bytes .../ch_6/approximate_e.c | 17 + .../ch_6/do_while | Bin 0 -> 8440 bytes .../ch_6/do_while.c | 10 + .../ch_6/largest_num | Bin 0 -> 8480 bytes .../ch_6/largest_num.c | 16 + .../ch_6/reverse_digits | Bin 0 -> 8480 bytes .../ch_6/reverse_digits.c | 17 + .../ch_6/sum_for | Bin 0 -> 8440 bytes .../ch_6/sum_for.c | 14 + .../ch_7/a.out | Bin 0 -> 8440 bytes .../ch_7/abbrev_name | Bin 0 -> 8488 bytes .../ch_7/abbrev_name.c | 19 + .../ch_7/char_nums | Bin 0 -> 8488 bytes .../ch_7/char_nums.c | 12 + .../ch_7/enter_to_continue | Bin 0 -> 8488 bytes .../ch_7/enter_to_continue.c | 19 + .../ch_7/enter_to_continue.py | 8 + .../ch_7/eval_math | Bin 0 -> 8480 bytes .../ch_7/eval_math.c | 36 + .../ch_7/frac_part | Bin 0 -> 8480 bytes .../ch_7/frac_part.c | 10 + .../ch_7/math_with_different_types | Bin 0 -> 8440 bytes .../ch_7/math_with_different_types.c | 35 + .../ch_7/sizeof | Bin 0 -> 8440 bytes .../ch_7/sizeof.c | 11 + .../ch_7/squares_types | Bin 0 -> 8440 bytes .../ch_7/squares_types.c | 36 + .../ch_7/typedefs | Bin 0 -> 8440 bytes .../ch_7/typedefs.c | 12 + .../ch_7/upper | Bin 0 -> 8520 bytes .../ch_7/upper.c | 17 + .../ch_7/word_length | Bin 0 -> 8488 bytes .../ch_7/word_length.c | 25 + .../ch_8/array_of_bool | Bin 0 -> 8616 bytes .../ch_8/array_of_bool.c | 17 + .../ch_8/cards.c | 9 + .../ch_8/designated_initializer | Bin 0 -> 8616 bytes .../ch_8/designated_initializer.c | 9 + .../ch_8/fib_seq | Bin 0 -> 8616 bytes .../ch_8/fib_seq.c | 14 + .../ch_8/random_walk | Bin 0 -> 8736 bytes .../ch_8/random_walk.c | 63 ++ .../ch_8/repeated_digits | Bin 0 -> 8656 bytes .../ch_8/repeated_digits.c | 34 + .../ch_8/sizeof_array | Bin 0 -> 8616 bytes .../ch_8/sizeof_array.c | 11 + .../chapter_2/benchy.sh | 35 + .../chapter_2/chapter_2.md | 39 + .../chapter_2/fewest_bills | Bin 0 -> 8440 bytes .../chapter_2/fewest_bills.c | 32 + .../chapter_2/fewest_bills.py | 14 + .../chapter_2/print_a_float | Bin 0 -> 8440 bytes .../chapter_2/print_a_float.c | 7 + .../chapter_2/vol | Bin 0 -> 8480 bytes .../chapter_2/volume_of_sphere.c | 16 + .../chapter_3/date_input | Bin 0 -> 8480 bytes .../chapter_3/date_input.c | 14 + .../chapter_3/printing_nums | Bin 0 -> 8440 bytes .../chapter_3/printing_nums.c | 13 + .../chapter_3/scanf_mixed_types | Bin 0 -> 8480 bytes .../chapter_3/scanf_mixed_types.c | 8 + 2025/checkmark/a.out | Bin 0 -> 8440 bytes 2025/checkmark/main.c | 11 + 2025/main.c | 11 + 2025/making_a_c_compiler | 1 + 2025/notes_how_i_program_c.md | 677 ++++++++++++++++++ 2025/number_guess/a.out | Bin 0 -> 8664 bytes 2025/number_guess/guess | Bin 0 -> 8712 bytes 2025/number_guess/guess.c | 35 + 2025/uninitialized_vars/a.out | Bin 0 -> 8440 bytes 2025/uninitialized_vars/main.c | 13 + 133 files changed, 2724 insertions(+) create mode 100644 2023/Makefile create mode 100755 2023/a.out create mode 100644 2023/algorithmic_thinking/README.md create mode 100755 2023/algorithmic_thinking/food_line create mode 100644 2023/algorithmic_thinking/food_line.c create mode 100644 2023/algorithmic_thinking/snowflakes/Makefile create mode 100644 2023/algorithmic_thinking/snowflakes/TODO.md create mode 100644 2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c create mode 100644 2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c create mode 100644 2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c create mode 100644 2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c create mode 100755 2023/algorithmic_thinking/snowflakes/snowflakes create mode 100644 2023/algorithmic_thinking/snowflakes/snowflakes.c create mode 100644 2023/algorithmic_thinking/snowflakes/snowflakes.md create mode 100644 2023/algorithmic_thinking/snowflakes/snowflakes.txt create mode 100755 2023/arrays_of_structs create mode 100644 2023/arrays_of_structs.c create mode 100644 2023/assert.c create mode 100755 2023/beginning_c__from_beginner_to_pro/a.out create mode 100644 2023/beginning_c__from_beginner_to_pro/hello_world.c create mode 100644 2023/beginning_c__from_beginner_to_pro/hello_world.o create mode 100644 2023/beginning_c__from_beginner_to_pro/limits.c create mode 100644 2023/beginning_c__from_beginner_to_pro/mult_table.c create mode 100644 2023/beginning_c__from_beginner_to_pro/printable_chars.c create mode 100755 2023/get_index_of_letter create mode 100644 2023/get_index_of_letter.c create mode 100644 2023/get_largest_number_for_x_bits.c create mode 100755 2023/get_num_digits_of_integer create mode 100644 2023/get_num_digits_of_integer.c create mode 100755 2023/leetcode/maximum_subarray_53 create mode 100644 2023/leetcode/maximum_subarray_53.c create mode 100644 2023/muratori/add/.gitignore create mode 100755 2023/muratori/add/a.out create mode 100644 2023/muratori/add/add.cpp create mode 100644 2023/muratori/add/how_many_instructions.s create mode 100755 2023/pointers create mode 100644 2023/pointers.c create mode 100755 2023/shifting create mode 100644 2023/shifting.c create mode 100755 2023/tiny_c_projects/greetings create mode 100644 2023/tiny_c_projects/greetings.c create mode 100755 2023/tiny_c_projects/pithy create mode 100644 2023/tiny_c_projects/pithy.c create mode 100644 2023/tiny_c_projects/pithy.txt create mode 160000 2023/tsoding/music_visualizer/music_visualizer create mode 100755 2025/a.out create mode 100755 2025/c_programming_a_modern_approach/ch_4/division_and_signs create mode 100644 2025/c_programming_a_modern_approach/ch_4/division_and_signs.c create mode 100755 2025/c_programming_a_modern_approach/ch_4/increment_a_float create mode 100644 2025/c_programming_a_modern_approach/ch_4/increment_a_float.c create mode 100755 2025/c_programming_a_modern_approach/ch_4/reverse_digits create mode 100644 2025/c_programming_a_modern_approach/ch_4/reverse_digits.c create mode 100755 2025/c_programming_a_modern_approach/ch_5/calc_num_digits create mode 100644 2025/c_programming_a_modern_approach/ch_5/calc_num_digits.c create mode 100755 2025/c_programming_a_modern_approach/ch_5/find_closest_flight create mode 100644 2025/c_programming_a_modern_approach/ch_5/find_closest_flight.c create mode 100644 2025/c_programming_a_modern_approach/ch_5/find_closest_flight.md create mode 100755 2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest create mode 100644 2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest.c create mode 100755 2025/c_programming_a_modern_approach/ch_6/all_even_squares create mode 100644 2025/c_programming_a_modern_approach/ch_6/all_even_squares.c create mode 100755 2025/c_programming_a_modern_approach/ch_6/approximate_e create mode 100644 2025/c_programming_a_modern_approach/ch_6/approximate_e.c create mode 100755 2025/c_programming_a_modern_approach/ch_6/do_while create mode 100644 2025/c_programming_a_modern_approach/ch_6/do_while.c create mode 100755 2025/c_programming_a_modern_approach/ch_6/largest_num create mode 100644 2025/c_programming_a_modern_approach/ch_6/largest_num.c create mode 100755 2025/c_programming_a_modern_approach/ch_6/reverse_digits create mode 100644 2025/c_programming_a_modern_approach/ch_6/reverse_digits.c create mode 100755 2025/c_programming_a_modern_approach/ch_6/sum_for create mode 100644 2025/c_programming_a_modern_approach/ch_6/sum_for.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/a.out create mode 100755 2025/c_programming_a_modern_approach/ch_7/abbrev_name create mode 100644 2025/c_programming_a_modern_approach/ch_7/abbrev_name.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/char_nums create mode 100644 2025/c_programming_a_modern_approach/ch_7/char_nums.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/enter_to_continue create mode 100644 2025/c_programming_a_modern_approach/ch_7/enter_to_continue.c create mode 100644 2025/c_programming_a_modern_approach/ch_7/enter_to_continue.py create mode 100755 2025/c_programming_a_modern_approach/ch_7/eval_math create mode 100644 2025/c_programming_a_modern_approach/ch_7/eval_math.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/frac_part create mode 100644 2025/c_programming_a_modern_approach/ch_7/frac_part.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/math_with_different_types create mode 100644 2025/c_programming_a_modern_approach/ch_7/math_with_different_types.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/sizeof create mode 100644 2025/c_programming_a_modern_approach/ch_7/sizeof.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/squares_types create mode 100644 2025/c_programming_a_modern_approach/ch_7/squares_types.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/typedefs create mode 100644 2025/c_programming_a_modern_approach/ch_7/typedefs.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/upper create mode 100644 2025/c_programming_a_modern_approach/ch_7/upper.c create mode 100755 2025/c_programming_a_modern_approach/ch_7/word_length create mode 100644 2025/c_programming_a_modern_approach/ch_7/word_length.c create mode 100755 2025/c_programming_a_modern_approach/ch_8/array_of_bool create mode 100644 2025/c_programming_a_modern_approach/ch_8/array_of_bool.c create mode 100644 2025/c_programming_a_modern_approach/ch_8/cards.c create mode 100755 2025/c_programming_a_modern_approach/ch_8/designated_initializer create mode 100644 2025/c_programming_a_modern_approach/ch_8/designated_initializer.c create mode 100755 2025/c_programming_a_modern_approach/ch_8/fib_seq create mode 100644 2025/c_programming_a_modern_approach/ch_8/fib_seq.c create mode 100755 2025/c_programming_a_modern_approach/ch_8/random_walk create mode 100644 2025/c_programming_a_modern_approach/ch_8/random_walk.c create mode 100755 2025/c_programming_a_modern_approach/ch_8/repeated_digits create mode 100644 2025/c_programming_a_modern_approach/ch_8/repeated_digits.c create mode 100755 2025/c_programming_a_modern_approach/ch_8/sizeof_array create mode 100644 2025/c_programming_a_modern_approach/ch_8/sizeof_array.c create mode 100755 2025/c_programming_a_modern_approach/chapter_2/benchy.sh create mode 100644 2025/c_programming_a_modern_approach/chapter_2/chapter_2.md create mode 100755 2025/c_programming_a_modern_approach/chapter_2/fewest_bills create mode 100644 2025/c_programming_a_modern_approach/chapter_2/fewest_bills.c create mode 100644 2025/c_programming_a_modern_approach/chapter_2/fewest_bills.py create mode 100755 2025/c_programming_a_modern_approach/chapter_2/print_a_float create mode 100644 2025/c_programming_a_modern_approach/chapter_2/print_a_float.c create mode 100755 2025/c_programming_a_modern_approach/chapter_2/vol create mode 100644 2025/c_programming_a_modern_approach/chapter_2/volume_of_sphere.c create mode 100755 2025/c_programming_a_modern_approach/chapter_3/date_input create mode 100644 2025/c_programming_a_modern_approach/chapter_3/date_input.c create mode 100755 2025/c_programming_a_modern_approach/chapter_3/printing_nums create mode 100644 2025/c_programming_a_modern_approach/chapter_3/printing_nums.c create mode 100755 2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types create mode 100644 2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types.c create mode 100755 2025/checkmark/a.out create mode 100644 2025/checkmark/main.c create mode 100644 2025/main.c create mode 160000 2025/making_a_c_compiler create mode 100644 2025/notes_how_i_program_c.md create mode 100755 2025/number_guess/a.out create mode 100755 2025/number_guess/guess create mode 100644 2025/number_guess/guess.c create mode 100755 2025/uninitialized_vars/a.out create mode 100644 2025/uninitialized_vars/main.c diff --git a/2023/Makefile b/2023/Makefile new file mode 100644 index 0000000..246a361 --- /dev/null +++ b/2023/Makefile @@ -0,0 +1,2 @@ +yep: + clang arrays_of_structs.c -o arrays_of_structs && ./arrays_of_structs diff --git a/2023/a.out b/2023/a.out new file mode 100755 index 0000000000000000000000000000000000000000..4dd11962e3588795952161134475c33fa4c7505f GIT binary patch literal 33024 zcmeI*Pe>F|9Ki9nYH4UHLI0>=FGh9Hv<`I;*K}#5P)P#`XXZLHZ@+)}*=kIz z*%;F#ty^1*F%L~#wv0I`Es$pI#Qx7%JaoPLyh&eeZ7Nz zhUU>|^Yz00)-DuFg=~JwPin09%-7R%($G8_dJUpsZ8?8Gn@`znesM*1G}e3W>&^Is zG;gdCt*6&v%heejo*uR%6XR30tC6U=h3<{L-XGf3R__sOmsTQ|LGu0UwaHu;iQ4f# zGwS88yC2xQ&u)&5-$;&*h4)CMVO^5D@CR!bt#jF!imp$uaz?sKKF>&gQF668A?>N; zdOjbdKPP>=v`J~v(7D>8^`rZB`(0&!?@rklm)0SzuUstj<+AgAskK~6a*b2cbRE^N z!S&j6p+BPF7}KHIC${wAc?7CxV@+tJYHhdZQ7x2xp#L?3Ie zMCY=N00IagfB*srAbnhE`O=tKA(}MF9nFc7@$S+F!WpE zVYuyte8oKxj`(Bsg#;S*ULl(=E!GnGjahR<6W!lzK0c5wyPUD<)$~HSl(w1lTq<2K zhnDBEd2`sf3E}jvSnZ4Fx~^l=x@1G&d7!zz3qg9_bfM+7#pR3if0wted~|8Q0eOp= AL;wH) literal 0 HcmV?d00001 diff --git a/2023/algorithmic_thinking/README.md b/2023/algorithmic_thinking/README.md new file mode 100644 index 0000000..ad16203 --- /dev/null +++ b/2023/algorithmic_thinking/README.md @@ -0,0 +1 @@ +This repo is to document my progress working through the book "Algorithmic Thinking". diff --git a/2023/algorithmic_thinking/food_line b/2023/algorithmic_thinking/food_line new file mode 100755 index 0000000000000000000000000000000000000000..87fdbb89434ca5c9eda3bc6c68f9275de55a1006 GIT binary patch literal 33248 zcmeI5&ud&&6vyvtT3gfF7m8Fx!P!KrLPBlD+EARddH9NH8%!!e+uly*M`mm?3G)M! zh%zA^$Ye50q2R_pAqYj>XiAGyLC~eU&O#x95eh9BU3C1O``$}uUUVb4k?$er-E+>p z_q=mIbF;kf{`lu_kM=5Mbt$EGL3>1ZD^*qzSV}zyb)j0148HBW@4S0KqMc%Ox_|3Q zo@ETwddwLg>l|X{M>}K6_xB(yijs_IT}aLqd)}(|*7B?c$fc9n!j( z{V0=7=u9>>gJ`(EFZ_Jt{tWTMIkuC*kl*13RO_L^u|a+2!ue6}*NA%S47i6|*&m|9 zR`!V2lQV5U!$0PWV62g-H{bV^@REFiJl<{=N4HNYGeAC2_ zV}{fx^JJaJeE$Y~*~h}>S?C6Q?0Yr0s8ky5e&`6aU4k`t?4R}lu*Wf9@`U9e0ukr| zXn!%E>z~R@^e5)067WTyhe{nke><8ycJHLS@?rYRKTclxy8{-zLBy?nHjD zYmps271QI*ubB65VvL{AlqYF6KSxp3oAP2T<5LX4GCuHiZz%X=O3 zL3vH4fO7TRmiy*iyDVpmkFL9mcjQ;Ne0xLA4bUb}cr(&6@&d6jj zmP;r-o6BSiDW&uASTEd2NhfJ+OKvvz>O5?G@7{D-uY6mw>hMg + +#define MAX_LINES 100 + +int main() +{ + int i, j, num_lines, num_new_people; + int lines[MAX_LINES]; + scanf("%d%d", &num_lines, &num_new_people); + for (i = 0; i < num_lines; i++) + { + scanf("%d", &lines[i]); + } + int smallest; + int smallest_index = -1; + for (i = 0; i < num_new_people; i++) + { + smallest = 101; + for (j = 0; j < num_lines; j++) + { + if (lines[j] < smallest) { + smallest = lines[j]; + smallest_index = j; + } + } + printf("%d\n", smallest); + lines[smallest_index] += 1; + } + return 0; +} diff --git a/2023/algorithmic_thinking/snowflakes/Makefile b/2023/algorithmic_thinking/snowflakes/Makefile new file mode 100644 index 0000000..3c2f31b --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/Makefile @@ -0,0 +1,6 @@ +build: + clang snowflakes.c -o snowflakes +run: + ./snowflakes < snowflakes.txt +go: + clang snowflakes.c -o snowflakes && ./snowflakes < snowflakes.txt diff --git a/2023/algorithmic_thinking/snowflakes/TODO.md b/2023/algorithmic_thinking/snowflakes/TODO.md new file mode 100644 index 0000000..0d2be6b --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/TODO.md @@ -0,0 +1,11 @@ +- demonstrate segfault when you iterate beyond the end of an array +- make sure `num_snowflakes` is on the stack, and that it goes out of scope after you return from get_num_snowflakes_from_stdin + - print the memory address, for example +- is snowflakes really empty after declaration? +- why do we have to create `snowflake` using malloc? its size is predictable at compile time, so why can't we use the stack? + +# Later +- how does `bool` work in C? The definitions are simple enough but what about the type? +- does clang put "0"s in a declared array? +- make the Snowflakes linked list, but putting the latest Entry at the end instead of the beginning +- make the Snowflakes linked list, but putting the latest Entry in the middle (?) diff --git a/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c new file mode 100644 index 0000000..427899f --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution.c @@ -0,0 +1,33 @@ +#include + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +int main() +{ + Node nodes[5]; + for (int i = 0; i < 5; i++) + { + nodes[i].value = i + 42; + if (i > 0) + { + nodes[i - 1].next = &nodes[i]; + } + } + for (int i = 0; i < 5; i++) + { + printf("node at %d has value %d\n", i, nodes[i].value); + if (i > 0) + { + printf("node at %d has value %d\n", i - 1, nodes[i - 1].value); + printf( + "that node's 'next' is %p and, as expected, its 'next' has value %d\n", + nodes[i - 1].next, nodes[i - 1].next->value + ); + } + } + return 0; +} diff --git a/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c new file mode 100644 index 0000000..583ad63 --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG.c @@ -0,0 +1,28 @@ +#include +#include + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +int main(void) +{ + Node nodes[5]; + + for (int i = 0; i < 5; i++) + { + Node curr_node = { i }; + nodes[i] = curr_node; + printf("Node in ARR current after insertion: %d\n", nodes[i].value); + printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]); + } + + for (int i = 0; i < 5; i++) + { + printf("Node in ARR: %d\n", nodes[i].value); + } + + return 0; +} diff --git a/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c new file mode 100644 index 0000000..fe7e857 --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG2.c @@ -0,0 +1,29 @@ +#include +#include + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +int main(void) +{ + Node *nodes[5]; + + for (int i = 0; i < 5; i++) + { + Node * curr_node = malloc(sizeof(Node)); + curr_node->value = i; + nodes[i] = curr_node; + printf("Node in ARR current after insertion: %d\n", nodes[i]->value); + printf("Node _address_ in ARR current after insertion: %p\n", &nodes[i]); + } + + for (int i = 0; i < 5; i++) + { + printf("Node in ARR: %d\n", nodes[i]->value); + } + + return 0; +} diff --git a/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c new file mode 100644 index 0000000..78f1912 --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/array_of_structs_trying_to_understand_snowflakes_optimized_solution_WRONG3.c @@ -0,0 +1,31 @@ +#include +#include + +#define ARRAY_LEN 1047505 + +typedef struct Node +{ + int value; + struct Node * next; +} Node; + +Node * make_node(int value) +{ + Node * node = malloc(sizeof(Node)); + node->value = value; + return node; +} + +int main() +{ + Node * nodes[ARRAY_LEN]; + for (int i = 0; i < ARRAY_LEN; i++) + { + nodes[i] = make_node(i); + } + for (int i = 0; i < ARRAY_LEN; i++) + { + printf("Node in ARR: %d\n", nodes[i]->value); + } + return 0; +} diff --git a/2023/algorithmic_thinking/snowflakes/snowflakes b/2023/algorithmic_thinking/snowflakes/snowflakes new file mode 100755 index 0000000000000000000000000000000000000000..40804d7a7a7ce89dea109c17884f890d2175d47e GIT binary patch literal 33664 zcmeI5UufG^6vyvxUH5-Wkl{8Imj^qXgRTBSbbq_qAO5Cpt8RfIjN2@Kn$R?@$uF)| zs+n0~+0Q^9)T#JTWsV{PhcKDou(GmLd{8v7J{0Uj)C4L7DXi*P{GOX%w&d4|4E7?N z1Lxj*?w|8J_j6DCc5lA_>-QUrh0vOW5DTDg(uG0{2@hIAtb-a*DLZyQqrarTcz}8v zMQb!~YmuLmm?-5T{qUj2DQbONW1q0?W^|JxBqdUs!G6;=P&xf|Y{O>IPkSICyS7$! zEEFkK+l7<~GkG*|v&-qPb63^h1NIEE>t%`UZ;{<8r9WZDBeAZVcH@3uxBWeAkCR<5 zbO!YzWioaw5(~&kEYynuXMCf!zr*$-vYkHa{n0tRr~I_S?r9_gUla ztSLW86pVQm-)^a4}hzmAJih_Yq zJSu;bCB)+xq8z4EzKH5E+rDVq#AYotbzF#5sE6MaqKtYuv;$f%OJI=J&bEH6*6zf5 zTI&?@?SZaHMTcVMpTLJ^o0Bn-`pcKcstv`*ONo=1y{m0*HUg-b!?4#)OK&yKq zFRN5rZFljtCdwPNKhz`-VJm7sJF9lBr0+fnr7_wwEa28|#e#Sa4kXNA&*nXw5m)4> znj|)$BL98hL-j`6CSEK|fC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1egF5 zU;<2l2`~XBzyz286JP>NfC(@GCcp%k025#WOn?b60Vco%m;e)C0!)AjFaajO1pcoC zyhi31BR%6bvOCt}-l>sIUOz92tI)Lc>qdIqedKZove~u%gxaE-pBXG0nPf5JE#&k( zb*BgOi(FYRT85>U4gbEpweVy{pSJXTlV13eE+oEx?az#N62nCdPpaW5OP}7RPq|Jk z#rPGyB1&t}C_(Y@e0ogF==st|l}ZI;+9|#Apyv8w><;4Tgd=A>g-v=sqfe#>Cxt6Z z-?dZpo(+l_r02Kg8s(=8`N)|0|q4?R2N@Y}Y4bjEYk_|yC z?QqOmhtJHPRfnpeP!&q2tMP~$Djc7uifi8a&E@o2d`9H-GF9opGQwMr7AVZ&fXX6upScMdsG&Uj~X`V{%h>627}?>2qLmHiy;uRSP= zpWv>jf#hh&olO`4mTAMU=Z$Zs7Z}JSjSTXs8gkJ{ zZ=VpZ69mTcQZFUqRA%)_OP^9(n7TRBS}}q4w2?8^8kTqZW`Ab&gvtkG-ehSHeJ4HM zN*iw2@5Rn_nkfEA5pNtftmb~&kVSi%%MG3T^+p9FqWJN3L^m`F)5u=xYlpAwh4Zki zcc`y;8E2mxI%Cg1eu-Lrh~1U#Ajf_+l>TanmIymVp1_E8fo9s{*4t_vq=Fa%DZ1@$ zRhQz=M>R?#idVCW7g1XK=8UH%TtjpV_>nQ=_&Dw6$mJs49Cj_Qw0wZN((<7y&#H1% zm7k()2?(F*!S(K7zo#YO=?zgk;twXI*(?3wpugL*V<$$IsXM^;V+BclO?P|gj(Zzw z(%SuWYS#NfC(@GCcp%k025#WOn?b60Vco%m;e)C z0!)AjFaajO1egF5U;<2l2`~XBzyz286JP>NfC(@GCcp%k025#Wcb$L-9_f*UFqEDa zse2eeRPXx zVfy^t)Gb54NVKu1E9r{|gzWJ}qrHA1`{I$984@z#_r*f+r%6F?GzfaIph=9qisFY? zV1|8(uvi&O_Q*u6_gE&9yep?EKBGk~w&(KC?^QOi;?)wG;IVCbO z`c3+k&|2RzM?XyAK@!YoE{VB1S=IXY(?z;aX8p&pNXM}_Y^jpL*;&nvyPd}W027@A AlK=n! literal 0 HcmV?d00001 diff --git a/2023/algorithmic_thinking/snowflakes/snowflakes.c b/2023/algorithmic_thinking/snowflakes/snowflakes.c new file mode 100644 index 0000000..e5ae8cc --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/snowflakes.c @@ -0,0 +1,147 @@ +#include +#include +#include + +#define STRING_THERE_ARE_DUPLICATE_SNOWFLAKES "Twin snowflakes found.\n" +#define STRING_SNOWFLAKES_ARE_UNIQUE "No two snowflakes are alike.\n" +#define LEN_SNOWFLAKE 6 +#define LARGEST_POSSIBLE_SUM 6000 + +int get_num_snowflakes_from_stdin(void) +{ + int num_snowflakes; // this is allocated on the stack + scanf("%d", &num_snowflakes); + return num_snowflakes; +} + +bool they_are_equal_right(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset) +{ + int sf1_tip, sf2_tip, sf2_index; + for (int i = 0; i < LEN_SNOWFLAKE; i++) + { + sf1_tip = sf1[i]; + + sf2_index = (i + sf2_offset) % LEN_SNOWFLAKE; + sf2_tip = sf2[sf2_index]; + + if (sf1_tip != sf2_tip) + return false; + } + return true; +} + +bool they_are_equal_left(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE], int sf2_offset) +{ + int sf1_tip, sf2_tip, sf2_index; + for (int i = 0; i < LEN_SNOWFLAKE; i++) + { + sf1_tip = sf1[i]; + + sf2_index = sf2_offset - i; + if (sf2_index < 0) + sf2_index = sf2_index + LEN_SNOWFLAKE; + sf2_tip = sf2[sf2_index]; + + if (sf1_tip != sf2_tip) + return false; + } + return true; +} + +bool they_are_equal(int sf1[LEN_SNOWFLAKE], int sf2[LEN_SNOWFLAKE]) +{ + for (int o = 0; o < LEN_SNOWFLAKE; o++) + { + if (they_are_equal_right(sf1, sf2, o)) + return true; + if (they_are_equal_left(sf1, sf2, o)) + return true; + } + return false; +} + +int get_hash(int snowflake[LEN_SNOWFLAKE]) +{ + int sum = 0; + for (int i = 0; i < LEN_SNOWFLAKE; i++) + { + sum += snowflake[i]; + } + return sum; +} + +typedef struct Entry { + int snowflake[LEN_SNOWFLAKE]; + struct Entry * next; +} Entry; + +void populate_snowflakes_from_stdin(Entry * snowflakes[LARGEST_POSSIBLE_SUM], int indices_to_check[LARGEST_POSSIBLE_SUM], int num_snowflakes) +{ + int hash; + Entry * snowflake; + int indices_index = 0; + for (int _ = 0; _ < num_snowflakes; _++) + { + snowflake = malloc(sizeof(Entry)); + for (int j = 0; j < LEN_SNOWFLAKE; j++) + { + scanf("%d", &snowflake->snowflake[j]); + } + hash = get_hash(snowflake->snowflake); + if (snowflakes[hash] != 0) + { + indices_to_check[indices_index] = hash; + indices_index++; + } + // snowflakes[hash] is 0 at the terminus + snowflake->next = snowflakes[hash]; + snowflakes[hash] = snowflake; + } +} + +int main(void) +{ + int num_snowflakes = get_num_snowflakes_from_stdin(); + + Entry * snowflakes[LARGEST_POSSIBLE_SUM]; // 48MB of memory, 6 million slots + int indices_to_check[LARGEST_POSSIBLE_SUM]; + + populate_snowflakes_from_stdin(snowflakes, indices_to_check, num_snowflakes); + + int prev_idx; + int idx_to_check; + + for (int i = 0; i < LARGEST_POSSIBLE_SUM; i++) + { + idx_to_check = indices_to_check[i]; + if (idx_to_check == 0) + break; + if (idx_to_check == prev_idx) + continue; + + printf("at index %d of indices_to_check => %d\n", i, indices_to_check[i]); + Entry * sf = snowflakes[idx_to_check]; + Entry * next; + while (sf->next) + { + next = sf->next; + if (they_are_equal(sf->snowflake, next->snowflake)) + { + printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES); + return 0; + } + } + prev_idx = idx_to_check; + } + + // for (int i = 0; i < num_snowflakes; i++) + // for (int j = i + 1; j < num_snowflakes; j++) + // if (they_are_equal(snowflakes[i], snowflakes[j])) + // { + // printf(STRING_THERE_ARE_DUPLICATE_SNOWFLAKES); + // return 0; + // } + + // printf(STRING_SNOWFLAKES_ARE_UNIQUE); + return 0; +} diff --git a/2023/algorithmic_thinking/snowflakes/snowflakes.md b/2023/algorithmic_thinking/snowflakes/snowflakes.md new file mode 100644 index 0000000..11b2f65 --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/snowflakes.md @@ -0,0 +1,25 @@ +Two snowflakes are identical if they are the same, +if we can make them the same by moving rightward +through one of the snowflakes (moving clockwise), +or if we can make them the same by moving leftward +through one of the snowflakes (moving counterclockwise). + +# Input + +The first line of input is an integer n, +the number of snowflakes that we’ll be processing. +The value n will be between 1 and 100,000. + +Each of the following n lines represents one snowflake: + each line has six integers, where each integer is + at least 0 and at most 10,000,000. + +# Output +Our output will be a single line of text: + +If there are no identical snowflakes, output exactly + "No two snowflakes are alike." +If there are at least two identical snowflakes, output exactly + "Twin snowflakes found." + +The time limit for solving the test cases is one second. diff --git a/2023/algorithmic_thinking/snowflakes/snowflakes.txt b/2023/algorithmic_thinking/snowflakes/snowflakes.txt new file mode 100644 index 0000000..b361343 --- /dev/null +++ b/2023/algorithmic_thinking/snowflakes/snowflakes.txt @@ -0,0 +1,8 @@ +4 +6 5 4 3 2 1 +4 6 5 1 2 3 +1 2 10 2 1 5 +15 12 12 12 12 12 +4 5 6 1 2 3 +1 1 1 1 1 1 + diff --git a/2023/arrays_of_structs b/2023/arrays_of_structs new file mode 100755 index 0000000000000000000000000000000000000000..3af52f77c28c17e0056134630fd69d1b024b5706 GIT binary patch literal 33192 zcmeI5&u^qrPNc>-vxV-_{ALAeDYk^OrBHa*|I)4bGDK` z-BQYJ@EnVyylV{uF#+a{&4se5v8wb)|&ePO+g`k+q`#^t`8V zJ^qi6$sR;p=q~7F67?pVVQsRp)NH^Pc@8Rdd^6sx=--dO|54@qrQ6A`EqTver~_3> zvg|RoVYThgk(UrL`;%$NA?#1`$p0oQa^GW6nUg)h!{5>w)`EH}OHmxOCSIRFU17~E zQZHbT{P+4wZO8bc^@SIO1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@ z1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNZ@1W14cNPq-LfCNb3|0nR4 zzx;!LFMIdzdS6Is{_1-h{^~1hFh7^Z)y>2Ny$!5czw3P_jkErW zx9+d*{siVN|B%0$Z1^|6#(3fbMAp@&G^=N#chab2!!7sQTdur$-#>nD^rm#A9!7$% z<}QD^DQ&HzJ0Fbyi1ESrZ|3(8{N|*x)Vvy2B~-43cBT@xI&EWL46fQ(Iavq;W6w03 zu+YSv)Z^&wqnF(i?!ocs;4`Y*3EOedbLTrfw=mtsblS7~yv>&#rBLb5EnyTL^rVZO z73uNag&y58+x-hSZ3E6OGWJ+Mofzv&ZglMSZ7+4-xcngj5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8} z0TLhq5+DH*AOR8}0TLhq5+DH*AORBi&k4A=gPc`92bE7OWIqy+J*!;K{vL?2OE~eo zG>D|vIrxBpgG0ZaGtf(z(^^MywSGm{7q95~YS_$27LsbOp>!)~)w@edcX8BxJTEC7 zt@g<%A0!x2zq|`ew-$A9EvP4PpcjK`Bj~9et!mg-yOmimSU&2cCz{JX_CrTu$oCS& sm+vTG6w}BAezShL;7=EPDMy&#@N-+*&__tQ*xuu1YE$OFMIm_~0>uyj1poj5 literal 0 HcmV?d00001 diff --git a/2023/arrays_of_structs.c b/2023/arrays_of_structs.c new file mode 100644 index 0000000..ef22992 --- /dev/null +++ b/2023/arrays_of_structs.c @@ -0,0 +1,16 @@ +#include +#include + +typedef struct MyStruct { + char name[10]; +} MyStruct; + + +int main() +{ + MyStruct * structs_p = malloc(5 * sizeof(MyStruct)); + MyStruct &structs_p[0] = { "Sylvia" }; + printf("structs[4] = '%s'\n", structs[4].name); + printf("pointer for structs[4] = '%p'\n", &structs[4].name); + return 0; +} diff --git a/2023/assert.c b/2023/assert.c new file mode 100644 index 0000000..a9ede9b --- /dev/null +++ b/2023/assert.c @@ -0,0 +1,8 @@ +#include + +int main() +{ + int age = 43; + assert(age == 44); + return 0; +} diff --git a/2023/beginning_c__from_beginner_to_pro/a.out b/2023/beginning_c__from_beginner_to_pro/a.out new file mode 100755 index 0000000000000000000000000000000000000000..4eba9a409d2d1e6e4dd9d32ab6bcd5ddae1b1e41 GIT binary patch literal 8448 zcmeHMJ!=#}7@o_A#+aBxP~sO%6VZT4Ohjz_h%tDg7&Q?NLB>mNFSoF_8+Z3&E=>+W z(89t_kbs3hK(G)}h#+XGrInpvW1*#m=kw0&-c2sILDC2^FT9y~zn+FYbk6bZouooVi4|)Cj}eJX-EgFHVncG!t!iTcQxbn_A0z>Q3lz% zbVYb^T!=Z(4H^?{#_Nypx*`y=iTfl0UJ&}WJ0B%9wn-e$ZPeQLegsDkWfj5-)Pw69O0LR=W185a{%SEtqPV*l3dYrMZvOKT>j9C8UU z?=@IU#*2BQBAqwlKi+b5wR3yr($&k87cQw;fuN>&!IlygQ={XYV5};q%o!^pHWt}M z*dwrsn||nX*fgKdXpf-X3B3l@g>p%Cd_7M6PrNnXyAVT&;W)G5a^Md;_Uv%J;^bkI z9Dve1?hdX_R8OAityWGvIh(4ij>5;GYD}PX?vj1UUC*J8LB!Nu}4BX5-`kHxsdJn!2-c)O~+S&@{@sfS}fd>8&^4FB>i*jYj#gMyJLoQO^D}N|c zkqg%=fC#t<~Uqs9ay*%Dh3Jq?(}u5-KXdgQ@(Wj1TY03+Hv^ z?8moALfNp&7^RGQ*?#2|aQ^H|ckYGxGWCsqARK>t`(16|I|7=dPc7zrJ2&fC0rv_# ztSnf3u9)+A5PH7F7jsS-k%I4)_&}bA9{zT-jU{C_u;*PXpHw@NTpAQTKiukYNyVFt zXS-V-t##=pV>zBX-)d=&qSD!%%YOkM53(eoZor9${hM(UK%&8E^zN`|@xWfT z-PX9EsGHFkl!k3>XFs1BL;^fMLKe z@c%Kuu_St{%tGm6K))Q&-_GT`AmSvlpT?`J_IN03;|nygfLH5~?(USN1lvtLinq%* z#x_wZ3Tw%lD~FaSTDiRCvmK?J?J~S!t4Bzc-j`B?5I)`5^-%}+9@Nl%2wLh&A$U@b N7v+ucLc;4)`vW?Azr+9l literal 0 HcmV?d00001 diff --git a/2023/beginning_c__from_beginner_to_pro/hello_world.c b/2023/beginning_c__from_beginner_to_pro/hello_world.c new file mode 100644 index 0000000..fc29a5b --- /dev/null +++ b/2023/beginning_c__from_beginner_to_pro/hello_world.c @@ -0,0 +1,10 @@ +#include +#define MESSAGE_WITH_A_LENGTH_GREATER_THAN_31_HOPEFULLY "Hi Mom!\n" + +int main(void) +{ + int a_number_whose_var_name_has_quite_a_long_length_wouldnt_you_say = 42; + printf(MESSAGE_WITH_A_LENGTH_GREATER_THAN_31_HOPEFULLY); + printf("%d\n", a_number_whose_var_name_has_quite_a_long_length_wouldnt_you_say); + return 0; +} diff --git a/2023/beginning_c__from_beginner_to_pro/hello_world.o b/2023/beginning_c__from_beginner_to_pro/hello_world.o new file mode 100644 index 0000000000000000000000000000000000000000..230aafe15fedbdf3d71c04318d06caab78cd1467 GIT binary patch literal 768 zcma)4u}T9$5S@!L38={-3JMkx6l?^w7ZeR#VL&888W}=N%oTSD@x+iy2#6L!KE={f zP|!|)La|w|ZD)mPslD}ba!RRNu2hv_huZ5QWO}pOtlh=81 z?{Xi~<6W#~K$r9^YWdph+`4-=o}bOvgIOur(0ee)wngxS=1zNuoR;gjqsdektL9Lj z0lHr%>^;(Y+C7bJj3u;B-X)eVj)U>ZlTwCG$VM3%G~^BTNnCVL@ +#include +#include + +int main(void) { + printf("variables of type char store values from %d to %d\n", CHAR_MIN, CHAR_MAX); + printf("variables of type unsigned char store values from 0 to %d\n", UCHAR_MAX); + printf("variables of type short store values from %d to %d\n", SHRT_MIN, SHRT_MAX); + printf("variables of type unsigned short store values from 0 to %d\n", USHRT_MAX); + printf("variables of type int store values from %d to %d\n", INT_MIN, INT_MAX); + printf("variables of type unsigned int store values from 0 to %d\n", UINT_MAX); + printf("variables of type long store values from %ld to %ld\n", LONG_MIN, LONG_MAX); + printf("variables of type unsigned long store values from 0 to %lu\n", ULONG_MAX); + printf("variables of type long long store values from %lld to %lld\n", LLONG_MIN, LLONG_MAX); + printf("variables of type unsigned long long store values from 0 to %llu\n", ULLONG_MAX); + printf("\nthe size of the smallest positive non-zero value of type float is %.3e\n", FLT_MIN); +} diff --git a/2023/beginning_c__from_beginner_to_pro/mult_table.c b/2023/beginning_c__from_beginner_to_pro/mult_table.c new file mode 100644 index 0000000..c42bb68 --- /dev/null +++ b/2023/beginning_c__from_beginner_to_pro/mult_table.c @@ -0,0 +1,46 @@ +#include +#include + +/* Write a program that will generate a multiplication table of a size entered by the user. +A table of size 4, for instance, would have four rows and four columns. +The rows and columns would be labeled from 1 to 4. +Each cell in the table will contain the product of the corresponding row and column numbers, +so the value in the position corresponding to the third row and the fourth column would contain 12.*/ + +void print_column_labels(int size_of_table) { + printf(" "); + for (int i=0; i 99) { + printf("%d"); + } else if (prev_prod > 9) { + printf(" %d", prod); + } else { + printf(" %d", prod); + } + prev_prod = prod; + } + printf("\n"); + prev_prod = 0; + } + return 0; +} + diff --git a/2023/beginning_c__from_beginner_to_pro/printable_chars.c b/2023/beginning_c__from_beginner_to_pro/printable_chars.c new file mode 100644 index 0000000..5c4b6a8 --- /dev/null +++ b/2023/beginning_c__from_beginner_to_pro/printable_chars.c @@ -0,0 +1,20 @@ +/* +Write a program that will output the printable characters for character code values from 0 to 127. +Output each character code along with its symbol with two characters to a line. +Make sure the columns are aligned. +(Hint: You can use the isgraph() function that’s declared in ctype.h to determine when a character is printable.)” +*/ + +#include +#include + +int main(void) { + for (int i=0; i<128; i++) { + if (isgraph(i)) { + printf("%d: %c\n", i, i); + } else { + printf("%d: %c\n", i, toascii(i)); + } + } + return 0; +} diff --git a/2023/get_index_of_letter b/2023/get_index_of_letter new file mode 100755 index 0000000000000000000000000000000000000000..f352a84b2669af6a6f7edf86be479c0f2e67ba97 GIT binary patch literal 33024 zcmeI*&r1|x7zglo)6&Y^r9%{T7zDEnG^#^~#I+n+F;r4Pf_T{ZzkmJN zrc|OuDYZu;g=kgku5x6g)G>*Sgw_*7SKS-#^>G|+Y>CEn*9>*;%R;TE+?lDyCFcBe z<1uCPy>b{KTQW`Sz*`P%B3*O6^q_nhIX+;QAdk*9hgwz5PMJ>YN>I&J{v2wqw`%L{ zvn!CtTf40n&1;>n1ZBT8XBRcstJ`{Ab{=`W;Tpu7*45H2zf{nEX?9U2n(IBY^=9lz z$eU}#>)|@An>xcoQ$za7_}FB4HPWG5N5A57$*G9%h-Qkhw9;r3$OLEif$>`#BE+|#Q{&1Cx(w(_5AUWD`3GCzJ`t9J- zi@`IGo-94?>K(mzPL4Se;b)>srx|0fs_k1#uXN`7A(tk5ub~d^^LWGOUX;KbzJp}) zZqsxOS&)4lgzeP=Epypa2CZKmiI+fC3bt z00k&O0SZun0u-PC1t>rP3Q&Lo6rcbFC_n)UP=Epypa2CZKmiI+fC3bt00k&O0SZun z0u-PC1t>rP3jFT^)0wr;nfjO1JNE~+`qtgfCO6#e`et^`-AZm$w;xPzWgZUTG2G{B z6Hghx#G}2L`kU0v*T1?~WgHYeXWk0}uk7@6=X;!Dw&LWA*|L*gEO<_L;ef0$&j6pm zKw7-Vv%PrsH;rB*dOjWFc*FC<9a1G_Dw&z^5#w-CE$sN00u-PC1t>rP3Q&Lo6rcbF zC_n)UP=Epypa2CZKmiI+fC3bt00k&O0SZun0u-PC1t>rP3Q&Lo6rcbFC_n)UP=Epy zpa2CZKmiI+fC3bt00k&Ofqy07$Oq%M&u1j?Qh@l20a_)G8eU=$H8W1juehh9BlcXp zkN{0DmHkpM8z%BMq19m|m~T<9?#M_l6t%bP<*R|Gi(a + +int main() +{ + char a = 'a'; + int char_code = a - 'a'; + printf("the letter '%c' has char code %d\n", a, char_code); + return 1; +} diff --git a/2023/get_largest_number_for_x_bits.c b/2023/get_largest_number_for_x_bits.c new file mode 100644 index 0000000..32b2ffa --- /dev/null +++ b/2023/get_largest_number_for_x_bits.c @@ -0,0 +1,10 @@ +#include +#include + +int main() +{ + uint64_t zero = 0; + uint64_t minus_one = zero - 1; + printf("%llu", minus_one); + return 0; +} diff --git a/2023/get_num_digits_of_integer b/2023/get_num_digits_of_integer new file mode 100755 index 0000000000000000000000000000000000000000..46865131a4bc78766e8a4b59613aa5ae32ff6e8e GIT binary patch literal 33112 zcmeI5&ubGw6vro4ZLL2NJop1W?4|xFr9ad|5!6(>7*SNTg(Aq%Hfgfhq@~$ld#FT; zU&ypt&(Up|7uB`$LOf_h&wjY+Qp1d zxB8T|cfc)*l#FQY=jMHDpoqu&d>)H|f5mnYI~;3V+f~&znL_KbUzsRxy5jMQ7lL?u zY!9)c(qZH6v`(!j%l@2K%5U1O>s_$%j@x#zqayzxDq2@c_q|e9d!?xvG{p0JW#f(6 zL1M>aMB~YScy8hh4~-1zOE<3H4*o_uSZByRUdsLu6}GZRw9d~&ego{k$CJKB(!qFZ zDd82(b8DYnyK?=iJA5VFBh^@5utmnqrU}|?)%edSRW)-~l`6n<+S&!!vX5oXL(sRd zWnUe7sLWD#qka<_m2l0`JfrPW@4)z${bf7sL+LytSBv3(@-4nn0Z z$bS&aO>E_Yd~Pq4{ams4VlVRYCeRUb3@Y(gt5_S#lhzhq6cQi-5+DH*AOR8}0TLhq z5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*AOR8}0TTFE0;8GbFPWvUspZ9u@5yd=V{IN0zbAX@%Wk7q*{F>+ zGArk1;agQ}lPGF#W2v4%*ZNfQ?C$!mpzC_gUC%7NPu+bN#{1xINNOM4M(Q<2EjHGk zHJi<4cfD3=)JE44v@ZxMM|cP7!Qf_>97}I@J;AqgdL}>6@61d&U0KH~Cj`}Vn=|4JRq*k{RT?U&Uqz&3QnyKN>`bnO?_U(XY~Q9 zx>(S;`P^j1&*?&LJe!+SyYe|-mnuaa+=8I?7RSAk+ApIqP%;g|B+W#@ya(V*B5}DB uLGqV(%*B>>(Z%k8qLiu;ybIrA%RLFQ*06N&qWzzl=~!mkWENklOz;~e>frPM literal 0 HcmV?d00001 diff --git a/2023/get_num_digits_of_integer.c b/2023/get_num_digits_of_integer.c new file mode 100644 index 0000000..2b83949 --- /dev/null +++ b/2023/get_num_digits_of_integer.c @@ -0,0 +1,18 @@ +#include +#include + +int get_num_digits(int num) +{ + int the_log_10 = log10(num); + printf("\nlog10 of %d is %d\n", num, the_log_10); + int num_digits = floor(the_log_10 + 1); + return num_digits; +} + +int main() +{ + int the_num = 42000; + int num_digits = get_num_digits(the_num); + printf("%d has %d digits\n\n", the_num, num_digits); + return 0; +} diff --git a/2023/leetcode/maximum_subarray_53 b/2023/leetcode/maximum_subarray_53 new file mode 100755 index 0000000000000000000000000000000000000000..bef777cae565b084082da9dc319b670e46471ce9 GIT binary patch literal 33264 zcmeI5O=w(I6vywF*0yPFLJ>76dKZl(5);zY8W*V(?cfW>wwMT|SZ*dWZ!(Y0M|dB! zu?>vGE`}lCvVt4Ig-f?~Awq=)EYy{_aN(w978SA)gC8CL=e;+XH)B;0-2D%nckemp z-ZS_7-kasU_s*|BJ=mvIs!u8P2y_s-v`49HN~5RL5Y&Pib9VNP%=ygQXT;krsc!S3 zmgM;!1C3eCoLlT363$O|`&4Z2g}bdX!WiGa>`O0_-h6{6Q4B)g#c{DwPDl2rTE~!l zo*yiGyRP1R_u_m<;t^seWq+LS(b#EB&huTTva)M;*SizvyAsF6PKvyPq#$3l;&lq{ zU0?0hsNSRTB(b~dO(sL%n4ogWspO4QDO5wddwrkA`Od}j#O_^FGNruN>tUW#vx~Fl z^)sgzqPLrl*42KGy{&w2qVlffn=@vm+No0Phv(B68%jEwA6rWJOTM;rzT5e^({E-@ z%|)wH^7gyFH`pTEp)-ob-Ozs=Gh}@-PwJeC?d!15!WK49LAPP!f23;fE47ODAXMT> zY1jNu?`H|~B~Py$hC@RSK_>#wohUiW6Zy4L9=6ESP+7;q)chwu4^4l(Q9p5GYUqcP z$Kcb@6jUk6ayaaVtoHrV_X0d&eG-O@pajXY)bm}+?@mJ{7JU!X_*|Fg^HNQa92%7M2MUKzJk@?-) z_*!4MS%8cWJof>7O2zroR%7o+*n?^{lvU5{+{$bnK+xJ;|JK@kX$RdKNAYN$kDZNR ztLe5{t$OBmBe;#<*+2k4oDSnNn~^1ZTakA*&f9ov$9iRJ;F@eRo%{-GNng0xl)b70 z2V29BV^C}O+3>0H^qlwXYGK5w_@jD850`>|Rm!?6w&xo!C~L>lo}YDnY~|ndPqXC8(T1kUShf?6%0=-)#x|Y23P=ZhNA6DI6e>!lmD-dFZ={ z8DnsgLhho;6)&1X)+u#+R)VaXSEg*2bE|90thzXnp&%(!Mm;I597xcoKD>&@k5|z( z<)X1K+quBEP0`NgZC4#A$c6Tu;~SQdSpMX9A z9Y9CUQ4o7Fw&nhFv0sLwm2N0=cqVNrF=^*}Y&k~(7p3psPcJ;%Ho8g!$6qTle_xRa JEBdcg)t}8~OPT-x literal 0 HcmV?d00001 diff --git a/2023/leetcode/maximum_subarray_53.c b/2023/leetcode/maximum_subarray_53.c new file mode 100644 index 0000000..e04b5a9 --- /dev/null +++ b/2023/leetcode/maximum_subarray_53.c @@ -0,0 +1,21 @@ +#include + +int find_largest_subarray_sum(int * nums, int size) +{ + // find the subarray with the largest sum and return that sum + int largest_sum = 10 << 4; + printf("sizeof(int) = %lu\n", sizeof(int)); + printf("largest_sum is starting out as %d\n", largest_sum); + +} + +int main() +{ + int result; + int input1_size = 9; + int input1[9] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; + result = find_largest_subarray_sum(input1, input1_size); + printf("result for input1 = %d\n", result); + printf("was hoping for 6 (the sum of {4, -1, 2, 1})\n"); + +} diff --git a/2023/muratori/add/.gitignore b/2023/muratori/add/.gitignore new file mode 100644 index 0000000..92275db --- /dev/null +++ b/2023/muratori/add/.gitignore @@ -0,0 +1,405 @@ +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ \ No newline at end of file diff --git a/2023/muratori/add/a.out b/2023/muratori/add/a.out new file mode 100755 index 0000000000000000000000000000000000000000..f730034905de6d5990687876d597eedb5e644254 GIT binary patch literal 16592 zcmeI3O>0w85QZm4sbXz{3qMfsu0%x=@dLUuiWhIG)SwlLP>*TvEe$lWv=Gs~m#r91q@~+?cq1?#0Ht_4b!r zPrcJPbD+I9X_pb}+c}25CQsnRz7JLfvp2NQBKXEdzgOR{IejxC{2dbgSuoG_mE}rM z%3RD~uHuX>lA|!%e=#Sr6ZUu*D=!KOkN^pg011!)36KB@kN^pg011!)36KB@kN^pg z011!)36KB@kN^pg011!)36KB@kN^pg011!)36KB@kN^pg011!)36KB@kN^pg011!) z36KB@kih>z;D*=y&|K{_tL^)4C+W7QH|JC`sU!C+0L^=ApVF_~j>gT^w%4p~J%588 z^=FxV*pYwI?RLGjm+9$EyDxn>#1^-@2d#VUIat+);m)-BanW7tVRT8`PBs%?G5=ZW zz&jPH9>$CQx;LIMvR{jJn54Ao_}Hq$HKlnI)PmV+B{1_r!4H;XSHbsXPpMEW%idPq q(OdRfD$&~j*YCsAB?Twi-`BlzjddSf^A0xifA4Ixv$35~qw)jKPlPf6 literal 0 HcmV?d00001 diff --git a/2023/muratori/add/add.cpp b/2023/muratori/add/add.cpp new file mode 100644 index 0000000..7614c1f --- /dev/null +++ b/2023/muratori/add/add.cpp @@ -0,0 +1,10 @@ +int __declspec(noinline) add(int A, int B) +{ + return A + B; +} + +#pragma optimize("", off) +int main(int ArgCount, char **Args) +{ + return add(1234, 5678); +} diff --git a/2023/muratori/add/how_many_instructions.s b/2023/muratori/add/how_many_instructions.s new file mode 100644 index 0000000..9dc9074 --- /dev/null +++ b/2023/muratori/add/how_many_instructions.s @@ -0,0 +1,44 @@ + .section __TEXT,__text,regular,pure_instructions + .build_version macos, 14, 0 sdk_version 14, 0 + .intel_syntax noprefix + .globl __Z3addii ## -- Begin function _Z3addii + .p2align 4, 0x90 +__Z3addii: ## @_Z3addii + .cfi_startproc +## %bb.0: + push rbp + .cfi_def_cfa_offset 16 + .cfi_offset rbp, -16 + mov rbp, rsp + .cfi_def_cfa_register rbp + mov dword ptr [rbp - 4], edi + mov dword ptr [rbp - 8], esi + mov eax, dword ptr [rbp - 4] + add eax, dword ptr [rbp - 8] + pop rbp + ret + .cfi_endproc + ## -- End function + .globl _main ## -- Begin function main + .p2align 4, 0x90 +_main: ## @main + .cfi_startproc +## %bb.0: + push rbp + .cfi_def_cfa_offset 16 + .cfi_offset rbp, -16 + mov rbp, rsp + .cfi_def_cfa_register rbp + sub rsp, 16 + mov dword ptr [rbp - 4], 0 + mov dword ptr [rbp - 8], edi + mov qword ptr [rbp - 16], rsi + mov edi, 1234 + mov esi, 5678 + call __Z3addii + add rsp, 16 + pop rbp + ret + .cfi_endproc + ## -- End function +.subsections_via_symbols diff --git a/2023/pointers b/2023/pointers new file mode 100755 index 0000000000000000000000000000000000000000..4ed737771d011baec80803359bcc493f889b3b26 GIT binary patch literal 33024 zcmeI5Pe>F|9LL|LrIndom6c<_p0u9 zz*Mk~TBGR~&P$od1zFFZHbu4l9+-ZY%slI;@g77?%Dn&3^OMr^r)FuQw%>~BH*PLs zUE3qtkN06o_37^$>yy`qheq{o#PxlK+-oC0AMDJQpAjjiXCvD{dhdRXRI4nmuQy6C zFSYL`QGL#C4i4S0`v=2kq)@Xi(#p)m9HM9c@ud@K9}v7@g_Tx^D{}(Y^TV19qaD~-u9%s zU%z?(e0B57>dJY_S>*aRxl6Me^IAp2kGeJr>i$@h(b@~@e&{)m8vpKPa-QRRAVvR1 zvFciAJ)`rvz)iPbYo~R2ld4FZp}_r@3PSMSoimzw!9V~6KmY_l00ck)1V8`;KmY_l z00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck) z1V8`;KmY_l00ck)1VG@nQ~K%@zgZjhFNU#USNH_1vuIbGlD+Me$xHTDIbU%WdtcGa zs@QtQ1?BuUWnSpaBc0iz%!1C`R~dV|+)25j&fVeMV($~J913N-Qe2Cb?27K+qi5Q= zPWSK9nbRs`x?R*cK0MpC<`h3#_dfh-eMxH0>a|*vheSpVGCYup*~~nrXDHvMCurpP ziL9G;{lHbnh50Nn{{&k!^0{6<-yWyP9-d9-an$(ia1)*VZ84{cN$TUp)uKXmh!F^Y z00@8p2!H?xfB*=900@8p2!H?xfB*=900@8p2!H?xfB*=900@8p2!H?xfB*=900@8p z2!H?xfB*=900@8p2!H?xfB*=900@8p2!H?xfB*=900@8p2!O!k literal 0 HcmV?d00001 diff --git a/2023/pointers.c b/2023/pointers.c new file mode 100644 index 0000000..dbd3b77 --- /dev/null +++ b/2023/pointers.c @@ -0,0 +1,23 @@ +#include +#include + +int main() +{ + int x; + int *px; + x = 42; + px = &x; + long px_copy; + px_copy = px; + printf("x = %d\n", x); + printf("px = %p\n", px); + printf("px_copy = %ld\n", px_copy); + printf("*px = %d\n", *px); + printf("incremented"); + x++; + printf("x = %d\n", x); + printf("px = %p\n", px); + printf("*px = %d\n", *px); + return 0; +} + diff --git a/2023/shifting b/2023/shifting new file mode 100755 index 0000000000000000000000000000000000000000..4da2b498a4f8d34d4af81c3d007866ca22404d65 GIT binary patch literal 33024 zcmeI*J!lj`6u|L06HRg^CRm7H#UjM7BJr!xLcC}WHz-Lo5uzYNlDp&%E*H5EOd2@| ziXjLV7Qsrev9z^|C|GJGDFqQyD5Nu3sPFC0n!Pi<7XN{{nKwK8Ztk~xcATAPizZ#?N4vrnoo)!N~{%jsL`>q9zPUlR42+8R^7QwO_Ss8AgCikTDs+q78(Y0Uw5qM%Bi2sO#x8@zTjy()xj{*V z=ex+LS8(0kz~+5+bztyHx_=}0i)wYltgCR;LXGvoQ9=`g{BXnJQNeB-&UYmd}U>F6^LG}dP! WNUPnTXnL)lypjIz^fpKzU)pay52P9Z literal 0 HcmV?d00001 diff --git a/2023/shifting.c b/2023/shifting.c new file mode 100644 index 0000000..4d4d713 --- /dev/null +++ b/2023/shifting.c @@ -0,0 +1,11 @@ +#include +#include + +int main() +{ + uint64_t one = 1; + uint64_t sixteen_times = one << 16; // this is hopefully 65_536 or so + uint64_t thirty_two_times = one << 32; // this is hopefully 65_536 or so + printf("\n%llu", sixteen_times); + printf("\n%llu\n", thirty_two_times); +} diff --git a/2023/tiny_c_projects/greetings b/2023/tiny_c_projects/greetings new file mode 100755 index 0000000000000000000000000000000000000000..9a689760ada8b6d537917fd452d6033a9b303c68 GIT binary patch literal 33144 zcmeI5&ubKC7{}i+wx%{UYlWh14|7R>A^D*dyjVAF21hI!YNSDs*Cad9h3u^C&bE;f zSqTM~?Sjxtk0REKm(Wv>g|-yD^y(jAA%T{9tCUjv{k}81ncWIK_2l;<&%E#VdEa^V zc|S8bzVH0}=Wl=SGR7M+#_WI>=sS)1-1z7ja{wN~t(~1d5u6V`I>TtcdHwRKig|8e zqP26u{9OMOcYm=zW-|R8!gQ46%-SScO)`OIFyGDhuo%SeXH%%V_qxzd)69lkV{JWY zEY^FW!F=bBr}_40GpO@6oaK8y3tGEWPip1(Qcw4Ya8*W<+x0qeHz7don7vOE#D71+43E+_EI(X z7@(h?&&OP6#q|D68S=_Mm+klL!;_~^1v4kR??`i?E@;}Sduh_Ktn1?M8q;Kbe9HJU zhcn%Tn1m)9KFe>RS;Num#x3#? zAM@}%&l`XDD4b(l0~G#^yzN?0&)KW>B&v+RKaRS}i*Ay60}bv5WbQG&I8y9LXN<813vu4}%xcY;x=N)+-FOgGXWe$}k3IA@S?XG4)-z z{=?*j@1Km_f_gG`+gs?$=RI4T5RI0U@yTliklc*L~tFehbiCpBMUuu-1@!jU2 zKUUw38{Gci*V%#PTkh-SzTYBRdUtO+`+4Ku;SpphxJ+&&-Co?ARA{DwLX$bM1Gwq- z8>d^Sd$ZIL0TB=Z5fA|p5CIVo0TB=Z5fA|p5CIVo0TB=Z5fA|p5CIVo0TB=Z5fA|p z5CIVo0TB=Z5fA|p5CIVo0TB=Z5fA|p5CIVo0TB=Z5fA|p5CIVo0TB=Z5fA|p`2Ps_ zNX-KbF2eb(^Zele)LrAZ#JlGDMSMZM*o~z3c{o78!=Q%`lY96WyRj?PrG=HGT#1am zQY**FGTKH`H`c|BXU)q@!Gi~e%*{3QY~`|zR->gx64}eqLMf`57b*+o*t~@E4lsen z>7!o3^;s{^V;Dt;=OIw@oCLJ&I(rk>yyhVed3|I+r{$zP@Zt2Vylw5{i}(L7)4DSM JYspAw{sEHy>3;wK literal 0 HcmV?d00001 diff --git a/2023/tiny_c_projects/greetings.c b/2023/tiny_c_projects/greetings.c new file mode 100644 index 0000000..2609584 --- /dev/null +++ b/2023/tiny_c_projects/greetings.c @@ -0,0 +1,30 @@ +#include +#include + +int main(int num_args, char *args[]) +{ + time_t now; + int hour; + + struct tm * clock = localtime(&now); + time(&now); + hour = clock->tm_hour; + + printf("Good "); + if (hour < 12) + { + printf("morning"); + } else if (hour < 17) + { + printf("afternoon"); + } else { + printf("evening"); + } + if (num_args < 2) + { + puts(", dude.\n"); + } else { + printf(", %s\n", args[1]); + } + return 0; +} diff --git a/2023/tiny_c_projects/pithy b/2023/tiny_c_projects/pithy new file mode 100755 index 0000000000000000000000000000000000000000..0316927f14406618b0deb8be0e31adbcc8b186fd GIT binary patch literal 33592 zcmeI5PiS049LHyyq)pS9U3!p+JuE^*MUz%rh?U}|cJ-CkG};78MaKQ}Hd(vb4ZAPe zh{cUzrD2WWA*DhO;;Ba!>Y*ClU?IhWe-OljhhEeT*c@vp+V%UJ_cnR^jNna>?+d34lzG=6HdwTl@aYmF(-??6}-OERLhW1n?A2PT;Br9=1_#6R{HC4O_P8P%@J z#(IU;C1*NW+6V>n?S9hDci8J8zF#`MeA~UC)~S+H%;v{8{MPRs_3}OKwTtf;*#p05 zJ)M6wn@{U(K2tzLaDA7&e5brY;s4jSG!>||MRO)?LS<492FVa`^E`KmkAH;ac7nB|Z zqR?*Wz;vlNkjo|q(lfa&hlgCv`uJ zG4E(Vj|Cj+V-UrSq6CVv>UeYLtw zUQaB|cxd?xquibY>C5Cg+lwXyJ;i<*JOZR+bx(`pampUinPM zRTte(hrUDXLsfr6nkv#XR}0OX%4#>pSXK9%pD>v_dd+IF;AhvZ$FGMga_1MoX0OTa zy!d4ur3&wA^wnV-eYZ{dhbixvQZC{)`nH*}-IUu+*=foyQ+A`A$~qG>{SL0F7u8rk zk<8gqrw}bn+4*QD3#YHt<%=hBxk4)8*wIOQvQV7qQhn)z=zl_$bX-ohsuAQ`yUB#@BbqRk^d5JimAu&8bGWWamR)5fItXOQ-qE~ zlh88sZRjQFW#}i+YtSE{zd&z!=e+SHBLP}7qWNN=a69a~S>3Cyo{PA0N>fh8S8q)B|--;Rl5ga3{lTL +#include +#include + +#define BSIZE 512 + +int main() +{ + const char filename[] = "pithy.txt"; + FILE *fp; + char buffer[BSIZE]; + char *line_of_text, *entry; + int items; + + fp = fopen(filename, "r"); + if (fp == NULL) + { + fprintf(stderr, "Unable to open file %s\n", filename); + exit(1); + } + + while (!feof(fp)) + { + entry = (char *)malloc(sizeof(char) * strlen(buffer) + 1); + if (entry == NULL) + { + fprintf(stderr, "Unable to allocate memory\n"); + exit(1); + } + line_of_text = fgets(buffer, BSIZE, fp); + if (line_of_text == NULL) + break; + strlcpy(entry, buffer, BSIZE); + printf("%d: %s", items, entry); + items++; + } + + fclose(fp); + + return 0; +} diff --git a/2023/tiny_c_projects/pithy.txt b/2023/tiny_c_projects/pithy.txt new file mode 100644 index 0000000..09df6e8 --- /dev/null +++ b/2023/tiny_c_projects/pithy.txt @@ -0,0 +1,5 @@ +Once upon a time, there was a text file. + +It was not a long text file, and it contained very little of any value. + +One day there was a very long line and it exceeded 256 characters. That is a super long line! Oh well, maybe there will be something of value in this text file after all: A segfault-causing line of text which is greater than 256 chars long. Amazing! Oh well, I guess this wasn't quite long enough, so here's a longer line. Are we at 256 chars? My vim is configured to be so minimal that I can't even find this info out at a glance. 😝 diff --git a/2023/tsoding/music_visualizer/music_visualizer b/2023/tsoding/music_visualizer/music_visualizer new file mode 160000 index 0000000..80c8257 --- /dev/null +++ b/2023/tsoding/music_visualizer/music_visualizer @@ -0,0 +1 @@ +Subproject commit 80c8257e7c20ae6eb0b1968bb40360704dde37bb diff --git a/2025/a.out b/2025/a.out new file mode 100755 index 0000000000000000000000000000000000000000..2997bcfb58df8d1a2fffa1cc1e3c635c47da9445 GIT binary patch literal 8440 zcmeHMzfTlF6rPhqK+#()4F28*gT#QMF+^hm!i6I$AYg)*nBm7Qa0z#3xIKmL5<+33 z(UL#I(n`;0qMeqOHYO%&0}V0wl9-A1r`_O0FbZfmZpkZOHsbVIGlXDb%u}q4fUGOk4rgS z$QOeGqkOjJtJeg?)c#KfzM{L3&ClwDC||4Q+o$8iVdI^KP0CXKUN)bR+5Aiafhb>> z<{Q_Wh@%`~KHlr9;_2%d>yf=fgQMQ<;{K}c)xTdo@;B3&!zC$a3xR6H+w=LlaZ^|~ z>aWhjNDJQEYXgH7C`5SwCz4s%AkZ6Q=l!^--^VzA6%+h=Yn05rtI%=99SWV?i zgU@@P)t&x+VZ9S;HrzWifsY}f=1Tkcqj(!)>i1!!q4qL)DpenicWxKf8S~zUe)Xno z)q;9Pmx`{FYroQty0TM9gq?(w|FJCu@9vbw)azf^&D8ymspT)$>*VGh@U11{MqsWj3-t;-*|ew zq5E!OzLaw=+AZy!Ge}Yz9_0iU9;pqr>&8gu{|j2TdIk^b-v|~<-wfd2UW;I(nHdF) z0!9I&fKk9GU=%P47zK8&KzZjBhQTOc6fg=H1&jhl0i%FXz$jo8FbWt2i~>f1|5*VW z#j#h(7Ac!h;YL)r4anY~Z)$F#>l$hvs=aK0$vfy+yQ)tnVp1I7Ca%|rN&{vxHz%D% zXS(D%a?Y8|I18d7H<`@~G_jdLV|pUyk(2kJ!d`EUm_0X)oGN2J-A3p5cY3Ml!)5&f DH_E6e literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_4/division_and_signs b/2025/c_programming_a_modern_approach/ch_4/division_and_signs new file mode 100755 index 0000000000000000000000000000000000000000..aba21bb7b6770d8ccc54c936009ae413e93f086e GIT binary patch literal 8440 zcmeHM-%C_M6rR_3{RI!3+b7{csjo{lLw|9fO8*{hd+*#*Uq#*?|YZK@gd&U zj+h76-%Q}~Ce&PMAAb-(hMYPdMjG~BArIfVZTxNroH_j-delqV*bDYEv{VZG*|x4W z?5i-YSGQmw|B5FB@9wC<)a!rPaQ4pU?DE&-o6IlLW$&GRg5d>^`%HYfuS1U*T8ANY zO@|&@p=ljjVaO~T)}gx$Q7SC#(xF=n{nlJ*9a?6{R2|fz>sD1G<|kfemtQAGUWxV8 zrzR-rrM=T$>p3r#ZxXE$M9><|RbhvhN<|jhP$jFiphdQd^&8;+X!gka*VaT^cVe!a^TBpe=P4Wh{oBmLQ(7zsvs;+#56g1_repHg#CT? V!vL95MtZu3PV~R@QqkMn`U`g0#^?Y5 literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_4/division_and_signs.c b/2025/c_programming_a_modern_approach/ch_4/division_and_signs.c new file mode 100644 index 0000000..5dc2a75 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_4/division_and_signs.c @@ -0,0 +1,14 @@ +#include + +int main(void) { + printf("8 / 5 -> %d\n", 8 / 5); + printf("-8 / 5 -> %d\n", -8 / 5); + printf("8 / -5 -> %d\n", 8 / -5); + printf("-8 / -5 -> %d\n", -8 / -5); + + printf("8 %% 5 -> %d\n", 8 % 5); + printf("-8 %% 5 -> %d\n", -8 % 5); + printf("8 %% -5 -> %d\n", 8 % -5); + printf("-8 %% -5 -> %d\n", -8 % -5); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_4/increment_a_float b/2025/c_programming_a_modern_approach/ch_4/increment_a_float new file mode 100755 index 0000000000000000000000000000000000000000..19be1ddc0d07ac34e6a3f5626756945ae01c8d74 GIT binary patch literal 8440 zcmeHMF;5gh6rO`aL4iAsF&c^427?xyqKP&JPq}bJ1tO3T5;Aby0-L~{x!sGV5eB?dL%u z60Jgr7I+#ymlWc@a4-|%Bs>q7ay&bno5|f7W3<^4&2Uddo=-@WaxynP*_@*G^I+37 zZbK|`E=w7>Ye6G1&R0k6)bz;-v+)`Fc_qH?{co;qoH;uV;liytD{H?RM-rZQklIVnIs(vN2;uSK*^_3zp?G&7Kv_0J_PQ4g- z|8n`=<>#L>Edz)-@bJtO9z#mamG;R;$#Ym!{|_S#wb#f~Z+bN0xmOV9TKGK-t1o4@ z7SuDb?g#E_|Mh;<GV@C+y-C2#P*T1l-{DUv~jji;X+z--ir_ya%H6F?B z5b%z+Q_}=tgtM7?{^8ZrSb3KC%7=5Ip5J(#o_!^L^==&k7PY}-&y&G<$Mc=uMHQib zJzRpBBekGb*D%t%^!wx7H0s?6<6|f3e1*`&A0jq#jz$#!BunJfOtOEZ_0S5=OSIHJBn^I + +int main(void) { + float a = 1.235230f; + a++; + printf("a is %f\n", a); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_4/reverse_digits b/2025/c_programming_a_modern_approach/ch_4/reverse_digits new file mode 100755 index 0000000000000000000000000000000000000000..c1599c36e2df7249f0c5f7132a6cea868417209a GIT binary patch literal 8568 zcmeHN&1+LZ5TDqpZT)EM!4DLC9wJtuRjVL{LR$@uXjQZ+MU*A&du<*zuO)d;>n$LH zC5nOv6-7Mi&7(JpWbEX0Mrf#l`n#q(^frC6=Hs>X5dV4)C0$&sPjC2~I> ztdr8$!^Vu1Bnja=Grm$F>f=pgA;+6jOIVNgN>d{hl}idC3VtwJST@ziyMn_x-e$ET z(xVMcD&7WV5Mr$0=Us1NS+BisR>d1pe%5v3H_%Clz`Nmkws5`iDLCr$E7!$iy*`E> zkKf_3jB~1Qs85_cf9_KF8o;~e9isP8lIw$+*RV;5i7D+FpsyY;3d5Ute?*Lzp65!P zojrZ-O!Cy}s79>1yr479$YevGno9dIqM|(0BFe(jrSvTH3()z@o!~Nb&f)OBu)a58 z&VHS^ZkY8vbwAH7?60XWwO}jzz+5+-K_TBc>5g{VHz#fAOuNCH$H#|D*S@~&-ur0c z#qizszHLWgv%oPhQ7eMC%DKe8@qXh$SmphspXdPY$8mb=>WI%h0cKyA|A4HNA6xcS4vk(g9`}x5^E4HfH+c4-Fk0b zefL0}ABX#C=pKfE_DYIiF?BDEy@T`E*bh@Fm@lOlN`tWXjD#B!5Hem|i}xY92s3?u zR;^YeXEpI0X;u?2Fgl*^eU`xUM!MN?cVO)UfncmYA7Az#w1{ zFbEg~3<3rLgTR_YAYN2|^cC0K)y#zq0tNwtfI+|@U=T0}7z7Lg1_6VBLBJqj5ReF1 z7`gY!{Vnbr=?x&gx5>T4h~zwu4yx9S%-29e7OXT})|d`D!RjP2y9IhS!g+7cAdS{LYaRho)1&Y8ot z#=<1{{OwXlpG$T-!=6|x9MS&;pkIphL={|&*B=K%@>Eg}MqTIobL%XU7CPPkW+!BJ RBFs)+M$N-3)nkEpe*xbl7P9~V literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_4/reverse_digits.c b/2025/c_programming_a_modern_approach/ch_4/reverse_digits.c new file mode 100644 index 0000000..b2fe2b0 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_4/reverse_digits.c @@ -0,0 +1,31 @@ +/* + write a program which asks the user to enter a two-digit number, + then prints the number with its digits reversed. +*/ + +#include + +int using_math(void) { + printf("enter a three digit number: "); + int entry; + scanf("%d", &entry); + int first_digit = entry / 100; + int last_digits = entry % 100; + int second_digit = last_digits / 10; + int third_digit = last_digits % 10; + printf("reversed: %d%d%d\n", third_digit, second_digit, first_digit); + return 0; +} + +int using_scanf(void) { + printf("enter a nine digit number: "); + int first, second, third; + scanf("%1d%1d%1d", &first, &second, &third); + printf("reversed: %d%d%d\n", third, second, first); + return 0; +} + +int main(void) { + using_scanf(); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_5/calc_num_digits b/2025/c_programming_a_modern_approach/ch_5/calc_num_digits new file mode 100755 index 0000000000000000000000000000000000000000..b45c3cbf7ebcea9b97c86c4b24fd856db1119d71 GIT binary patch literal 8480 zcmeHM-%C_M6rR1KcJx3-h=`l`wya^Ao>9k3$0PvcjnI3*{$9RU*^EMXU;h@-<RO|lHBb@t~D8z_0I#N4DuBVN4 zQT{f>NKuj$Av}A=Qw~f$-ZUl>o~x#iAFP$3Fe|B)G(r@-Vydtas>ge!@LJW3zz-HQ zDZK3}AjEjV%RBDGhF^Q$4+`(N%9F1P-GMGb6x~~nn--3nnL?tzzP%ua&O53WkzbFY zGMoty9M zYTv*WtEVq`BfPFI_#|8w>*v%^#?LZVl6!K-vWT2feir^o__XH%=n{OQeifGDJE22R zU1)BgdR!mB=5_B&C3nLj#pvC{i-mlA(n-bBvy*A~BnP2X$Kr{B>9zjz`=8u?{NU}E zkk>`_n#*X5AghTIK7uxq6lyW}Lz4E1O)Pj13XA7R4 z>$uc`x}221`Uni+FL1`1V2>$(3I4y3>xsE{iSnoDiuIk~R;&u0H1Y7l7|c`lzTfw5 zN>i*NauLJwooe&lwt1`itOefU*R?sTI&W3V%b_`|^6|}lu>yoRQkYLwpIT0oUqr{A zugBVPvFouz_}Z>#=XsL5#aznHcXAd>Gk?sdGCb`}I9{QX$I>m5?{=V&=$3dSp1WX4 z>9>HSOMW*$bXToFiK<(K>tE;Q6f-gm7zPXjh5^HXVZbn87%&WMAp_Bp`h!~9!XnJp zh5^HXVZbn87%&VN1`Gp+0mFb{z%XDKFbw=h1~{;(_c;os9+%#%(%XvEOAJJgqv+yl z%}81o0zhaF24Wgra4*9|VLa((5EP8Ua)U;;iY{G)$Yq5+V~-a-TV(BI+Rn4)T+(sb zF7#!oji;tZLjER%zYiM0An5Z`e-19Cq#VXq_vt*;j|(hEr~3D8h1|BjWCHvJ(=53m literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_5/calc_num_digits.c b/2025/c_programming_a_modern_approach/ch_5/calc_num_digits.c new file mode 100644 index 0000000..3e4182c --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_5/calc_num_digits.c @@ -0,0 +1,17 @@ +#include + +/* + Write a program which calculates how many digits a number contains. +*/ +int main(void) { + int num; + int num_digits = 1; + printf("enter a number: "); + scanf("%d", &num); + while (num > 9) { + num /= 10; + num_digits++; + } + printf("number of digits: %d\n", num_digits); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_5/find_closest_flight b/2025/c_programming_a_modern_approach/ch_5/find_closest_flight new file mode 100755 index 0000000000000000000000000000000000000000..91c63381433e482440434adc99ce0443268ab345 GIT binary patch literal 8560 zcmeHNO>7%Q6n<{f;Ff};9)OBUT@D6HN@6<>O|~E3bP>@3XfJCw(YvIfM|2EoLWQ_6Hfg+NTs+UwRtdDa`+A(` z7A6WYWz0;qPLX(C*e{-c5HVJiBt;0@x@LP0Onbf+Oyqnwcry_8>sT4+rd4UZMu@!a z%;g)Q_I%YIH{TI&M%}Lif?hsMlOn`?-p-}73k|<*zf3m9zVpTjzF)ku8zCW_?A3HO zDbm^0GNSGEZF%`-JOumgIU32(;dA&9D#XOt)R-7Qf9{fd8sNL-9O~!cS-u~vJcmO< zEG+wup<}*!8U``GF3gv|&gMOP{p`8djES@LJEFDK1)pU_7VD0AT^WxPt;#huqD4do zJ%17Y1^B$?Q_xNLTtnm;qEXmKp*-#vzBjD?I{o9^*MM)iU*3erc?`<;X28kk2A0xu z1Ig=4N%$;JL%EJyAB_KLhND+Lx%~D|ftxqKj3K5$15lz6l80npa^L-a_X~*1{mF5n z=dnM|Q)w%L*ByrPm|OoOevAUmT2RlW>v`MC^uOAVy3%u)2zeID`EP4PLD-X?zX|`p zkZH5{OR=z5baqR|-Wg-J5QHrn)!%wb#x88hsLtL=rTRlBZph_$zPYxeH86d0+FGGX zg#y7g z)t^)OR~UF*Aj@2LcFpzaDxyCkijpqBvHNo1d!)Z>?C@G8r}E7AJ`ZMQ3VI|k0nfBd~a z2VXZaUwIg_3cZP&k({v@?DN@;wD69@6^IQhlpz7AX+19TRAPE#o{k6E;~8R zPG>A#qi`&#Lupu#L=qW_M)c@Qup@eWklm<06mxBKgsnapU4e<|@zbz_0Kvpu6W1fd zG8i9%8PbQ?oOZ`X^cc@XS`Knro;tp?oVW6}mb6w9IoruuvMMc|*TVTzS|XQAU%>}P zOW5!aV_zVbjzd}e7MDPN|3+b1`P;=ZD%)nmzgvzVORvl%~PSgx?bDBKy5~Q1CrQGA@7wfurb%6}*DmDH8elL^g$>pywFO3v|)OAv6`);AQ4O zBAun9&?l)&WEO>W&6;;?ODtN6q?MyX3zjXC=~Rjy%hVq@D!~YsEYpPoZV|lx6VMPk z8z`K8&GY$vlzo2R + +/* + find the closest flight + + Departure Time Arrival Time + 8:00am 10:16am + 9:43am 11:52am + 11:19am 1:31pm + 12:47pm 3:00pm + 2:00pm 4:08pm + 3:45pm 5:55pm + 7:00pm 9:20pm + 9:45pm 11:58pm +*/ + +int min(int num1, int num2) { + return num1 < num2 ? num1 : num2; +} + +int get_diff(int flight_time, int current_time) { + int diff = flight_time - current_time; + if (diff < 0) { + diff = -diff; + } + int flight_time_to_midnight = (24 * 60) - flight_time; + int mid_diff = flight_time_to_midnight + current_time; + return min(mid_diff, diff); +} + +int main(void) { + printf("Enter a 24-hour time: "); + int hours, minutes; + scanf("%2d:%2d", &hours, &minutes); + + int minutes_since_midnight = minutes + (hours * 60); + int smallest_diff = 60 * 24; + int diff; + + char *departure_time; + char *arrival_time; + + diff = get_diff(8 * 60, minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "8:00am"; + arrival_time = "10:16am"; + } + + diff = get_diff((9 * 60) + 43, minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "9:43am"; + arrival_time = "11:52am"; + } + + diff = get_diff((11 * 60) + 19, minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "11:19am"; + arrival_time = "1:31pm"; + } + + diff = get_diff((12 * 60) + 47, minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "12:47pm"; + arrival_time = "3:00pm"; + } + + diff = get_diff((14 * 60), minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "2:00pm"; + arrival_time = "4:08pm"; + } + + diff = get_diff((15 * 60) + 45, minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "3:45pm"; + arrival_time = "5:55pm"; + } + + diff = get_diff((19 * 60), minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "7:00pm"; + arrival_time = "9:20pm"; + } + + diff = get_diff((21 * 60) + 45, minutes_since_midnight); + if (diff < smallest_diff) { + smallest_diff = diff; + departure_time = "9:45pm"; + arrival_time = "11:58pm"; + } + + printf("Closest departure time is %s, arriving at %s\n", departure_time, arrival_time); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_5/find_closest_flight.md b/2025/c_programming_a_modern_approach/ch_5/find_closest_flight.md new file mode 100644 index 0000000..3daf631 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_5/find_closest_flight.md @@ -0,0 +1,15 @@ +If it's 2am then the closest flight is 9:45pm, not 8:00am because +it's 4.25 hours between 9:45pm and 2am, versus six hours for the latter. + +With the following implementation, + +```c +int get_diff(int flight_time, int current_time) { + int diff = flight_time - current_time; + return diff < 0 ? -diff : diff; +} +``` + +When `current_time` is 120 and `flight_time` is 8:00am -> 480, the difference is 360. + +When `current_time` is 120 and `flight_time` is 9:45pm -> 1_305, the difference is 1_185, but what we'd like it to show is 255. diff --git a/2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest b/2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest new file mode 100755 index 0000000000000000000000000000000000000000..01437ba2e2a64026e8587d020d0d21516f680394 GIT binary patch literal 8480 zcmeHMPe_zO6n~pmT9!+P=-*)u8r4Cie?%5!HMEjxq=E!7tnR0~u)E5B-;y4x6$@Gx z=~f;+cd4LD=Yo#iqJswsgFP5{@E{@8@69)#&Tbt$#XNfZ-tWDcH}Ct+Ai?|enk9^*y*kuLC6m%dDL*KV`dgv;dTltI>$R8_5so%A znR>fSK&df5C}i>zTe$XnyG*@7lV@xT-+?Vk74!Eq`IO4!$0w1f@9(jxH)1w1uCHO& z<9k@pb*}de^{8tD{e$6cfbW`ni0;E!ejlv7hC@nCOj_3f{P%iM8Q#SEBVoSmKDXal zUvK|SdA&F6ib&M;1(1pXH?oG5=w%&m5Pl$%^GF@O0b%xkK?2+=!jiW`af z;q}D)t+l>+xr*!~ge!8REUOi{UY0BHyXE@B7L5=?2C=oWT&~E~vRs1SEmt`Ygcvf2 zEqBZ6gI%z?&QiBrc?h(`dyCb|@Cv$-VK>eCyv@Yihpv0?f3<(!L-ect3x21vUefo3 zmk+#x7@sT_L-mAm_?$@QQ-Z_UWMN_}ikf`a$Dk~> z<$L3My#mY1e|uQ2=-=%hej6>Q5;v~~Fnm7SOPuEza11yG90QI4$ADwNG2j@m7>Jk5 ze;7+v(7BEQ$ADwNG2j?*3^)cH1C9a5fMdWh;23ZW{6`E3d<)M$N1!}&vWG|ZSdnLm zk!U}TDXuk)oT$YEAW_fEYJcR2Xgto%rxfo-fm%*~Dr47F}=I`P8SY!nz)qftV^s)6V GbHU$)SM5~* literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest.c b/2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest.c new file mode 100644 index 0000000..ea2507b --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_5/find_smallest_and_largest.c @@ -0,0 +1,33 @@ +#include + +int main(void) { + // find the largest and smallest numbers; use no more than four "if"s + + int one, two, three, four; + printf("please enter four integers: "); + scanf("%d %d %d %d", &one, &two, &three, &four); + + int smallest = one; + int largest = two; + if (one > two) { + smallest = two; + largest = one; + } + + int smallest2 = three; + int largest2 = four; + if (three > four) { + smallest2 = four; + largest2 = three; + } + + if (smallest2 < smallest) { + smallest = smallest2; + } + + if (largest2 > largest) { + largest = largest2; + } + + printf("the smallest is %d and the largest is %d\n", smallest, largest); +} diff --git a/2025/c_programming_a_modern_approach/ch_6/all_even_squares b/2025/c_programming_a_modern_approach/ch_6/all_even_squares new file mode 100755 index 0000000000000000000000000000000000000000..0312099f77675a5e6b92d3549725c36087880c8a GIT binary patch literal 8480 zcmeHM%}Z2K6u*;GnOQTmh@yhK$Ua`9-7-lF2^ z;4o=>Cw#;xNs$n)oOZPVQJZf98!6wc-a>W|t6Wi5&`U}Pk$1gRe#=#x@2k$&sCNW* z5Kyo4?a~e*hVyRDc1E}C>icf!eEoWz>@ZOe!X$*}OxaFa*v`l}7HaG312}ZwAss|^ zZH{n0>caw9h|Z3|4sorwXTa|U@UH4Z&<`!?eGt z-r0@rp6ga;cko7dOg?7oY!^+_8Hi8<{|JRY=XW7N&day zK5&?5Z@^)HSpUFl-j`n74~Zp4@21tu=UTINsx>{6O~WQS0;W2iF1>s9X0~l@`NDwx zK00~%9DE!c0W%gu@|e;U_R+`DW_Z>8)H-9Qa6igJ?>tO&?nN-I`8ao}m$Fq0>KT~H zyK=1MY76SJQ-1Ub2+F^}8LNjpt?fnF|AKBN<~}BhU*fCQPfC|~bY&W1iG7m3wXB5q z9iCfISFJK3F>jS)&#m&DRhqX-FX%o`zWpe!Gp~8x!-`i%p8N13>X^U1xDuJO%9Xg! zug|U|im&2#UT!wkquHBH4fsmOl{ubtxD91gscGJQGMnWS_#Jk65}KQlT;7!q&rf>E zoXqo-bf+ZjdonkU*fh7@OPn>O@zbDsbrdBKg(cNYJ!*#}roSUZ?dn_kP@fx-C9Yop zO#dlVwiwMIU=T0}7z7Lg1_6VBLBJreO$6cv{ZD3Ln~6-gLBJqj5HJWB1PlTO0fT@+ zz#w1{FbEg~3~jcAbE@#bDLhuBSz + +int main(void) { + int n; + printf("enter an integer and I'll print all the even squares between zero and it: "); + scanf("%d", &n); + int i, product; + for (i = 1, product = i * i; i <= n / 10, product % 2 == 0; i++, product = i * i) { + printf("%d\n", i * i); + } + return 0; + } diff --git a/2025/c_programming_a_modern_approach/ch_6/approximate_e b/2025/c_programming_a_modern_approach/ch_6/approximate_e new file mode 100755 index 0000000000000000000000000000000000000000..90cf07bc5dfeb37a006f0e9431f62456f1528638 GIT binary patch literal 8480 zcmeHMO=uHA6n<;#e>JHG{||Gp{`Aml^&pC9)zws`7OhYVI;P3iETl=zZfGn11cQhA zgCd9?1rJ_5doiNWi{Md@UW7m^6xxG`sNb8+HrrM4;zgK;FK^z@eDn63p$PBY*U#S< z6Il&Jv;Z21UT7q`NdhaPbJs+xuP%*LsymG5`NaWmE)>Np;c&)l(iMRpY*?c4 z7U_VLseI0>HyFjcqw#{C*k|7VB*63hf}6Xj3!-@UH6Gs)3;SV-VoO=fjkvkAbaR7w z6h`qrXuLiR!hRGZjL-M_Sm7M*?Cq3?PIjNJ-7fBL-n|C*t1Z7bR<7ZYlo#`L*4Y0& zUI4?Jcz+ecV3WRqKjKMEZiyB2=@roL;K*9IDnhu0rPez5>)tg#C@- z5_k8#fO$^Ur#0`rGQ}^5Ji>A&7GL;K*=Boo!k%VYZrjs)?Jrl)aBfWJrVt#rtL3Xz z>)QI6SgT!~iJi5lW@61YQq^+h2eOkNdCL*C{+gu#pZIw8n>q>pQ3_;{S|c9 zjmqa%bt%k_N}~KmoO?|_S}rdm{9eH?sNe7B#YiUUi{XNkay>Vn6J92n&5996q>|ZG zF`M)qvBlZ4UrGIhrr84?n%SMg z4hz4JCZNRi%Yj>aLg6B_HVhaB3GHFkl$?PZ@}p^ndP3|LF&qwhaS@0mFb{ zz%XDKFbo(53GHFkl$?3j+dqo>lfic|H^#^n}NNJo^hot>c)whK7f{&jNt8 z3JW=mskJ{y(n}?Cg9u9EusWcDDj0$-kV6^ij5?{J@5qdkOgjZyI+S#Cv;s3Ko|5VE tm{mK3@mE7*Snv#seV%Q>@>0nr{Dyu09-d_dR$#LH># + +int main(void) { + printf("to what precision shall we calculate 'e'? " ); + int n; + scanf("%d", &n); + float e = 1; + for (int i = 1; i < n; i++) { + float prod = 1; + for (int j = i; j > 1; j--) { + prod *= j; + } + e += 1 / prod; + } + printf("e is approximately %.22f\n", e); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_6/do_while b/2025/c_programming_a_modern_approach/ch_6/do_while new file mode 100755 index 0000000000000000000000000000000000000000..5201ccd28d47e05a74a06d9f49e88151667b4e8a GIT binary patch literal 8440 zcmeHMO-NKx6h5OTHpkx?zQKd41PrcXx-l{Anbu0QkC0UgD8L%N6tEJ!43 zX^XbiCI||mRziX8YF+D~MM1l!i0b?9+?RJ=E?dbxa5(q;-0z+H-TPMO?T;_NHi;x^ zMWhCrrY1#hiiefRE@%d-RBvmif7!p-!)Uc6s@*j`;@m=^QvLowe|3r8&kx&T{0)dv z(UKOW!eBNu4onaXs zRc$@peU;k)-&OYz-G{M!A5_*bq}14ybq(;>#;ZfE7Hs1FNXVD|o(ZhJvkP6_=l%As z=#6->zTi{J6{9h1oD(7?-LfY#h??WZAB2AdKDlXtF2QF%?_dwYZiV(jZDB3d#<%la zzwy@QdpmMSNjk@UeHh9TpB<-}OXzcZZBmzW#??$}D`$%zsX=_&*7^;+G%o7g;QpGq=z1Y)|A-ez67i z-nZ`yetG^{S+0LLx}2GRoxbu)em8aCx|*^c3dqx5D8;rsGdVd6OMPni;o27RS`U=8 zM$(xI^>&e4I|dvBjseGjW56-s7;p?Y2G$n?rS)|jcN)ilW56-s7;p?Y1{?#90mp!2 zz%k$$a11yG;tY5QaQ4ZOD~Dh90L&gMa+Vm0UZ*j|wT6*;F9&aG8WZqvRM#ey?BIg& zTKS5ZVvU*{SHWyBQVat%9t>rJ8QDBJl*`LD(FIX?Dp3I^?oMb5i)g;jSsz@cv=E-^ OVG{i>ywdP?TYmuY?Um~Q literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_6/do_while.c b/2025/c_programming_a_modern_approach/ch_6/do_while.c new file mode 100644 index 0000000..e61e60a --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_6/do_while.c @@ -0,0 +1,10 @@ +#include + +int main(void) { + int i = 9384; + do { + printf("%d ", i); + i /= 10; + } while (i > 0); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_6/largest_num b/2025/c_programming_a_modern_approach/ch_6/largest_num new file mode 100755 index 0000000000000000000000000000000000000000..08ca03d14daeddca204e5e83896097ac57f1ffad GIT binary patch literal 8480 zcmeHMO=wd=5Z<)0)>?h32T>}rh!ORW`gf>O!Dt^%skFsLS_--(%}WzWUQ6DKZEt~6 zP(-};BJ|?jyB;bkq8IO;yod!ADs81mv3|4J*KT5uJ$TrG$?VMRemn1*Jp|_C?;n5m zG8SrNj5RnueSKXDZ$up;zaB%! zqdv?_obGrsE^Z9;4^_JXzH9mr^g~&CAEdO0LPCsBXl;PMGhP5ghv?Iot&{8A{UjPgMSV_-FXDM0H5B)GAzaSLkFR{ z(B44xxW4|(>)w}29)v}T(Yxs=d!>#-KHZU-DP-W2v_q+m_=}%kpWW>`{xtRJMB#1w zhYN^tXb8%f1#nBQCHLVc;j@U!?~{4PPUHIshu*m^bZ;k=a#fyt!{kR58x~6(VYh9?Ptw4#WZo{p%UEgHZh5^HXVZbn87%&VN1`Gp+ zfn8@HQc-_AE4vPD01X3%0mFb{z%XDKFbo(53GHFkl$?FBsscf@Ys1P?}xo zLnwW$NVCL1Wt~QLMb0GkP4q+iCF$MRtOn74{H;bTP6qXk>vSrK`8$>ZDoM~sQ z>^maoq%uy4?JK78E<1p^EDaykb}Ll9gzyhTEi8gLKh53XQcB86{OUe^56#&E%Q30` N_gW#ZtuL7X{{k@?#a#dZ literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_6/largest_num.c b/2025/c_programming_a_modern_approach/ch_6/largest_num.c new file mode 100644 index 0000000..43272a4 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_6/largest_num.c @@ -0,0 +1,16 @@ +#include + +#define PROMPT "Enter a number: " + +int main(void) { + float num = 0; + float largest_num = num; + do { + printf(PROMPT); + scanf("%f", &num); + if (num > largest_num) { + largest_num = num; + } + } while (num > 0); + printf("The largest number entered was %f\n", largest_num); +} diff --git a/2025/c_programming_a_modern_approach/ch_6/reverse_digits b/2025/c_programming_a_modern_approach/ch_6/reverse_digits new file mode 100755 index 0000000000000000000000000000000000000000..53f8a337a4869c9b5b1852aa5474e7849ac4139e GIT binary patch literal 8480 zcmeHM&r4K69G}(I%&c8HL{W!1WMv2K*CB#P&4(+AN*YKJ!|J}>eYE?OeQ!+<#exK> zL>-kb{s(o9Iur%frJxR7y97ZA8whO@+2=cNp3ZI_J#?5aeBbxS%zWlMpBa{g@B6it zKRXzU)HBBFpmEYDV|N&b$JhaA0xHB%SFd%|x;#kPYKc^bTSkO)7ZZgTu|`L#r^xlR zu`ViZK#mk8NfE-gr+pQ`)Z$HIBH_)eDHMlmWh%-_YDgL(Jij>RZKP`P))Zcgni0m~ zf+mI6s1ibqdw#)jGaGUBc^h#Us;^s(Q>+W!fi6N6-J6b^5{{dmz(8$%357SR7ExS_ zq2tj#EJ~c7u8}Trad2QbxDD`Kbr0cvC`<2yl;%)Mh|Gj`4T!hK3t?yxtq+Cx((BxO zXZ?Kxm#m(?@Qv`Ax)76aS!^(-T$w+^SV`{58OtJbTE$t!ClJ$~`=QH->0OjzDZdLk z1l5J+hN|av|1+<7Uutj)+fkP@7OXx3L->oFu_o9y6)z*+4!M$;`;wUb9)E59Cb-1C^Cg(a>}x)V9!XQI zAd`eUd#l23Us$jzb580i8s*#68_jCoC!bevov^@8*Z@w*@G1b@K>=C$Kri zc!mMPfMLKeU>GnA7zPXjhJkHjAYM{`HcQ(CXkZKjh5^HXVZbn87%&VN1`Gp+0mFb{ zz%XDK_|F*NsE2x=qfqKs>5V77tw_DZP^2G67guXW(z*x$B75P8Np!*eG!x!<(oG{N z7^UR~^{k9eT%E{gg*|PL7kyh~?PSU>u$}p& + +int main(void) { + int num; + printf("enter an integer of any length and I'll reverse the digits: "); + scanf("%d", &num); + int next_digit; + while (num > 0) { + next_digit = num - (num / 10 * 10); + printf("%d", next_digit); + num /= 10; + } + printf("\n"); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_6/sum_for b/2025/c_programming_a_modern_approach/ch_6/sum_for new file mode 100755 index 0000000000000000000000000000000000000000..ed8c9de4572f342f5d744043d4945d1525a1861d GIT binary patch literal 8440 zcmeHM%}Z2K6u(cWQnO|#kiHggV^om#1qKmh`EaCANh1m5`Y}%(a7LMVL%I+Kh9Hrs zrEQ~iv~4FC*seDH1r41%`@K&H zr(OtA2Tf38LOc*IJRy!klTa!9JG;Gm-t9h~R*O>|?idm0Aqu4&^o9njOVoXS*c2V_ z!5I}TDUmYp=K>wT)Z)#eka&4r!Z^CCj$)#yQ_4u04+>*Z^>)0E8gH+@6vYungT{;U zQjX_?Tsk|c3u^oOqVb}h7@NWwm?C8%dq15`$#iyN28mj{4UIRXFEOsgF!4BtWrfq# zIoK(0^z{ye(*WO9bBN}lE#C)~H9V4Xa>f_~;+^ptP^$!&cs~;IrM{;Rw%*yTp5B{Y zS5Ndt+?u`+Q!1fR7$(joA&RPHSBMeRT-5Oh;&X_}%@OD_V)pY6_7v=XXg|~x)>2J; zGf)4GyEEShkwZ!}L)#1aT>Er-tUWb9okC1G4rL$DuVjZC-*+5-=J;p57k7VMz?lmT zzZu8ljVoVqAA1}-gPi((uoU!OBo5!XDg5qbD0AU^=*Hj3+3E%T49w>PKht)-4Sl7@ z!rLcdi2uYDg0nlVWAgPs&P;Of2Sn|rR*z(iwKT4Y- zs9QgVlX|bR=v9^qmE@zVt}D@L)pKI#L{>!}EHvft_NV%uGpt&L1xXzMS$$Q+`e~W=7Ll oaX_eos66h3;Kb!#FAj%0I>y}H!O~Sic&dv{^mcfu;LW!F0?|UKz5oCK literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_6/sum_for.c b/2025/c_programming_a_modern_approach/ch_6/sum_for.c new file mode 100644 index 0000000..19e547b --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_6/sum_for.c @@ -0,0 +1,14 @@ +#include + +int main(void) { + int sum = 0; + for (int i = 0; i < 10; i++) { + if (i % 2) { + continue; + } + sum += i; + printf("%d\n", sum); + } + printf("%d\n", sum); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/a.out b/2025/c_programming_a_modern_approach/ch_7/a.out new file mode 100755 index 0000000000000000000000000000000000000000..18dc27d8a7faa9bb42037658a014ef462b711625 GIT binary patch literal 8440 zcmeHML2FY%5Z<&=D^?Rdh*l2|q0)M2we3L!q0xq>Ds8b5iXfY|d1)WEX-QtN?I}=& zdJsJp58_Yo;8_I)y;blx2p$wXib(bQHv7ybMo*r~3`}NcXZG89-)v5^{r$_%KBZ#q zN~tzzk{Va)p7O9#>Nqq5)p{~Dn!cI7KEd5ai8Z<{J>qO4QR{4aI@_3H&huhdtiK0) zRFq^y>(E~ftpn4Hw}M3Cov|tOqqAl!uBtX$!4R3X1bKp&9R~#XD@{^j%>OT+zBxyb~1jIw%&F5opGHZtO?APie5)CQ;o=M_RW&7hO68CZVX6GDQijcM{fAt30wrmX%>KVxTt6r) zVL%uV2801&Ko}4PgaKh-cQ8=h9iJgzAq)ru!hkR!3Sdp{DNX$BkDXue&G-o-4(<_*OhXs=x)9MHp+;3N3G2v^|OAFdx z_2(*~uNVB8ykAoLmu7;ZI;c!Sl%9yyz=?Yln!qBO?{n@4mmwpBr+Sz~{|T=Ryj#{U D=}C<7 literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/abbrev_name b/2025/c_programming_a_modern_approach/ch_7/abbrev_name new file mode 100755 index 0000000000000000000000000000000000000000..22600fdf7c291711e0bc3e494a9a3cb218b2e437 GIT binary patch literal 8488 zcmeHM&r4KM6h2Raxa&o|1Pje_IM~TpeP&9>c`rdw3~v z`g%uu#r>hdk?1zScg;QM`%o*t4_3}$mk`-G;~HS^kEdby5TDn=e3^ajy|ae{gAbB@ z1Nx14b$!8R>5$DvF}0QcTSOIE(<3Uvb5q$x*wA?3f4!?6#xbAHz$0DBl@n_`h^@4sz7RrG?+j*}OedVX3 z)0eR@{%en@3F{SQGhYWGW2tAWsl}h}m*f`1Rk`d^ax=cXvbgSr4Qs#Gs5&Tp8<8)F zd_9J*IcSY9ha0KqclhSsQS}m*wQulk!gn$9fjYXel3M)iKK?}8iB??jcA^cxi8NjF z66wxn$?FBAv6w6K#eCky%DSq!<-I9UFsSOzQumI2Fv zW#G^;;8xT>u9ZXAnr+B3U>UFsSOzQumI2FvWxz6E8L$jk1}p=XfdgT{0}9VR$Dus? zGKW~^SdnLmTIAlv6xSF=@;L_pPAfJd>j&xFWQjx(82BRRPiFiQ9iN@d7w9C(1f3ptqGDX94I0Nr&-;0P2a`im68JUksGKfK + +int main(void) { + char first_char, last_entered_char; + // skip leading spaces + while ((first_char = getchar()) == ' '); + // skip first word after the first letter + while ((last_entered_char = getchar()) != ' '); + + while ((last_entered_char = getchar()) != '\n') { + printf("%c", last_entered_char); + } + printf(", %c.\n", first_char); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/char_nums b/2025/c_programming_a_modern_approach/ch_7/char_nums new file mode 100755 index 0000000000000000000000000000000000000000..20f735d09f07e7af8046515d60b312d01ddd264c GIT binary patch literal 8488 zcmeHM&uddb5T4gYZMB+EL|XLlF0oai73)C|!D>TOl(yIk!Gmj)ye5Gr4ao~O;2|JF zMZ~M1g2#HRH&0$Y`cEhnRPb1=Sijk~+iqfxp2`f&n;$znGyClZ0`vLTk3UCfobQ4OVcYK2t{An6g&i+1_d>ONe?amx-KE8*SI?vSP z;-tDcH@gts1{!L+2iu^L{63gDhFwY(R-J2r-94X8!<%@&CC1C`bN`*)o|(Osnwqh1 z#P8?}Hq)qXHd-^P_FpDy>6$)K6`l!WS7Bd-%{7O>8?YJrZAkVX1J8q9;#kYB-_^JL zj`w92Pe5Xc_}z>*>b3EeQg%GwT*<>`It}JNhO?RN&C1n@H_wLNzZ-m1yab;Qj?NTE z^0?MD_OTbS^H@uOEsf%QoQL1JOI-H~nAaklr|~oL_Ip7;3(a~Ml*ew2p|4Ul+I<#+ z^S||p`XJ94oALS|bT|FZ*#Tbhs|n`_eYZXk)s-uGYDgHC7+eCxDLtu%HYDfON{3LvLtjSy%It028k{R z2m*qDARq_`0)l`bAP5{t1QIRt=eKnrk0*x`1Ox#=KoAfF1OY)n5D)|e0YN|z5CjAP z@__-*J~LpRpSgoDcdW>>I!jtlV2bMuBYB^P1l~y~st|;^;!=%N731)5*>3a{4YJOK zdg&)-j6JGcRKZ%1YlML+221&%M*ZcbQiYC_4zS^IFDk}$cv2XLV(0xl1BA&-nv(ds Uc2v%mCGs(`{ +#include + +#define ENTER_KEY_NUM 10; + +int main(void) { + while (true) { + char c = getchar(); + printf("you typed '%d'\n", c); + } + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/enter_to_continue b/2025/c_programming_a_modern_approach/ch_7/enter_to_continue new file mode 100755 index 0000000000000000000000000000000000000000..28eafc1db4a8e0590e0ef2228508e4a904a12c26 GIT binary patch literal 8488 zcmeHMO=uHQ5S~q=)>=&z@rM@NOGJyLq8=0}w6#l9l(w`Hil~n^*|vc;3CS+Dw@45y zRlMrWn;r!(dh=HBpl7{T3RUnR7)!B!^Y(4?5_{~Shc__$-psuDX7+m<2+Ze=AAb)J zSuI2qfyP;*M32bEj_5Qr1r=hnZ_v5pjE%6jQLILDM~iVD!cS{2@Y zH21fu@Fr9e+szm{9^b=r zi8Iib?h`jghR1{30N)Mw5Z;Gc`TJnyGwc##rl4H|?49vK7*68+P?#^h&-!BX(0?uvyN@W`mg8O8*t2imYi972&z8>>})ou({?b=xf+aeGM!74?#zvy71Xh zb-%t}vzxvz6+D6!A>!|*+b@^8=Wt2O&EZ}(%KO?K&3;G#fEPL*JvOkHw za#KO}SuBkI(k5!fdS2Pg*KWw|)Y59I`aQnlY%<)6Q{y*Xc53f?`(yOt_n+zp`JJh! zJw0&0p*6mnwFDm3TALSH|FurbehuMkLHH;ZBv(IJOHOV5-B#iPE^;f8#Ism(%Vpac zPCH|^SFkgMyqC-SZZesqM79lilGje&K=hUQVm^DZvhr`9rB^=d55ALjpu{DT^as~g zKVo)<0mFb{z%XDKFbo(53GH zFkl!k3>XG>fdLyBJo}u0^6X0=V(DW=o+XAN_i;>dwP7UZSpcw(V +#include + +#define ENTER_KEY_NUM 10; + +int main(void) { + int i = 1; + while (true) { + if (i % 1000000 == 0) { + printf("press ENTER to continue..."); + while (getchar() != 10) { + continue; + } + } + printf("%d\n", i); + i++; + } + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/enter_to_continue.py b/2025/c_programming_a_modern_approach/ch_7/enter_to_continue.py new file mode 100644 index 0000000..b5c552a --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/enter_to_continue.py @@ -0,0 +1,8 @@ +i = 1 +while True: + if i % 1_000_000 == 0: + print("press ENTER to continue...") + while input() != "": + continue + print(i) + i += 1 diff --git a/2025/c_programming_a_modern_approach/ch_7/eval_math b/2025/c_programming_a_modern_approach/ch_7/eval_math new file mode 100755 index 0000000000000000000000000000000000000000..d70ed49ff081e402e0acdedcaa4701be5e0819fd GIT binary patch literal 8480 zcmeHM%}Z2K6hGrvX4Z^FQWWtPO_Y(A1x65P`EaCCNfVXwHtHLl(5KAIBW)1_QWQ}w z+PH{t(KZlT)gp|FmeC&&v`A325XOZ>H2uzf_l<9+mhHR;&O7&f-g|%dj0oq`&+ost z5Cw{fs0bWlE+M)@23ACS!4a?!9ZfA}pLw~Rt@#wlSO2MToqGrrqRZ^*$`6tGys@8@ zUJ4sCjwDHllr@=B3PfSO2?TPzA1Z`(FISpMD6L#l2$4)V1Ie7JuwFkl=X#YY!qdHk za#gQP8H5;2rV?>`IH%{|_lBz1p!}@+iQm9aLOAx#xIHA|c5EDu!t*^>^?Fni>xDJ^ z_4plTWS!=wt|oE5y{*%I4d9*k4&Hkx$^F61F>DfIc-;34(EnY}E5k{g?-AqWKj%iD zU2JW;U^cgUJz^A`3p$e_59|8WQrb@urRAOm(FiOjl|BOfICS1~4|od+SyA zqxtH3*bHz0OcX@%pj=DrOCFchz$))2{X~axKd!@l?kDco(_r?wIJd}7$(;q~>6}WY ztg+g2wK!LNz|B4c!S!bhqH@S1N}q-PKj=#2{z7DWCG^q!#pNOo&whiLqs7mzwjzbI z4$BAE^2N1eV0npU@Qs<7UaxRUm(s5Pwd-Gl|4Gh&)Tzn@L*`m0*ki6|f>q}Fa=V*( z%-IdkC3r&Yx%$@i*16uATwbr2x62>iufl1Wmab>BSyiQP9v9)C^ju((Q9CgonSLL- z_AVQKT0xWzKf~9wQ&z%=+J-ebk+71<__%$>pm2=BgVY@zbu42%mgfuC*2j?N_QHN% zfc%o*mEXM)l9~TDF*VBX#)o^S5?vHhT?N%`uZL0@NrnEaX|5%Y{ zi5|&y2vc0&Fp~2EC=l3*g_yt;JQX969E{pA7zMFeZct3Cn35HVu@PZST7yo?5+hc0 z$V$-Gv1r_;?U>8*7)^x-1MVhVzZx9G!khE+JPazkBpt-puk(F){^dynlj^^>6>{6c HBXhw&w}bZb literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/eval_math.c b/2025/c_programming_a_modern_approach/ch_7/eval_math.c new file mode 100644 index 0000000..ae6e3ff --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/eval_math.c @@ -0,0 +1,36 @@ +#include +#include + +/* + Write a program that evaluates an expression e.g. + + Enter an expression: 1+2.5*3 + Value of expression: 10.5 + +*/ + +int main(void) { + float total, rhs; + char operator; + printf("Enter an expression: "); + scanf("%f", &total); + while (true) { + scanf("%c", &operator); + if (operator == '\n') { + break; + } + scanf("%f", &rhs); + + if (operator == '+') { + total += rhs; + } else if (operator == '-') { + total -= rhs; + } else if (operator == '*') { + total *= rhs; + } else { + total /= rhs; + } + } + printf("Value of expression: %.1f\n", total); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/frac_part b/2025/c_programming_a_modern_approach/ch_7/frac_part new file mode 100755 index 0000000000000000000000000000000000000000..2b1684f202da37029b3d4946f7b23630c32bdbc1 GIT binary patch literal 8480 zcmeHM&r4KM6h2R<(z4#vA_^nk!jY2Del0>0vK$;KR8m32xQsJzoEQ3(H*ZK=4M@-| zqE%o!{{-nTFbZl{n_5Q@xF`^&g+yfEx$i!^Gu6(8i+kX_bH8)%J@?%2+{J_Q?#H)Z zhls3ZB5HyrSrbH$$iax{G&BPhVzhrSJ(a#Y!rpqZ>ft{T$9ar}LX4*;$Lp8K{d}-4 z3Lk)v6)8y(B6OER1z;NEEn*?ZbJY^Y@m}dlP(@8iBSa}I&y;pujqyIJcx`G$jN=V0 zD&9fm5F%F!1J9q`#r607tm38AJY!wB2D%7Q_8)kDUU+_C0TYe+wSXMH@1)wqxG{zv zkL$1s6=I-&ykFcJ86JzO0lw?%5Z6Olejlv7hFwC;E@)-=Z@f4RZ{q#2FkgC}f8N=h zq2b%Mp6L)3;klx45%?l7pLq(p2F%oN!Lt7dbQG!! zuZ>mr>*G&O!~0T`$6zr<{BC;6rJ!fdo9W3f&*gzx+M%4sM73JodU*Z$v*5+q`|Vxt zFT>|REhv$V;I>W{5VgRbS9lG0FJvN9{gPSvntYS~$>B0j`leylX>%w-S!v4K@bqC= zvyGnA7zPXjhJk&-K(eBKRxA4=9g~D%z%XDKFbo(53GH zFkl!k3>XFs1AESZgNWSwoP=_}OK)81ZAI=S#v;c_baAz2B=55jz&e3}SVR{*ERZPW zvVH+hLF|?XG}9J(i%nv_DBLACR}NiKbhCLkpw{`U=hIR2Wx4sNmfKcz2**DOwK0hM h{M@SpQ5u|)s? literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/frac_part.c b/2025/c_programming_a_modern_approach/ch_7/frac_part.c new file mode 100644 index 0000000..0a8eaa0 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/frac_part.c @@ -0,0 +1,10 @@ +#include + +int main(void) { + float f, frac_part; + printf("enter a fraction: "); + scanf("%f", &f); + frac_part = f - (int) f; + printf("frac_part: %f\n", frac_part); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/math_with_different_types b/2025/c_programming_a_modern_approach/ch_7/math_with_different_types new file mode 100755 index 0000000000000000000000000000000000000000..a274d4ba5e0384b9a272043f69ae3ac3eee3feb4 GIT binary patch literal 8440 zcmeHMPi$007@utmQbb-gO8gfx9xzaqt>VE;lU8|QiSj226KmZlIB1 z6Jpe$(E}k(xOjp@4th7j5(wocF(K)ZLydb-54*%PR$YI;nR(sW6>b_X^JTxxe82hT z_rCX=H+%Vh_~X~V9}*(dD}?BQ55SM~3GtDTmBhWCuf zvxr2c#=`Nj&J?|$4|dJQYk;NBWu+>G6P0#iH(w34lkYoQ6l46_)+grL0r`?jbs|}X zy7^A7HGCMfrE$7po#ji{mC8?-lXijKe4kjp4VHj0wZET%FR9GLI0 zc8zE1no`yB@wgnRxLlmZLN}k}#{hiXZerZc;qviZ=iy52932}~Z|vK9(3~#Luj5?P z^R*-2na&(SN*$YSt9F0Bb;#9*O|EWyZF}T;x&7U~wR`WI;m+NrDj{T7U5IJRX3!ii z&#OYr>5?Vx1#))S_z2=5#MI_V_&j3Pa|`2Ej1R;2!(C%8-Q{=JIln3It@lQ(VM*}2 z$ySq@>{L9Fjb^7J#I$GNtYhQ3i^D%YvE%Vezn{PQ^QPa zk%Bh~7s2zifxZS5T&XStxs54!=F_C$O1RtrPXjzy<`ePYSqyGH``GPFj)uINA>qSv!Wu74BQ3^%?6M|E#C}&0al8jP# zYh12ulSRu1T?OBR=8n7rydxtR=^xntUCv$OJ==hLGN5mrULmD7uYgy;E8rFI3U~#) z0uNe&xd&ZngDDfC4#Ct^z4WAxWnpA}og?zPAP?LqDQDH{( tmyX6|@d$bt~1V?JG?ll*UbrRm+W{sBc&K(PP- literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/math_with_different_types.c b/2025/c_programming_a_modern_approach/ch_7/math_with_different_types.c new file mode 100644 index 0000000..65481cc --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/math_with_different_types.c @@ -0,0 +1,35 @@ +#include + +int main(void) { + char c = '\1'; + short s = 2; + int i = -3; + long m = 5; + float f = 6.5f; + double d = 7.5; + + printf("c is %d\n", c); + printf("i is %d\n", i); + printf("c * i = %d\n", c * i); + + printf("s is %d\n", c); + printf("m is %d\n", i); + printf("s + m = %d\n", c + i); + + printf("f is %f\n", f); + printf("c is %d\n", c); + printf("f / c = %f\n", f / c); + + printf("d is %f\n", d); + printf("s is %d\n", s); + printf("d / s = %lf\n", d / s); + + printf("f is %f\n", f); + printf("d is %lf\n", d); + printf("f - d = %lf\n", f - d); + + printf("f is %f\n", f); + printf("(int) f = %d\n", (int) f); + + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/sizeof b/2025/c_programming_a_modern_approach/ch_7/sizeof new file mode 100755 index 0000000000000000000000000000000000000000..a9421668d29632cc3bf17308426cf44729a3405d GIT binary patch literal 8440 zcmeHMO-NKx6uzTVX<0M0h@=*8V`vMfzl8)rnu8;SN-9VYSI2qdJQ(N6%o|cO7?L0t z{Vb(*%NA|gN>EUXpmuG7RzZt`XjKyB_uY4&-Fdof75Bj5+;i?Z-#hQSXLsIie*L{q zh(xOpq6M0!CWW{o9Grycgyx`9j`a^^uVt@}GTJPOW_MSQICqgKWb2=f8$Dd(;D-i&H&JezVDN@!eH@r$gdX=J&i#Xnj#+%Yb^y3&N9^YYI z;SBaq^vg@5Bje#~fbXVvh~7h6&Igq_3`sfX8_xiLcf2;_DnSwJBOzbvdphuU&aMoP zT+R*-M>FEY`hrjS5gLVI;+zwrp;~r?m`2W7?N7r$1)tm;g|5SAKks0>VGlsZpr$aF zYT}#sJim5!=lc-ua3wmRnR>07DSP=$VX0hzPdNr6oxmY&WiiwgXBrvssDqep!WuGIOnGDyM0jJh4V0^CS|)9^fSIx3*7nMfnN0G z<-_U|FvNf82*J1O(LVY57c!Y!`;=S#l75x_MY!C(3(GJYZ1Ji1Tna-s8R8EiG#Q3e z78^onzzFHAOc+vyI_r2Cx&^brKSE(U%<1R3)tBk(FT|fNK6=etcKu>^t>jmOp7T!E za(#ak^D6O3*{{sSl8R-2CZ1OC>-n-9OJuFne8V?pmlV^I48G?@Sn5yxwmh4{ndrcD zr1iAG4adezvS-VHWxz6E8L$jk1}p=X0n5Nn1{ymUFs zSOzQumI2Ga|HOcUpW*J4D_1U%%q5auJs}$I64fC(r?JH~mXWHK%PYBn4R}ygxd|x_ z^T2qk_=e?Ni<~b>chQ}#2d*r+GX=LQ+UIAyia01#LX@6Lgy6(I0!`r%?f1DagUc%= Qgr_>#MDK)`3f^pM3*K|ar~m)} literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/sizeof.c b/2025/c_programming_a_modern_approach/ch_7/sizeof.c new file mode 100644 index 0000000..dddd8e2 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/sizeof.c @@ -0,0 +1,11 @@ +#include + +int main(void) { + printf("sizeof(short): %zu\n", sizeof(short)); + printf("sizeof(int): %zu\n", sizeof(int)); + printf("sizeof(long): %zu\n", sizeof(long)); + printf("sizeof(float): %zu\n", sizeof(float)); + printf("sizeof(double): %zu\n", sizeof(double)); + printf("sizeof(long double): %zu\n", sizeof(long double)); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/squares_types b/2025/c_programming_a_modern_approach/ch_7/squares_types new file mode 100755 index 0000000000000000000000000000000000000000..935f9a34450564ebbda49b76d3acd3b91db676e1 GIT binary patch literal 8440 zcmeHM%}W(g6u&RE%&aFCVcFugu+%{N0)sHp92_Y>NCgSxzOPZI^u5QMnbbgf@CXu# zTDpp$ef0;l5GrhEe?&##1TI=gMD;s&?%2GS%T{p@9M3)Xocp_He)sq`XXE?VpZge# zRWZi)KpRM_8GFDuR*W5knouD|disr9#?4{cEtgoiyQ@by50NOum@zR{o+8iF#Zggy z4faS;k`y6aYr$0xOeJ0(iG=rDrH~(G6(PLryq(PmJ3Ey_pc1dB@FvtD@+&c-c=Qgd z5~r_ctVdiM9vTf^1ALdgL--!bQh$(A4s8iBor^pJ{N3?tkt+p<=zJ)|m%LAH{OPkB z14Gx1zJahuctu_CNwxzcF`_sZ87s7# z&SQ^5gFEAB-nd*#?yDbHw<9LM50(VA7YT>@JSueWB`C#$e(0B-vQrD{8C`T-Yp$cW z19jP{;PfdN!hgaUqql2QKJoQ0WZYc-WR`rsZu}rPbKTg2DXtqneesIn$6p%0xoTcE zwwAKWEp4-bUMX-ws=&MgaNurkCjNbGr zEa_qOJ3;HHP+!yor9pKJ+@N)$NqVgr&Gy|Fe&46Y=GoTqLXP{W_YELtu8PE)9 z1~dbj0nLDBKr^5j&Gy|G}|BV5T4;q2e$d!gi(E(C)tVpxOP~^G+Q(PJrDv@Vt za3%7XfV)$2J0@5oEoi@rZDFRkN6gI#Yr#r;t|exyWX3A6{c}k>%MLP`5T?gt0XX5( dv@VWCINzsP8C(iUA$U@bN%Y_FLc&MO`VG1}+6({y literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/squares_types.c b/2025/c_programming_a_modern_approach/ch_7/squares_types.c new file mode 100644 index 0000000..20b6aa6 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/squares_types.c @@ -0,0 +1,36 @@ +#include +/* + This is interesting. The compiler and linter give me warnings about implicit conversion to `long` when I assign a value to `n` above ~2_147_400_000. Meanwhile, if I stay at or below that value the results are as expected. However, somewhere above 2146923072 the sign flips: + + i (46340) * i (46340) = 2147395600 + breaking becauses we're about to overflow + + This happens because, indeed, `i * i` _is_ less than `n`, but only because there's been an overflow. + + The exact highest number for `short` is 2_147_483_647 + + What's also interesting is that I get a compilation warning if I assign a value one larger than the largest int, even if I use the type `long` or `long long` for `n`. + + I conclude that the number of bits my machine uses to store an `int` is 32 bits: one bit for the sign, and 31 bits for the number: + + 2^31 = 2_147_483_648 + +*/ + +#define LARGEST_INT 2147483647 +#define LARGEST_SHORT 32767 + +int main(void) { + long long n = LARGEST_INT + 1; + for (int i = 1; (i * i) < n; i++) { + if (i * i < 0) { + printf("breaking becauses we're about to overflow\n"); + printf("the next product is %d x %d = %d\n", i, i, i * i); + break; + } + if (i * i <= n) { + printf("i (%d) * i (%d) = %d\n", i, i, i * i); + } + } + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/typedefs b/2025/c_programming_a_modern_approach/ch_7/typedefs new file mode 100755 index 0000000000000000000000000000000000000000..8237d9a6ea5b1e76b74b885c5c5c03cd2f528565 GIT binary patch literal 8440 zcmeHM&r4KM6uzTlDOn>eqNv5)7#gVkTC_2=eE6hLNdpVw>Nrn*;EXc!hIBU|K@$C- zMM(cc(57h7qA0ROkbgkSf))z1jEL&{?z=DVOwy{2a1R{LJwNVu&-?D3*_@Bxw|?#w zBGo8_Xn?j+(?Z-55-TB&Kz*oEqy2;4b??duqqUN%b$9fLa~p+9je8U0wIzB#54Oel zO^8v^k`|?+U@0;VOg-Kr3W?{L68gzr9ZHLeNok{0DJo}6e?s+m&kf!lQ<3<|hGv78 z3@je*53A2fQJ%)|Pdsx#r z1O4Ou>f*@oSbQ7cyXGE}`!JU8gUT9)l$u?zt^xkec+IHQf=%3?2>H_InZWityF4^} z$r~6--iWO03qIvrY&3?Ab5@9oZdnR3iJCLUpM-xBKDjvzU4zelHefqo_d!RYwy>6J zS@Fz zG(Izf$D7e>&3*cQ`UGPy+Rzmb6a@sIVf}Sdl^d*oV_M1CF(}2h{C@J&2TOfs zxZ>Ita@-1z+6?<}<7=>s+}bhV7;p?Y1{?#90mp!2z%j7<8K~_3PjFx07;p?Y1{?#9 z0mp!2z%k$$a11yG90QI4$H2cBkVta&$&o7uUiQ$-9xHN|n227tVTx-FBlTVm+teZ^ z;L(h(O(}7Z3&tBo6*I*Km7i0=QZQYP0yP&*<$|JU$xnp^fuT4HC@jyUVsPSe3Yftn YneTJH2bU==tfxv$qW^|h8s2W}H_HB(b^rhX literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/typedefs.c b/2025/c_programming_a_modern_approach/ch_7/typedefs.c new file mode 100644 index 0000000..2a396cb --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/typedefs.c @@ -0,0 +1,12 @@ +#include + +typedef short Int8; +typedef int Int16; +typedef long Int32; + +int main(void) { + Int32 a = 32000000000; + Int16 b = 5; + printf("a - b -> %ld\n", a - b); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/upper b/2025/c_programming_a_modern_approach/ch_7/upper new file mode 100755 index 0000000000000000000000000000000000000000..c5087dde3b4b522023996b3e4c9fa0fe9a6aa270 GIT binary patch literal 8520 zcmeHMOKVd>6h5&J8?7c5e4*fUQD0z+kA+r+R(okmOD(oy5z3GzH_e4KEy<0xsI*Xp zD3q=Q7ybYjF8T*7LIrWVL#4%O#t#^#)_Rj=@| zAJC%nHR}N>lLa^LzJhiB zd1r(DLjzWCzyC%=U0uK|aaF80r(HFEiD*IXDTp!{xu|glxEq-FJOW)2-n)vn6I}-0 z3mt|A#Xobf&fvWA5nuDZ^n(4c0?O~EvslP?W}S)7)Lb?N%yJCMb#$z6guC8c{kS}T z;MK^$+q)MqCZHiGQ3S~&%2(VsE;pXVXq8j=r*S@Yp1LA)En{OQ+MZ$cZOvdybDUevN*xeld#ZADg#TUgJ3%`h8=IsY!c_&z$mMwO25dkEhj_)a?aZ82qwgvi>iYv)BG zmlFM_vRP4_nX&W9M8Ou0+a;(YNf|rOKsbeV(UC;3x~t=`EJ4e6z<-OnVOg8>@1Fgj z@IAL8OH{uAK<_C87a89$U>GnA7zPXjh5^HXVZbo3BL<=i`k%~&9i=hBh5^HXVZbn8 z7%&VN1`Gp+0mFb{z%XDKFbw>+3&3V)zCz_uKf4z7^ + +char to_upper(char a_char) { + if (a_char >= 97 && a_char <= 97 + 26) { + return a_char - 32; + } + return a_char; +} + +int main(void) { + printf("give me a letter and I'll uppercase it: "); + char letter; + scanf("%c", &letter); + letter = to_upper(letter); + printf("here it is! %c\n", letter); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_7/word_length b/2025/c_programming_a_modern_approach/ch_7/word_length new file mode 100755 index 0000000000000000000000000000000000000000..7099b2a4cb815cc78b9b76aca6d7dfde9860639b GIT binary patch literal 8488 zcmeHMK}b|l6n&FpsagLg5=AZk#;_Ke)uM#RDL;-RDrzJ`y+-FdGopWF=1=M-28N)5 z2wb&vQPI*}i$Dho+7+}4Y8wn#AdHJ4B0Kl}_nkjK+e8Z&@8X^N?tS;3cjvsP2=~K} z@4pT(7OG>6?StyjJ7LD|GXpEejzgnR&W9s?=7c#iMA2Fa)w;WSg!2iBoR68~W3?%w zo(}el^y?Axl(so{tVO4q7{n{0cf$K9Q^?0pRl+P^bqFu(#PfP0pLqY9059ugQg%Wn1o56pyaBmSzF&gaoagL?lpW_O zdpeE8Al|0L8VNE?|!AF_eM%N6yiLQuChk{ z?sy&y6;Zt>#J|7K>bo82AG~h%_Pbp%W(4|zPtq=$b@${_#4j+G7iSub%_DMN`t$J5 z!lyHjL0`hBJFUP{{2+7~>KDrO)F1b+OGeG}F__|ahW#piQ~8Bq5-X#m&6T5unJ51=W z(w@t6pLi)dy`Z1brL1GkbzJR0U#UsA@DvQ;uNaIqz@C;q@%2CCM)cv=XyKdw&iqMm z(Z`qH!>qE>YZ&cjv8)|2E7ES2RuFt(mbJBFq0;1ZX=`S=P%(0?WlgVN#$H()H!GrK zqlIGkWNeFKTN~>Xo2kh|geoSAIGKJ&S3{+UNYG6bi7zKOwhKE!a0zjx03!bnX8c8uekMXrL?G=VN zYoRqARL6>qSn;_ew-&9boMZ8%6^mOL);JeS+3XNzQb-`7bS>l_M&}$AzB{ubpJrOH bv=q{YPrvUT=N-$KfeG`U$8vG(Zms_Ys*UtS literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_7/word_length.c b/2025/c_programming_a_modern_approach/ch_7/word_length.c new file mode 100644 index 0000000..96973fa --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_7/word_length.c @@ -0,0 +1,25 @@ +#include +#include +/* + Write a program that calculates the average word length for a sentence: + + Enter a sentence: It was deja vu all over again. + Average word length: 3.4 +*/ + +int main(void) { + printf("Enter a sentence: "); + float num_words = 0; + float num_chars = 0; + char next_char; + while ((next_char = getchar()) != '\n') { + if (next_char == ' ') { + num_words++; + } else { + num_chars++; + } + } + num_words++; // when the next char is '\n' we've finished one more word + printf("Average word length: %.1f\n", num_chars / num_words); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/array_of_bool b/2025/c_programming_a_modern_approach/ch_8/array_of_bool new file mode 100755 index 0000000000000000000000000000000000000000..0ebe754acbcecde5a840aaf217dd2383f78e6d41 GIT binary patch literal 8616 zcmeHMO=whC6uvK+v5ARh=t2~`c%?>K7m2nCibNXC;0@InY6Pnd*O|;q^2q#zyf^qm zgb5+iI1J6EkY$#F3*G2K5QG*on^HI8UMNOlp{pWYbozbwy~(^8>!!G9;U04CJ>R+K zo_pT+Zo*8?FMs~_*9Ssa?Lvq)Xo4COVoBJrgxCX>P^Cr(PbV)WzdlEAvslgW9})9> zhd`yqk{8FCL-c+g9F-8q;iIA?ElT;$H9rKHTJy~!kol5fQN;DVdRI)8^c)UKd44hN zZMjzVZLplL#YYRpL6q5Te$got6{$5;W}|t_zt3?R55?m&1aOG zpDbXZwZ0e*W_=gKO~kD^qWSn9R-sA_4UP?}Q|Cs`2X6y>Ys%{Hp=IX%pfZL_N=+3a zui>5fY{Y88Cho5b`HJGkJv%cz@>OzZxPC|Mmb!o`cXYAAT4-wjVIfMobUS#?FG9Qu zd=L@jW;gUHFl!k7IN+{}HZ&@Xr8eE4XaMYk*pJ_zw`3OCy!!d{x)7h@`Iv|I zJSsf*D3oi#eK@Ue-c~KB=lpfgcXE9v`%ssg4tDRy#QarThz`u3g_wN354j+hf0YZb z*N{t=JOD2MgA1|INxZ%^TMu1)Of%yh zljR4rWLdA>9*VE*vw9vrm6d@P*mqxSJY(iyRW3fUY;uV%B$6#?3i-&n@IrS=ESX3e@-OBV?yUXWkp$t1mHAAB2CaXh(&2vm03w;jIPK zRYqfsZ5S{N7zPXjh5^HXVZbo(Ut%Cp3O=wUO8m@+Zvdt6yIDy?GZ+R81BL;^fMLKe zU>GnA7zPXjh5^HXVZbo(zh}V46R`Jr11*PMx>@2F`fm2u`oXD;RD#SWnZxQ*dGR!|h{*`;yN$0BBb3o}hEeHf#7umumd cVCE+$Y=s4-H85Pl#^b$RrqX5pTPm3MZ&meAtpET3 literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_8/array_of_bool.c b/2025/c_programming_a_modern_approach/ch_8/array_of_bool.c new file mode 100644 index 0000000..eec431c --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_8/array_of_bool.c @@ -0,0 +1,17 @@ +#include +#include + +int main(void) { + bool bools[100] = {false}; + bool multi_dimensional_bools[3][5] = {false}; + for (size_t i = 0; i < 100; i++) { + printf("bools[%zu] => %b\n", i, bools[i]); + } + for (size_t i = 0; i < 3; i++) { + for (size_t j = 0; j < 5; j++) { + printf("multi_dimensional_bools[%zu][%zu] => %b\n", i, j, multi_dimensional_bools[i][j]); + } + } + + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/cards.c b/2025/c_programming_a_modern_approach/ch_8/cards.c new file mode 100644 index 0000000..5f21a27 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_8/cards.c @@ -0,0 +1,9 @@ +#include +#include +#include + +int main(void) { + rand() + + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/designated_initializer b/2025/c_programming_a_modern_approach/ch_8/designated_initializer new file mode 100755 index 0000000000000000000000000000000000000000..57561af447c1798fa8939ad37c71299962840e9b GIT binary patch literal 8616 zcmeHM&r8%n6rZoZOUtc87)5+N7}Y`7>X4wI<>FT|wKS5_#?}3*i|#7!F6kxKgOP}I ziSSyNJX+8t5P|}|)jtt>}g`Mi1a=FMkD+1>Z`*N?qY zA*@0nL;*BFEfQj0*cb_M7V1Ey>~3ibJq+FJqBU2nTzDj6oabCPlIK$jnH3XY8TO1lte)+$qxNb4!pNtsBdh7ws*e!LQF&UmM_M+RpW zl<9aS+92g{A~_w6k7jZ1d8>51YCTWv7v6ziq)f%1MB@<|jgL%XB0s-I9j{L>BF>ND zkH>phR&iQedRyeJuFf9sHo$kzJ!I}f_k15z`p_ih=%nu&fRB!6!&eCwaeYR}mmeRz zvyS%8+o9I>%p0-u@&cx;sARo4JyiDVLZns7ws+2J8ZQF};X!Vypx=O*LwB_hMyX*7) zd;9y`!dY4y#IXM7$CvQh9D!*)w9SJre&B?&)bto&Rczj+Ewu&11JBT`4BIep3LbUn zu+Ig(&*Gh3r(q*iUjNCN`@$RsK5xQ9?APoZXS=6bK8En4;DFs^*F-RCXvhxhgBdec z@s7@OImF+SZ{9~UdMf{CP?}YH(C|(x5G9~*8;~K57zPXjh5^HXVZbn87%&VN2L3Sv zfwcF*Gmz$IS9}9V>+fc11x;fZFbo(53GHFkl!k3>XFs1BL;^z<5$1QodJr6TJIbli9OYH!|By1e-aGFx3`KMGc F?r*!O+-3j( literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_8/designated_initializer.c b/2025/c_programming_a_modern_approach/ch_8/designated_initializer.c new file mode 100644 index 0000000..feeaea8 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_8/designated_initializer.c @@ -0,0 +1,9 @@ +#include + +int main(void) { + int a[] = {[100] = 42}; + printf("a[0] = %d\n", a[0]); + printf("a[99] = %d\n", a[99]); + printf("a[100] = %d\n", a[100]); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/fib_seq b/2025/c_programming_a_modern_approach/ch_8/fib_seq new file mode 100755 index 0000000000000000000000000000000000000000..8d0cdb0998beabf267de245fe94f8c44ad5df05c GIT binary patch literal 8616 zcmeHMO-NKx6uwXGSIePAKh)wC*x$mRYEe+&R6|cPi!_iZZlm+mA!iil4OWYo8jNI& zR)LF%z!oi9v?z!`kV=X+wQpSr7PSg#;q-m?-KTFxSJ5WA2hY9doO{l>=YIDpGoBB> zzRj-^!m1QPRDdJoDj`OM4M~VCU!Bn1a5RS~ZQ5Qu$h*eQlBBy3ONSX1nU6}<_dA?iNoT)Zw zk3bIsR_lB#wL!}6j5m}__b%wA=N;GiYV3p3! zh;SnD@%a5&LhE6%!Mx{&Y-XsTKiSogxZ0n9PTC4)9q&JVPbPL8 zd2oNCZBzBliO>6Bv%wZvh+1r3t7L_J)!nMSu&VQ^bs=`+e9Xgl9wzR45X?3IJ)BT4 zWuX?-(|$GMxvBc2^{6Y^i~ zS~)h&us|Iho1O}(`6Q|<=i>Kck@)t z9AkCb&re97*_SxygSA}3`*(7SAP)0A`L4KcX*dWIVn>pXZA>#nOYhq3Nj*4~X_ zkG)pi|2DdT!ZYuc_uh<|%)d>fW;HGjd^c6yok$uo2p9wm0tNwtfI+|@U=a9k z5s2jc4}_5%KU3lxKu&)*%PG|i1_6VBLBJqj5HJWB1PlTO0fT@+z#w1{FbMqf2-r9Q zdsvsiY$Jx7L*cd}dx-%lIf4#qs2RyP3;#f{&_uLa!53hV?6AkXFU#(}%d#h)>@T(S zX5&K%AyaND<9b34qIK@|a1%@VJ$L>?qEh@oFS0_W`lNfs?aq3x>~rG@cSx*B#gl2V zPACV4(Y@C4i()++mG1Y+sIz~DnWCb4jA1=mrni*o%uh_%YV%85)L{}fj<>W-smlCQ HD46#*&YCwT literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_8/fib_seq.c b/2025/c_programming_a_modern_approach/ch_8/fib_seq.c new file mode 100644 index 0000000..b558409 --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_8/fib_seq.c @@ -0,0 +1,14 @@ +#include + +int main(void) { + // fill an array with the first 40 numbers in the Fibonacci sequence + size_t fib_seq[40] = { 1, 1 }; + for (size_t i = 0; i < 39; i++) { + fib_seq[i + 2] = fib_seq[i] + fib_seq[i + 1]; + } + for (size_t i = 0; i <= 39; i++) { + printf("%zu (%zu) \n", fib_seq[i], i + 1); + } + + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/random_walk b/2025/c_programming_a_modern_approach/ch_8/random_walk new file mode 100755 index 0000000000000000000000000000000000000000..5c2c8d209f8d5346ca6735685967812c55f68c18 GIT binary patch literal 8736 zcmeHM-D@0G6u*-XZJL-YQlwQMRxpxQNK7r5LWD#UdV@8sHqs!~>t=V8EXi)_?hGbx zTZS!JmZiQ3e!TkNlZYThVI_XRrceF>zK9@fNR=R>tzg#Q@6OEb?6^t`c@gHox%YhB zd(OGPJ9n0y``w>EAKWj5(J6%J07uAOLKH+4k`O1r60Fq4$#c`MPQQGC)>blF)wUYr zY+|5NbJO#4twZ#DCiau7cf&?TNt%>0lk29dpbN&kih+#x5f=oa?#V6OOp??rX%0=ge#>v((UA*E{aUL#K(lpSTBpQp(D_mC7Vk zDzmr-N3gyrH{QHEiFz=GKOXntT^(m?a&A&Re_{5Lvm3y>Wgoo#aC^c@<`|lkT3YjM z1N8Rs#xPbBCb?UpRpRAgMnm&CJ3lk~!t~UP_l!gXbwMXZ-9@qih1|6SL1GzRdM6zWHI_$ z3o(TCF%F+|KQZqZm_7%pb2vsuvli5IX(MMQ)5FgVqps9~Gy4g2j9-fifk#Q4bamo& z81#xP{v@y6`T^^crS9v1U6zC_PT#H;t{l|sOfkA$tk&Mgg3*=HRSWa3r&^eEC0VY# z0de&AFWyF^+8l<-YC*z8;Hm{WDZx1AO|dH!A0Dtz1CTvYdlHjACv5Niai{ps6nu7t zt+29oMW-}VC7s*et{1f+<2$mfgUgkhL(Acku@vv_QC zcLS9=sSPi6PcyY~FR8u3cbobE%Xd*Hd<4kE$NK*HdcKvsrP#Ku>X5v_s(C9dL^RBqTVp9fDejmp}4!v zfz`qgWK=DrU7>h%!2Xn4wGeGOD8iAa!+TW99IYRhQkniz*JiPB27+B-E3AktKl?Fe zd?IQ$(Cr;7eMSeYA2`+xhO;Z2+++2_e%-OhI2uPtSKwC6LA%*{y{;Wh?K&v#PQZba z*N4N+lUI82wmiFI^^R`KYv1xzzV=NGF-3jwN4)mxgST}1i*EOI`>Sq$LpvN5g9&j| zzn>oEB#^)7dx!6(3G`(Cc9JG^+iY+S@t!pB-r{H=)O`5&CR`=2eZ)vyT>2oVSo2oVSo2oVSo2oVSo2oVSo z2oVSo2od<-AP_~Ry!%`Q0Htj>ME|$%|)TIu?(y^r!!OOq^`kG z;z0`hV9_b|akbE)(#tBjo{U>&QY|NAiDXvvq+_WJFK9V{#Nh*mQ#9*+NbAl$1$EwE zpr@#57_DEQ4Cpff{iT3@IiSBD&{+>L=JyqTTOs}|)mc`1AKW4e=>EA}rI&k{F#ips CRju&= literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_8/random_walk.c b/2025/c_programming_a_modern_approach/ch_8/random_walk.c new file mode 100644 index 0000000..cd1679f --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_8/random_walk.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +#define LEFT 0 +#define RIGHT 1 +#define UP 2 +#define DOWN 3 +#define NUM_STEPS 26 +#define NUM_DIRECTIONS 4 + +int main(void) { + srand((unsigned) time(NULL)); + int x = 0; + int y = 0; + int arr[10][10] = { 0 }; + + size_t steps_taken = 1; + arr[0][0] = 1; + + int direction; + + for (size_t i = 0; i < NUM_STEPS; i++) { + while (true) { + direction = rand() % NUM_DIRECTIONS; + printf("%zu: direction: %d", steps_taken, direction); + if (direction == LEFT && x != 0 && !arr[--x][y]) { + x--; + arr[x][y] = steps_taken; + break; + } else if (direction == RIGHT && x != 9 && !arr[++x][y]) { + x++; + arr[x][y] = steps_taken; + break; + } else if (direction == UP && y != 0 && !arr[x][--y]) { + y--; + arr[x][y] = steps_taken; + break; + } else if (direction == DOWN && y != 9 && !arr[x][++y]) { + y++; + arr[x][y] = steps_taken; + break; + } + } + steps_taken++; + } + int val; + for (size_t i = 0; i < 10; i++) { + for (size_t j = 0; j < 10; j++) { + val = arr[i][j]; + if (val == 0) { + printf(". "); + } else { + printf("%d", val); + } + if (j == 9) { + printf("\n"); + } + } + } + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/repeated_digits b/2025/c_programming_a_modern_approach/ch_8/repeated_digits new file mode 100755 index 0000000000000000000000000000000000000000..fd9f20b06f1ea6291ad57bccd0a8d07c098020ab GIT binary patch literal 8656 zcmeHNO=uNY6h32;nyB?XEJ(F2Ora5@k~CIP3qsOna6MGDy->%UveBdUF2!Uv*n>I+P{mz{`>CEV^ZiIW_%)Q^a z=bU@yyLa*4JMXv05C7RpWONYG7HFQeljw6YF(cXowV^_c4WF_;vd*7lYg&vnd?w;N zSK%ncxHU1Jc9HSCaZ(iSf{hg=NfE+#W_<-PWb@6yk@F3zq8P`qGOCk8IjV;co*xvw zO;a}Cci5a$y{bH790zo(d@m`35GBvAyS2$poPOUMDqp`^XPgv1gQN%%)ULR-vT$pa z%UHT-S@&3897oyVu->Gwm&qIxTKUmp^O+rjwP8F%B)SgEI|^S1 zejAv%;j>%@#=WQ6H;I_L?a(o3QrMR@b3{oO8bf?f$`XN{7)O26ed_TEP1b#!# z!}mNXyzg-+uSNIal)NdMwV<90bDr;1``_zFU2ZXo-iLwnw@spMjC~3(13wSBXfNEi z8xMx;X4e8Zx0_@ySR1#jWlnB?d2%0K{0CZn1p`o@9D+71Tjg|qeelm4+u*voCjIAE zVtkKr(`x_3tAls!zaH5O7nfjJ`eLMO{VgN+<6>jO2;NYZ6=k7dJ6mp!bgdX`J!-mc zV3NvYwe7}yo34C~qOM^B6g0yd-m+R42#c+4-VsE#uv?tl2zJ@cpoNgRDC8uDHLkK{ zHx>=Ncx&xf5D}teCZc&}!#>dpx(65S#xFyk{Je@+CVJ94f%PZ7MfohtX9Z8I<~wz> zVAg_a(W$>{Qtzh$op)xOg71{gvODSe`@O!%Zpu6KI;VpqN4^_;cZM*seyc7!&y&LU zZwIpE)y)Ky)g^uOTr;2<&Gy|Fe&46a$MPVQxMjz(#A%7mkH-Iquu9gqg*NRY@ z^+PkD8PE)91~dbj0nLDBKr^5j&Gy|G}7nT7Nm2>Ztdz0M$Nj7$pZAI=S#v7jqH!AfF?1S6!Cm%3>5?c-T@saoJDs*n28DW=MAfN!j!$9+?Q_3E z!YdVOm3Tig1npwLKTvegALvbP5!ET-%sQpOcf^!aC_8oPsTSNCy-d;}cF!46kzC(y zXbzM3e*)YK1LP$s+?P+{;S3(l;14sH%VW-%N3f&HP1|556BImMzL4erAG-bp@J1@_ literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/ch_8/repeated_digits.c b/2025/c_programming_a_modern_approach/ch_8/repeated_digits.c new file mode 100644 index 0000000..d65115f --- /dev/null +++ b/2025/c_programming_a_modern_approach/ch_8/repeated_digits.c @@ -0,0 +1,34 @@ +#include +#include + +#define PROMPT "Enter a number: " +#define RESPONSE "Repeated digit(s): " + +int main(void) { + printf(PROMPT); + size_t number; + size_t digit; + scanf("%zu", &number); + bool digits_seen[10] = { false }; + bool digits_repeated[10] = { false }; + + do { + digit = number % 10; + // printf("digit: %zu\n", digit); + if (digits_seen[digit] == true && digits_repeated[digit] == false) { + digits_repeated[digit] = true; + } else { + digits_seen[digit] = true; + } + number /= 10; + // printf("%zu\n", number); + } while (number > 0); + printf(RESPONSE); + for (size_t i = 0; i < 10; i++) { + if (digits_repeated[i] == true) { + printf("%zu ", i); + } + } + + return 0; +} diff --git a/2025/c_programming_a_modern_approach/ch_8/sizeof_array b/2025/c_programming_a_modern_approach/ch_8/sizeof_array new file mode 100755 index 0000000000000000000000000000000000000000..39dc2c188c526161fe58588e831374d6775bed36 GIT binary patch literal 8616 zcmeHM&r4KM6uzTYrj7s%ltH~!>Ng7Bg*BR&OJaopGd5_f9v@kM} zHZEMXZWFX|69hq_)+UMmfHs1Q1S6=3$iDBsJNjmH6>Xw>nDg#EKkvEUy)(?rdG}-W z*DfI<w59+N1_4bDp2bZKz7~HDht++pj$e zav`8n=i8|jQugP(teY9!kc;=BUVXWkV#Kev4KrbmA+YsyeioW_SvN6S;!6WAT|fVOORPZ$5A1O zT{Y+6FwvJ>bbrDQm^UCE@1HHS4;l-+2VC#xvh~AmqCPn`oPZWYe zf_gf~a-Nf}yHJO^+(Zz43(ld-_Emrj1{p&%kSC2C9YF8aHk)Omx6PHKsPE+_gP5N3A`5&WH3&tfT@Z z?NE*d%1)(J@r`dL-`d~ilznm@F4@3^_X9YHxj2R#&2b$T zoc|Fn1`3ZnOP=+4jN~T$`N4UZcwhG5=0w%)Q5IEu*vHHb0tNwtfI+|@U=T0}7zF-T z1fu!i%V9Ln@0a)mkk`M>@=7!hgMdN6AYc$M2p9wm0tNwtfI+|@U=T0}7zF-#1T5@; zz0W={8*1T3QMj$hUSfgNJcw8bpoti12lqfB* + +int main(void) { + int a[] = {[100] = 42}; + size_t size_of_a = sizeof(a); + size_t size_of_item = sizeof(a[0]); + printf("size of a in bytes: %zu\n", size_of_a); + printf("size of a[0] in bytes: %zu\n", size_of_item); + printf("number of items in a: %zu\n", size_of_a / size_of_item); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/chapter_2/benchy.sh b/2025/c_programming_a_modern_approach/chapter_2/benchy.sh new file mode 100755 index 0000000..bd8982d --- /dev/null +++ b/2025/c_programming_a_modern_approach/chapter_2/benchy.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# save as memory_avg.sh + +runs=10 +cmd="$*" +total_memory=0 + +echo "Running '$cmd' $runs times..." + +for i in $(seq 1 $runs); do + output=$(/usr/bin/time -l bash -c "$cmd" 2>&1) + + # Extract the number after "maximum resident set size" + mem=$(echo "$output" | grep "maximum resident set size" | awk '{print $1}') + + if [[ -n "$mem" && "$mem" =~ ^[0-9]+$ ]]; then + total_memory=$((total_memory + mem)) + echo "Run $i: $mem bytes" + else + echo "Run $i: Failed to get memory usage" + echo "Debug: maximum resident set size line:" + echo "$output" | grep -A 1 "maximum resident set size" + runs=$((runs - 1)) + fi +done + +if [ $runs -gt 0 ]; then + avg=$((total_memory / runs)) + avg_kb=$(echo "scale=2; $avg/1024" | bc) + avg_mb=$(echo "scale=2; $avg/1024/1024" | bc) + + echo "----------------------------------------" + echo "Average memory usage over $runs runs:" + echo "$avg bytes ($avg_kb KB, $avg_mb MB)" +fi diff --git a/2025/c_programming_a_modern_approach/chapter_2/chapter_2.md b/2025/c_programming_a_modern_approach/chapter_2/chapter_2.md new file mode 100644 index 0000000..819dc6e --- /dev/null +++ b/2025/c_programming_a_modern_approach/chapter_2/chapter_2.md @@ -0,0 +1,39 @@ +# Floats + +It's recommended to append 'f' to the end of a float value: + +```c +float amount = 2.52f; +``` + +To print a float use the "%f" placeholder, and to specify the number of digits do +`.%f`: + +```c +printf("amount: %.2f", amount); +// amount: 2.52 +``` + +# Directives + +You don't need semicolons at the end of `#directive`s. + +To define a constant you do + +```c +#define CONSTANT_NAME 7 +``` + +You can assign expressions to constants but they must be wrapped in parentheses. + +You can use constants in the body of your C file; they'll be replaced by the values in the preprocessing step. + +# `main()` + +You can end this with `exit(0)` if you want, instead of `return 0`. + +# gcc flags + +```bash +> gcc -Wall -W -pedantic -std=c99 progname.c -o progname +``` diff --git a/2025/c_programming_a_modern_approach/chapter_2/fewest_bills b/2025/c_programming_a_modern_approach/chapter_2/fewest_bills new file mode 100755 index 0000000000000000000000000000000000000000..a4da59089e797050250e36779e535ac4dc649310 GIT binary patch literal 8440 zcmeHMTTc@~6rMssu5K~WsA#eeV7w6SL_=aiwGAaI7{LT;bfC0_#!?8|Mo5Thfh47g!2W7oR5S@M~YL#dU~;2 zq+J1@trX@wn^?#?iC#PtwG-Y$Srpm$IpSw|#~?f-n~NEHE-zk7rGoswAEr z=X`QDV>ktR@wz3RyG4XmwA-R#Ca-{oJFD+ z?}@}4l^|q$G2HlcuImCP6dVci>qCRX*6Ct{Mdxa7SdMhxNGXS0oKMX%Lfe){E?myNy*V&=BODsAs$xv{)CHSl#~K8N8>gGGyeL`6cY&NPX^+EhhfUm^ zgs#J;dNweg$5;!!4Rs6UlDhHTahiYBOY?mcF@#thv@K`Mx203Dw)kQ?4x6MAN_B)T zzu7#y+SRyfHpI7ou8noVr$eoGYWNs5F&EtXpZhN&CiaJs1hwZ0$1M72toL4lpJLX1 z=*MU2b8107!;3~XG1Gdj6?LUzNQ9h&68;OFF*>^o(k8zCh1`v-e2pw`=>;uNF^5eS zw1)654DqOs3eSVL1ubeOwcc8@`a5QnvqL%bxV4+q9-BR=C7;wXhy{)}iB)6o3fd@E zy74Lx#QQ;b^eBP10q}xW3h%Xul&bF=;nAZ6-qL=&mhkRMc#GCxPf;_nc9s4xUimg7 zB3G0q+!L0&L%Q(>;l5|NPh)tN+rsiFIoM`3h7L2b{6W9}p8aY5L^kAQA8u6;W009k zp{CAuJ(fzRjb6REwLMwGnmW9W9&f^)9j_7h@r=M_%stXQqkD*#3B5!gxtv=w>q&RH z4k!VEBz!n0C5i#XfMP%~pcqgLCBUu zIWy5fD&sTLJh70N%w-dNIx!JX%(Lp5iByKwqu(PtOq8zqEO5d-A#AH3L^kz_Fw#^A Q<&)~@9Pfwc0^VKLUt#bYP5=M^ literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/chapter_2/fewest_bills.c b/2025/c_programming_a_modern_approach/chapter_2/fewest_bills.c new file mode 100644 index 0000000..4c28a06 --- /dev/null +++ b/2025/c_programming_a_modern_approach/chapter_2/fewest_bills.c @@ -0,0 +1,32 @@ + +/* + Write a program that asks the user to enter a dollar amount + and then shows how to pay that amount using the smallest number of + 20, 10, 5 and 1-denominated bills. +*/ + +#include +#include + +int main(void) { + float amount = 1011; + // printf("Enter a dollar amount: "); + // scanf("%f", &amount); + + float num_20s = floor(amount / 20); + amount -= num_20s * 20; + + float num_10s = floor(amount / 10); + amount -= num_10s * 10; + + float num_5s = floor(amount / 5); + amount -= num_5s * 5; + + int num_1s = amount; + + printf("$20 bills: %.0f\n", num_20s); + printf("$10 bills: %.0f\n", num_10s); + printf("$5 bills: %.0f\n", num_5s); + printf("$1 bills: %d\n", num_1s); + +} diff --git a/2025/c_programming_a_modern_approach/chapter_2/fewest_bills.py b/2025/c_programming_a_modern_approach/chapter_2/fewest_bills.py new file mode 100644 index 0000000..291d84d --- /dev/null +++ b/2025/c_programming_a_modern_approach/chapter_2/fewest_bills.py @@ -0,0 +1,14 @@ +amount = 1011 + +num_20s = amount // 20 +amount -= num_20s * 20 +num_10s = amount // 10 +amount -= num_10s * 10 +num_5s = amount // 5 +amount -= num_5s * 5 +num_1s = amount + +print("$20 bills: %.0f" % num_20s) +print("$10 bills: %.0f" % num_10s) +print("$5 bills: %.0f" % num_5s) +print("$1 bills: %d" % num_1s) diff --git a/2025/c_programming_a_modern_approach/chapter_2/print_a_float b/2025/c_programming_a_modern_approach/chapter_2/print_a_float new file mode 100755 index 0000000000000000000000000000000000000000..573f63f5ab4915b9cf81d6c65ef0fd6d65e01ace GIT binary patch literal 8440 zcmeHMO;1xn6rDm*P_)$;gVDuv0e&R5Dl7~}14=`Q28cjHNX+ojS7<_CmA(eLYeHPO zFdA1ZUAS;%{0U7o(VfPXf560ruG|nM=sEM|(diR+MmBT9<<6P8bLPG?)75?dtVE-(B+?iBY~fYA4@SokEPihQ&p#0mxVOEA!=@sVLu;=G&(;LL6>r*L>l+ zlnW)V+$b>07sto2zOW`@>i#zaU)f(Nc#AqA%Ga*>4(oMd)1ur`R=m}Mmy-oAUqWJ( zuS@eyYZBrphsn=--B3J3gOh{u=Gf>&aJ#s_rh5(VS4X}#I&(NB{H3z)VU1jS+U^a)W@B|G?CAg5R3S>TJn)TRr* z0nBgW-Y=F!Cp=v#ucVg?^XXi5IR{KT31=P8KCZr~ z_VnFe=--$-A$`ukP&wz~bfb>4CXosp9$lPIsy|&q?aV zJv1}&+o{a}BK@bwJ1F1d3#cSUp*=L()Ugx(rE kbTFnTVgWgMk1HJX-iX + +int main(void) { + float amount = 2.52f; + printf("amount: %.3f\n", amount); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/chapter_2/vol b/2025/c_programming_a_modern_approach/chapter_2/vol new file mode 100755 index 0000000000000000000000000000000000000000..407ffe69d41a448f058068312c7991f54e7ce4a8 GIT binary patch literal 8480 zcmeHM%}*0S6o2JIp@JeL{tZjLhm&YJs<-f(LrbeD#TDvUpyJVG053k3Dt(%MvU_Tg+h$PM@MQ)(IyDIq5FRn|D(9?yf} zMZDe<=HGwL>bt$#KX4`9+g~{qkr6l-VirRlwlb$&nLkTZl6x9N7BXj4Y#}~{nD;yY zT|~_9>>DiS_dtiBe&JeA{dvFt(FnX(HMtiSQ}P{l7M((8)*kOn&t=nU{tzsmlKSvXfFFps} z60Od{AX<{9xQR>_?ph?;$nR?{(iZ=TfQQ@hP5%&W<-cvnyuo~HxAgDAZ;P+<0@NnU zaIPUrzQqpLQL$9F%3pQ5oS0vUjxGOge;FqF-Tn$AODqODW6R$Yu^B7n8n$D&mT44H zX}jnc`HV4_FBS}E+A<4fcQ{P#847d3{d~4KWpY7E%^EYdYk3RXJGwHA#g;eXyFCnL z@mszpzQ-6WEC20ciOI1#@LOmBN>sfPgq7z~EzzE4Kr^5j&Gy|Fe&A^UgAX-xY z@RfGdXX%g83}^;41DXNNfM!55pc&8%Xa+O`ngPv#W?+i}10MG(N1@y^@;86{Z9wk* zc_RHNx~{(FA+HMoAhZvTm`2yy&5&>=Qn?J0f;cTVsHbu>JTYYnbJmtQ7igZDDcW7p~@zVzaJWb!+kA|x!(oLu9PDf{V{(J_qRN2ptJn + +/* + Write a program which computes the volume of a sphere with a 10-meter radius + using the formula + + v = 4/3 * π * r^3 +*/ +int main(void) { + float r; + printf("what is the radius of your sphere?\n\n"); + scanf("%f", &r); + float pi = 3.14; + float v = pi * r * r * r * 4 / 3; + printf("\nthe volume of a sphere with radius %.2f is %.2f\n", r, v); +} diff --git a/2025/c_programming_a_modern_approach/chapter_3/date_input b/2025/c_programming_a_modern_approach/chapter_3/date_input new file mode 100755 index 0000000000000000000000000000000000000000..91829392ae4731548092d9dd23d9924b9dcacee0 GIT binary patch literal 8480 zcmeHM&ubGw6n?SMR;?y@5JeAjFrp$&tyNSgwA#=u{YigFDag3(Zkj-|Dai&KPk|yR zrFas0EZ#f_{SyR5kAi=Jpn{-64jzh#^?S3k?I!9?dYVVyzWLtFy!YmtAr#)*AK!kp z62)4GXdg7m8Yg-{0wbc6Pzx&Mc;7%~HZw8C-bRTv!oMPp^AHQAoXX5hH7-&6IdN1p z-VYxuN>U=F?=Jfqz%<8O!a|OBTQ6Z8?p3Zh)%27yQkMP7TzSXU9PhP`*REHDINZ>t z;~mfrDd)?6Deo=p;KuX5)A5G%JmaYF9YjUSigz#XIWq5Mi7^{%)e0$|;C)@Lbk72iytFIZr@0fSLL_Ec*{b$DvW- zwV_7+(eZoH^uF}uQCLh7zngTWTuLwI=h9Af(E(;T1?4*2&B6Pv6IY%;{qpg{ll5nJ zyWkVh7?dc1;0ZNW+{YisFTktLr{;;y;d~s2-+5Fx?=>jT1w0R^m$Fj}>Y1#TeYenY zvjcVI=Yrj5VL1M}AZmj>uki-(|Bz|x(I;!=Yw}IzCx^4vdWK;(>GSmf@td;NDqGgt z^ml7*q>h;sT&tD(>h#v?2z))W!3M7G(hFGHFkl$iV+N8n{ZDCa zj}x18h5^HXVZbn87%&VN1`Gp+0mFb{z%XDKFbwPw0|I%t_c;UQzE-qB6>TeWFEJD~ zPNIt&X-4wC7y`tOVIY^#1^2Tg%k#FEg;Nr{Re~0(qkFec7IM;EcIPX;D|4>xxFtGR zu=5@rL0^{JdwO{y79`>L$Ds)f!ahIu + +/* + write a program which accepts a date from the user + in the form mm/dd/yyyy and then displays it in the form + yyyymmdd +*/ +int main(void) { + int month, day, year; + printf("month, day and year in the format mm/dd/yyyy please: "); + scanf("%d/%d/%d", &month, &day, &year); + printf("\n%d%02d%02d", year, month, day); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/chapter_3/printing_nums b/2025/c_programming_a_modern_approach/chapter_3/printing_nums new file mode 100755 index 0000000000000000000000000000000000000000..00d0d0750d4fc60bfb9a706801986e5f626aa595 GIT binary patch literal 8440 zcmeHMO-oc!7(Sz8saZ3%h@yhKndpP&BNL1Y)aK$y`B5r}5XXV%FpAEYqBfVik3 zYUwX%-M-C$ffYp2sy`68sFf~;km`BPz1O)j8MtT@oEP8wzRx-DdG70T?yTOAKfeDw zD1@(32+;r!!YBMftO^5}5GUaYxRgWPz2>-icaW>K=BtH!BJzAep_C)$*hp=OI?oq- z&B0ArbDi5#=B%Y$wXmMAjNZw&=#&uSuWEi#tOD|7bNQ+4?ovHp6fY;=0jI*n?tx~9 z&)t`DdNG}?HdxQs>F~Kd5mWcS8ThifOe#I=6x8!wcla(k`@~+W=aw>`ew0clWhy;G zjq3U49KJD!gt(r=%g=jVQ#|qRk#2c&aA4TJUEE*Iy}I}7M1F5{)^JJ6*~O}A#C!8K zqgELX@w{u)U%k&+sD8J%`vz{A@jkmNA&k1dfN2#wC=M^r6(NeMWdpwp)U-Qz0yqjx zZBD?~fZ5L{#B+#;;6re)v6k-T_wF-)YwWG}5$s`0w810!Y$h_Fnu;Ws=aay+Q*ici zzvuOt%<9$kr$dh$UT*DNyNophZoe~tA45RpN_+ov{{`%+{|}Ld-izcZyC0}pwcp!` zoO|~7(5t?b-Coeo@Nzb1Erff*=qoj4A3Tdd{uhQ2yu0%bre6QTMiWoICRV-$H_RQ< zZI~6RzZD2JjjMRn+%DxSrO`^MzY;H*mBh0fCN^G)O)7&rb9*Z=UPjW- zC#e5v9)x)#wWQWvi1Y|lrFE%i^}+jU1541kNkIEbt0g+q3TOqi0$KsBfL1^&pcT*x z>{o%}ehYP-RzNGD70?Q31+)TM0j+>mKr5gX&mnuGdxshIna_1FtN_fs9K*C$Cz-vQIs)EYmf_b(ps{n^Epe-S(8aCBU%{gpf6_}u?``WhCa1S7 literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/chapter_3/printing_nums.c b/2025/c_programming_a_modern_approach/chapter_3/printing_nums.c new file mode 100644 index 0000000..4248a37 --- /dev/null +++ b/2025/c_programming_a_modern_approach/chapter_3/printing_nums.c @@ -0,0 +1,13 @@ +#include + +int main(void) { + int i; + float x; + + i = 40; + x = 839.21f; + + printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i); + printf("|%10.3f|%10.3e|%-10g\n", x, x, x); + return 0; +} diff --git a/2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types b/2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types new file mode 100755 index 0000000000000000000000000000000000000000..cec053b84a7e7fb829d04ce2b7d7daf50bbd466b GIT binary patch literal 8480 zcmeHM%}bO)7@t?SGPCxjLlgzQMB69$Q4taZYA(K3e&7d)5RMj2SFWrTNs7ukciy9E3&!DEwJIVC%Ic4l*<5}+yO*lQTi19EdPW$B3+go9 z;XS;GY;G={ncRyj&-+Q^g~w%V3h%%aDf5{p=}bbVGszhYRM)qm@kaC_#?=@m9`9jU z;dHhSwaa^by@MsHP*BA^gavBL_d(?xrlg#lF|Gme{&*n_7jb)<;;CXwW7<{uTS64no}LgXWNzv>g}50p_dE?2G*d?@JG!fW?sDyJ^a2=bEO{<4uW$sRUxmSt#r17?^o{`OBT=r`O+{|M2bg=Nrg* zPzNeR6yQ!*^~9_icr8ti*E{enw8Y7SuDikj(|tjUA1s zD?MIXeF=v6%bpN*uvc}wg7|;Pu>az#zxdsK7ym^#f9dXhW%4`z((tyw)Kgp)n=MFf zMcw$$R&;djWquoePZ9omOuvA!)tXJ#*0Sa=uDg%k?#6E8`gUWj_{Ng4L@WtYA4}B3 zA&=*v6w{Iua@Gb*UFsSOzQumI2FvWxz6E8TdyAJj`S7a|Fu1nAzYm z+luTZhN65IU0kCX$#o6@oRe_mEV|&iq>$N(u}l(4DVSCp)QTedcQtZ4C4>23BA*Ln zDi})ybK>aqSUMw)qc6(_pPn9dN}CY>6f_Ely*tM2>A})fauHuM=6Tq|3$2Gv_21h{ KwQY6D1pEV#^QJ!l literal 0 HcmV?d00001 diff --git a/2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types.c b/2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types.c new file mode 100644 index 0000000..6c98912 --- /dev/null +++ b/2025/c_programming_a_modern_approach/chapter_3/scanf_mixed_types.c @@ -0,0 +1,8 @@ +#include + +int main(void) { + float a; int b; float c; + scanf("%f%d%f", &a, &b, &c); + printf("%f\n%d\n%f", a, b, c); + return 0; +} diff --git a/2025/checkmark/a.out b/2025/checkmark/a.out new file mode 100755 index 0000000000000000000000000000000000000000..211cee38b11f15cb0ac335d2543b96e59df25969 GIT binary patch literal 8440 zcmeHMOK4L;6rHqDTdgLD_(74`R5Xjm-$D_=XhTyhZLtxGDAT5SZ6CI2X+OA zsyA26sY^qHBc>bByP*$pKkUl)L1zuOl$x4z+VD@l4%BMHA)b$odg<@kjX&>fpnvdk zzPCSqBeLmUz_jmX(Ht(%X(8(R%2J3i)SR+-4EQ)Owb>6}1Ll2RV?By>2YeXr8f)n; zznf?LQf{vIF659BUGQwJx{#d-OWE?$Oc|JV5U%gB=lz3~CubktUj6p&@w6(Q#J+@^ zZ>I2gQ##h#C+{aS$m!pQm4>_5$-{T<8ozr6&YXD-W{`;di@W(TDbeMu>3jwBL9A&BK(nb>}7PIv)n-dV;>GN zfVnF*rREo~(%)G98N0^5b%75L0QHugK9gLTQ1<^S>9#+Iop$p>j hRKk#xcP~7JAfE4Y76-_bHqz52CeeS>D^2fSYaKK=p8Eg* literal 0 HcmV?d00001 diff --git a/2025/checkmark/main.c b/2025/checkmark/main.c new file mode 100644 index 0000000..c5819df --- /dev/null +++ b/2025/checkmark/main.c @@ -0,0 +1,11 @@ +#include + +int main(void) { + printf(" *\n"); + printf(" * \n"); + printf(" * \n"); + printf("* * \n"); + printf(" * * \n"); + printf(" * \n"); + return 0; +} diff --git a/2025/main.c b/2025/main.c new file mode 100644 index 0000000..7edf22f --- /dev/null +++ b/2025/main.c @@ -0,0 +1,11 @@ +#include + +int main(void) { + /* write a program that computes + the volume of a sphere with a 10-meter radius + using the formula v = 4/3 * pi * r * r * r + */ + int r = 10; + float volume = 4 / 3 * 3.14 * r * r * r; + printf("volume: %.2f", volume); +} diff --git a/2025/making_a_c_compiler b/2025/making_a_c_compiler new file mode 160000 index 0000000..8eaddcc --- /dev/null +++ b/2025/making_a_c_compiler @@ -0,0 +1 @@ +Subproject commit 8eaddccf913f02b26586d6fd42fdbe527c719bc0 diff --git a/2025/notes_how_i_program_c.md b/2025/notes_how_i_program_c.md new file mode 100644 index 0000000..8eb1dd4 --- /dev/null +++ b/2025/notes_how_i_program_c.md @@ -0,0 +1,677 @@ +# from YouTube: Eskil Steenberg https://www.youtube.com/watch?v=443UNeGrFoM&pp=ygUNaG93IGkgd3JpdGUgYw%3D%3D + + +- uses C89 because C99 is "too new and too broken" +- doesn't use + - goto + - register + - do + - continue + - auto + - volatile +- 0 unwrapt dependencies + - this might mean that he doesn't use dependencies at all + - "i wrap everything so I have 100% control over my code" + +- his code is durable, "i want my code to last forever" + +- "really important to read your code and understand it" + - there can't be any misunderstanding between how the compiler views your code and how you do. -> no ambiguity + - therefore C++ is a terrible language + - tries to be clever, which is the root of all evil + - tries to hide things; but things should be explicit + - overloading is bad + +# Good Tooling + Simple Language = 👍 + +- e.g. he generates documentation websites of his C projects using his own tool, which parses C +- it's harder to make cool tools in more complex, heavier languages + +# Naming + +- long names are good; it means you're being descriptive +- being descriptive is good! +- snake case > camel, but camel might be more differentiated from vars (if you use snake for vars) +- func() > func () + +# Functions + +- less flipping around +- write code that does something (no handlers, managers, controllers) + - ~ verbs +- WIDE and LONG == good + - you can track the state if it's all in one function +- _really_ long functions are great, you can follow it step by step + +## Function Names + +- module first, file second, object third, verb last + - `vast_db_session_get` + +# API Design + +- create the API you want, then fill in the implementation +- one "entrypoint" (?) "external" file e.g. `main.h` which contains everything from the library modules + - not `main.c` and `main.h` + - put internal things into something called `*_internal.h` + +## Object Orientation (!) + +- no such thing as object-oriented on the machine level + - data + code is impossible at the machine level + - stops malicious attacks + - all memory is write-protected + - "objects are fundamentally wrong" + +In C, + +```c + // can't and _shouldn't_ do + + object thing(); + thing.do_something(); + + // this is the way + + thing = object_create(); + object_do_something(thing); + +``` + +For internal functions being exposed externally, force their usage via void pointer, by exposing them as such. + +```c +// external module +typedef void RShader + +extern RShader *shader_shader_create(...) +extern int shader_shader_make_opaque(...) + +// internal +type def struct{ + ... +}RShader; + + +``` + +# Macros Are Mostly Bad and Dangerous + +Even the first example below has problems: The debugger won't be able to help you. + +## Except + +### Duplicative code for different types + +```c +func(float *array_a, float *array_b, uint count) { + uint i; + for (i = 0; i < count; i++) { + array_a[i] *= array_b[i]; + } +} + +// but you want a bunch of different versions of this with different types +// e.g. double *array_a, etc. +``` + +### __FILE__ and __LINE__ + +These interpolate as the filename and the line number. + +This allows _easy finding of memory leaks_!: + +```c +extern void *f_debug_mem_malloc(uint size, char *file, uint line); +extern void f_debug_mem_print(uint min_allocs); +// goes through the registry and makes sure the allocations are intact; if not, it gives an easy to read list of stomped memory +extern boolean f_debug_memory(); +// if you have an allocation that happens a lot of times, you can comment on each allocation so the registry entry has more detail. +extern boolean f_debug_mem_comment(void *buf, char *comment); + +#ifdef F_MEMORY_DEBUG +# define malloc(n) f_debug_mem_malloc(n, __FILE__, __LINE__) +# define realloc(n, m) f_debug_mem_malloc(n, m, __FILE__, __LINE__) +# define free(n) f_debug_mem_free(n, __FILE__, __LINE__) +``` + +Creates a registry of what _file_ and _line_no_ allocated or deallocated memory! (and other things, see comments) + +He turns it on ~ once a week for debugging. It slows the execution, so not for use all the time. + +### Debugging Data Packing/Unpacking + +In binary files/network protocol stuff. + +- add a `char *name` to your `pack` and `unpack` functions +- has a flag enabling a macro to actually use `name`, so it's only on when trying to debug packing/unpacking or callers thereof + +```c +#define testify_pack_uint8(n, m, l) testify_pack_uint8_internal(n, m, l, __FILE__, __LINE__) +#define testify_unpack_uint8 ... +``` +# Only Use Unsigned Integers + +- 'nuff said, use them by default + +# Memory + +Understanding and having control over memory is a good thing, even if you don't program C. + +## If Something Exists, It Exists _Somewhere_ + +A thing can be described by "what" as well as "_where_". _where_ is a pointer, which is a number. It's like a street address. + +### Pointers + +Why do pointers have types? + +Because you can't _use_ the pointer if you don't know what/how long the thing is it's pointing to. + +```c + +void *p; // untyped, this is fine as seen before (internal objects) +short *short_pointer; +int *int_pointer; + +a = *short_pointer; // reads 2 bytes as a short +b = *int_pointer; // reads 4 bytes as an int + +short_pointer++; // adds 2 bytes = sizeof(short) +int_pointer++; // adds 4 bytes = sizeof(int) + +if (short_pointer != int_pointer) + printf("Not the same!"); + +``` + +Pointers point to one thing, but they sort of also point to the _next_ thing. They's also _kind of arrays_, they're pointers to the starting point of a street/array of addresses (midpoint really, you can go forwards or backwards). + +## `malloc` + +```c +double *a; + +a = malloc(sizeof(float)); // looks like a function, but malloc is an operator in C. this is typically how malloc gets used, but below is better. + +// better to write it this way, because it's more of a _cast_ + +a = malloc((sizeof *a) * 10); // this respects what it _is_ and it also is dynamic, since if `a`'s type changes this will remain correct. + +sizeof (float)a; // this casts `a` as a float +``` + +## Arrays + +- taking a pointer, stepping forward, then reading what's there + +```c +a = x[3]; + +// equivalent to + +uint *p; +p = x; +x = p + (sizeof *x) * 3; +a = *x; +``` + +## Counters + +```c + +// these are equivalent, but the bottom one is faster +// because the first one requires multiplication +// (or not, because compilers are sophisticated) + +uint i; +for (i = 0; i < 10; i++) + // p + (sizeof *p) * i + p[i] = 0; + + +void *end; +for (end = &p[10]; p != end; p++) + *p = 0; +``` + +# Structs + +Don't fool yourself that the computer knows the names in a struct! It's just a bunch of offsets. + +```c +typedef struct{ + uint type; + char name[32]; + float size; +}MyStructType; + +// "scary code" to get the offset of size +offset = (uint)(&((MyStructType *)NULL)->size); +``` + +## Object-Oriented Inheritance + +```c +typedef enum{ + MST_INT, + MST_FLOAT, + MST_COUNT +}MyStructType; + +typedef struct{ + char name[32]; + MyStructType type; +}MyHeader; + +typedef struct{ + MyHeader head; + int value; +}MyStructInt; + +typedef struct{ + MyHeader head; + float value; +}MyStructFloat; + +void my_set_value(MyHeader *h, double value) { + if (h->type == MST_INT) + ((MyStructInt *)h)->value = (int)value; + else + ((MyStructFloat *)h)->value = (float)value; +} + +// a pointer to MyStructFloat _is_ a pointer to MyHeader, which is _also_ a pointer to MyHeader->name +``` + +## Alignment + +Computers like to store things on even byte addresses. + +```c +typedef struct{ + uint8 a; // 1 byte + uint32 b; // 4 byte +}MyStructType; + +sizeof(MyStructType) == 8 // we expected 5 +// the compiler (computer?) stores `a` with three bytes of padding +// this is true even if you reverse the order + +typedef struct{ + uint8 a; // 1 byte + uint8 a; // 1 byte + uint8 a; // 1 byte + uint8 a; // 1 byte + uint32 b; // 4 bytes +}MyStructType; + +// uses exactly the same amount of memory as the one above + +typedef struct{ + uint8 a; // 1 byte + uint32 b; // 4 bytes + uint8 x; // 1 byte +}MyStructType; + +// sizeof (MyStructType) == 12 + +``` + +The takeaway is that you should try to order your struct attributes to land on even/factors of 4 of memory. + + +### Manual Packing + +```c +uint8 *memory; +MyStructType *s; + +memory = malloc(5 * 10); + +for (i = 0; i < 10; i++) { + s = (MyStructType *)&memory[i * 5]; + s->b = i; + s->a = i; +} +// this may be slow or even cause a crash +``` + +## Memory Blocks + +Memory is one big array, BUT it's divided into blocks of 4k. + +Each block has read/write/execute flags. + +Each address isn't the *actual* address, they're virtualized. + +"This piece of memory is for this program; this piece is for that other program. If an application tries to write into memory allocated for another application, I will _crash_ it." + +Since memory addresses are virtualized, you can rearrange them. + +### `realloc` + +`realloc` takes a piece of memory and reallocates the _size_ of it; the contents remain the same. + +It's sometimes seen as bad because it may require copying of memory contents. However, since memory addresses are virtualized... + +#### `gflags.exe` + +If you allocate one byte of memory, the processor will assign you an entire block (4k bytes). Then if you ask for ten bytes more, it'll allocate from the original block. You allocate some more things, and the memory block looks like this: + +---------------------------- +1222222222222233333344444444 + +Then you allocate a fifth thing, and you are allocated a second block of 4k: + +--------------------------- +55555555555555555555555 + +Let's say `a1` is the first var, which only has one byte, then `b1` is the second one, and they're stored contiguously. + +Then you try to do + +```c +a1[1] = 0; +``` + +The computer isn't going to stop you because you're overwriting memory in _your_ application. + +```c +{ + uint a[10], i; + for (i = 0; i < 11; i++) + a[i] = 0; +} +``` + +This will become an infinite loop because `i` when initialized is probably going to be directly after `a` in memory. So when `a[11] = 0` happens, `i` is overwritten as `0`. + +For this reason "I'm always scared when I put an array on the stack." Just be aware this can happen, it's okay to actually do it. + +You can tell Gflags: "Every time I do a new allocation, give me a new block." It puts every item in its own block _separated by a 'kill block'_ which you may not touch. + +When you do that, your computer won't work very well BECAUSE THERE ARE MEMORY LEAKS EVERYWHERE. + +But turn it on every few weeks, run your application, and see if it crashes. + +### Memory is Slow + +Accessing data: + +- register: 0 cycles +- level 1 cache: 2-3 cycles +- level 2 cache: 10-15 +- main memory: 50 cycles + +It's gotten _worse_ over the last 30 years and will continue to do so. + +```c +// this might be faster +a = a * a * a * a * a * a * a * a * a * a * a * a * a * a * a * a * a; +b = b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b; +c = c * c * c * c * c * c * c * c * c * c * c * c * c * c * c * c * c; +d = d * d * d * d * d * d * d * d * d * d * d * d * d * d * d * d * d; + +// than this + +b = *a; // memory access +``` + +#### Linked List + +They're the worst thing you can do for memory coherence. You're jumping around in memory, so lots of cache misses. + +```c +typedef struct{ + void *data; + void *next; +}LinkedList; +// uses twice as much memory as the other options here because it needs +// -> next + +// not memory coherent +for (l = list; l != NULL; l = l->next) + do_something(l); + +// memory coherent (adjacency) +for (i = 0; i < array_length; i++) + do_something(&array[i]); + +// realloc rarely when adding +if (array_length == array_allocated) { + array_allocated += 16; // allocate 16 more values + // then reallocate (every 16 times) + array = realloc(array, (sizeof *array) * array_allocated); +} +// operting on this piece of memory is very fast, everything is adjacent +array[array_length] = value; +array_length++; + +// remove +array[i] = array[--array_length]; // fast + +// if you need to preserve the order, backwards remove is still faster than an LL + +for (i = array_length; i != 0;) { + i--; + if (array[i] == the_one_we_want_to_remove) + break; + last = save; + save = array[i]; + array[i] = last; +} + +// usually you don't need to preserve the order though + +``` + +# Don't Store Data Twice + +```c +typedef struct{ + float width; + float length; + float area; +}MyPlane; + +// a bug waiting to happen because one of the attributes could be updated without one of the others being updated +// ideally, just don't store `area` since math is "free" as we've seen + +// but if you must, + +typedef void MyPlane; + +void my_plain_width_set(MyPlane *plane, float width); +float my_plain_width_get(MyPlane *plane); +void my_plain_length_set(MyPlane *plane, float length); +float my_plain_length_get(MyPlane *plane); +float my_plain_area_get(MyPlane *plane); + +// this is safe because the only public interface is this set of functions +``` + +# Smart Allocations + +## Array with Length + +```c +// typical way of storing an array with length + +typedef struct{ + uint length; + uint8 *data; +}MyArrayOfBytes; + +MyArrayOfBytes *array; +array = malloc(sizeof *array); +array->length = length; +array->data = malloc((sizeof *array->data) * array->length); + +// the flaw is that the struct and its instance may not be contiguous +// therefore slow + +// 🚀 better: + +typedef struct{ + uint length; + uint8 data[1]; // in c99: data[]; + // if you add any additional members don't put them here! put them before `data[1]` +}MyArrayOfBytes; + +MyArrayOfBytes *array; +array = malloc((sizeof *array) + sizeof(uint8) * (length - 1)); // because there's already one in `data`; in c99 no need for ' - 1' +array->length = length; + +for (i = 0; i < length; i++) + array->data[i] = 0; + +// only one allocation! it's faster + +``` + +## Instantiate Two Structs + +Only one allocation! + +```c +typedef struct{ + uint64 a; + float b; +}MyStructA; + +typedef struct{ + float x; + uint64 y; + double z; +}MyStructB; + +MyStructA *a; +MyStructB *b; + +a = malloc(sizeof(MyStructA) + sizeof(MyStructA)); +b = (MyStructB *)&a[1]; + +// this could be dangerous because the alignments might not be right, depending on the machine +// however, it can boost performance +``` + +## Stride + +```c +// requires rgb_pixels to be adjacent to each other +void rgb_color_correct(uint8 *rgb_pixels, uint pixel_count); +// more flexible +void rgb_color_correct(uint8 *rgb_pixels, uint pixel_count, uint stride); +// how to call +rgb_color_correct(rgba, pixel_count, sizeof(uint8) * 4); + +typedef struct{ + float size; + uint8 color[3]; + char name[16]; +}Thing; + +Thing t[10]; + +rgb_color_correct(t[0].color, 10, sizeof *t); +``` + +# Don't Be Afraid to Write Code: Build a Mountain + +"Don't implement anything, everything is hard." == conventional wisdom + +aliases: "Don't write your drivers, don't write an operating system." + +Better: "This is something I don't know how to do; therefore I should be doing it." You learn by implementing things. + +When you wrote the code, you can fix it/change it, you have an _expert_ == YOU. Super valuable in the long run. + +## Concrete Advice + +- build huge technology, small applications + E.g. if you build a music player, + - build an interface library (tech) + - build a sound library (tech) + - then build the music player (app) + +## Example: Apple + +They built the mountain: + +- QuickTime -> iTunes + -> webobject -> iTunes Store + +- OS X -> OpenGL + -> iOS + -> App Store + +- Safari -> iPhone + +# Fix Your Code NOW + +- don't fix it later; it snowballs and will be harder later +- have zero technical debt +- if you deliver things quickly at work because you hacked something together, it will create an expectation + - give them very little at the beginning + - only show your manager what's finished, debugged, ready + - then they'll have a realistic idea of what you can build + - (don't spend forever, but write it right from the beginning) +- really important, will always pay off + +# How To Do UIs, Applications, etc. + +Widgets can either 1) draw 2) have an event or 3) update itself, which all happens inside `input`. + +So you can have a button which can be called many times for different reasons. + +```c +void my_widget(BInputState *input) { + if (input->mode == BAM_DRAW) { // draw widget } + else if (input->mode == BAM_EVENT) { // deal with events } + else if (input->mode == BAM_MAIN) { // advance time } +} + +// most UI libs work like this, it's annoying and object-oriented +// lots of ids to keep track of etc +void button_func(void *user) { printf("button press"); } +void init_ui() { + element = element_create; + id = element_button_add(element, x, y, "hello", button_func, NULL); + element_surface_add(element, x, y, size); +} +main_loop() { + element_move(element, id, sin(time), cos(time)); + element_run(element); +}; + + +// nice to have +// problem: this looks nice, but it can't be done +// I don't know if I'm going to draw a surface after the button press +// can't switch the order either +main_loop() { + if (draw_button(sin(time), cos(time), "hello")) + printf("button press"); + draw_surface(x, y, size); +} + +// Better: +main_loop() { + if(draw_button_add(id1, sin(time), cos(time), "hello")) + printf("button press); + draw_surface(id2, x, y, size); + + // the `id`s are pointers and can be to ANYTHING. + // turns out you usually have a pointer: + + slider(&value, &value, x, y, size); + + // if you need an id: + + malloc() + + // the id could even be a string, because everything has an address, and therefore pointers can be `id`s + +} +``` diff --git a/2025/number_guess/a.out b/2025/number_guess/a.out new file mode 100755 index 0000000000000000000000000000000000000000..6f6cb63d1fc19cba3102d21fa3e4b198fb2c06fd GIT binary patch literal 8664 zcmeHN&ubGw6n<-~w)IDXia$_vMJigPu~o2CidMTAskEi7P?R#H&9;p+sbn{(2O&@e zLlN<&Cq)l>5d}|zhe8YWrbqn)l%Q35C|E@rzc)KuH(L)PUW9q@=FRtJ=IxtrhOo)Y z>+hczR}&c(M6?1LWvwK-P9}Op+n{l%5M8aOtaDa(Cwt4qD2KmA9Onim3ejuz^_EYO z>-oV!QFtYMtVl_U5UzdMRRAU&?;<90yoYKEV}Gr5RZ?CJNh5^g<_4T4S2*4#Je=e0 zR5N_+FQ`)SRw;)NgN~a`Wrmk<`Fw;gsz3ga!xijni_~DuZ$*vS++wtkLQn{ zEIyiQid`Rnb1@YtC3d7#3faN~MCeFk{W5Q<}&v#Ba7-z`!#q zS}+sV{84McJq+)l=Z(R{Z(c28!a?p%Aq$_GoTsJ3}^;41DXNNfM(#Y7>MS*m&<6L-%s%k zAkXigY}MC_yo9>Z3}^;41DXNNfM!55pc&8%Xa+O`ngPv#X5hbNz(nR;`%FT)92G1y zsqg>rdbLE^@%vF!aRbFjUT5G31OsI?0|j&A0JIrSaS;`9cZdYn&PX_ei41__Avcw_ zd7+>!FtS)#L33587T>Q%%~+&Ignii_%(=E0u@gxF!f36(JSD*ylh literal 0 HcmV?d00001 diff --git a/2025/number_guess/guess b/2025/number_guess/guess new file mode 100755 index 0000000000000000000000000000000000000000..08ce5c92a3758205d4e90157e147e6b3ae73b0dc GIT binary patch literal 8712 zcmeHNL1+|L82+b;HLlSt9;{+%$I_Kn!Dtl}t<|(nU5O;Yq!0voZnBfzk?gGO%wPho zEZ9Pq5DLB2;w6+GN-0*vi{L@<5IkAv%}aYoVL*il0i%fP_rG})XUFs)cv0RD|NGzf zzyJOFzwf;)v-5KM@%^VOiHtTPdJCFmZ72GUO!SC0Li11|26~QIC#>WB>}?gJ6)uZ7 z&-VxvV#pdEY7LR|`Gb?9@Cx`?QIZrPe0#!I04AO93<5ddKPrTAJXgBfsjh~k5yJC> z5pT(r&NqM$=X@Wkh#1EcI#j;5l|zV8&#yUdX$iNk*H`%tsd2_h;XO!-5P^Hvaf`xn z$Ep}e*LPLr8&;DTr*kCp@jhITc@Fms^@uO}`v#-k0N*Y95buYwd_Gt?hFwCGs)=m? zUOwLr#7e;=o*xVI6?4YE=jZI$(Y{{m@X`2;n5nveSvu4rxgJ@!^b+kRs>_niXq~$h zz5vYUn5S)khQO;}f7?uS3U)Pg0GgC|&B;2GqA^)w;)>k?!K; zcoCRo6O`+ix%KHo@6MicKVItWoy}kD{RBP}YCws)aCp09Uvl66L;F^E<@)3}(MMRH z%+qqnqtD%o7#@qLIwDWXQZ1-waMJVb%J$ER z&LhZmcQwF-0{N*R#HAgsc+^&xn<4V?-`;L zUXkrpv{Wm&#cHK5sO&Ry#SE1K+w%@)J~Js-q_R^g(|Gl4fV+ONJ_xSte0OP$D)Ri_LvI&R4z-Pa6g1~dbj0nLDBKr^5j zc!L?p)}sf&Y@MGW@e82N&xmaCI4P}ql&njr8_j@bKr^5j&Gy|Fe&46Y=GoTsJ z3}^;kI|fWt%y*wi#6c}6 +#include +#include + +#define MAX_NUM 100 + +int generate_random_num(void) { + srand(time(NULL)); // seed rand + return rand() % MAX_NUM + 1; +} + +int main(void) +{ + int random_num = generate_random_num(); + int guess = 0; + char* high_or_low = ""; + printf("randomNum: %d\n", random_num); + while (guess != random_num) { + printf("guess?\n> "); + scanf("%d", &guess); + + /* TODO: an interesting bug is that if I hit at this moment it goes + into an infinite loop of printing this prompt over and over without taking + any input. The same thing happens if you give it a float as the input + instead of an int! These loops are only terminated with */ + if (guess > random_num) { + high_or_low = "high"; + } else { + high_or_low = "low"; + } + printf("guess is %s\n", high_or_low); + } + printf("you got it!\n"); + exit(0); +} diff --git a/2025/uninitialized_vars/a.out b/2025/uninitialized_vars/a.out new file mode 100755 index 0000000000000000000000000000000000000000..d4b98ca29e9f0f56e187e29e6fe71c471b3fb08d GIT binary patch literal 8440 zcmeHM%}Z2K6hD(weq_ziB8pl(0;3PmzMwAxEhk3|l~fWTu8#B60cSMl4e4$`f+C`p zE`s_K+D9N1)UIe#t9F5bfnW;>seZrv?#nxqv?|=(1BY|Zx##}wdB1bt>b(8_^+&Bp ztVTq(L6g+D$Q|*p64?(;LzU`n>qrfyuJ-%;*FaW`q5b(ic8sSX`@szC}oPjL)Ca64c-ou5&6-9I)fMO zD>YgSW^#q-{%X8W2JfH&pl=I%V2e_v!tGokt8#_0DQr~ZtsA^SbBKO5hK<0L*=tI;GWBER)%wb5W@hPhf@HfY+L#`Ga;`vC(m%h&gHs0B_&hD;M zduQ}UysEn3Q`SPGF>IVOB4u5&Co+tj)5af$e*!+a*#}*K&wAd$9)aBn?SLul#91+OXFAeqo)Va6K74^7B% z^dH)me38#qSn3DEE!VcNCyn4VX}SgXZ%@TO>eh|{$ADwNG2j?*3^)cH1CD{MWT3p2 zm%FP9{7mU})3TBGiRDMGFv;Jr)@YRGrlJ#e#K0lHx$S% + +int main(void) { + int a; + int b; + float d; + float e; + printf("a: %d\n", a); + printf("b: %d\n", b); + printf("d: %f\n", d); + printf("e: %f\n", e); + return 0; +}