From 4dfe8d367922819f533e94f62fd7c004d2644436 Mon Sep 17 00:00:00 2001 From: "hengxin(Hengfeng Wei)" Date: Thu, 31 Oct 2024 13:38:29 +0800 Subject: [PATCH] 6-data-types/ --- .idea/codeStyles/codeStyleConfig.xml | 5 + 6-data-types/CMakeLists.txt | 27 +++ 6-data-types/README.md | 23 +++ 6-data-types/char.c | 16 ++ 6-data-types/compare.c | 28 +++ 6-data-types/exact-width.c | 10 ++ 6-data-types/explict-conversion.c | 18 ++ 6-data-types/float-limits.c | 29 ++++ 6-data-types/for-unsigned.c | 14 ++ 6-data-types/implicit-conversion.c | 35 ++++ 6-data-types/int-limits | Bin 0 -> 15968 bytes 6-data-types/int-limits.c | 35 ++++ 6-data-types/integer-promotion.c | 13 ++ 6-data-types/loop.c | 17 ++ 6-data-types/precision.c | 23 +++ 6-data-types/signed-overflow-fix.c | 162 ++++++++++++++++++ 6-data-types/size | Bin 0 -> 16016 bytes 6-data-types/size.c | 35 ++++ 6-data-types/sizet.c | 22 +++ 6-data-types/sum-product.c | 25 +++ 6-data-types/timing-primes.c | 39 +++++ 6-data-types/timing.c | 25 +++ 6-data-types/unsigned-wrap-fix.c | 90 ++++++++++ 6-data-types/unsigned-wrap.c | 16 ++ 6-data-types/unsigned.c | 19 ++ 6-recursion/CMakeLists.txt | 2 - 7-recursion/CMakeLists.txt | 24 +++ 7-recursion/README.md | 30 ++++ 7-recursion/bsearch-iter.c | 57 ++++++ 7-recursion/bsearch-re.c | 47 +++++ 7-recursion/fib-array.c | 22 +++ 7-recursion/fib-iter.c | 27 +++ 7-recursion/fib-re.c | 25 +++ 7-recursion/gcd-iter.c | 29 ++++ 7-recursion/gcd-re.c | 34 ++++ .../hilbert-curve-text.c | 0 .../hilbert-curve-ui.c | 0 7-recursion/main-re.c | 22 +++ 7-recursion/merge.c | 40 +++++ 7-recursion/mergesort.c | 89 ++++++++++ 7-recursion/min-re.c | 29 ++++ 7-recursion/min.c | 22 +++ 7-recursion/n-queens.c | 69 ++++++++ 7-recursion/sum-re.c | 29 ++++ CMakeLists.txt | 3 +- 45 files changed, 1323 insertions(+), 3 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 6-data-types/CMakeLists.txt create mode 100644 6-data-types/README.md create mode 100644 6-data-types/char.c create mode 100644 6-data-types/compare.c create mode 100644 6-data-types/exact-width.c create mode 100644 6-data-types/explict-conversion.c create mode 100644 6-data-types/float-limits.c create mode 100644 6-data-types/for-unsigned.c create mode 100644 6-data-types/implicit-conversion.c create mode 100644 6-data-types/int-limits create mode 100644 6-data-types/int-limits.c create mode 100644 6-data-types/integer-promotion.c create mode 100644 6-data-types/loop.c create mode 100644 6-data-types/precision.c create mode 100644 6-data-types/signed-overflow-fix.c create mode 100644 6-data-types/size create mode 100644 6-data-types/size.c create mode 100644 6-data-types/sizet.c create mode 100644 6-data-types/sum-product.c create mode 100644 6-data-types/timing-primes.c create mode 100644 6-data-types/timing.c create mode 100644 6-data-types/unsigned-wrap-fix.c create mode 100644 6-data-types/unsigned-wrap.c create mode 100644 6-data-types/unsigned.c delete mode 100644 6-recursion/CMakeLists.txt create mode 100644 7-recursion/CMakeLists.txt create mode 100644 7-recursion/README.md create mode 100644 7-recursion/bsearch-iter.c create mode 100644 7-recursion/bsearch-re.c create mode 100644 7-recursion/fib-array.c create mode 100644 7-recursion/fib-iter.c create mode 100644 7-recursion/fib-re.c create mode 100644 7-recursion/gcd-iter.c create mode 100644 7-recursion/gcd-re.c rename {6-recursion => 7-recursion}/hilbert-curve-text.c (100%) rename {6-recursion => 7-recursion}/hilbert-curve-ui.c (100%) create mode 100644 7-recursion/main-re.c create mode 100644 7-recursion/merge.c create mode 100644 7-recursion/mergesort.c create mode 100644 7-recursion/min-re.c create mode 100644 7-recursion/min.c create mode 100644 7-recursion/n-queens.c create mode 100644 7-recursion/sum-re.c diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/6-data-types/CMakeLists.txt b/6-data-types/CMakeLists.txt new file mode 100644 index 0000000..9e607ee --- /dev/null +++ b/6-data-types/CMakeLists.txt @@ -0,0 +1,27 @@ +# Objects, size, precision, width, limits +add_executable(size size.c) +add_executable(precision precision.c) +add_executable(int-limits int-limits.c) +add_executable(exact-width exact-width.c) + +add_executable(unsigned unsigned.c) +add_executable(for-unsigned for-unsigned.c) +add_executable(sizet sizet.c) +add_executable(timing-primes timing-primes.c) + +add_executable(char char.c) + +add_executable(unsinged-wrap unsigned-wrap.c) +add_executable(unsigned-wrap-fix unsigned-wrap-fix.c) + +add_executable(implicit-conversion implicit-conversion.c) +add_executable(integer-promotion integer-promotion.c) +add_executable(explict-conversion explict-conversion.c) + +add_executable(float-limits float-limits.c) + +add_executable(sum-product sum-product.c) +add_executable(loop loop.c) + +add_executable(compare compare.c) +target_link_libraries(compare m) \ No newline at end of file diff --git a/6-data-types/README.md b/6-data-types/README.md new file mode 100644 index 0000000..db7cb46 --- /dev/null +++ b/6-data-types/README.md @@ -0,0 +1,23 @@ +# 7-data-types + +## `int-limits.c` + +## `unsigned.c` + +## `timing.c` + +## `char.c` + +## `int-overflow.c` + +## `implicit-inversion.c` + +## `explicit-inversion.c` + +## `float-limits.c` + +## `sums.c` + +## `loop.c` + +## `compare.c` \ No newline at end of file diff --git a/6-data-types/char.c b/6-data-types/char.c new file mode 100644 index 0000000..6b74598 --- /dev/null +++ b/6-data-types/char.c @@ -0,0 +1,16 @@ +// Created by hfwei on 2024/10/30. + +#include +#include + +int main() { + printf("CHAR_MIN = %d\n", CHAR_MIN); + printf("CHAR_MAX = %d\n", CHAR_MAX); + + char c = 150; + int i = 900; + + printf("i / c = %d\n", i / c); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/compare.c b/6-data-types/compare.c new file mode 100644 index 0000000..fdd9022 --- /dev/null +++ b/6-data-types/compare.c @@ -0,0 +1,28 @@ +/** + * file: compare.c + * + * See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ + * + * Created by hfwei on 2022/11/10. + */ + +#include +#include +#include +#include + +#define EPSILON 1e-5 + +bool IsEqual(double x, double y); + +int main() { + printf("%d\n", IsEqual(DBL_MAX, DBL_MAX - 100)); + + printf("%.50f\n", DBL_MAX - (DBL_MAX - 100)); + + return 0; +} + +bool IsEqual(double x, double y) { + return fabs(x - y) <= EPSILON; +} \ No newline at end of file diff --git a/6-data-types/exact-width.c b/6-data-types/exact-width.c new file mode 100644 index 0000000..014df94 --- /dev/null +++ b/6-data-types/exact-width.c @@ -0,0 +1,10 @@ +// Created by hfwei on 2024/10/31. + +#include + +int main(void) { + int8_t small = -100; + int32_t large = 100000; + + return 0; +} \ No newline at end of file diff --git a/6-data-types/explict-conversion.c b/6-data-types/explict-conversion.c new file mode 100644 index 0000000..925ef49 --- /dev/null +++ b/6-data-types/explict-conversion.c @@ -0,0 +1,18 @@ +// Created by hfwei on 2024/10/30. + +#include +#include + +int main() { + double pi = 3.14159; + + // below: obtain its fractional part + double fraction = pi - (int)pi; + + int num = 100000000; // (9 digits) + printf("LLONG_MAX = %lld\n", LLONG_MAX); + long long llint = (long long)num * num; + printf("i = %lld\n", llint); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/float-limits.c b/6-data-types/float-limits.c new file mode 100644 index 0000000..597a111 --- /dev/null +++ b/6-data-types/float-limits.c @@ -0,0 +1,29 @@ +// Created by hfwei on 2024/10/30. + +#include +#include + +int main() { + // float pi = 3.14F; + + // 3.402823e+38 + printf("FLT_MAX = %e\n", FLT_MAX); + // 1.175494e-38 + printf("FLT_MIN = %e\n", FLT_MIN); + // 1.401298e-45 + printf("FLT_TRUE_MIN = %e\n", FLT_TRUE_MIN); + // 1.192093e-07 + printf("FLT_EPSILON = %e\n\n", FLT_EPSILON); + + // %lf for scanf + // 1.797693e+308 + printf("DBL_MAX = %e\n", DBL_MAX); + // 2.225074e-308 + printf("DBL_MIN = %e\n", DBL_MIN); + // 4.940656e-324 + printf("DBL_TRUE_MIN = %e\n", DBL_TRUE_MIN); + // 2.220446e-16 + printf("DBL_EPSILON = %e\n\n", DBL_EPSILON); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/for-unsigned.c b/6-data-types/for-unsigned.c new file mode 100644 index 0000000..6b34e0b --- /dev/null +++ b/6-data-types/for-unsigned.c @@ -0,0 +1,14 @@ +// Created by hfwei on 2024/10/31. + +#include +#define LEN 100 + +int main(void) { + int numbers[LEN] = {0}; + + for (unsigned int i = LEN; i >= 0; i--) { + printf("%u : %d\n", i, numbers[i]); + } + + return 0; +} \ No newline at end of file diff --git a/6-data-types/implicit-conversion.c b/6-data-types/implicit-conversion.c new file mode 100644 index 0000000..6eb3918 --- /dev/null +++ b/6-data-types/implicit-conversion.c @@ -0,0 +1,35 @@ +// Created by hfwei on 2024/10/30. + +#include +#include + +int SquareInt(int num); +double SquareDouble(double num); + +int main() { + // narrowing conversion (still in the range) + int i = 3.14159; + + // out of the range: undefined behavior!!! + int j = UINT_MAX; + + // arguments; narrowing conversion + double pi = 3.14; + SquareInt(pi); + + // return value; narrowing conversion + int val = SquareDouble(pi); + + // from int to float; narrowing conversion + int big = 1234567890; + float approx = big; + + printf("big = %d\t approx = %f\t diff = %d\n", big, approx, + big - (int)approx); + + return 0; +} + +int SquareInt(int num) { return num * num; } + +double SquareDouble(double num) { return num * num; } \ No newline at end of file diff --git a/6-data-types/int-limits b/6-data-types/int-limits new file mode 100644 index 0000000000000000000000000000000000000000..91dbdd14aab5d1d311fa95b8ac4ec93d9fcad36b GIT binary patch literal 15968 zcmeHOYit}>6~4QU6DLjLO`6a=XuOmbHB_G1iDQKzll3c}t^6W(M5ri}wRi2^Y9D5I zHnj_C450`&jR=2=MEfI1NU21rDp4ULp<=f*lt)qXFNlZIh*YV>BP|65Dzbd%-g7qN zVVy`kepH$(?SA(>zH{%Kxifob=icMJ{RhLLkdjiTKA}iD8Znv}8_u+I1rSsF)H-}O zsU2!1@fqdnsOn$6dPDy2PEDWs?@*_8nqIX9ug97xpFUc2t`owaS*Sb3@rQc zPVT3m*sJYDiHs7}-&O1<2NXdskGeA7tj%h^V<{ft_Kq?Nk3{0V%Dh*ZCwP_H6O?>X zPUy3b^XbPH*r*8ld1>bPvB%JqAhkhdz*-XAX-8{Xoy}7;3Diy5Vx?$>96dxK>eS?P; z!Yq)E)2PC;MwWPF+r)PA|3&+eNd4!(_x)q(4D-tUR=xe5l{VAs;FfPlgD| zGCs)1u{^e!#)L)AAZ^d!OrxVUZ zCZEV=9!V+3LE^`qlA9>HPA-wjtEplp?@lOEQ~KLgUw^!-+u3dHw)R%*d)kx}A02R# zsbXp}Q*u+q(Sh!4A)gvejAbe6WUi3ssE%>1>4@%yk^ee;=$p<(SnUMLcq(tptfsTF zhjlM}{YjjLusQ&lid^S%eZ@8y{Q_UdqF1bMh#9-+MZUlR=^Rt;q+Imqz2oAFM^EEO z%2khE-~ZTf&7(Kt$QWK{{VIqumL*nzK?Z^h1Q`f25M&_8K#+kT1MmL~{H1ZnKkO5K zXs}P#|7w>~_G1ffxbnJv;>Qgy>w~N8{U+4!RPK2S(AcENKTVc*-g=`_shlPm*6(U- zkJ5I?Wv|@&$N1d&&)RdB>=VDgIy~Cjy3qPl`&7rz5T&wt1fK6sSdC3z*6l$*1C4#E zzDV$~Ywl)rt)1%V+Um;1#->?ve3=pcaqYjii(uzT&$e{

