@@ -0,0 +1,2 @@ | |||||
BasedOnStyle: LLVM | |||||
IndentWidth: 4 |
@@ -0,0 +1,19 @@ | |||||
#include <float.h> | |||||
#include <limits.h> | |||||
#include <stdio.h> | |||||
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); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex1.c -o ex1 | |||||
./ex1 |
@@ -0,0 +1,18 @@ | |||||
#include <stdio.h> | |||||
#include <string.h> | |||||
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); | |||||
} | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex2.c -o ex2 | |||||
./ex2 |
@@ -0,0 +1,54 @@ | |||||
#include <stdio.h> | |||||
#include <string.h> | |||||
/* 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); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex3.c -o ex3 | |||||
./ex3 |
@@ -0,0 +1,28 @@ | |||||
#include <ctype.h> | |||||
#include <stdio.h> | |||||
#include <string.h> | |||||
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); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex4.c -o ex4 | |||||
./ex4 |
@@ -0,0 +1,21 @@ | |||||
#include <stdio.h> | |||||
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)); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex5.c -o ex5 | |||||
./ex5 |
@@ -0,0 +1,58 @@ | |||||
#include <stdio.h> | |||||
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); | |||||
} |
@@ -0,0 +1,4 @@ | |||||
#!/bin/sh | |||||
gcc ex6.c -o ex6 | |||||
./ex6 |