Skip to content

Commit

Permalink
+5-function
Browse files Browse the repository at this point in the history
  • Loading branch information
hengxin committed Oct 24, 2024
1 parent 2764d85 commit 32dda6a
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 86 deletions.
Binary file removed 5-function/selection-sort
Binary file not shown.
Binary file modified pdf/5-function.pdf
Binary file not shown.
Binary file added template/5-function/5-function-template.pdf
Binary file not shown.
13 changes: 5 additions & 8 deletions template/5-function/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
# 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(stars-func stars.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(insertion-sort-func insertion-sort.c)
add_executable(binary-insertion-sort-func binary-insertion-sort.c)

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)
add_executable(game-of-life-transformed game-of-life-transformed.c)
3 changes: 1 addition & 2 deletions template/5-function/binary-search.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ int main(void) {
int mid = (low + high) / 2;
if (dictionary[mid] == key) {
index = mid;
// break;
high = mid - 1;
break;
} else if (dictionary[mid] > key) {
high = mid - 1;
} else {
Expand Down
3 changes: 1 addition & 2 deletions template/5-function/game-of-life.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ int main(void) {
}

// #include <unistd.h> (Linux, macOS)
// <windows.h> Sleep(1000)
sleep(1);
// <windows.h>
// Sleep(1000)

// <stdlib.h> (Linux)
// Windows: stdlib.h (system("cls"))
Expand Down
13 changes: 0 additions & 13 deletions template/5-function/leap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,11 @@ int main(void) {
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 {
Expand Down
22 changes: 1 addition & 21 deletions template/5-function/palindrome.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,19 @@ int main() {
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) {
for (int i = 0, j = len - 1; i < j; i++, j--) {
if (string[i] != string[j]) {
is_palindrome = false;
break;
}
i++;
j--;
}


printf("\"%s\" is %s a palindrome.\n", string,
is_palindrome ? "" : "not");

Expand Down
6 changes: 1 addition & 5 deletions template/5-function/primes.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ int main(void) {

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++) {
Expand All @@ -27,10 +23,10 @@ int main(void) {

if (is_prime) {
count++;
// printf("%d ", i);
}
}

printf("\n %d ", count);

return 0;
}
58 changes: 23 additions & 35 deletions template/5-function/selection-sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,44 @@
#define LEN 20
int numbers[LEN] = { 0 };

void SelectionSort(int arr[], int len);
void Print(const int arr[], int len);

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");
Print(numbers, len);
SelectionSort(numbers, len);
Print(numbers, len);

// TODO: selection sort
return 0;
}

void SelectionSort(int arr[], int len) {
for (int i = 0; i < len; i++) {
// find the minimum of numbers[i .. len - 1]
int min = numbers[i];
// find the minimum value of numbers[i .. n-1]
int min = arr[i];
int min_index = i;

for (int j = i + 1; j < len; j++) {
if (numbers[j] < min) {
min = numbers[j];
for (int j = i + 1; j <= len - 1; ++j) {
if (arr[j] < min) {
min = arr[j];
min_index = j;
}
}

// swap numbers[i] and numbers[min_index]
int temp = numbers[min_index];
numbers[min_index] = numbers[i];
numbers[i] = temp;
// swap arr[i] and arr[min_index]
int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}

void Print(const int arr[], int len) {
printf("\n");
for (int i = 0; i < len; i++) {
printf("%d ", numbers[i]);
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

0 comments on commit 32dda6a

Please sign in to comment.