Skip to content

Commit

Permalink
+5-function/
Browse files Browse the repository at this point in the history
  • Loading branch information
hengxin committed Oct 23, 2024
1 parent 436b9a5 commit 640a43a
Show file tree
Hide file tree
Showing 29 changed files with 1,313 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 4-loops/insertion-sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions 5-function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions 5-function/binary-insertion-sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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;
}
49 changes: 49 additions & 0 deletions 5-function/binary-search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Created by hfwei on 2024/10/23.

#include <stdio.h>

#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;
}
100 changes: 100 additions & 0 deletions 5-function/game-of-life-transformed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Created by hfwei on 2024/10/23.
// Run it with "Terminal"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#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");
}
98 changes: 98 additions & 0 deletions 5-function/game-of-life.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Created by hfwei on 2024/10/23.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <synchapi.h>

#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 <unistd.h>
sleep(1);
// Windows: #include <windows.h>: Sleep(ms)
// Sleep(1000);

// clear the screen
// Linux: #include <stdlib.h>
system("clear");
// Windows: #include <stdlib.h> 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;
}
Loading

0 comments on commit 640a43a

Please sign in to comment.