Skip to content

Commit

Permalink
template: +3-for-a-while
Browse files Browse the repository at this point in the history
hengxin committed Oct 9, 2024
1 parent 33bbfed commit b135775
Showing 12 changed files with 275 additions and 0 deletions.
15 changes: 15 additions & 0 deletions template/3-for-a-while/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# for
add_executable(stars stars.c)
add_executable(primes primes.c)

# while (do-while)
add_executable(binary-search binary-search.c)
add_executable(binary-search-for binary-search-for.c)

add_executable(digits-while digits-while.c)
add_executable(digits-do-while digits-do-while.c)
add_executable(digits-for digits-for.c)

# for-a-while
add_executable(selection-sort selection-sort.c)
add_executable(palindrome palindrome.c)
72 changes: 72 additions & 0 deletions template/3-for-a-while/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# `3-for-a-while`

## `stars.c`

- double loops
- `for (int i = 0) + for (int j = 0)`

## `primes.c`

- double loops
- `int is_prime = 1;`: why 1? why not 0?
- `if (is_prime)` vs. `if (is_prime != 0)` vs. `if (is_prime == 1)`
- testing
- https://www.wolframalpha.com/input?i=+primes+less+than+100000
- mma: `PrimePi[100000]`
- `number = 2`
- `break`
- `i * i <= number` vs. `i * i < number`
- `stdbool.h`
- C89, C99, C23
- `bool b = 5`
- `(bool) 3.5`
- [x] timing
- `clock_t start = clock(); clock_t end = clock(); (end - start) / CLOCKS_PER_SEC`

# `binary-search.c`

- already sorted array
- Fib
- `int index = -1;`
- `printf`
- `break`
- testing
- `1`: the leftmost/rightmost one
- search for the leftmost/rightmost one
- [ ] learn from the standard library???
- `(low + high) / 2`
- `low + (high - low) / 2`
- [ ] try it???

## `digits.c`

- testing
- `do-while`

## `selection-sort.c`

- preparation: scanf
- with comments
- `swap`
- `while (scanf ...)`
- https://en.cppreference.com/w/c/io/fscanf
- Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before
the first receiving argument was assigned)
- or `EOF` if input failure occurs before the first receiving argument was assigned
- How to run this?
- Linux: `Ctrl + D` at the beginning of a line
- Mac: `Cmd + D` at the beginning of a line
- Windows: `Ctrl + Z` at the beginning of a line
- more `printf` (after each iteration)
- `sizeof`
- Input&Output indirection
- Linux/Windows Cmd

## `palindrome.c`

- `#define`: pre-processing
- `scanf("%20s", string);`
- `strlen`
- comma expression
- `for` version
- `while` version
23 changes: 23 additions & 0 deletions template/3-for-a-while/binary-search-for.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

#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 index = -1;



if (index == -1) {
printf("Not found!\n");
} else {
printf("The index of %d is %d.\n", key, index);
}

return 0;
}
23 changes: 23 additions & 0 deletions template/3-for-a-while/binary-search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

#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);

// TODO: binary search: search for key in dictionary[]



// if (index == -1) {
// printf("Not found!\n");
// } else {
// printf("The index of %d is %d.\n", key, index);
// }

return 0;
}
1 change: 1 addition & 0 deletions template/3-for-a-while/birth
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1958 1961 1969 1954 1969
16 changes: 16 additions & 0 deletions template/3-for-a-while/digits-do-while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

int main(void) {
int number = 0;
scanf("%d", &number);

// TODO: number of digits


// printf("Number of digits is %d\n",
// num_of_digits);

return 0;
}
13 changes: 13 additions & 0 deletions template/3-for-a-while/digits-for.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

int main(void) {
int number = 0;
scanf("%d", &number);


// printf("Number of digits is %d\n", num_of_digits);

return 0;
}
17 changes: 17 additions & 0 deletions template/3-for-a-while/digits-while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

int main(void) {
int number = 0;
scanf("%d", &number);

// TODO: number of digits



// printf("Number of digits is %d\n",
// num_of_digits);

return 0;
}
28 changes: 28 additions & 0 deletions template/3-for-a-while/palindrome.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define LEN 21
char string[LEN] = "";

int main() {
// example: nolemon,nomelon
printf("Input a string containing at most 20 characters.\n");
scanf("%20s", string);

// printf("The length of \"%s\" is %d.\n", string, len);

// TODO: test palindrome

// TODO: the for version

// TODO: the while version


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

return 0;
}
14 changes: 14 additions & 0 deletions template/3-for-a-while/primes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>
#include <stdbool.h>

int main(void) {
int max = 0;
scanf("%d", &max);

// TODO: print primes between 1 and max


return 0;
}
40 changes: 40 additions & 0 deletions template/3-for-a-while/selection-sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

#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;
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++) {
// printf("%d ", numbers[i]);
// }
// printf("\n");

return 0;
}
13 changes: 13 additions & 0 deletions template/3-for-a-while/stars.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Created by hfwei on 2024/10/10.

#include <stdio.h>

int main(void) {
int lines = 0;
scanf("%d", &lines);

// TODO: print stars pyramid


return 0;
}

0 comments on commit b135775

Please sign in to comment.