diff --git a/week01/ex4 b/week01/ex4 index d913823..0efc61a 100755 Binary files a/week01/ex4 and b/week01/ex4 differ diff --git a/week02/.clang-format b/week02/.clang-format new file mode 100644 index 0000000..5bf2f83 --- /dev/null +++ b/week02/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +IndentWidth: 4 \ No newline at end of file diff --git a/week02/ex1 b/week02/ex1 new file mode 100755 index 0000000..0bb6ad4 Binary files /dev/null and b/week02/ex1 differ diff --git a/week02/ex1.c b/week02/ex1.c new file mode 100644 index 0000000..a2323aa --- /dev/null +++ b/week02/ex1.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int main() { + int i = INT_MAX; + unsigned short us = USHRT_MAX; + long l = LONG_MAX; + float f = FLT_MAX; + double d = DBL_MAX; + + printf("int: %lu, %d\n" + "unsigned short: %lu, %hu\n" + "long: %lu, %ld\n" + "float: %lu, %f\n" + "double: %lu, %lf\n", + sizeof(i), i, sizeof(us), us, sizeof(l), l, sizeof(f), f, sizeof(d), + d); +} \ No newline at end of file diff --git a/week02/ex1.sh b/week02/ex1.sh new file mode 100755 index 0000000..d554831 --- /dev/null +++ b/week02/ex1.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex1.c -o ex1 +./ex1 \ No newline at end of file diff --git a/week02/ex2 b/week02/ex2 new file mode 100755 index 0000000..2f03a04 Binary files /dev/null and b/week02/ex2 differ diff --git a/week02/ex2.c b/week02/ex2.c new file mode 100644 index 0000000..7538e42 --- /dev/null +++ b/week02/ex2.c @@ -0,0 +1,18 @@ +#include +#include + +int main() { + char s[257], c; + int i = 0; + while ((c = getc(stdin)) != '.') { + if (c == '\n') + break; + s[i++] = c; + } + s[i] = '\0'; + // i is already the size of s. I've used strlen to follow the 'hints' + // section. + for (i = strlen(s) - 1; i >= 0; --i) { + putc(s[i], stdout); + } +} \ No newline at end of file diff --git a/week02/ex2.sh b/week02/ex2.sh new file mode 100755 index 0000000..49e4f0d --- /dev/null +++ b/week02/ex2.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex2.c -o ex2 +./ex2 \ No newline at end of file diff --git a/week02/ex3 b/week02/ex3 new file mode 100755 index 0000000..3245d8e Binary files /dev/null and b/week02/ex3 differ diff --git a/week02/ex3.c b/week02/ex3.c new file mode 100644 index 0000000..7572bec --- /dev/null +++ b/week02/ex3.c @@ -0,0 +1,54 @@ +#include +#include + +/* DISCLAIMER: As far as I understand, I need to create a function, + which takes a long long number (not a string), convert it to a + string, use that string as it was a number in the source + number system, and then print out in the target number system. + + I think it is way better to use string for the number itself and + read it as a string from the beginning. But I will follow the + assignment instructions since I don't want to get a lower grade. + Please, make it more clear next time. + */ + +void convert(long long n, int s, int t) { + if (!(s >= 2 && s <= 10 && t >= 2 && t <= 10)) { + printf("cannot convert!\n"); + return; + } + + // convert n from source number system to normal long long integer + long long n_actual = 0, cur = 1; + while (n) { + if (n % 10 >= s) { + printf("cannot convert!\n"); + return; + } + n_actual += cur * (n % 10); + cur *= s; + n /= 10; + } + + // convert the actual n to target number system + char buf[65]; + int i = 0; + while (n_actual) { + buf[i++] = (n_actual % t) + '0'; + n_actual /= t; + } + buf[i] = '\0'; + + for (--i; i >= 0; --i) { + putc(buf[i], stdout); + } + + putc('\n', stdout); +} + +int main() { + long long number; + int source, target; + scanf("%lld%d%d", &number, &source, &target); + convert(number, source, target); +} \ No newline at end of file diff --git a/week02/ex3.sh b/week02/ex3.sh new file mode 100755 index 0000000..482c6bd --- /dev/null +++ b/week02/ex3.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex3.c -o ex3 +./ex3 \ No newline at end of file diff --git a/week02/ex4 b/week02/ex4 new file mode 100755 index 0000000..a42c2f9 Binary files /dev/null and b/week02/ex4 differ diff --git a/week02/ex4.c b/week02/ex4.c new file mode 100644 index 0000000..797a2f0 --- /dev/null +++ b/week02/ex4.c @@ -0,0 +1,28 @@ +#include +#include +#include + +int count(char s[], char c) { + int cnt = 0, sz = strlen(s); + for (int i = 0; i < sz; ++i) { + if (tolower(s[i]) == tolower(c)) + cnt++; + } + + return cnt; +} + +void countAll(char s[]) { + int sz = strlen(s); + for (int i = 0; i < sz; ++i) { + int cnt = count(s, s[i]); + if (cnt > 0) + printf("%c:%d%s", tolower(s[i]), cnt, (i == sz - 1 ? "\n" : ", ")); + } +} + +int main() { + char s[257]; + scanf("%s", s); + countAll(s); +} \ No newline at end of file diff --git a/week02/ex4.sh b/week02/ex4.sh new file mode 100755 index 0000000..7c5306b --- /dev/null +++ b/week02/ex4.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex4.c -o ex4 +./ex4 \ No newline at end of file diff --git a/week02/ex5 b/week02/ex5 new file mode 100755 index 0000000..74b5561 Binary files /dev/null and b/week02/ex5 differ diff --git a/week02/ex5.c b/week02/ex5.c new file mode 100644 index 0000000..4c9995c --- /dev/null +++ b/week02/ex5.c @@ -0,0 +1,21 @@ +#include + +long long tribonacci(int n) { + long long a = 0, b = 1, c = 1; + if (n == 0) + return a; + if (n == 1) + return b; + if (n == 2) + return c; + + for (int i = 3; i <= n; ++i) { + long long d = a + b + c; + a = b, b = c, c = d; + } + return c; +} + +int main() { + printf("%lld\n%lld\n", tribonacci(4), tribonacci(36)); +} \ No newline at end of file diff --git a/week02/ex5.sh b/week02/ex5.sh new file mode 100755 index 0000000..d924681 --- /dev/null +++ b/week02/ex5.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex5.c -o ex5 +./ex5 \ No newline at end of file diff --git a/week02/ex6 b/week02/ex6 new file mode 100755 index 0000000..dc33eb8 Binary files /dev/null and b/week02/ex6 differ diff --git a/week02/ex6.c b/week02/ex6.c new file mode 100644 index 0000000..4359552 --- /dev/null +++ b/week02/ex6.c @@ -0,0 +1,58 @@ +#include + +void triangle(int n) { + for (int i = 1; i <= n; ++i) { + for (int j = 1; j <= n - i; ++j) + printf(" "); + + for (int j = 1; j <= i * 2 - 1; ++j) { + printf("*"); + } + printf("\n"); + } +} + +void triangle2(int n) { + for (int i = 1; i <= n; ++i) { + for (int j = 1; j <= i; ++j) { + printf("*"); + } + printf("\n"); + } +} + +void arrow(int n) { + for (int i = 1; i <= (n + 1) / 2; ++i) { + for (int j = 1; j <= i; ++j) { + printf("*"); + } + printf("\n"); + } + for (int i = n / 2; i >= 1; --i) { + for (int j = 1; j <= i; ++j) { + printf("*"); + } + printf("\n"); + } +} + +void rectangle(int n, int m) { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + printf("*"); + } + printf("\n"); + } +} + +int main() { + int n; + printf("Specify number of rows: "); + scanf("%d", &n); + // triangle(n); + triangle2(n); + printf("\n"); + arrow(n); + printf("\n"); + rectangle(n, n); +} \ No newline at end of file diff --git a/week02/ex6.sh b/week02/ex6.sh new file mode 100755 index 0000000..1ef5ed3 --- /dev/null +++ b/week02/ex6.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc ex6.c -o ex6 +./ex6 \ No newline at end of file