-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
760 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
add_executable(game-of-life game-of-life.c) | ||
add_executable(game-of-life-chatgpt game-of-life-chatgpt.c) | ||
|
||
add_executable(insertion-sort insertion-sort.c) | ||
add_executable(insertion-sort-bsearch insertion-sort-bsearch.c) | ||
add_executable(binary-insertion-sort binary-insertion-sort.c) | ||
|
||
add_executable(merge merge.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 4-loops | ||
|
||
- `Alt + 6`: Problems on the status bar | ||
- `SonarLint` on the status bar | ||
|
||
## `game-of-life.c` | ||
|
||
- play with it | ||
- [wiki](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) | ||
- [Demo](https://playgameoflife.com/) | ||
- [Gosper_glider_gun](https://playgameoflife.com/lexicon/Gosper_glider_gun) | ||
- [LifeWiki](https://conwaylife.com/wiki/Main_Page) | ||
- [Life Lexicon Home Page](https://conwaylife.com/ref/lexicon/lex_home.htm) | ||
- 2D-array | ||
- initialization (Section 8.2.1) | ||
- row-major | ||
- row by row | ||
- indicator | ||
- extension of board | ||
- how many boards? | ||
- one round | ||
- multiple rounds | ||
- pause | ||
- screen clear | ||
- [ ] try a new board? | ||
- [Life Lexicon Home Page](https://conwaylife.com/ref/lexicon/lex_home.htm) | ||
|
||
# `merge.c` | ||
|
||
- examples | ||
- for `merge-sort.c` later | ||
|
||
# `insertion-sort.c` | ||
|
||
- `for` + `while` version | ||
- `for` + `for` version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Created by hfwei on 2024/10/16. | ||
// Code generated by ChatGPT. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <unistd.h> | ||
|
||
// Define grid dimensions | ||
#define ROWS 20 | ||
#define COLS 40 | ||
|
||
// Function to initialize the grid randomly | ||
void initializeGrid(int grid[ROWS][COLS]) { | ||
for (int i = 0; i < ROWS; i++) { | ||
for (int j = 0; j < COLS; j++) { | ||
grid[i][j] = rand() % 2; // 0 (dead) or 1 (alive) | ||
} | ||
} | ||
} | ||
|
||
// Function to print the grid | ||
void printGrid(int grid[ROWS][COLS]) { | ||
for (int i = 0; i < ROWS; i++) { | ||
for (int j = 0; j < COLS; j++) { | ||
if (grid[i][j] == 1) { | ||
printf("#"); // Alive cell | ||
} else { | ||
printf(" "); // Dead cell | ||
} | ||
} | ||
printf("\n"); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
// Function to update the grid for the next generation | ||
void updateGrid(int grid[ROWS][COLS]) { | ||
int newGrid[ROWS][COLS]; | ||
|
||
for (int i = 0; i < ROWS; i++) { | ||
for (int j = 0; j < COLS; j++) { | ||
int neighbors = 0; | ||
|
||
// Count neighbors | ||
for (int x = -1; x <= 1; x++) { | ||
for (int y = -1; y <= 1; y++) { | ||
if (x == 0 && y == 0) { continue; } // Skip the current cell | ||
int newX = i + x; | ||
int newY = j + y; | ||
|
||
if (newX >= 0 && newX < ROWS && newY >= 0 && newY < COLS) { | ||
neighbors += grid[newX][newY]; | ||
} | ||
} | ||
} | ||
|
||
// Apply Game of Life rules | ||
if (grid[i][j] == 1) { | ||
newGrid[i][j] = (neighbors == 2 || neighbors == 3) ? 1 : 0; | ||
} else { | ||
newGrid[i][j] = (neighbors == 3) ? 1 : 0; | ||
} | ||
} | ||
} | ||
|
||
// Update the grid | ||
for (int i = 0; i < ROWS; i++) { | ||
for (int j = 0; j < COLS; j++) { | ||
grid[i][j] = newGrid[i][j]; | ||
} | ||
} | ||
} | ||
|
||
int main(void) { | ||
int grid[ROWS][COLS]; | ||
|
||
// Seed the random number generator with the current time | ||
srand(time(NULL)); | ||
|
||
// Initialize the grid | ||
initializeGrid(grid); | ||
|
||
// Number of generations | ||
int generations = 50; | ||
|
||
for (int gen = 0; gen < generations; gen++) { | ||
system("clear"); // Use "clear" on Unix-based systems (Linux, macOS) | ||
printf("Generation %d:\n", gen); | ||
printGrid(grid); | ||
updateGrid(grid); | ||
sleep(1); // Sleep for 100ms | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Created by hfwei on 2024/10/16. | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.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(void) { | ||
// TODO: play game-of-life | ||
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 initial 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"); | ||
|
||
// old_board = apply the rule > new_board | ||
int new_board[SIZE + 2][SIZE + 2] = { 0 }; | ||
|
||
for (int i = 0; i < 10; ++i) { | ||
for (int row = 1; row <= SIZE; row++) { | ||
for (int col = 1; col <= SIZE; col++) { | ||
// counting live cells in the neighbour on old_board[row][col] | ||
int neighbors = | ||
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]; | ||
|
||
// apply the rule | ||
if (old_board[row][col]) { | ||
new_board[row][col] = (neighbors == 2 || neighbors == 3); | ||
} else { | ||
new_board[row][col] = (neighbors == 3); | ||
} | ||
} | ||
} | ||
|
||
for (int row = 1; row <= SIZE; row++) { | ||
for (int col = 1; col <= SIZE; col++) { | ||
printf("%c ", new_board[row][col] ? '*' : ' '); | ||
} | ||
printf("\n"); | ||
} | ||
// Linux: unistd.h | ||
sleep(1); | ||
// Windows: windows.h | ||
// Sleep(1000); | ||
|
||
// Linux: stdlib.h | ||
system("clear"); | ||
// Window: stdlib.h | ||
// system("cls"); | ||
|
||
// old_board <- new_board | ||
for (int row = 1; row <= SIZE; row++) { | ||
for (int col = 1; col <= SIZE; col++) { | ||
old_board[row][col] = new_board[row][col]; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#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"); | ||
|
||
// insertion sort with binary search | ||
for (int i = 1; i < size; i++) { | ||
int key = numbers[i]; | ||
int low = 0; | ||
int high = i - 1; | ||
|
||
int pos = -1; | ||
while (low <= high) { | ||
int mid = (low + high) / 2; | ||
if (key > numbers[mid]) { | ||
low = mid + 1; | ||
} else if (key < numbers[mid]) { | ||
high = mid - 1; | ||
} else { | ||
pos = mid; | ||
low = mid + 1; | ||
} | ||
} | ||
|
||
if (pos == -1) { | ||
pos = low; | ||
} else { | ||
pos++; | ||
} | ||
|
||
for (int j = i - 1; j >= pos; j--) { | ||
numbers[j + 1] = numbers[j]; | ||
} | ||
|
||
numbers[pos] = 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; | ||
} |
Oops, something went wrong.