(y;s78bbybf_V=q@~nQ%;AW$3t}l&;bM`$Uu;RAOk@Lf(!&1 z2r>|4Ajm+Ffgl4x2Hx)(p!Zwx!BJ-*J{awY-jiHoq|PHoTBG`h2K&UCh1tl)4jGCi z9@=Gzk9Ja_?R6igce?ZrYW~la3jK$00eBjC1^5i`8gLl+#??w?0Z4`3nNkToa!7@y zBcbhUS2di*3o+8u+JH0Qe1sf&R_RxCccV(}*w9CTYJKG3`Yrc2-u7_AtU9o5|3~-S ztzSbD|1GnwxVyZ09Eh$$XCMM>sE$;h#rw5C_x5-3!uG$jEXTp&|7RfU!TkYSybrT}<4t5)ciPw8y+3;I;jwbwEk|2htv0KrxwWjt z)<<{mwp!YDvic^$V4}RNP;XcKUX=F`EKhJ^qU>-xu$DiamoQe}p7tFV@kqbm)6=?K z(#v%LQI|NrLu#9f#EdNGjaFHQ)CZJ|b7NKN3#d?>>UGA3uWJ9z)pfq&e-@Qm{a^05 zUeIwiDf>eb;Re=<^OE+zL;279&o%A#>N@l=gha*3gF{-6 zyG6{SM70_Eh}v4sr&!yd-x%JYS1HDe~V2^{v_C22->>P-9Lyb)hPOr^Y zyXTN{`uc~uI{TfWg9k@?N1f5muKr#y{CfamvgH3(%luQxgqu+MNO<2sl}ft>kQvy` zHUJ1q_XIe}Qo%_l@=4k+5Fdh9GLv`8rBqTOANX>{N+ss%jR}r}JqB`+wE3Xsj)JB8 z8JVMrZKC2|=AGjya_Ss1E0j6k_G{wt(;4K6L* z%T4j`HPszw^|D zoPQN%PsT|W1^oQiVDR$O%oA)ABBP)GYajm%^97&v@n!so`Z6kXjmUF|JnzYVNWZ_x zf^UP{=;6yed7&9f6!A0C4O#U6gn`Dcv@i2l+21PrSn2wsB0M>d-+_+uCwzGxoZm&> zc+Y2``Yn9Hl^}chD#orY2BjSdAUu)Rf=oJTU*>rc=KKArEpMTDHR;&BY7+i^RgI|# z-;33;=kbkkz@yx;J7L4@x0*k>udIen`tG5tGxVT@a!DF8qV6kKHPCdo7XM3ojn}qY G@c#w18#NyQ literal 0 HcmV?d00001 diff --git a/6-data-types/int-limits.c b/6-data-types/int-limits.c new file mode 100644 index 0000000..e1f39ef --- /dev/null +++ b/6-data-types/int-limits.c @@ -0,0 +1,35 @@ +// Created by hfwei on 2024/10/30. +// Run on Windows and Linux + +#include +#include + +int main() { + // INT_MIN = -2147483648 + // INT_MAX = 2147483647 (10 digits) + printf("INT_MIN = %d\n", INT_MIN); + printf("INT_MAX = %d\n\n", INT_MAX); + + // printf("UINT_MIN = %u\n", 0U); + // printf("UINT_MAX = %u\n\n", UINT_MAX); + + printf("LONG_MIN = %ld\n", LONG_MIN); + printf("LONG_MAX = %ld\n\n", LONG_MAX); + + // printf("ULONG_MIN = %lu\n", 0UL); + // printf("ULONG_MAX = %lu\n\n", ULONG_MAX); + + // long long int: >= 64 bits + + // LLONG_MIN = -9223372036854775808 + // LLONG_MAX = 9223372036854775807 (19 digits) + printf("LLONG_MIN = %lld\n", LLONG_MIN); + printf("LLONG_MAX = %lld\n\n", LLONG_MAX); + + // printf("ULONG_LONG_MIN = %llu\n", 0ULL); + // printf("ULONG_LONG_MAX = %llu\n\n", ULONG_LONG_MAX); + // + // printf("ULLONG_MAX = %llu\n\n", ULLONG_MAX); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/integer-promotion.c b/6-data-types/integer-promotion.c new file mode 100644 index 0000000..412df4f --- /dev/null +++ b/6-data-types/integer-promotion.c @@ -0,0 +1,13 @@ +// Created by hfwei on 2024/10/31. + +#include + +int main(void) { + signed char left = 100; + signed char mid = 3; + signed char right = 4; + + signed char result = left * mid / right; + + printf("result = %d\n", result); +} \ No newline at end of file diff --git a/6-data-types/loop.c b/6-data-types/loop.c new file mode 100644 index 0000000..9cb806d --- /dev/null +++ b/6-data-types/loop.c @@ -0,0 +1,17 @@ +// Created by hengxin on 2024/10/30. + +#include + +int main() { + /** + * Do not use a counter of type float/double, + * although it works on some platforms. + * + * 0.1 cannot be exactly represented in machines. + */ + for (double x = 0.1; x <= 1.0; x += 0.1) { + printf("%.20f\n", x); + } + + return 0; +} \ No newline at end of file diff --git a/6-data-types/precision.c b/6-data-types/precision.c new file mode 100644 index 0000000..5f826a4 --- /dev/null +++ b/6-data-types/precision.c @@ -0,0 +1,23 @@ +// Created by hfwei on 2024/10/31. + +#include +#include +#include + +unsigned int pow2(unsigned int exp); + +int main(void) { + unsigned int exp = 30; + + unsigned int pow = pow2(exp); + printf("2^%d = %d\n", exp, pow); +} + +unsigned int pow2(unsigned int exp) { + if (exp >= sizeof(unsigned int) * CHAR_BIT) { + printf("Exp is too large!\n"); + exit(1); + } + + return 1 << exp; +} \ No newline at end of file diff --git a/6-data-types/signed-overflow-fix.c b/6-data-types/signed-overflow-fix.c new file mode 100644 index 0000000..58b5182 --- /dev/null +++ b/6-data-types/signed-overflow-fix.c @@ -0,0 +1,162 @@ +// Created by hfwei on 2024/10/31. + +#include +#include +#include + +int Add(int left, int right); +int Sub(int left, int right); +int Mul(int left, int right); +int Div(int left, int right); +int Mod(int left, int right); +int Neg(int left); + +int main(void) { + // addition + int left_add = INT_MAX / 2 + 1; + int right_add = INT_MAX / 2 + 1; + + printf("%d + %d = %d\n\n", left_add, right_add, Add(left_add, right_add)); + + // subtraction + int left_sub = INT_MIN; + int right_sub = 1; + + printf("%d - %d = %d\n\n", left_sub, right_sub, Sub(left_sub, right_sub)); + + // multiplication + int left_mul = INT_MAX; + int right_mul = 2; + + printf("%d * %d = %d\n", left_mul, right_mul, Mul(left_mul, right_mul)); + + // division + int left_div = INT_MIN; + int right_div = -1; + + printf("%d / %d = %d\n", left_div, right_div, Div(left_div, right_div)); + + // mod (remainder) + int left_mod = INT_MIN; + int right_mod = -1; + + printf("%d %% %d = %d\n", left_mod, right_mod, Mod(left_mod, right_mod)); + + // negation + int left_neg = INT_MIN; + + printf("-%d = %d\n", left_neg, Neg(left_neg)); +} + +int Add(int left, int right) { + // int sum = left + right; + // return sum; + + // if (left + right > INT_MAX) { + // printf("Too Big!\n"); + // exit(1); + // } else { + // int sum = left + right; + // return sum; + // } + + if ((left > 0 && right > INT_MAX - left) || + (left < 0 && right < INT_MIN - left)) { + printf("Overflow!\n"); + exit(1); + } else { + int sum = left + right; + return sum; + } +} + +int Sub(int left, int right) { + // int sub = left - right; + // return sub; + + // if (left - right < 0) { + // printf("The result is negative!\n"); + // exit(1); + // } else { + // int sub = left - right; + // return sub; + // } + + if ((left > 0 && right < INT_MIN + left) || + (left < 0 && right > INT_MAX + left)) { + printf("Overflow!\n"); + exit(1); + } else { + int sub = left - right; + return sub; + } +} + +int Mul(int left, int right) { + // int mul = left * right; + // return mul; + + // if (left * right > INT_MAX) { + // printf("The result is negative!\n"); + // exit(1); + // } else { + // int mul = left * right; + // return mul; + // } + + if (left > 0) { + if (right > 0) { // left > 0 && right > 0 + if (left > INT_MAX / right) { + printf("Overflow!\n"); + exit(1); + } + } else { // left > 0 && right < 0 + if (right < INT_MIN / left) { + printf("Overflow!\n"); + exit(1); + } + } + } else { // left <= 0 + if (right > 0) { // left <= 0 && right > 0 + if (left < INT_MIN / right) { + printf("Overflow!\n"); + exit(1); + } + } else { // left <= 0 && right <= 0 + if (left != 0 && right < INT_MAX / left) { + printf("Overflow!\n"); + exit(1); + } + } + } + + int mul = left * right; + return mul; +} + +int Div(int left, int right) { + if (right == 0 || (left == INT_MIN && right == -1)) { + printf("Overflow!\n"); + exit(1); + } + + return left / right; +} + +int Mod(int left, int right) { + if (right == 0 || (left == INT_MIN && right == -1)) { + printf("Overflow!\n"); + exit(1); + } + + return left % right; +} + +int Neg(int left) { + if (left == INT_MIN) { + printf("Overflow!\n"); + exit(1); + } + + return -left; +} \ No newline at end of file diff --git a/6-data-types/size b/6-data-types/size new file mode 100644 index 0000000000000000000000000000000000000000..86b4013f73064dd85214adcac0656e118fcba2b6 GIT binary patch literal 16016 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 literal 0 HcmV?d00001 diff --git a/6-data-types/size.c b/6-data-types/size.c new file mode 100644 index 0000000..936e101 --- /dev/null +++ b/6-data-types/size.c @@ -0,0 +1,35 @@ +// Created by hfwei on 2024/10/31. + +#include + +int main() { + // Integer types + printf("Size of char: %zu bytes\n", sizeof(char)); + printf("Size of signed char: %zu bytes\n", sizeof(signed char)); + printf("Size of unsigned char: %zu bytes\n\n", sizeof(unsigned char)); + + printf("Size of short: %zu bytes\n", sizeof(short)); + printf("Size of unsigned short: %zu bytes\n\n", sizeof(unsigned short)); + + printf("Size of int: %zu bytes\n", sizeof(int)); + printf("Size of unsigned int: %zu bytes\n\n", sizeof(unsigned int)); + + printf("Size of long: %zu bytes\n", sizeof(long)); + printf("Size of unsigned long: %zu bytes\n\n", sizeof(unsigned long)); + + printf("Size of long long: %zu bytes\n", sizeof(long long)); + printf("Size of unsigned long long: %zu bytes\n\n", + sizeof(unsigned long long)); + + // (Real) Floating-point types + printf("Size of float: %zu bytes\n", sizeof(float)); + printf("Size of double: %zu bytes\n", sizeof(double)); + printf("Size of long double: %zu bytes\n\n", sizeof(long double)); + + // Array types + int numbers[] = {0, 1, 2, 3, 4}; + size_t len = sizeof numbers / sizeof(int); + printf("Length of numbers: %zu\n", len); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/sizet.c b/6-data-types/sizet.c new file mode 100644 index 0000000..aa75095 --- /dev/null +++ b/6-data-types/sizet.c @@ -0,0 +1,22 @@ +// Created by hfwei on 2024/10/31. + +#include +#include + +#define SIZE UINT_MAX + +char string[SIZE] = {'A', 'B', 'C', 'D', 'E', 'F'}; + +void Print(const int string[], size_t size); + +int main(void) { + Print(string, SIZE); + + return 0; +} + +void Print(const int string[], size_t size) { + for (int i = 0; i < size; i++) { + printf("%d : %d\n", i, string[i]); + } +} \ No newline at end of file diff --git a/6-data-types/sum-product.c b/6-data-types/sum-product.c new file mode 100644 index 0000000..6deab10 --- /dev/null +++ b/6-data-types/sum-product.c @@ -0,0 +1,25 @@ +/** + * file: sums.c + * See + * https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ + * + * Created by hengxin on 2024/10/30. + */ + +#include + +int main() { + // 0.1: 0.0 0011 0011 0011 + float f = 0.1F; + + float sum = 0.0F; + for (int i = 0; i < 10; ++i) { + sum += f; + } + + float product = f * 10; + + printf("sum = %.15f\nmul = %.30f\n", sum, product); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/timing-primes.c b/6-data-types/timing-primes.c new file mode 100644 index 0000000..28a9ad5 --- /dev/null +++ b/6-data-types/timing-primes.c @@ -0,0 +1,39 @@ +// Created by hfwei on 2024/10/31. + +#include +#include +#include + +bool IsPrime(int number); + +int main(void) { + int max = 0; + scanf("%d", &max); + + int count = 0; + + // return the current time in seconds since the Unix epoch (January 1, 1970) + time_t start = time(NULL); + for (int number = 2; number <= max; number++) { + if (IsPrime(number)) { + count++; + } + } + printf("\ncount = %d\n", count); + + // return the current time in seconds since the Unix epoch (January 1, 1970) + time_t end = time(NULL); + printf("Time elapsed: %lld seconds\n", end - start); + + return 0; +} + +bool IsPrime(int number) { + for (int factor = 2; factor * factor <= number; factor++) { + if (number % factor == 0) { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/6-data-types/timing.c b/6-data-types/timing.c new file mode 100644 index 0000000..f90fba4 --- /dev/null +++ b/6-data-types/timing.c @@ -0,0 +1,25 @@ +// Created by hfwei on 2024/10/30. + +#include +#include + +long long Fib(int n); + +int main() { + int n; + scanf("%d", &n); + + time_t start = time(NULL); + printf("Fib(%d) = %lld\n", n, Fib(n)); + time_t end = time(NULL); + + return 0; +} + +long long Fib(int n) { + if (n <= 1) { + return n; + } + + return Fib(n - 1) + Fib(n - 2); +} \ No newline at end of file diff --git a/6-data-types/unsigned-wrap-fix.c b/6-data-types/unsigned-wrap-fix.c new file mode 100644 index 0000000..b545e5f --- /dev/null +++ b/6-data-types/unsigned-wrap-fix.c @@ -0,0 +1,90 @@ +// Created by hfwei on 2024/10/31. + +#include +#include +#include + +unsigned int Add(unsigned int left, unsigned int right); +unsigned int Sub(unsigned int left, unsigned int right); +unsigned int Mul(unsigned int left, unsigned int right); +unsigned int Div(unsigned int left, unsigned int right); +unsigned int Mod(unsigned int left, unsigned int right); + +int main(void) { + // addition + unsigned int left_add = UINT_MAX / 2 + 1; + unsigned int right_add = UINT_MAX / 2 + 1; + + printf("%u + %u = %u\n\n", left_add, right_add, Add(left_add, right_add)); + + // subtraction + unsigned int left_sub = 1; + unsigned int right_sub = 2; + + printf("%u - %u = %u\n\n", left_sub, right_sub, Sub(left_sub, right_sub)); + + // multiplication + unsigned int left_mul = UINT_MAX; + unsigned int right_mul = 2; + + printf("%u * %u = %u\n", left_mul, right_mul, Mul(left_mul, right_mul)); + + // division +} + +unsigned int Add(unsigned int left, unsigned int right) { + // unsigned int sum = left + right; + // return sum; + + if (left + right > UINT_MAX) { + printf("Too Big!\n"); + exit(1); + } else { + unsigned int sum = left + right; + return sum; + } +} + +unsigned int Sub(unsigned int left, unsigned int right) { + // unsigned int sub = left - right; + // return sub; + + if (left - right < 0) { + printf("The result is negative!\n"); + exit(1); + } else { + unsigned int sub = left - right; + return sub; + } +} + +unsigned int Mul(unsigned int left, unsigned int right) { + // unsigned int mul = left * right; + // return mul; + + if (left * right > UINT_MAX) { + printf("The result is negative!\n"); + exit(1); + } else { + unsigned int mul = left * right; + return mul; + } +} + +unsigned int Div(unsigned int left, unsigned int right) { + if (right == 0) { + printf("Division by zero!\n"); + exit(1); + } + + return left / right; +} + +unsigned int Mod(unsigned int left, unsigned int right) { + if (right == 0) { + printf("Division by zero!\n"); + exit(1); + } + + return left % right; +} \ No newline at end of file diff --git a/6-data-types/unsigned-wrap.c b/6-data-types/unsigned-wrap.c new file mode 100644 index 0000000..0bfca09 --- /dev/null +++ b/6-data-types/unsigned-wrap.c @@ -0,0 +1,16 @@ +// Created by hfwei on 2024/10/30. + +#include +#include +int main() { + printf("UINT_MAX = %u\n", UINT_MAX); + + unsigned int max = UINT_MAX; + unsigned int one = 1U; + unsigned int two = 2U; + + printf("max + one = %u\n", max + one); + printf("one - two = %u\n", one - two); + + return 0; +} \ No newline at end of file diff --git a/6-data-types/unsigned.c b/6-data-types/unsigned.c new file mode 100644 index 0000000..74d1b49 --- /dev/null +++ b/6-data-types/unsigned.c @@ -0,0 +1,19 @@ +// Created by hfwei on 2024/10/10. + +#include + +int main() { + const int array[] = {0, 1, 2, 3, 4}; + int i = -1; + + size_t size = sizeof array / sizeof array[0]; + printf("The size of the array is %zu\n", size); + + if (i <= size) { + printf("i <= sizeof array\n"); + } else { + printf("i > sizeof array\n"); + } + + return 0; +} \ No newline at end of file diff --git a/6-recursion/CMakeLists.txt b/6-recursion/CMakeLists.txt deleted file mode 100644 index aecceee..0000000 --- a/6-recursion/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(hilbert-curve-text hilbert-curve-text.c) -add_executable(hilbert-curve-ui hilbert-curve-ui.c) \ No newline at end of file diff --git a/7-recursion/CMakeLists.txt b/7-recursion/CMakeLists.txt new file mode 100644 index 0000000..a407971 --- /dev/null +++ b/7-recursion/CMakeLists.txt @@ -0,0 +1,24 @@ +add_executable(main-re main-re.c) + +add_executable(min min.c) + +add_executable(min-re min-re.c) +add_executable(sum-re sum-re.c) + +add_executable(fib-re fib-re.c) +add_executable(fib-iter fib-iter.c) +add_executable(fib-array fib-array.c) + +add_executable(gcd-re gcd-re.c) +add_executable(gcd-iter gcd-iter.c) + +add_executable(bsearch-iter bsearch-iter.c) +add_executable(bsearch-re bsearch-re.c) + +add_executable(merge-array merge.c) +add_executable(mergesort mergesort.c) + +add_executable(n-queens n-queens.c) + +add_executable(hilbert-curve-text hilbert-curve-text.c) +add_executable(hilbert-curve-ui hilbert-curve-ui.c) \ No newline at end of file diff --git a/7-recursion/README.md b/7-recursion/README.md new file mode 100644 index 0000000..2704124 --- /dev/null +++ b/7-recursion/README.md @@ -0,0 +1,30 @@ +# `6-recursion` + +# 6-recursion + +## Recursion + +- `main-re.c` + +- `min.c` + - stack/heap + - automatic variable +- `min-re.c` + +- `sum-re.c` + - static storage + - static variable + +- `fib-re.c` +- `fib-iter.c` + +- `gcd-re.c` +- `gcd-iter.c` + +- `bsearch-iter.c` +- `bsearch-re.c` + +## Backup + +- `hanoi.c` +- `quicksort.c` \ No newline at end of file diff --git a/7-recursion/bsearch-iter.c b/7-recursion/bsearch-iter.c new file mode 100644 index 0000000..2999738 --- /dev/null +++ b/7-recursion/bsearch-iter.c @@ -0,0 +1,57 @@ +// +// Created by hfwei on 2023/11/9. +// + +#include + +#define LEN 10 + +// dictionary: out of any functions; global variables +// life time: program start to end +// scope: from this point on until the end of the file (file scope) +// int dictionary[LEN] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + +/** + * @brief Search for the key in the dict using the binary search algorithm. + * @param key the key to search for + * @param dict the dictionary to search + * @param len the length of the dictionary + * @return the index of the key in the dictionary; -1 if not found + */ +int BinarySearch(int key, const int dict[100], int len); + +int main(void) { + const int dictionary[LEN] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + + int key = 0; + scanf("%d", &key); + + int index = BinarySearch(key, dictionary, LEN); + + if (index == -1) { + printf("Not found!\n"); + } else { + printf("The index of %d is %d.\n", key, index); + } + + return 0; +} + +int BinarySearch(int key, const int dict[], int len) { + int low = 0; + int high = len - 1; + + while (low <= high) { + int mid = (low + high) / 2; + + if (key > dict[mid]) { + low = mid + 1; + } else if (key < dict[mid]) { + high = mid - 1; + } else { // key == dict[mid] + return mid; + } + } + + return -1; +} \ No newline at end of file diff --git a/7-recursion/bsearch-re.c b/7-recursion/bsearch-re.c new file mode 100644 index 0000000..53ac6a8 --- /dev/null +++ b/7-recursion/bsearch-re.c @@ -0,0 +1,47 @@ +// file: bsearch-re.c +// +// Visualization (search for 2 as an example): +// https://pythontutor.com/visualize.html#code=%23include%20%3Cstdio.h%3E%0A%0A%23define%20LEN%2010%0A%0Aint%20BinarySearch%28int%20key,%20const%20int%20dict%5B%5D,%20int%20low,%20int%20high%29%3B%0A%0Aint%20main%28%29%20%7B%0A%20%20const%20int%20dictionary%5BLEN%5D%20%3D%20%7B%200,%201,%201,%202,%203,%205,%208,%2013,%2021,%2034%20%7D%3B%0A%0A%20%20int%20key%20%3D%202%3B%0A%0A%20%20printf%28%22The%20index%20of%20%25d%20is%20%25d.%5Cn%22,%20key,%0A%20%20%20%20%20%20%20%20%20BinarySearch%28key,%20dictionary,%200,%20LEN%20-%201%29%29%3B%0A%0A%20%20return%200%3B%0A%7D%0A%0Aint%20BinarySearch%28int%20key,%20const%20int%20dict%5B%5D,%20int%20low,%20int%20high%29%20%7B%0A%20%20if%20%28low%20%3E%20high%29%20%7B%0A%20%20%20%20return%20-1%3B%0A%20%20%7D%0A%0A%20%20int%20mid%20%3D%20%28low%20%2B%20high%29%20/%202%3B%0A%0A%20%20if%20%28dict%5Bmid%5D%20%3D%3D%20key%29%20%7B%0A%20%20%20%20return%20mid%3B%0A%20%20%7D%0A%0A%20%20if%20%28dict%5Bmid%5D%20%3E%20key%29%20%7B%0A%20%20%20%20return%20BinarySearch%28key,%20dict,%20low,%20mid%20-%201%29%3B%0A%20%20%7D%0A%0A%20%20return%20BinarySearch%28key,%20dict,%20mid%20%2B%201,%20high%29%3B%0A%7D&cumulative=true&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// Created by hfwei on 2023/11/9. + +#include + +#define LEN 10 + +int BinarySearch(int key, const int dict[], int low, int high); + +int main() { + const int dictionary[LEN] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; + + int key; + scanf("%d", &key); + + printf("The index of %d is %d.\n", key, + BinarySearch(key, dictionary, 0, LEN - 1)); + + return 0; +} + +int BinarySearch(int key, const int dict[], int low, int high) { + // if (low == high) { + // if (dict[low] == key) { + // return low; + // } + // return - 1; + // } + + if (low > high) { + return -1; + } + + int mid = (low + high) / 2; + if (dict[mid] == key) { + return mid; + } + + if (dict[mid] > key) { + return BinarySearch(key, dict, low, mid - 1); + } + + return BinarySearch(key, dict, mid + 1, high); +} \ No newline at end of file diff --git a/7-recursion/fib-array.c b/7-recursion/fib-array.c new file mode 100644 index 0000000..ac75760 --- /dev/null +++ b/7-recursion/fib-array.c @@ -0,0 +1,22 @@ +// +// Created by hfwei on 2023/11/9. +// + +#include + +#define LEN 93 + +int main() { + long long fibs[LEN] = { 0LL, 1LL }; + + int n; + scanf("%d", &n); + + for (int i = 2; i <= n; ++i) { + fibs[i] = fibs[i - 1] + fibs[i - 2]; + } + + printf("Fib(%d) = %lld\n", n, fibs[n]); + + return 0; +} \ No newline at end of file diff --git a/7-recursion/fib-iter.c b/7-recursion/fib-iter.c new file mode 100644 index 0000000..effed4e --- /dev/null +++ b/7-recursion/fib-iter.c @@ -0,0 +1,27 @@ +// +// Created by hfwei on 2023/11/9. +// + +#include + +// Fib(92) = 7 540 113 804 746 346 429 +// long long: 9 223 372 036 854 775 807 +// Fib(93) = 12 200 160 415 121 876 738 +int main() { + int n; + scanf("%d", &n); + + long long fib0 = 0L; + long long fib1 = 1L; + + long long fib2 = 0L; + for (int i = 2; i <= n; i++) { + fib2 = fib0 + fib1; + fib0 = fib1; + fib1 = fib2; + } + + printf("Fib(%d) = %lld ", n, fib2); + + return 0; +} \ No newline at end of file diff --git a/7-recursion/fib-re.c b/7-recursion/fib-re.c new file mode 100644 index 0000000..52d4145 --- /dev/null +++ b/7-recursion/fib-re.c @@ -0,0 +1,25 @@ +// +// Visualization (for n = 4): https://pythontutor.com/render.html#code=%23include%20%3Cstdio.h%3E%0A%0Along%20long%20Fib%28int%20n%29%3B%0A%0Aint%20main%28%29%20%7B%0A%20%20int%20n%20%3D%204%3B%0A%0A%20%20printf%28%22%25lld%5Cn%22,%20Fib%28n%29%29%3B%0A%7D%0A%0Along%20long%20Fib%28int%20n%29%20%7B%0A%20%20if%20%28n%20%3D%3D%200%29%20%7B%0A%20%20%20%20return%200%3B%0A%20%20%7D%0A%0A%20%20if%20%28n%20%3D%3D%201%29%20%7B%0A%20%20%20%20return%201%3B%0A%20%20%7D%0A%0A%20%20return%20Fib%28n%20-%201%29%20%2B%20Fib%28n%20-%202%29%3B%0A%7D&cumulative=false&curInstr=55&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// Created by hfwei on 2023/11/9. +// + +#include + +long long Fib(int n); + +int main() { + int n; + scanf("%d", &n); + + printf("Fib(%d) = %lld\n", n, Fib(n)); + + return 0; +} + +long long Fib(int n) { + if (n <= 1) { + return n; + } + + return Fib(n - 1) + Fib(n - 2); +} \ No newline at end of file diff --git a/7-recursion/gcd-iter.c b/7-recursion/gcd-iter.c new file mode 100644 index 0000000..050df3a --- /dev/null +++ b/7-recursion/gcd-iter.c @@ -0,0 +1,29 @@ +// file: gcd-iter.c +// +// Euclidean algorithm: +// gcd(a, b) = gcd(b, a % b) +// +// Created by hfwei on 2023/11/9. + +#include + +int GCD(int a, int b); + +int main() { + int a = 130; + int b = 124; + + printf("gcd(%d, %d) = %d\n", a, b, GCD(a, b)); + + return 0; +} + +int GCD(int a, int b) { + while (b != 0) { + int temp = a; + a = b; + b = temp % b; + } + + return a; +} \ No newline at end of file diff --git a/7-recursion/gcd-re.c b/7-recursion/gcd-re.c new file mode 100644 index 0000000..b90e74c --- /dev/null +++ b/7-recursion/gcd-re.c @@ -0,0 +1,34 @@ +// file: gcd-re.c +// +// Euclidean algorithm: +// gcd(a, b) = gcd(b, a % b) +// +// Visualization (gcd(64, 48) for illustration): +// https://pythontutor.com/visualize.html#code=%23include%20%3Cstdio.h%3E%0A%0Aint%20GCD%28int%20a,%20int%20b%29%3B%0A%0Aint%20main%28%29%20%7B%0A%20%20int%20a%20%3D%2064%3B%0A%20%20int%20b%20%3D%2048%3B%0A%0A%20%20printf%28%22gcd%28%25d,%20%25d%29%20%3D%20%25d%5Cn%22,%20a,%20b,%20GCD%28a,%20b%29%29%3B%0A%0A%20%20return%200%3B%0A%7D%0A%0A//%20gcd%28130,%20124%29%20%3D%202%0A//%20gcd%28662,%20414%29%20%3D%202%0Aint%20GCD%28int%20a,%20int%20b%29%20%7B%0A%20%20if%20%28b%20%3D%3D%200%29%20%7B%0A%20%20%20%20return%20a%3B%0A%20%20%7D%0A%0A%20%20return%20GCD%28b,%20a%20%25%20b%29%3B%0A%7D&cumulative=true&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// +// Created by hfwei on 2023/11/9. +// + +#include + +int GCD(int a, int b); + +int main() { + int a = 0; + int b = 0; + scanf("%d %d", &a, &b); + + printf("GCD(%d, %d) = %d\n", a, b, GCD(a, b)); + + return 0; +} + +// gcd(130, 124) = 2 +// gcd(414, 662) = 2 +int GCD(int a, int b) { + if (b == 0) { + return a; + } + + return GCD(b, a % b); +} \ No newline at end of file diff --git a/6-recursion/hilbert-curve-text.c b/7-recursion/hilbert-curve-text.c similarity index 100% rename from 6-recursion/hilbert-curve-text.c rename to 7-recursion/hilbert-curve-text.c diff --git a/6-recursion/hilbert-curve-ui.c b/7-recursion/hilbert-curve-ui.c similarity index 100% rename from 6-recursion/hilbert-curve-ui.c rename to 7-recursion/hilbert-curve-ui.c diff --git a/7-recursion/main-re.c b/7-recursion/main-re.c new file mode 100644 index 0000000..cee5908 --- /dev/null +++ b/7-recursion/main-re.c @@ -0,0 +1,22 @@ +// file: main-re.c +// +// Created by hfwei on 2023/11/9. +// +// WARNING: You can even call the "main" function in itself. +// But, do NOT write code like this. +// Never call the "main" function in your own code. +// + +#include + +int main(int argc, char *argv[]) { + if (argc == 1) { + return 0; + } + + printf("%s\n", argv[argc - 1]); + + main(argc - 1, argv); + + return 0; +} \ No newline at end of file diff --git a/7-recursion/merge.c b/7-recursion/merge.c new file mode 100644 index 0000000..5cb8340 --- /dev/null +++ b/7-recursion/merge.c @@ -0,0 +1,40 @@ +// +// Created by hfwei on 2023/10/19. +// + +#include + +#define LEN_L 5 +#define LEN_R 6 + +int L[LEN_L] = { 1, 3, 5, 7, 9 }; +int R[LEN_R] = { 0, 2, 4, 6, 8, 10 }; + +int main(void) { + // TODO: merge L and R into a sorted array + int l = 0; + int r = 0; + + while (l < LEN_L && r < LEN_R) { + if (L[l] <= R[r]) { + printf("%d ", L[l]); + l++; + } else { + printf("%d ", R[r]); + r++; + } + } + + // l >= LEN_L || r >= LEN_R + while (r < LEN_R) { + printf("%d ", R[r]); + r++; + } + + while (l < LEN_L) { + printf("%d ", L[l]); + l++; + } + + return 0; +} \ No newline at end of file diff --git a/7-recursion/mergesort.c b/7-recursion/mergesort.c new file mode 100644 index 0000000..954fea0 --- /dev/null +++ b/7-recursion/mergesort.c @@ -0,0 +1,89 @@ +// +// Created by hfwei on 2023/11/15. +// + +#include + +#define LEN 7 + +/** + * @brief sort nums[left .. right] using merge sort + * @param nums + * @param left + * @param right + */ +void MergeSort(int nums[], int left, int right); + +/** + * @brief merge nums[left .. mid] and nums[mid + 1 .. right] + * @param nums + * @param left + * @param mid + * @param right + */ +void Merge(int nums[], int left, int mid, int right); + +int main() { + int numbers[LEN] = {38, 27, 43, 3, 9, 82, 10}; + + for (int i = 0; i < LEN; i++) { + printf("%d ", numbers[i]); + } + + // TODO + MergeSort(numbers, 0, LEN - 1); + + for (int i = 0; i < LEN; i++) { + printf("%d ", numbers[i]); + } + + return 0; +} + +void MergeSort(int nums[], int left, int right) { + if (left == right) { + return; + } + + int mid = (left + right) / 2; + MergeSort(nums, left, mid); // ask the Mirror + MergeSort(nums, mid + 1, right); // ask the Mirror + + Merge(nums, left, mid, right); +} + +void Merge(int nums[], int left, int mid, int right) { + static int copy[LEN] = {0}; + + int left_index = left; + int right_index = mid + 1; + int copy_index = left; + + while (left_index <= mid && right_index <= right) { + if (nums[left_index] <= nums[right_index]) { + copy[copy_index] = nums[left_index]; + left_index++; + } else { + copy[copy_index] = nums[right_index]; + right_index++; + } + + copy_index++; + } + + while (left_index <= mid) { + copy[copy_index] = nums[left_index]; + left_index++; + copy_index++; + } + + while (right_index <= right) { + copy[copy_index] = nums[right_index]; + right_index++; + copy_index++; + } + + for (int i = left; i <= right; ++i) { + nums[i] = copy[i]; + } +} \ No newline at end of file diff --git a/7-recursion/min-re.c b/7-recursion/min-re.c new file mode 100644 index 0000000..85c23ed --- /dev/null +++ b/7-recursion/min-re.c @@ -0,0 +1,29 @@ +// +// Created by hfwei on 2023/11/9. +// Visualization: https://pythontutor.com/visualize.html#code=%23include%20%3Cstdio.h%3E%0A%0A%23define%20NUM%203%0Aint%20numbers%5BNUM%5D%20%3D%20%7B65,%2028,%2037%7D%3B%0A%0Aint%20Min%28const%20int%20nums%5B%5D,%20int%20len%29%3B%0A%0Aint%20main%28%29%20%7B%0A%20%20int%20min%20%3D%20Min%28numbers,%20NUM%29%3B%0A%20%20%0A%20%20printf%28%22min%20%3D%20%25d%5Cn%22,%20min%29%3B%0A%0A%20%20return%200%3B%0A%7D%0A%0Aint%20Min%28const%20int%20numbers%5B%5D,%20int%20len%29%20%7B%0A%20%20if%20%28len%20%3D%3D%201%29%20%7B%0A%20%20%20%20return%20numbers%5B0%5D%3B%0A%20%20%7D%0A%0A%20%20int%20partial_min%20%3D%20Min%28numbers,%20len%20-%201%29%3B%0A%20%20return%20numbers%5Blen%20-%201%5D%20%3C%20partial_min%20%3F%20numbers%5Blen%20-%201%5D%20%3A%20partial_min%3B%0A%7D&cumulative=true&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// + +#include + +#define NUM 3 +const int numbers[NUM] = { 65, 28, 37 }; + +int Min(const int nums[], int len); + +int main() { + int min = Min(numbers, NUM); + + printf("min = %d\n", min); + + return 0; +} + +int Min(const int nums[], int len) { + if (len == 1) { + return nums[0]; + } + + int partial_min = Min(nums, len - 1); + + return partial_min < nums[len - 1] ? partial_min : nums[len - 1]; +} \ No newline at end of file diff --git a/7-recursion/min.c b/7-recursion/min.c new file mode 100644 index 0000000..fcd2b5b --- /dev/null +++ b/7-recursion/min.c @@ -0,0 +1,22 @@ +// +// Created by hfwei on 2023/11/9. +// Visualization of function call: https://pythontutor.com/visualize.html#code=%23include%20%3Cstdio.h%3E%0A%0Aint%20Min%28int%20a,%20int%20b%29%3B%0A%0Aint%20main%28%29%20%7B%0A%20%20int%20a%20%3D%2025%3B%0A%20%20int%20b%20%3D%2037%3B%0A%20%20%0A%20%20int%20min%20%3D%20Min%28a,%20b%29%3B%0A%20%20printf%28%22%25d%22,%20min%29%3B%0A%0A%20%20return%200%3B%0A%7D%0A%0Aint%20Min%28int%20a,%20int%20b%29%20%7B%0A%20%20return%20a%20%3E%20b%20%3F%20b%20%3A%20a%3B%0A%7D&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// + +#include + +int Min(int a, int b); + +int main() { + int a = 25; + int b = 37; + + int min = Min(a, b); + printf("%d", min); + + return 0; +} + +int Min(int a, int b) { + return a > b ? b : a; +} \ No newline at end of file diff --git a/7-recursion/n-queens.c b/7-recursion/n-queens.c new file mode 100644 index 0000000..2db640e --- /dev/null +++ b/7-recursion/n-queens.c @@ -0,0 +1,69 @@ +// Created by hfwei on 2024/10/26. + +#include +#include + +#define N 8 // Change N as needed + +// Function to print the board configuration +void printBoard(int board[][N]) { + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + printf("%s ", board[i][j] ? "Q" : "."); + } + printf("\n"); + } + printf("\n"); +} + +// Function to check if placing a queen at board[row][col] is safe +bool isSafe(int board[][N], int row, int col) { + // Check column for conflicts + for (int i = 0; i < row; i++) { + if (board[i][col]) + return false; + } + + // Check upper left diagonal + for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) { + if (board[i][j]) + return false; + } + + // Check upper right diagonal + for (int i = row, j = col; i >= 0 && j < N; i--, j++) { + if (board[i][j]) + return false; + } + + return true; +} + +// Recursive function to solve N-Queens +bool solveNQueens(int board[][N], int row) { + if (row >= N) { + printBoard(board); + return true; + } + + bool res = false; + for (int col = 0; col < N; col++) { + if (isSafe(board, row, col)) { + board[row][col] = 1; + res = solveNQueens(board, row + 1) || res; + board[row][col] = 0; + } + } + + return res; +} + +int main() { + int board[N][N] = {0}; + + if (!solveNQueens(board, 0)) { + printf("Solution does not exist for %d-Queens.\n", N); + } + + return 0; +} \ No newline at end of file diff --git a/7-recursion/sum-re.c b/7-recursion/sum-re.c new file mode 100644 index 0000000..eaf531c --- /dev/null +++ b/7-recursion/sum-re.c @@ -0,0 +1,29 @@ +// +// Created by hfwei on 2023/11/9. +// +// Visualization: https://pythontutor.com/visualize.html#code=%23include%20%3Cstdio.h%3E%0A%0Aint%20Sum%28int%20numbers%5B%5D,%20int%20len%29%3B%0A%0Aint%20main%28%29%20%7B%0A%20%20%20%20int%20numbers%5B%5D%20%3D%20%7B1,%202,%203,%204,%205%7D%3B%0A%0A%20%20%20%20int%20sum%20%3D%20Sum%28numbers,%20sizeof%20numbers%20/%20sizeof%20numbers%5B0%5D%29%3B%0A%20%20%20%20printf%28%22sum%20%3D%20%25d%5Cn%22,%20sum%29%3B%0A%0A%20%20%20%20return%200%3B%0A%7D%0A%0Aint%20Sum%28int%20numbers%5B%5D,%20int%20len%29%20%7B%0A%20%20%20%20if%20%28len%20%3D%3D%200%29%20%7B%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20int%20partial_sum%20%3D%20Sum%28numbers,%20len%20-%201%29%3B%0A%0A%20%20%20%20int%20sum%20%3D%20numbers%5Blen%20-%201%5D%20%2B%20partial_sum%3B%0A%0A%20%20%20%20return%20sum%3B%0A%7D&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=c_gcc9.3.0&rawInputLstJSON=%5B%5D&textReferences=false +// + +#include + +int Sum(const int nums[], int len); + +int main() { + const int numbers[] = { 1, 2, 3, 4, 5 }; + + int sum = Sum(numbers, sizeof numbers / sizeof numbers[0]); + + printf("sum = %d\n", sum); + + return 0; +} + +int Sum(const int nums[], int len) { + if (len == 0) { + return 0; + } + + int partial_sum = Sum(nums, len - 1); + + return partial_sum + nums[len - 1]; +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 905bcd8..c9d7cc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,4 +9,5 @@ add_subdirectory(2-if-for-array) add_subdirectory(3-for-a-while) add_subdirectory(4-loops) add_subdirectory(5-function) -add_subdirectory(6-recursion) \ No newline at end of file +add_subdirectory(6-data-types) +add_subdirectory(7-recursion) \ No newline at end of file