diff --git a/4-loops/insertion-sort.c b/4-loops/insertion-sort.c index a150dd0..06009f0 100644 --- a/4-loops/insertion-sort.c +++ b/4-loops/insertion-sort.c @@ -35,7 +35,7 @@ int main(void) { int j = i - 1; while (j >= 0 && numbers[j] > key) { numbers[j + 1] = numbers[j]; - j = j - 1; + j--; } // a for-loop version diff --git a/5-function/CMakeLists.txt b/5-function/CMakeLists.txt new file mode 100644 index 0000000..abb629e --- /dev/null +++ b/5-function/CMakeLists.txt @@ -0,0 +1,20 @@ +# 0-intro +add_executable(guess-func guess.c) + +# 2-if-for-array +add_executable(leap-func leap.c) + +# 3-for-a-while +add_executable(stars-func stars.c) +add_executable(primes-func primes.c) +add_executable(binary-search-func binary-search.c) +add_executable(palindrome-func palindrome.c) +add_executable(selection-sort-func selection-sort.c) + +# 4-loops +add_executable(merge-func merge.c) +add_executable(game-of-life-func game-of-life.c) +add_executable(game-of-life-transformed game-of-life-transformed.c) + +add_executable(insertion-sort-func insertion-sort.c) +add_executable(binary-insertion-sort-func binary-insertion-sort.c) \ No newline at end of file diff --git a/5-function/binary-insertion-sort.c b/5-function/binary-insertion-sort.c new file mode 100644 index 0000000..6b1e12a --- /dev/null +++ b/5-function/binary-insertion-sort.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#define MAX_LEN 10000 +#define RANGE 10 + +int main() { + int numbers[MAX_LEN] = { 0 }; + + int size = 0; + scanf("%d", &size); + + srand(time(NULL)); + for (int i = 0; i < size; i++) { + numbers[i] = rand() % RANGE; + } + + // print the original array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + // TODO + for (int i = 1; i < size; i++) { + int key = numbers[i]; + int low = 0; + int high = i - 1; + + while (low <= high) { + int mid = (low + high) / 2; + if (key >= numbers[mid]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + for (int j = i - 1; j >= low; j--) { + numbers[j + 1] = numbers[j]; + } + numbers[low] = key; + + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + } + + // Print the sorted array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/5-function/binary-search.c b/5-function/binary-search.c new file mode 100644 index 0000000..896b522 --- /dev/null +++ b/5-function/binary-search.c @@ -0,0 +1,49 @@ +// Created by hfwei on 2024/10/23. + +#include + +#define LEN 10 + +// global variable +// file scope +// const int dictionary[LEN] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }; + +// int dict[]: the address of the first element of the array +int BinarySearch(int key, const int dict[], int len); + +int main(void) { + const int dictionary[LEN] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }; + + int key = 0; + scanf("%d", &key); + + // dictionary (actual argument): const int[] + // dict (formal parameter): int[] + 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/5-function/game-of-life-transformed.c b/5-function/game-of-life-transformed.c new file mode 100644 index 0000000..b038d84 --- /dev/null +++ b/5-function/game-of-life-transformed.c @@ -0,0 +1,100 @@ +// Created by hfwei on 2024/10/23. +// Run it with "Terminal" + +#include +#include +#include + +#define SIZE 6 + +void ExtendBoard(const int origin_board[][SIZE], + int extended_board[][SIZE + 2]); +void PrintExtendedBoard(const int extended_board[][SIZE + 2]); +void GenerateNewBoard(const int old_board[][SIZE + 2], + int new_board[][SIZE + 2]); +void CopyExtendedBoard(const int src_board[][SIZE + 2], + int dest_board[][SIZE + 2]); +void SleepAndClear(int sec); + +int main() { + const int board[SIZE][SIZE] = { + { 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0 } + }; + + int old_board[SIZE + 2][SIZE + 2] = { 0 }; + ExtendBoard(board, old_board); + PrintExtendedBoard(old_board); + SleepAndClear(1); + + int new_board[SIZE + 2][SIZE + 2] = { 0 }; + for (int round = 0; round < 10; round++) { + GenerateNewBoard(old_board, new_board); + SleepAndClear(1); + PrintExtendedBoard(new_board); + CopyExtendedBoard(new_board, old_board); + } + + return 0; +} + +void ExtendBoard(const int origin_board[][SIZE], + int extended_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + extended_board[row][col] = origin_board[row - 1][col - 1]; + } + } +} + +void PrintExtendedBoard(const int extended_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + printf("%c ", extended_board[row][col] ? '*' : ' '); + } + printf("\n"); + } +} + +void GenerateNewBoard(const int old_board[][SIZE + 2], + int new_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + // count the number of neighbours of old_board[row][col] + int neighbours = + old_board[row - 1][col - 1] + + old_board[row - 1][col] + + old_board[row - 1][col + 1] + + old_board[row][col - 1] + + old_board[row][col + 1] + + old_board[row + 1][col - 1] + + old_board[row + 1][col] + + old_board[row + 1][col + 1]; + + // evaluate the new board + if (old_board[row][col]) { // old_board[row][col] is alive + new_board[row][col] = (neighbours == 2 || neighbours == 3); + } else { // old_board[row][col] is dead + new_board[row][col] = (neighbours == 3); + } + } + } +} + +void CopyExtendedBoard(const int src_board[][SIZE + 2], + int dest_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + dest_board[row][col] = src_board[row][col]; + } + } +} + +void SleepAndClear(int sec) { + sleep(sec); + system("clear"); +} \ No newline at end of file diff --git a/5-function/game-of-life.c b/5-function/game-of-life.c new file mode 100644 index 0000000..0669db1 --- /dev/null +++ b/5-function/game-of-life.c @@ -0,0 +1,98 @@ +// Created by hfwei on 2024/10/23. + +#include +#include +#include +#include + +#define SIZE 6 +const int board[SIZE][SIZE] = { + { 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0 } +}; + +//const int board[SIZE][SIZE] = { +// [1][1] = 1, [1][2] = 1, +// [2][1] = 1, [2][2] = 1, +// [3][3] = 1, [3][4] = 1, +// [4][3] = 1, [4][4] = 1 +//}; + +int main() { + // extended board + int old_board[SIZE + 2][SIZE + 2] = { 0 }; + + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + old_board[row][col] = board[row - 1][col - 1]; + } + } + + // print the original board + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + printf("%c ", old_board[row][col] ? '*' : ' '); + } + printf("\n"); + } + system("clear"); // clear the screen/terminal + + int new_board[SIZE + 2][SIZE + 2] = { 0 }; + + for (int round = 1; round < 10; round++) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + // count the number of neighbours of old_board[row][col] + int neighbours = + old_board[row - 1][col - 1] + + old_board[row - 1][col] + + old_board[row - 1][col + 1] + + old_board[row][col - 1] + + old_board[row][col + 1] + + old_board[row + 1][col - 1] + + old_board[row + 1][col] + + old_board[row + 1][col + 1]; + + // evaluate the new board + if (old_board[row][col]) { // old_board[row][col] is alive + new_board[row][col] = (neighbours == 2 || neighbours == 3); + } else { // old_board[row][col] is dead + new_board[row][col] = (neighbours == 3); + } + } + } + + // print the new board + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + printf("%c ", new_board[row][col] ? '*' : ' '); + } + printf("\n"); + } + + // sleep for a while + // Linux: #include + sleep(1); + // Windows: #include : Sleep(ms) + // Sleep(1000); + + // clear the screen + // Linux: #include + system("clear"); + // Windows: #include system("clr); + // system("clr"); + + // start the next round + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + old_board[row][col] = new_board[row][col]; + } + } + } + + return 0; +} \ No newline at end of file diff --git a/5-function/guess.c b/5-function/guess.c new file mode 100644 index 0000000..6eb7a83 --- /dev/null +++ b/5-function/guess.c @@ -0,0 +1,61 @@ +// Created by hfwei on 2024/10/23. + +#include +#include +#include + +int main(void) { + int high = 100; + int number_of_tries = 7; + + /* + * (1) print the rules of the game to players + */ + printf("Let us play the Guess the Number game.\n" + "The computer will generate a random number between 1 and %d.\n" + "You have %d tries.\n", + high, number_of_tries); + + /* + * (2) generate a random number between 1 and high + */ + srand(time(NULL)); + int secret = rand() % high + 1; + printf("Test: secret = %d\n", secret); + + while (number_of_tries > 0) { + /* + * (3) ask the player to enter his/her guess + */ + printf("Please enter your guess number.\n" + "You still have %d tries.\n", number_of_tries); + + /* + * (4) obtain the guessed number, compare it with the secret number, + * and inform the player of the result. + */ + int guess = 0; + scanf("%d", &guess); + printf("Test: guess = %d\n", guess); + + if (guess == secret) { + printf("You Win!\n"); + break; + } else if (guess > secret) { + printf("guess > secret\n"); + } else { + printf("guess < secret\n"); + } + + /* + * (5) repeat (3) and (4) until the player wins or loses. + */ + number_of_tries--; + + if (number_of_tries == 0) { + printf("You Lose!\n"); + } + } + + return 0; +} \ No newline at end of file diff --git a/5-function/insertion-sort.c b/5-function/insertion-sort.c new file mode 100644 index 0000000..70dcbe3 --- /dev/null +++ b/5-function/insertion-sort.c @@ -0,0 +1,57 @@ +// Created by hfwei on 2024/10/16. +// Code generated by ChatGPT. + +#include +#include +#include + +#define MAX_LEN 10000 +#define RANGE 10 + +int main(void) { + int numbers[MAX_LEN] = { 0 }; + + int size = 0; + scanf("%d", &size); + + // generate an array of random integers between 0 and RANGE - 1 + srand(time(NULL)); + for (int i = 0; i < size; i++) { + numbers[i] = rand() % RANGE; + } + + // print the original array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + // TODO: insertion sort + for (int i = 1; i < size; i++) { + // numbers[0 .. i - 1] is already sorted + int key = numbers[i]; + + int j = i - 1; + while (j >= 0 && numbers[j] > key) { + numbers[j + 1] = numbers[j]; + j--; + } + numbers[j + 1] = key; + + // numbers[0 .. i] is already sorted + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + } + // i = size + // numbers[0 .. size - 1] is already sorted + + // print the sorted array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/5-function/leap.c b/5-function/leap.c new file mode 100644 index 0000000..4b9f445 --- /dev/null +++ b/5-function/leap.c @@ -0,0 +1,34 @@ +// Created by hfwei on 2024/10/23. + +#include +#include + +bool IsLeapYear(int year); + +int main(void) { + // local variable + // scope: block scope + int year = 0; + scanf("%d", &year); + + // caller + // callee + bool leap = IsLeapYear(year); + + if (leap) { + printf("%d is a leap year\n", year); + } else { + printf("%d is a common year\n", year); + } + + return 0; +} + +// year: formal parameter +// block scope +bool IsLeapYear(int year) { + // local variable + // scope: block scope + bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); + return leap; +} \ No newline at end of file diff --git a/5-function/merge.c b/5-function/merge.c new file mode 100644 index 0000000..47f5436 --- /dev/null +++ b/5-function/merge.c @@ -0,0 +1,39 @@ +// Created by hfwei on 2024/10/23. + +#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 }; + +void Merge(const int left[], int left_len, + const int right[], int right_len); + +int main(void) { + 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++; + } + } + + 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/5-function/palindrome.c b/5-function/palindrome.c new file mode 100644 index 0000000..5379203 --- /dev/null +++ b/5-function/palindrome.c @@ -0,0 +1,32 @@ +// Created by hfwei on 2024/10/23. + +#include +#include +#include + +#define LEN 21 +char string[LEN] = ""; + +bool IsPalindrome(const char str[]); + +int main() { + printf("Input a string containing at most 20 characters.\n"); + scanf("%20s", string); + + printf("\"%s\" is %s a palindrome.\n", string, + IsPalindrome(string) ? "" : "not"); + + return 0; +} + +bool IsPalindrome(const char str[]) { + int len = strlen(str); + + for (int i = 0, j = len - 1; i < j; i++, j--) { + if (str[i] != str[j]) { + return false; + } + } + + return true; +} \ No newline at end of file diff --git a/5-function/primes.c b/5-function/primes.c new file mode 100644 index 0000000..a83c23c --- /dev/null +++ b/5-function/primes.c @@ -0,0 +1,34 @@ +// Created by hfwei on 2024/10/23. + +#include +#include + +bool IsPrime(int number); + +int main(void) { + int max = 0; + scanf("%d", &max); + + int count = 0; + + for (int number = 2; number <= max; number++) { + if (IsPrime(number)) { + count++; + printf("%d ", number); + } + } + + printf("\ncount = %d\n", count); + + 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/5-function/selection-sort b/5-function/selection-sort new file mode 100644 index 0000000..f9d6166 Binary files /dev/null and b/5-function/selection-sort differ diff --git a/5-function/selection-sort.c b/5-function/selection-sort.c new file mode 100644 index 0000000..4d09a23 --- /dev/null +++ b/5-function/selection-sort.c @@ -0,0 +1,68 @@ +// Created by hfwei on 2024/10/23. + +#include + +#define LEN 20 +int numbers[LEN] = { 0 }; + +void SelectionSort(int arr[], int len); +int GetMinIndex(const int arr[], int begin, int end); +void Swap(int left, int right); +void Print(const int arr[], int len); + +int main(void) { + int len = -1; + while (scanf("%d", &numbers[++len]) != EOF); + + Print(numbers, len); + SelectionSort(numbers, len); + Print(numbers, len); + + return 0; +} + +void SelectionSort(int arr[], int len) { + for (int i = 0; i < len; i++) { + int min_index = GetMinIndex(arr, i, len); + + // swap arr[i] and arr[min_index] + // Swap(left, right) + // arr[i] = 3 arr[min_index] = 5 + // left = 3 right = 5 + // Swap(arr[i], arr[min_index]); + + int temp = arr[i]; + arr[i] = arr[min_index]; + arr[min_index] = temp; + } +} + +int GetMinIndex(const int arr[], int begin, int end) { + int min = arr[begin]; + int min_index = begin; + + for (int j = begin + 1; j <= end - 1; ++j) { + if (arr[j] < min) { + min = arr[j]; + min_index = j; + } + } + + return min_index; +} + +// left = 3 right = 5 +void Swap(int left, int right) { + int temp = left; + left = right; + right = temp; + // left = 5 right = 3 +} + +void Print(const int arr[], int len) { + printf("\n"); + for (int i = 0; i < len; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} \ No newline at end of file diff --git a/5-function/stars.c b/5-function/stars.c new file mode 100644 index 0000000..c122966 --- /dev/null +++ b/5-function/stars.c @@ -0,0 +1,27 @@ +// Created by hfwei on 2024/10/23. + +#include + +void Print(char ch, int count); + +int main(void) { + int lines = 0; + scanf("%d", &lines); + + for (int i = 0; i < lines; ++i) { + Print(' ', lines - 1 - i); + Print('*', 2 * i + 1); + + if (i < lines - 1) { + printf("\n"); + } + } + + return 0; +} + +void Print(char ch, int count) { + for (int i = 0; i < count; ++i) { + printf("%c", ch); + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 51f50cc..905bcd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,4 +8,5 @@ add_subdirectory(1-types-io) 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 diff --git a/pdf/5-function.pdf b/pdf/5-function.pdf new file mode 100644 index 0000000..a841f77 Binary files /dev/null and b/pdf/5-function.pdf differ diff --git a/template/5-function/CMakeLists.txt b/template/5-function/CMakeLists.txt new file mode 100644 index 0000000..abb629e --- /dev/null +++ b/template/5-function/CMakeLists.txt @@ -0,0 +1,20 @@ +# 0-intro +add_executable(guess-func guess.c) + +# 2-if-for-array +add_executable(leap-func leap.c) + +# 3-for-a-while +add_executable(stars-func stars.c) +add_executable(primes-func primes.c) +add_executable(binary-search-func binary-search.c) +add_executable(palindrome-func palindrome.c) +add_executable(selection-sort-func selection-sort.c) + +# 4-loops +add_executable(merge-func merge.c) +add_executable(game-of-life-func game-of-life.c) +add_executable(game-of-life-transformed game-of-life-transformed.c) + +add_executable(insertion-sort-func insertion-sort.c) +add_executable(binary-insertion-sort-func binary-insertion-sort.c) \ No newline at end of file diff --git a/template/5-function/binary-insertion-sort.c b/template/5-function/binary-insertion-sort.c new file mode 100644 index 0000000..6b1e12a --- /dev/null +++ b/template/5-function/binary-insertion-sort.c @@ -0,0 +1,58 @@ +#include +#include +#include + +#define MAX_LEN 10000 +#define RANGE 10 + +int main() { + int numbers[MAX_LEN] = { 0 }; + + int size = 0; + scanf("%d", &size); + + srand(time(NULL)); + for (int i = 0; i < size; i++) { + numbers[i] = rand() % RANGE; + } + + // print the original array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + // TODO + for (int i = 1; i < size; i++) { + int key = numbers[i]; + int low = 0; + int high = i - 1; + + while (low <= high) { + int mid = (low + high) / 2; + if (key >= numbers[mid]) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + for (int j = i - 1; j >= low; j--) { + numbers[j + 1] = numbers[j]; + } + numbers[low] = key; + + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + } + + // Print the sorted array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/template/5-function/binary-search.c b/template/5-function/binary-search.c new file mode 100644 index 0000000..b2c8382 --- /dev/null +++ b/template/5-function/binary-search.c @@ -0,0 +1,37 @@ +// Created by hfwei on 2024/10/10. + +#include + +#define LEN 10 +int dictionary[LEN] = { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }; + +int main(void) { + int key = 0; + scanf("%d", &key); + + int low = 0; + int high = LEN - 1; + + int index = -1; + // TODO: binary search: search for key in dictionary[] + while (low <= high) { + int mid = (low + high) / 2; + if (dictionary[mid] == key) { + index = mid; +// break; + high = mid - 1; + } else if (dictionary[mid] > key) { + high = mid - 1; + } else { + low = mid + 1; + } + } + + if (index == -1) { + printf("Not found!\n"); + } else { + printf("The index of %d is %d.\n", key, index); + } + + return 0; +} \ No newline at end of file diff --git a/template/5-function/game-of-life-transformed.c b/template/5-function/game-of-life-transformed.c new file mode 100644 index 0000000..89e652f --- /dev/null +++ b/template/5-function/game-of-life-transformed.c @@ -0,0 +1,102 @@ +// +// Created by hengxin on 10/19/22. +// Run it with "Terminal" +// + +#include +#include +#include + +#define SIZE 6 + +void ExtendBoard(const int origin_board[][SIZE], + int extended_board[][SIZE + 2]); +void PrintExtendedBoard(const int extended_board[][SIZE + 2]); +void GenerateNewBoard(const int old_board[][SIZE + 2], + int new_board[][SIZE + 2]); +void CopyExtendedBoard(const int src_board[][SIZE + 2], + int dest_board[][SIZE + 2]); +void SleepAndClear(int sec); + +int main() { + const int board[SIZE][SIZE] = { + { 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0 } + }; + + int old_board[SIZE + 2][SIZE + 2] = { 0 }; + ExtendBoard(board, old_board); + PrintExtendedBoard(old_board); + SleepAndClear(1); + + int new_board[SIZE + 2][SIZE + 2] = { 0 }; + for (int round = 0; round < 10; round++) { + GenerateNewBoard(old_board, new_board); + SleepAndClear(1); + PrintExtendedBoard(new_board); + CopyExtendedBoard(new_board, old_board); + } + + return 0; +} + +void ExtendBoard(const int origin_board[][SIZE], + int extended_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + extended_board[row][col] = origin_board[row - 1][col - 1]; + } + } +} + +void PrintExtendedBoard(const int extended_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + printf("%c ", extended_board[row][col] ? '*' : ' '); + } + printf("\n"); + } +} + +void GenerateNewBoard(const int old_board[][SIZE + 2], + int new_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + // count the number of neighbours of old_board[row][col] + int neighbours = + old_board[row - 1][col - 1] + + old_board[row - 1][col] + + old_board[row - 1][col + 1] + + old_board[row][col - 1] + + old_board[row][col + 1] + + old_board[row + 1][col - 1] + + old_board[row + 1][col] + + old_board[row + 1][col + 1]; + + // evaluate the new board + if (old_board[row][col]) { // old_board[row][col] is alive + new_board[row][col] = (neighbours == 2 || neighbours == 3); + } else { // old_board[row][col] is dead + new_board[row][col] = (neighbours == 3); + } + } + } +} + +void CopyExtendedBoard(const int src_board[][SIZE + 2], + int dest_board[][SIZE + 2]) { + for (int row = 1; row <= SIZE; row++) { + for (int col = 1; col <= SIZE; col++) { + dest_board[row][col] = src_board[row][col]; + } + } +} + +void SleepAndClear(int sec) { + sleep(sec); + system("clear"); +} \ No newline at end of file diff --git a/template/5-function/game-of-life.c b/template/5-function/game-of-life.c new file mode 100644 index 0000000..eb4cfcb --- /dev/null +++ b/template/5-function/game-of-life.c @@ -0,0 +1,111 @@ +// Created by hfwei on 2024/10/16. + +#include +#include +#include + +#define SIZE 6 + +const int board[SIZE][SIZE] = { + { 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 1, 1, 0, 0, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0, 0, 0, 1, 1, 0 }, + { 0 } +}; + +//const int board1[SIZE][SIZE] = { +// [0] = { 0 }, +// [1] = { [1] = 1, [2] = 1 }, +// [2] = { [1] = 1, [2] = 1 }, +// [3] = { [3] = 1, [4] = 1 }, +// [4] = { [3] = 1, [4] = 1 }, +//}; + +//const int board2[SIZE][SIZE] = { +// [1][1] = 1, [1][2] = 1, +// [2][1] = 1, [2][2] = 1, +// [3][3] = 1, [3][4] = 1, +// [4][3] = 1, [4][4] = 1 +//}; + +//const int board3[SIZE][SIZE] = { +// 0, 0, 0, 0, 0, 0, +// 0, 1, 1, 0, 0, 0, +// 0, 1, 1, 0, 0, 0, +// 0, 0, 0, 1, 1, 0, +// 0, 0, 0, 1, 1, 0, +//}; + +int main(void) { + // TODO: play game-of-life + + // expand this board + int old_board[SIZE + 2][SIZE + 2] = {0}; + for (int i = 1; i <= SIZE; i++) { + for (int j = 1; j <= SIZE; j++) { + old_board[i][j] = board[i - 1][j - 1]; + } + } + + // print the old_board + for (int i = 1; i <= SIZE; i++) { + for (int j = 1; j <= SIZE; j++) { + printf("%c ", old_board[i][j] ? '*' : ' '); + } + printf("\n"); + } + system("clear"); + + // count live neighbors for each cell + int new_board[SIZE + 2][SIZE + 2] = {0}; + + for (int i = 0; i < 10; ++i) { + for (int i = 1; i <= SIZE; i++) { + for (int j = 1; j <= SIZE; j++) { + int num_of_live_neighbors = + old_board[i - 1][j - 1] + + old_board[i - 1][j] + + old_board[i - 1][j + 1] + + old_board[i][j - 1] + + old_board[i][j + 1] + + old_board[i + 1][j - 1] + + old_board[i + 1][j] + + old_board[i + 1][j + 1]; + + if (old_board[i][j] == 1) { + new_board[i][j] = (num_of_live_neighbors == 2 || num_of_live_neighbors == 3); + } else { + new_board[i][j] = num_of_live_neighbors == 3; + } + } + } + + // print the new board + for (int i = 1; i <= SIZE; i++) { + for (int j = 1; j <= SIZE; j++) { + printf("%c ", new_board[i][j] ? '*' : ' '); + } + printf("\n"); + } + + // #include (Linux, macOS) + sleep(1); + // + // Sleep(1000) + + // (Linux) + // Windows: stdlib.h (system("cls")) + system("clear"); + + // copy new board onto old board + for (int i = 1; i <= SIZE; i++) { + for (int j = 1; j <= SIZE; j++) { + old_board[i][j] = new_board[i][j]; + } + } + } + + return 0; +} \ No newline at end of file diff --git a/template/5-function/insertion-sort.c b/template/5-function/insertion-sort.c new file mode 100644 index 0000000..70dcbe3 --- /dev/null +++ b/template/5-function/insertion-sort.c @@ -0,0 +1,57 @@ +// Created by hfwei on 2024/10/16. +// Code generated by ChatGPT. + +#include +#include +#include + +#define MAX_LEN 10000 +#define RANGE 10 + +int main(void) { + int numbers[MAX_LEN] = { 0 }; + + int size = 0; + scanf("%d", &size); + + // generate an array of random integers between 0 and RANGE - 1 + srand(time(NULL)); + for (int i = 0; i < size; i++) { + numbers[i] = rand() % RANGE; + } + + // print the original array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + // TODO: insertion sort + for (int i = 1; i < size; i++) { + // numbers[0 .. i - 1] is already sorted + int key = numbers[i]; + + int j = i - 1; + while (j >= 0 && numbers[j] > key) { + numbers[j + 1] = numbers[j]; + j--; + } + numbers[j + 1] = key; + + // numbers[0 .. i] is already sorted + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + } + // i = size + // numbers[0 .. size - 1] is already sorted + + // print the sorted array + for (int i = 0; i < size; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/template/5-function/leap.c b/template/5-function/leap.c new file mode 100644 index 0000000..1f505d9 --- /dev/null +++ b/template/5-function/leap.c @@ -0,0 +1,37 @@ +// Created by hfwei on 2024/10/6. + +#include + +int main(void) { + int year = 0; + scanf("%d", &year); + + int leap = 0; + + // TODO: leap year or not (logical expressions) + // &&: and + // ||: or + // !: not !A is true <=> A is false + // short-circuit (短路求值) + // A && B: A; if A is false, B is passed + // A || B: A; if A is true, B is passed + if ((year % 4 == 0 && year % 100 != 0) || + year % 400 == 0) { + leap = 1; + } + +// leap = (year % 4 == 0 && year % 100 != 0) || +// year % 400 == 0; + + // 0: false; not 0: true + + // if (!leap) + // !leap is true <=> leap is false <=> leap == 0 + if (leap == 0) { + printf("%d is a common year\n", year); + } else { + printf("%d is a leap year\n", year); + } + + return 0; +} \ No newline at end of file diff --git a/template/5-function/merge.c b/template/5-function/merge.c new file mode 100644 index 0000000..9f724e3 --- /dev/null +++ b/template/5-function/merge.c @@ -0,0 +1,37 @@ +// Created by hfwei on 2024/10/16. + +#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++; + } + } + + 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/template/5-function/palindrome.c b/template/5-function/palindrome.c new file mode 100644 index 0000000..db44a2b --- /dev/null +++ b/template/5-function/palindrome.c @@ -0,0 +1,52 @@ +// Created by hfwei on 2024/10/10. + +#include +#include +#include + +#define LEN 21 +char string[LEN] = ""; + +int main() { + // example: nolemon,nomelon + printf("Input a string containing at most 20 characters.\n"); + scanf("%20s", string); + +// int len = 0; +// while (string[len] != '\0') { +// len++; +// } + + int len = strlen(string); + printf("The length of \"%s\" is %d.\n", string, len); + + // TODO: test palindrome + bool is_palindrome = true; + + // TODO: the for version +// for (int i = 0, j = len - 1; i < j; i++, j--) { +// if (string[i] != string[j]) { +// is_palindrome = false; +// break; +// } +// } + + // TODO: the while version + // Generated by ChatGPT + int i = 0; + int j = len - 1; + while (i < j) { + if (string[i] != string[j]) { + is_palindrome = false; + break; + } + i++; + j--; + } + + + printf("\"%s\" is %s a palindrome.\n", string, + is_palindrome ? "" : "not"); + + return 0; +} \ No newline at end of file diff --git a/template/5-function/primes.c b/template/5-function/primes.c new file mode 100644 index 0000000..b2360b9 --- /dev/null +++ b/template/5-function/primes.c @@ -0,0 +1,36 @@ +// Created by hfwei on 2024/10/10. + +#include +#include + +int main(void) { + int max = 0; + scanf("%d", &max); + + int count = 0; + + // scope + // TODO: print primes between 1 and max + for (int i = 2; i <= max; i++) { + // decide if i is a is_prime + // _Bool + // C99: bool, true (1), false (0) + bool is_prime = true; + + for (int j = 2; j * j <= i; j++) { + // if j is a factor of i + if (i % j == 0) { + is_prime = false; + break; + } + } + + if (is_prime) { + count++; +// printf("%d ", i); + } + } + + printf("\n %d ", count); + return 0; +} \ No newline at end of file diff --git a/template/5-function/selection-sort.c b/template/5-function/selection-sort.c new file mode 100644 index 0000000..9f020ac --- /dev/null +++ b/template/5-function/selection-sort.c @@ -0,0 +1,60 @@ +// Created by hfwei on 2024/10/10. + +#include + +#define LEN 20 +int numbers[LEN] = { 0 }; + +int main(void) { + /* + * Input the array + * + * Note: fails to run this program in "Run" (Ctrl + D) + * See: https://youtrack.jetbrains.com/issue/CPP-5704 + * Use "Terminal" instead. + * + * TODO: CLion; Terminal + * Linux: Ctrl + D (works now; in the new line, or Ctrl + D twice) + * See https://stackoverflow.com/a/21365313/1833118 (send and clear the buffer) + * Windows: Ctrl + Z (does not work on my platform) + * TODO: Input&Output redirection + * See https://stackoverflow.com/a/11788475/1833118 + */ + int len = -1; + // stream + // (1) input failure: EOF (-1) + // (2) match failure: assigned items >= 0 + while (scanf("%d", &numbers[++len]) != EOF); + + // sizeof numbers / sizeof(numbers[0]) + for (int i = 0; i < len; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + // TODO: selection sort + for (int i = 0; i < len; i++) { + // find the minimum of numbers[i .. len - 1] + int min = numbers[i]; + int min_index = i; + + for (int j = i + 1; j < len; j++) { + if (numbers[j] < min) { + min = numbers[j]; + min_index = j; + } + } + + // swap numbers[i] and numbers[min_index] + int temp = numbers[min_index]; + numbers[min_index] = numbers[i]; + numbers[i] = temp; + } + + for (int i = 0; i < len; i++) { + printf("%d ", numbers[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/template/5-function/stars.c b/template/5-function/stars.c new file mode 100644 index 0000000..f3602c5 --- /dev/null +++ b/template/5-function/stars.c @@ -0,0 +1,27 @@ +// Created by hfwei on 2024/10/10. + +#include + +int main(void) { + int lines = 0; + scanf("%d", &lines); + + // TODO: print stars pyramid + for (int i = 0; i < lines; i++) { + // print n - 1 - i spaces + for (int j = 0; j < lines - 1 - i; ++j) { + printf(" "); + } + + // print 2 * i + stars + for (int j = 0; j < 2 * i + 1; ++j) { + printf("*"); + } + + if (i < lines - 1) { + printf("\n"); + } + } + + return 0; +} \ No newline at end of file