├── .gitignore ├── Additional Exercises └── Control preparation CS 1 - 31.10.2023 │ ├── README.md │ └── Solutions │ ├── task1.cpp │ ├── task2.cpp │ ├── task3.cpp │ ├── task4.cpp │ ├── task5.cpp │ ├── task6.cpp │ └── task7.cpp ├── Control & Homework Solutions ├── Computer Science 1 │ ├── Control 1 │ │ ├── Variant 1 │ │ │ ├── task1.cpp │ │ │ ├── task2.cpp │ │ │ ├── task3.cpp │ │ │ └── task4.cpp │ │ └── Variant 2 │ │ │ ├── task1.cpp │ │ │ ├── task2.cpp │ │ │ ├── task3.cpp │ │ │ └── task4.cpp │ ├── Control 2 │ │ ├── Variant 1 │ │ │ ├── task1.cpp │ │ │ ├── task2.cpp │ │ │ └── task3.cpp │ │ └── Variant 2 │ │ │ ├── task1.cpp │ │ │ ├── task2.cpp │ │ │ └── task3.cpp │ ├── Control Practicum │ │ ├── task1.cpp │ │ └── task2.cpp │ └── Homework │ │ ├── task1.cpp │ │ └── task2.cpp └── Informatics │ ├── Control 1 │ ├── Variant 1 │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ └── task4.cpp │ └── Variant 2 │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ └── task4.cpp │ ├── Control 2 │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp │ ├── Homework 1 │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp │ ├── Homework 2 │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp │ └── Homework 3 │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp ├── Practicum ├── Week 01 │ ├── README.md │ ├── snippets │ │ ├── 01-dataTypes.cpp │ │ ├── 02-consoleIO.cpp │ │ ├── 03-declarationAndInitialization.cpp │ │ ├── 04-constants.cpp │ │ ├── 05-arithmeticOperators.cpp │ │ ├── 06-compoundAssignment.cpp │ │ ├── 07-incrementAndDecrement.cpp │ │ ├── 08-relationalAndComparisonOperators.cpp │ │ ├── 09-logicalOperators.cpp │ │ ├── 10-ternaryOperator.cpp │ │ ├── 11-climits.cpp │ │ ├── 12-cmath.cpp │ │ └── 13-iomanip.cpp │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4_1.cpp │ │ ├── task4_2.cpp │ │ ├── task5_1.cpp │ │ ├── task5_2.cpp │ │ ├── task6.cpp │ │ └── task7.cpp ├── Week 02 │ ├── README.md │ ├── snippets │ │ ├── 01-absTypes.cpp │ │ ├── 02-switchFallThroughs.cpp │ │ ├── 03-shortCircuitEval.cpp │ │ └── 04-ifElseIf.cpp │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task5.cpp │ │ ├── task6.cpp │ │ └── task7.cpp ├── Week 03 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5.cpp │ │ └── task6.cpp ├── Week 04 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5.cpp │ │ ├── task6.cpp │ │ ├── task7.cpp │ │ └── task8.cpp ├── Week 05 │ ├── README.md │ └── solutions │ │ └── tasks.cpp ├── Week 06 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5_1.cpp │ │ ├── task5_2.cpp │ │ ├── task6.cpp │ │ ├── task7.cpp │ │ └── task8.cpp ├── Week 07 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5.cpp │ │ └── task6.cpp ├── Week 08 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5.cpp │ │ ├── task6.cpp │ │ ├── task7.cpp │ │ └── task8.cpp ├── Week 09 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5.cpp │ │ ├── task6.cpp │ │ ├── task7.cpp │ │ └── task8.cpp ├── Week 10 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ ├── task5.cpp │ │ ├── task6.cpp │ │ ├── task7.cpp │ │ └── task8.cpp ├── Week 11 │ ├── README.md │ └── solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ ├── task3.cpp │ │ ├── task4.cpp │ │ └── task5.cpp ├── Week 12 │ └── README.md └── Week 13 │ └── README.md ├── README.md ├── Seminars ├── Week 01 │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp ├── Week 02 │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp ├── Week 03 │ ├── NumericSystemConversion.cpp │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ └── Task3.cpp ├── Week 04 │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp ├── Week 05 │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3-1.cpp │ │ ├── Task3-2.cpp │ │ ├── Task3.cpp │ │ ├── Task4-1.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp ├── Week 06 │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ └── Task6.cpp ├── Week 07 │ ├── BinarySearch.cpp │ ├── BubbleSort.cpp │ ├── InsertionSort.cpp │ ├── README.md │ ├── SelectionSort.cpp │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp ├── Week 08 │ ├── README.md │ ├── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp │ └── tic-tac-toe.cpp ├── Week 09 │ ├── README.md │ ├── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp │ ├── debug_new.h │ └── showcasing_debug_new.cpp ├── Week 10 │ ├── README.md │ ├── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ └── Task7.cpp │ └── String_Functions.cpp ├── Week 11 │ ├── README.md │ ├── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ └── Task4.cpp │ ├── notes.cpp │ └── stack_size.c ├── Week 12 │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ └── Task4.cpp ├── Week 13 │ ├── README.md │ └── Solutions │ │ ├── task1.cpp │ │ ├── task2.cpp │ │ └── task3.cpp └── Week 14 │ └── README.md └── Utils └── InstallationGuide.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | # VS Code 35 | .vscode -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/README.md: -------------------------------------------------------------------------------- 1 |

Подготовка за контролно 1 КН

2 | 3 |

Задачи

4 | 5 | **Задача 1:** Да се въведе цяло положително число n и цяло число x, след което да се въведат n на брой цели числа и накрая да се отпечата, колкото от тези числа са по-малки от x. 6 | 7 | *Вход:*
8 | *3 5*
9 | *6 1 -5* 10 | 11 | *Изход: 2* 12 | 13 | *Вход:*
14 | *7 -5*
15 | *2 -1 -10 2 -9657 -5 2* 16 | 17 | *Изход: 2* 18 | 19 | **Задача 2:** Да се направи функция, на която се подава цяло положително число n и отпечатва ромб, който да е с големина 2n - 1. 20 | 21 | *Вход: 4*
22 | *Изход:* 23 |

24 |     *
25 |    ***
26 |   *****
27 |  *******
28 |   *****
29 |    ***
30 |     * 31 |

32 | 33 | **Задача 3:** Да се напише функция, която при подадени цяло положително число n и реално число x, да въвежда n на брой реални числа и да пресмята стойността на полинома: *a1 * x^(n) + a2 * x^(n-1) + … + an * x + an+1* 34 | 35 | *Вход:*
36 | *3 0.5*
37 | *1 3 1 2* 38 | 39 | *Изход: 3.375* 40 | 41 | **Задача 4:** Да се въведе цяло положително число n. Да се направи функция, която взима цифрите на дадено число и намира сбора им. Трябва да прилагаме тази функция върху n, докато n не стане цифра. 42 | 43 | *Вход: 3921478*
44 | *Изход: 7* 45 | 46 | **Задача 5:** Пешо пуска топка от някаква височина x, топката има коефицент на отскок y. При пускане на топката след отскока тя достига височина x * y. Да се напише функция, която при подадени x и y, намира колко отскока ще направи топката, така че максималната и височина да е > 1. 47 | 48 | *Вход: 10 0.66*
49 | *Изход: 5* 50 | 51 | *Пояснение:*
52 | 10
53 | 6.6
54 | 4.356
55 | 2.87496
56 | 1.89747
57 | 1.25233
58 | 0.82654 59 | 60 | **Задача 6:** Да се напише функция, която принтира всички Armstrong числа в интервалът [1, 1000]. Armstrong число е такова число е такова, число на което сумата от цифрите му вдигната на степен броя цифри на числото, да дава самото число. 61 | 62 | *Изход:*
63 | *1 2 3 4 5 6 7 8 9 81 512* 64 | 65 | **Задача 7:** Да се направи функция, която при подадени 3 цели положителни числа a, b. c, да се отпечатат всички числа в интервал [a, b], които имат c прости делителя. 66 | 67 | *Вход:5 25 2*
68 | *Изход: 6 10 12 14 15 18 20 21 22 24* -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int countNumbersLessThanX(int n, int x) 4 | { 5 | int number; 6 | int counter = 0; 7 | for (int i = 1; i <= n; i++) 8 | { 9 | std::cin >> number; 10 | if (number < x) 11 | counter++; 12 | } 13 | return counter; 14 | } 15 | 16 | int main() 17 | { 18 | int x, n; 19 | std::cin >> n >> x; 20 | std::cout << countNumbersLessThanX(n, x) << std::endl; 21 | } -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printStarPyramid(int n) 4 | { 5 | 6 | for (int i = 0; i <= n; i++) 7 | { 8 | for (int j = n - 1; j >= i; j--) 9 | std::cout << ' '; 10 | for (int j = 1; j <= 2 * i - 1; j++) 11 | std::cout << '*'; 12 | 13 | std::cout << "\n"; 14 | } 15 | for (int i = n - 1; i > 0; i--) 16 | { 17 | for (int j = i; j <= n - 1; j++) 18 | std::cout << ' '; 19 | for (int j = 1; j <= 2 * i - 1; j++) 20 | std::cout << '*'; 21 | std::cout << "\n"; 22 | } 23 | } 24 | 25 | int main() 26 | { 27 | int n; 28 | std::cin >> n; 29 | printStarPyramid(n); 30 | } -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double pow(double x, int y) 4 | { 5 | double result = 1; 6 | for (int i = 1; i <= y; i++) 7 | { 8 | result *= x; 9 | } 10 | return result; 11 | } 12 | 13 | double f(double x, int n) 14 | { 15 | double powerX = pow(x, n); 16 | double sum = 0; 17 | double coef; 18 | for (int i = 0; i <= n; i++) 19 | { 20 | std::cin >> coef; 21 | sum += powerX * coef; 22 | powerX /= x; 23 | } 24 | return sum; 25 | } 26 | 27 | int main() 28 | { 29 | double x, n; 30 | std::cin >> x >> n; 31 | std::cout << f(x, n) << std::endl; 32 | } -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sumOfDigits(int n) 4 | { 5 | int sum = 0; 6 | while (n != 0) 7 | { 8 | sum += n % 10; 9 | n /= 10; 10 | } 11 | return sum; 12 | } 13 | 14 | int addDigits(int n) 15 | { 16 | while (n >= 10) 17 | { 18 | n = sumOfDigits(n); 19 | } 20 | return n; 21 | } 22 | 23 | int main() 24 | { 25 | int n; 26 | std::cin >> n; 27 | std::cout << addDigits(n) << std::endl; 28 | } -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int countBounces(double n, double coef) 4 | { 5 | int count = 0; 6 | while (n > 1) 7 | { 8 | if (n * coef > 1) 9 | count++; 10 | n = n * coef; 11 | } 12 | return count; 13 | } 14 | 15 | int main() 16 | { 17 | double n, coef; 18 | std::cin >> n >> coef; 19 | std::cout << countBounces(n, coef) << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int pow(int n, int power){ 4 | 5 | int result = 1; 6 | 7 | for(int i = 0; i < power; ++i) 8 | result *= n; 9 | 10 | return result; 11 | 12 | } 13 | 14 | bool IsArmstrong(int n){ 15 | 16 | int counter = 0, sum = 0, copyN = n; 17 | 18 | while(copyN){ 19 | 20 | sum += copyN % 10; 21 | ++counter; 22 | copyN /= 10; 23 | 24 | } 25 | 26 | return n == pow(sum, counter); 27 | 28 | } 29 | 30 | int main(){ 31 | 32 | for(int i = 1; i <= 1000; ++i) 33 | if(IsArmstrong(i)) 34 | std::cout << i << ' '; 35 | 36 | } -------------------------------------------------------------------------------- /Additional Exercises/Control preparation CS 1 - 31.10.2023/Solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int getPrimeDivisorsCount(int n) 4 | { 5 | int count = 0; 6 | int lastNum = 1; 7 | int index = 2; 8 | while (n > 1) 9 | { 10 | if (n % index == 0) 11 | { 12 | n /= index; 13 | if (lastNum != index) 14 | count++; 15 | lastNum = index; 16 | } 17 | else 18 | index++; 19 | } 20 | return count; 21 | } 22 | 23 | void printNumbers(int a, int b, int c) 24 | { 25 | 26 | for (int i = a; i <= b; i++) 27 | { 28 | if (getPrimeDivisorsCount(i) == c) 29 | std::cout << i << ' '; 30 | } 31 | } 32 | 33 | int main() 34 | { 35 | int a, b, c; 36 | std::cin >> a >> b >> c; 37 | printNumbers(a, b, c); 38 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 1/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char Relation(unsigned a, unsigned b){ 4 | return a < b ? '<' : a > b ? '>' : '='; 5 | } 6 | 7 | int main(){ 8 | 9 | unsigned a, b, c; 10 | std::cin >> a >> b >> c; 11 | 12 | if(!a || !b || !c){ 13 | 14 | std::cout << "Invalid input\n"; 15 | return -1; 16 | 17 | } 18 | 19 | std::cout << a << ' ' << Relation(a, b) << ' ' << b << ' ' << Relation(b, c) << ' ' << c << ' ' << Relation(c, a) << ' ' << a << '\n'; 20 | 21 | return 0; 22 | 23 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 1/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Divisors(int a, int b, int x){ 4 | 5 | for(int i = a; i <= b; ++i){ 6 | 7 | if(!i) continue; 8 | if(!(x % i)) std::cout << i << ' '; 9 | 10 | } 11 | 12 | } 13 | 14 | int main(){ 15 | 16 | int a, b, x; 17 | std::cin >> a >> b >> x; 18 | Divisors(a, b, x); 19 | 20 | return 0; 21 | 22 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 1/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned N, a, prev, count = 1; 6 | std::cin >> N; 7 | 8 | if(N == 0){ 9 | 10 | std::cout << 0 << '\n'; 11 | return 0; 12 | 13 | } 14 | 15 | std::cin >> prev; 16 | 17 | for(unsigned i = 1; i < N; ++i){ 18 | 19 | std::cin >> a; 20 | 21 | if(a >= prev) ++count; 22 | else break; 23 | 24 | prev = a; 25 | 26 | } 27 | 28 | std::cout << count << '\n'; 29 | 30 | return 0; 31 | 32 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 1/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned Factorial(unsigned n){ 4 | 5 | unsigned fact = 1; 6 | for(unsigned i = 2; i <= n; ++i) fact *= i; 7 | return fact; 8 | 9 | } 10 | 11 | int main(){ 12 | 13 | unsigned N, count = 0; 14 | std::cin >> N; 15 | 16 | if(!N){ 17 | 18 | std::cout << "Invalid input\n"; 19 | return -1; 20 | 21 | } 22 | 23 | for(unsigned i = 1; Factorial(i) <= N; ++i) ++count; 24 | 25 | std::cout << count << '\n'; 26 | 27 | return 0; 28 | 29 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 2/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(unsigned &a, unsigned &b){ 4 | 5 | unsigned c = a; 6 | a = b; 7 | b = c; 8 | 9 | } 10 | 11 | int main(){ 12 | 13 | unsigned a, b, c; 14 | std::cin >> a >> b >> c; 15 | 16 | if(!a || !b || !c){ 17 | 18 | std::cout << "Invalid input\n"; 19 | return -1; 20 | 21 | } 22 | 23 | if(a > b) Swap(a, b); 24 | if(b > c) Swap(b, c); 25 | if(a > b) Swap(a, b); 26 | 27 | std::cout << a << ' ' << b << ' ' << c << '\n'; 28 | 29 | return 0; 30 | 31 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 2/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int Multiples(int a, int b, int x){ 4 | 5 | int count = 0; 6 | 7 | for(int i = a; i <= b; ++i){ 8 | 9 | if(!i) continue; 10 | count += !(i % x); 11 | 12 | } 13 | 14 | return count; 15 | 16 | } 17 | 18 | int main(){ 19 | 20 | int a, b, x; 21 | std::cin >> a >> b >> x; 22 | std::cout << Multiples(a, b, x) << '\n'; 23 | 24 | return 0; 25 | 26 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 2/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned S, N, a, count = 0, sum = 0; 6 | std::cin >> S >> N; 7 | 8 | if(N == 0){ 9 | 10 | std::cout << 0 << '\n'; 11 | return 0; 12 | 13 | } 14 | 15 | for(unsigned i = 0; i < N; ++i){ 16 | 17 | std::cin >> a; 18 | sum += a; 19 | 20 | if(sum < S) ++count; 21 | else break; 22 | 23 | } 24 | 25 | std::cout << count << '\n'; 26 | 27 | return 0; 28 | 29 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 1/Variant 2/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned Pow(unsigned n, unsigned power){ 4 | 5 | unsigned result = 1; 6 | for(unsigned i = 1; i <= power; ++i) result *= n; 7 | return result; 8 | 9 | } 10 | 11 | int main(){ 12 | 13 | unsigned N, K, count = 0; 14 | std::cin >> N >> K; 15 | 16 | if(!N){ 17 | 18 | std::cout << "Invalid input\n"; 19 | return -1; 20 | 21 | } 22 | 23 | for(unsigned i = 1; Pow(K, i) <= N; ++i) ++count; 24 | 25 | std::cout << count << '\n'; 26 | 27 | return 0; 28 | 29 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 2/Variant 1/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const size_t COLS=10, ROWS=5; 4 | 5 | bool AreSymtrical(double row1[],double row2[]){ 6 | for(int i=0;i>matrix[i][j]; 30 | } 31 | } 32 | std::cout<< (MatrixHasSymetricalRows(matrix) ? "Yes" : "No")< 2 | 3 | const size_t MAX_SIZE = 100; 4 | 5 | bool CanFormRigthTriangle(double a, double b, double c) 6 | { 7 | if (a == 0 || b == 0 || c == 0) 8 | return false; 9 | if (a * a + b * b == c * c) 10 | return true; 11 | if (c * c + b * b == a * a) 12 | return true; 13 | if (a * a + c * c == b * b) 14 | return true; 15 | return false; 16 | } 17 | 18 | bool HasTriangles(double arr1[], double arr2[], double arr3[], size_t n) 19 | { 20 | for (int i = 0; i < n; i++){ 21 | for (int j = 0; j < n; j++){ 22 | for (int k = 0; k < n; k++){ 23 | if (CanFormRigthTriangle(arr1[i], arr2[j], arr3[k])) 24 | return true; 25 | } 26 | } 27 | } 28 | return false; 29 | } 30 | 31 | void InputArr(double *arr, size_t n) 32 | { 33 | for (int i = 0; i < n; i++) 34 | std::cin >> arr[i]; 35 | } 36 | 37 | int main() 38 | { 39 | size_t n; 40 | double arr1[MAX_SIZE], arr2[MAX_SIZE], arr3[MAX_SIZE]; 41 | std::cin >> n; 42 | InputArr(arr1, n); 43 | InputArr(arr2, n); 44 | InputArr(arr3, n); 45 | std::cout << "Can " << (HasTriangles(arr1, arr2, arr3, n) ? "" : "not ") << "form a rigth triangle" << std::endl; 46 | } 47 | /* 48 | 3 49 | 1 3 5 50 | 1 2 4 51 | 0 3 6 52 | 53 | Can form a rigth triangle 54 | ---------------------------- 55 | 3 56 | 1 3 5 57 | 1 2 4 58 | 1 4 6 59 | 60 | Can not form a rigth triangle 61 | 62 | */ -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 2/Variant 1/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const size_t MAX_SIZE=100; 5 | 6 | void zip(char *a, char *b, char *result) 7 | { 8 | int index = 0, index1 = 0, index2 = 0; 9 | int n1 = strlen(a), n2 = strlen(b); 10 | while (index1 < n1 && index2 < n2){ 11 | 12 | while (index1 < n1 && a[index1] != ' '){ 13 | result[index] = a[index1]; 14 | index1++; 15 | index++; 16 | } 17 | result[index]=' '; 18 | index++; 19 | index1++; 20 | 21 | 22 | while (index2 < n2 && b[index2] != ' '){ 23 | result[index] = b[index2]; 24 | index2++; 25 | index++; 26 | } 27 | result[index]=' '; 28 | index++; 29 | index2++; 30 | } 31 | 32 | result[index]=0; 33 | if(index1>=n1) 34 | strcat(result,b+index2); 35 | else if(index2>=n2) 36 | strcat(result,a+index1); 37 | 38 | } 39 | 40 | int main() 41 | { 42 | char a[MAX_SIZE] = "Study programming it's very easy", 43 | b[MAX_SIZE] = "computer because not"; 44 | char res[MAX_SIZE]; 45 | zip(a,b,res); 46 | std::cout< 2 | 3 | const size_t COLS = 10, ROWS = 5; 4 | 5 | bool AreLinearlyDependent(double row1[], double row2[]) 6 | { 7 | double k = 0; 8 | bool calcK = false; 9 | for (int i = 0; i < COLS; i++){ 10 | 11 | if (row1[i] == 0 && row2[i] == 0) 12 | continue; 13 | else if (row1[i] * row2[i] == 0) 14 | return false; 15 | else 16 | { 17 | if (calcK){ 18 | if (k != row1[i] / row2[i]) 19 | return false; 20 | else 21 | continue; 22 | } 23 | else{ 24 | calcK = true; 25 | k = row1[i] / row2[i]; 26 | } 27 | } 28 | } 29 | return true; 30 | } 31 | 32 | bool MatrixHasLDRows(double matrix[][COLS]) 33 | { 34 | for (int i = 0; i < ROWS; i++) 35 | { 36 | for (int j = i + 1; j < ROWS; j++) 37 | { 38 | if (AreLinearlyDependent(matrix[i], matrix[j])){ 39 | return true; 40 | } 41 | } 42 | } 43 | return false; 44 | } 45 | 46 | int main() 47 | { 48 | double matrix[ROWS][COLS]; 49 | for (int i = 0; i < ROWS; i++) 50 | { 51 | for (int j = 0; j < COLS; j++) 52 | { 53 | std::cin >> matrix[i][j]; 54 | } 55 | } 56 | std::cout << (MatrixHasLDRows(matrix) ? "Yes" : "No") << std::endl; 57 | } 58 | /* 59 | 1 2 3 4 5 6 7 8 9 10 60 | 1 2 3 4 5 6 7 8 9 10 61 | 2 1 3 4 5 6 8 9 0 10 62 | 0 0 0 0 0 0 0 0 0 0 63 | 2 1 3 4 1 3 8 7 0 10 64 | Yes 65 | 66 | 1 2 3 4 5 6 7 8 7 10 67 | 1 2 3 4 5 6 7 8 9 10 68 | 2 1 3 4 5 6 8 9 0 10 69 | 0 0 0 0 0 0 0 0 0 0 70 | 2 1 3 4 1 3 8 7 0 10 71 | No 72 | 73 | 0 0 0 0 0 0 0 0 0 0 74 | 0 0 0 0 0 0 0 0 1 0.5 75 | 0 0 0 0 0 0 0 0 2 1 76 | 3 0 0 0 0 0 0 0 0 0 77 | 0 4 0 0 0 0 0 0 0 0 78 | Yes 79 | 80 | 0 0 0 0 0 0 0 0 0 0 81 | 0 0 0 0 0 0 0 0 1 0 82 | 0 0 0 0 0 0 0 0 2 1 83 | 3 0 0 0 0 0 0 0 0 0 84 | 0 4 0 0 0 0 0 0 0 0 85 | No 86 | 87 | 0 0 0 0 0 0 0 0 0 0 88 | 0 0 0 0 0 0 0 0 1 0 89 | 0 0 0 0 0 0 0 0 2 0 90 | 3 0 0 0 0 0 0 0 0 0 91 | 0 4 0 0 0 0 0 0 0 0 92 | Yes 93 | */ -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 2/Variant 2/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const size_t MAX_SIZE = 100; 4 | 5 | bool AreSum(long a, long b, long c) 6 | { 7 | if (a + b == c) 8 | return true; 9 | if (c + b == a) 10 | return true; 11 | if (a + c == b) 12 | return true; 13 | return false; 14 | } 15 | 16 | long CountSums(long arr1[], long arr2[], long arr3[], size_t n) 17 | { 18 | int counter=0; 19 | for (int i = 0; i < n; i++){ 20 | for (int j = 0; j < n; j++){ 21 | for (int k = 0; k < n; k++){//може и с двоично 22 | counter+=AreSum(arr1[i], arr2[j], arr3[k]); 23 | } 24 | } 25 | } 26 | return counter; 27 | } 28 | 29 | void InputArr(long *arr, size_t n) 30 | { 31 | for (int i = 0; i < n; i++) 32 | std::cin >> arr[i]; 33 | } 34 | 35 | int main() 36 | { 37 | size_t n; 38 | long arr1[MAX_SIZE], arr2[MAX_SIZE], arr3[MAX_SIZE]; 39 | std::cin >> n; 40 | InputArr(arr1, n); 41 | InputArr(arr2, n); 42 | InputArr(arr3, n); 43 | std::cout << "Count of sums: " << CountSums(arr1, arr2, arr3, n) << std::endl; 44 | } 45 | /* 46 | 3 47 | 1 3 5 48 | 2 4 5 49 | 0 3 6 50 | 51 | 5 52 | => (1,2,3), (5,2,3), (1,5,6), (1,4,3), (5,5,0) 53 | ---------------------------- 54 | 3 55 | 1 3 5 56 | 2 4 5 57 | 0 3 5 58 | 59 | 6 60 | => (1,2,3), (5,2,3), (1,4,5), (1,4,3), (5,5,0), (3,2,5) 61 | */ -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control 2/Variant 2/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const size_t MAX_SIZE = 100; 5 | 6 | int GetMaxLen(char *s) 7 | { 8 | int maxLen = 0, len = 0, index = 0; 9 | while (s[index]) 10 | { 11 | if (s[index] == ' ') 12 | { 13 | maxLen = std::max(maxLen, len); 14 | len = 0; 15 | } 16 | else 17 | len++; 18 | index++; 19 | } 20 | maxLen = std::max(maxLen, len); 21 | return maxLen; 22 | } 23 | 24 | void split(char *s, char *a, char *b) 25 | { 26 | char *word = new (std::nothrow) char[GetMaxLen(s) + 1]; 27 | if (!word) 28 | { 29 | std::cout << "Memory error" << std::endl; 30 | return; 31 | } 32 | 33 | int index = 0, wordIndex = 0, n=strlen(s); 34 | bool onlyLowerCase = true; 35 | a[0] = 0; 36 | b[0] = 0; 37 | while (index= 'a' && s[index] <= 'z'); 45 | index++; 46 | wordIndex++; 47 | } 48 | word[wordIndex] = 0; 49 | wordIndex++; 50 | 51 | if (onlyLowerCase) 52 | { 53 | strcat(a, word); 54 | strcat(a, " "); 55 | } 56 | else 57 | { 58 | strcat(b, word); 59 | strcat(b, " "); 60 | } 61 | 62 | index++; 63 | } 64 | 65 | a[strlen(a) - 1] = 0; 66 | b[strlen(b) - 1] = 0; 67 | } 68 | 69 | int main() 70 | { 71 | char s[MAX_SIZE] = "Please give me 4 COLD beers NOW!"; 72 | char a[MAX_SIZE], b[MAX_SIZE]; 73 | split(s, a, b); 74 | std::cout << "|" << a << "|" << b << "|" << std::endl; 75 | } 76 | -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Control Practicum/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 10; 4 | 5 | bool IsValidMove(unsigned const size, unsigned const x, unsigned const y){ 6 | return x < size && y < size; 7 | } 8 | 9 | void FillMatrix(unsigned matrix[][CAPACITY], unsigned const size, unsigned const x, unsigned const y, unsigned number = 0){ 10 | 11 | if(!IsValidMove(size, x, y) || matrix[y][x]) return; 12 | 13 | matrix[y][x] = number; 14 | 15 | FillMatrix(matrix, size, x + 1, y, number ^ (1 << (x))); 16 | FillMatrix(matrix, size, x, y + 1, number ^ (1 << (size + y - 1))); 17 | FillMatrix(matrix, size, x - 1, y, number ^ (1 << (x - 1))); 18 | FillMatrix(matrix, size, x, y - 1, number ^ (1 << (size + y - 2))); 19 | 20 | } 21 | 22 | void PrintMatrix(unsigned const matrix[][CAPACITY], unsigned const size){ 23 | 24 | for(unsigned i = 0; i < size; ++i){ 25 | 26 | for(unsigned j = 0; j < size; ++j) 27 | std::cout << matrix[i][j] << ' '; 28 | std::cout << '\n'; 29 | 30 | } 31 | 32 | } 33 | 34 | int main(){ 35 | 36 | unsigned matrix[CAPACITY][CAPACITY] = {0, }, n, startX, startY; 37 | 38 | std::cin >> n >> startX >> startY; 39 | 40 | if(n >= CAPACITY || startX >= n || startY >= n){ 41 | 42 | std::cout << "Invalid input\n"; 43 | return -1; 44 | 45 | } 46 | 47 | FillMatrix(matrix, n, startX, startY); 48 | PrintMatrix(matrix, n); 49 | 50 | return 0; 51 | 52 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Computer Science 1/Homework/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | unsigned const FULL_HOUR = 10; 6 | unsigned const HALF_HOUR = 5; 7 | 8 | bool IsValidActivity(char const * const str){ 9 | return (str[0] == 'h' && str[1] == 'w') 10 | || (str[0] == 'h' && str[1] == 'f') 11 | || (str[0] == 'h' && str[1] == 'm') 12 | || (str[0] == 's' && str[1] == 't'); 13 | } 14 | 15 | bool Validate(char const * const str){ 16 | 17 | size_t const length = strlen(str); 18 | if(length & 1) return false; 19 | 20 | for(size_t i = 0; i < length; i += 2) 21 | if(!IsValidActivity(str + i)) return false; 22 | 23 | return true; 24 | 25 | } 26 | 27 | unsigned CalculateTimeSpentForHomework(char const * const str){ 28 | 29 | unsigned time = 0; 30 | 31 | for(size_t i = 0; str[i]; i += 2) 32 | if(str[i] == 'h' && str[i + 1] == 'w') time += FULL_HOUR; 33 | else if(str[i] == 's' && str[i + 1] == 't') time += HALF_HOUR; 34 | 35 | return time; 36 | 37 | } 38 | 39 | int main(){ 40 | 41 | size_t const STRING_CAPACITY = 27, SIZE = 5; 42 | unsigned N, totalTime = 0; 43 | std::cin >> N; 44 | std::cin.ignore(); 45 | 46 | char strings[SIZE][STRING_CAPACITY]; 47 | 48 | for(size_t i = 0; i < SIZE; ++i){ 49 | 50 | std::cin.getline(strings[i], STRING_CAPACITY); 51 | if(!Validate(strings[i])){ 52 | 53 | std::cout << "Incorrect input!\n"; 54 | return -1; 55 | 56 | } 57 | 58 | } 59 | 60 | for(size_t i = 0; i < SIZE; ++i) 61 | totalTime += CalculateTimeSpentForHomework(strings[i]); 62 | 63 | if(N * FULL_HOUR <= totalTime) std::cout << "You got it!\n"; 64 | else std::cout << "You should work more\n"; 65 | 66 | return 0; 67 | 68 | } 69 | 70 | /* 71 | 10 72 | hwhwhwhfhfhfhfhmhmhfhmhmhm 73 | stststststhfhfhfhfhfhfhfhf 74 | sthmhmhmhmhwhwhfhfhfhfhfhf 75 | ststhwhmhmhmhmhmhfhfhfhfhf 76 | hfhfhfhfhfhfhfhfststhfhfhf 77 | */ -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Control 1/Variant 1/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double Distance(int const x1, int const y1, int const x2, int const y2){ 5 | return std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 6 | } 7 | 8 | bool IsInsideCircle(int const x, int const y, int const r, int const x2, int const y2){ 9 | return Distance(x, y, x2, y2) <= r; 10 | } 11 | 12 | int main(){ 13 | 14 | double const EPSILON = 0.001; 15 | int x1, x2, y1, y2, r1, r2; 16 | std::cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2; 17 | 18 | double const distance = Distance(x1, y1, x2, y2); 19 | 20 | if(std::abs(distance - (r1 + r2)) > EPSILON) std::cout << "Circles have no common points\n"; 21 | else if(std::abs(distance - (r1 + r2)) < EPSILON) std::cout << "Circles are touching\n"; 22 | else if(IsInsideCircle(x1, y1, r1, x2, y2) && distance - r1 > EPSILON) std::cout << "Circle 2 is inside circle 1\n"; 23 | else if(IsInsideCircle(x2, y2, r2, x1, y1) && distance - r2 > EPSILON) std::cout << "Circle 1 is inside circle 2\n"; 24 | else std::cout << "Circles are overlapping\n"; 25 | 26 | return 0; 27 | 28 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Control 1/Variant 1/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool IsPrime(unsigned const number){ 5 | 6 | for(unsigned i = 2; i <= std::sqrt(number); ++i) 7 | if(number % i == 0) 8 | return false; 9 | return true; 10 | 11 | } 12 | 13 | int main(){ 14 | 15 | unsigned n, counter = 0, number = 2; 16 | std::cin >> n; 17 | 18 | while(counter < n){ 19 | 20 | if(IsPrime(number) && IsPrime(number + 2)){ 21 | 22 | std::cout << number << ' ' << number + 2 << '\n'; 23 | counter++; 24 | 25 | } 26 | 27 | number++; 28 | 29 | } 30 | 31 | return 0; 32 | 33 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Control 1/Variant 1/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | void ReadArray(int arr[], size_t const n){ 6 | 7 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 8 | 9 | } 10 | 11 | void PrintArray(int const arr[], size_t const n){ 12 | 13 | for(size_t i = 0; i < n; ++i) std::cout << arr[i] << ' '; 14 | std::cout << '\n'; 15 | 16 | } 17 | 18 | bool IsSortedAscending(int const arr[], size_t const n){ 19 | 20 | for(size_t i = 0; i < n - 1; ++i) 21 | if(arr[i] > arr[i + 1]) return false; 22 | return true; 23 | 24 | } 25 | 26 | bool IsSortedDescending(int const arr[], size_t const n){ 27 | 28 | for(size_t i = 0; i < n - 1; ++i) 29 | if(arr[i] < arr[i + 1]) return false; 30 | return true; 31 | 32 | } 33 | 34 | bool IsSorted(int const arr[], size_t const n){ 35 | return IsSortedAscending(arr, n) || IsSortedDescending(arr, n); 36 | } 37 | 38 | unsigned CountEvenSetBits(int const number){ 39 | 40 | unsigned count = 0; 41 | for(unsigned i = 0; i < sizeof(number) * 8; i += 2) 42 | count += !!(number & 1 << i); 43 | return count; 44 | 45 | } 46 | 47 | unsigned CountEvenEvenNumbers(int const arr[], size_t const n){ 48 | 49 | unsigned counter = 0; 50 | 51 | for(size_t i = 0; i < n; ++i) 52 | counter += CountEvenSetBits(arr[i]) % 2 == 0; 53 | 54 | return counter; 55 | 56 | } 57 | 58 | void ClearArray(int arr[], size_t &n){ 59 | 60 | for(size_t i = 0; i < n; ++i) 61 | if(CountEvenSetBits(arr[i]) % 2 == 0){ 62 | 63 | for(size_t j = i; j < n - 1; ++j) arr[j] = arr[j + 1]; 64 | --n; 65 | --i; 66 | 67 | } 68 | 69 | } 70 | 71 | int main(){ 72 | 73 | int arr[CAPACITY]; 74 | size_t n; 75 | std::cin >> n; 76 | 77 | if(n > CAPACITY){ 78 | 79 | std::cout << "Wrong input\n"; 80 | return -1; 81 | 82 | } 83 | 84 | ReadArray(arr, n); 85 | PrintArray(arr, n); 86 | 87 | std::cout << CountEvenEvenNumbers(arr, n) << '\n'; 88 | 89 | ClearArray(arr, n); 90 | PrintArray(arr, n); 91 | 92 | return 0; 93 | 94 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Control 1/Variant 2/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool IsPrime(unsigned const number){ 5 | 6 | if(number < 2) return false; 7 | 8 | for(unsigned i = 2; i <= std::sqrt(number); ++i) 9 | if(number % i == 0) 10 | return false; 11 | return true; 12 | 13 | } 14 | 15 | bool HasTwoDifferentPrimeFactors(unsigned const number){ 16 | 17 | for(unsigned i = 0; i < number; ++i) 18 | if(i != number - i && IsPrime(i) && IsPrime(number - i)) 19 | return true; 20 | return false; 21 | 22 | } 23 | 24 | int main(){ 25 | 26 | unsigned n, counter = 0, number = 2; 27 | std::cin >> n; 28 | 29 | if(n > 100'000'000){ 30 | 31 | std::cout << "Wrong input\n"; 32 | return -1; 33 | 34 | } 35 | 36 | for(unsigned i = 0; i * i < n; ++i) 37 | if(HasTwoDifferentPrimeFactors(i * i)) 38 | std::cout << i * i << '\n'; 39 | 40 | return 0; 41 | 42 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Control 1/Variant 2/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | void ReadArray(double arr[], size_t const n){ 6 | 7 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 8 | 9 | } 10 | 11 | void PrintArray(double const arr[], size_t const n){ 12 | 13 | for(size_t i = 0; i < n; ++i) std::cout << arr[i] << ' '; 14 | std::cout << '\n'; 15 | 16 | } 17 | 18 | bool IsSorted(double const arr[], size_t const n){ 19 | 20 | for(size_t i = 0; i < n - 1; ++i) 21 | if(arr[i] > arr[i + 1]) return false; 22 | return true; 23 | 24 | } 25 | 26 | unsigned NumbersToClear(double const arr[], size_t const n){ 27 | 28 | unsigned counter = 0; 29 | bool mapping[CAPACITY] = {false, }; 30 | 31 | for(size_t i = 0; i < n - 1; ++i) 32 | for(size_t j = i + 1; j < n; ++j) 33 | if(!mapping[j]) 34 | mapping[j] |= arr[i] > arr[j]; 35 | 36 | for(size_t i = 0; i < n; ++i) 37 | counter += mapping[i]; 38 | 39 | return counter; 40 | 41 | } 42 | 43 | void ClearArray(double arr[], size_t &n){ 44 | 45 | for(size_t i = 0; i < n - 1; ++i) 46 | for(size_t j = i + 1; j < n; ++j) 47 | if(arr[i] > arr[j]){ 48 | 49 | for(size_t k = j; k < n - 1; ++k) arr[k] = arr[k + 1]; 50 | --n; 51 | --j; 52 | 53 | } 54 | 55 | } 56 | 57 | int main(){ 58 | 59 | double arr[CAPACITY]; 60 | size_t n; 61 | std::cin >> n; 62 | 63 | if(n > CAPACITY){ 64 | 65 | std::cout << "Wrong input\n"; 66 | return -1; 67 | 68 | } 69 | 70 | ReadArray(arr, n); 71 | PrintArray(arr, n); 72 | 73 | std::cout << NumbersToClear(arr, n) << '\n'; 74 | 75 | ClearArray(arr, n); 76 | PrintArray(arr, n); 77 | 78 | return 0; 79 | 80 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Control 2/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool IsLetter(char const ch){ 5 | return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'); 6 | } 7 | 8 | bool IsDigit(char const ch){ 9 | return '0' <= ch && ch <= '9'; 10 | } 11 | 12 | bool IsWordCharacter(char const ch){ 13 | return IsLetter(ch) || IsDigit(ch); 14 | } 15 | 16 | bool strcmp(char const *lhs, char const *rhs){ 17 | 18 | while(*lhs == *rhs && IsWordCharacter(*lhs)){ 19 | 20 | ++lhs; 21 | ++rhs; 22 | 23 | } 24 | 25 | if(!IsWordCharacter(*lhs) && !IsWordCharacter(*rhs)) return true; 26 | if(!IsWordCharacter(*lhs) && IsWordCharacter(*rhs) || *lhs < *rhs) return false; 27 | if(!IsWordCharacter(*rhs) && IsWordCharacter(*lhs) || *lhs > *rhs) return true; 28 | return true; 29 | 30 | } 31 | 32 | char const *FindNextWord(char const *str){ 33 | 34 | while(*str && IsWordCharacter(*str)) ++str; 35 | while(*str && !IsWordCharacter(*str)) ++ str; 36 | return str; 37 | 38 | } 39 | 40 | char const *MinWord(char const * const str){ 41 | 42 | char const *minWord = IsWordCharacter(*str) ? str : FindNextWord(str); 43 | char const *current = FindNextWord(minWord); 44 | 45 | while(*current){ 46 | 47 | if(strcmp(minWord, current)) minWord = current; 48 | current = FindNextWord(current); 49 | 50 | } 51 | 52 | return minWord; 53 | 54 | } 55 | 56 | int main(){ 57 | 58 | size_t const CAPACITY = 1024; 59 | char str[CAPACITY]; 60 | std::cin.getline(str, CAPACITY); 61 | 62 | std::cout << MinWord(str) << '\n'; 63 | return 0; 64 | 65 | } 66 | 67 | /* 68 | me? why always me? 69 | this sentence is false. 70 | - What is C++? - A 37-year old programming language. 71 | a cat is sleeping and purring on a chair. 72 | */ -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Homework 1/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | const unsigned mask1 = (1 << 6) - 1; // 111111 for minutes and seconds 5 | const unsigned mask2 = (1 << 4) - 1; // 1111 for hour_s 6 | const unsigned mask3 = (1 << 2) - 1; // 11 for hour_f 7 | const unsigned mask4 = 1; // 1 for day_format 8 | 9 | unsigned number; 10 | std::cin >> number; 11 | 12 | // -- Seconds -- 13 | const unsigned seconds = number & mask1; 14 | number >>= 6; 15 | 16 | if (seconds > 59) 17 | { 18 | std::cout << seconds << " is not a valid value for seconds!" << std::endl; 19 | return -1; 20 | } 21 | 22 | // -- Minutes -- 23 | const unsigned minutes = number & mask1; 24 | number >>= 6; 25 | 26 | if (minutes > 59) 27 | { 28 | std::cout << minutes << " is not a valid value for minutes!" << std::endl; 29 | return -1; 30 | } 31 | 32 | // -- First digit of the hours -- 33 | const unsigned hour_s = number & mask2; 34 | number >>= 4; 35 | 36 | if (hour_s > 9) 37 | { 38 | std::cout << "Invalid second hour digit value!" << std::endl; 39 | return -1; 40 | } 41 | 42 | // -- Second digit of the hours -- 43 | const unsigned hour_f = number & mask3; 44 | number >>= 2; 45 | 46 | if (hour_f > 2) 47 | { 48 | std::cout << "Invalid first hour digit value!" << std::endl; 49 | return -1; 50 | } 51 | 52 | // -- Exact hours -- 53 | const unsigned hh = hour_f * 10 + hour_s; 54 | 55 | if (hh >= 24) 56 | { 57 | std::cout << hh << " is not a valid value for hours!" << std::endl; 58 | return -1; 59 | } 60 | 61 | // -- Day format -- 62 | const bool is_24_hour_format = (number & mask4) ? true : false; 63 | 64 | if (!is_24_hour_format && hh > 12) 65 | { 66 | std::cout << hh << " is not a valid value for a 12 hour format!" << std::endl; 67 | return -1; 68 | } 69 | 70 | // -- Printing the result -- 71 | std::cout << (hh < 10 ? "0" : "") << hh << ":" << (minutes < 10 ? "0" : "") << minutes << ":" << (seconds < 10 ? "0" : "") << seconds << std::endl; 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Control & Homework Solutions/Informatics/Homework 2/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool CanFormFrom(int const A[], int const B[], size_t const n, size_t const m){ 4 | 5 | if(m >= n) return false; 6 | 7 | size_t index = 0; 8 | 9 | for(size_t i = 0; i < n; ++i) index += A[i] == B[index]; 10 | 11 | return index == m; 12 | 13 | } 14 | 15 | int main(){ 16 | 17 | size_t const CAPACITY = 1024; 18 | int A[CAPACITY], B[CAPACITY]; 19 | size_t n, m; 20 | 21 | std::cin >> n >> m; 22 | 23 | if(n >= CAPACITY || m >= CAPACITY){ 24 | 25 | std::cout << "Invalid input\n"; 26 | return -1; 27 | 28 | } 29 | 30 | for(size_t i = 0; i < n; ++i) std::cin >> A[i]; 31 | for(size_t i = 0; i < m; ++i) std::cin >> B[i]; 32 | 33 | std::cout << std::boolalpha << CanFormFrom(A, B, n, m) << '\n'; 34 | 35 | return 0; 36 | 37 | } -------------------------------------------------------------------------------- /Practicum/Week 01/README.md: -------------------------------------------------------------------------------- 1 |

Променливи и оператори

2 | 3 | ## Задачи 4 | 5 | **За по-запознатите** - за да решите задачите **не** се позволява използването на условни оператори като `if`, `switch`, `тернарен оператор` и `цикли`. 6 | 7 | ### Задача 1: 8 | Въведете в конзолата малка латинска буква, програмата да изведе на екрана нейната съответстваща главна буква. 9 | 10 | ### Задача 2: 11 | Въведете в конзолата три цели числа `a, b, c`. Да се напишат булеви изрази, които имат стойност `true`, ако условията посочени по-долу са изпълнени и стойност `false`, ако условията не са в сила. Резултатите да се изведат на екрана, всеки на нов ред. 12 | 13 | - `а` се дели на **3**; 14 | - `b` принадлежи на интервала **[-9, 13]**; 15 | - `c` **не** принадлежи на интервала **[0, 8]**; 16 | - Поне едно от числата `a`, `b` **или** `c` е **отрицателно**; 17 | - Числата `a`, `b` и `c` са равни помежду си. 18 | 19 | ### Задача 3: 20 | Въведете в конзолата произволно трицифрено цяло число. Да се изведе на екрана дали всички цифри на това число са нечетни - съответно да се изведе `true` или `false`. 21 | 22 | ### Задача 4: 23 | Намерете лицата на съответните фигури: 24 | 25 | - Триъгълник със страни `a`, `b` и `c`. 26 | 27 | - Окръжност с радиус `r`. Дефинирайте ваша собствена константа `PI` по подходящия начин. За стойност може да използвате `3.14159`. 28 | 29 | ### Задача 5: 30 | Намерете обемите на съответните фигури: 31 | 32 | - Обем на сфера с радиус `r`. 33 | 34 | - Обем на конус с радиус `r` и височина `h`. 35 | 36 | 37 | ### Задача 6: 38 | Да се напише програма, която прочита три двойки числа. Първата са цели числа, втората са от тип `double` и третата са от тип `float`. Помислете кои от операциите събиране, изваждане, умножение, целочислено деление и деление с остатък могат да се приложат към съответните двойки и изведете резултатите на екрана. Вижте какво ще се получи, когато се опитате да приложите операциите към различни типове (например `float` и `double`, `int` и `double`, `float` и `int`). 39 | 40 | ### Задача 7: 41 | Въведете в конзолата три различни цели числа `a, b, c`: 42 | 43 | - Отпечатайте на екрана (с `true` или `false`) дали числата отговарят на следното условие: `min(a,c)` < `b` < `max(a,c)`. 44 | 45 | #### Бонус: 46 | - Отпечатайте на екрана (с `true` или `false`) дали числата отговарят на следното условие: `min(a,c)` < `b` < `max(a,c)` , изполвайки точно 1 сравнение. -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/01-dataTypes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | // Character type 6 | char ch = 'a'; // At least 8 bits 7 | char ch2 = '!', ch3 = '3'; 8 | 9 | // Integer types (signed) 10 | short a = 10; // Not smaller thar `char`. At least 16 bits 11 | int b = 10000; // Not smaller thar `short`. At least 16 bits 12 | long c = -100000; // Not smaller thar `int`. At least 32 bits 13 | long long d = 10000000; // Not smaller thar `long`. At least 64 bits 14 | 15 | // Integer types (unsigned) 16 | unsigned int x = 100000; 17 | // unsigned int invalidValue = -5; 18 | // std::cout << invalidValue << std::endl; 19 | 20 | // If you want to fix the size of the integer types, you can use these: 21 | int8_t e = 200; 22 | uint16_t f = 60000; 23 | int32_t g = 4000000000; 24 | 25 | // Floating-point types 26 | float h = 2.222; 27 | double i = 3.333333; // Precision not less than `float` 28 | long double j = 4.44444444444; // Precision not less than `double` 29 | 30 | // Boolean type 31 | bool true1 = true; 32 | bool true2 = 1; 33 | bool false1 = false; 34 | bool false2 = 0; 35 | 36 | std::cout << true1 << " " << true2 << " " << false1 << " " << false2 << std::endl; 37 | 38 | // std::boolaplha will print `true` and `false` instead of `1` and `0` 39 | std::cout << std::boolalpha << true1 << " " << true2 << " " << false1 << " " << false2 << std::endl; 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/02-consoleIO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | std::cout << "Hello, World!" << std::endl; // prints "Hello, World!" to the console 6 | 7 | int a; 8 | std::cout << "Enter a value for 'a': "; 9 | std::cin >> a; 10 | std::cout << "The value of 'a' is: " << a << '\n'; 11 | 12 | // std::endl and the symbol '\n' both insert a new line 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/03-declarationAndInitialization.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | int declaredButNotInitializedVariable; 6 | int declared_but_not_initialized_variable; 7 | //int declared-but-not-initialized-variable; 8 | 9 | int declaredAndInitializedVariable = 10; 10 | 11 | int dummyVariable; // declared variable 12 | dummyVariable = 20; // assigning a value 13 | 14 | dummyVariable = 200; 15 | dummyVariable = declaredAndInitializedVariable; 16 | declaredAndInitializedVariable = dummyVariable; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/04-constants.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | const int STOTINKAS_IN_A_LEV = 100; 6 | const double PI = 3.14; 7 | 8 | // const double PI2; 9 | // PI = 2; 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/05-arithmeticOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | int a = 14, b = 4; 6 | 7 | std::cout << a + b << std::endl; 8 | std::cout << a - b << std::endl; 9 | std::cout << a * b << std::endl; 10 | std::cout << a / b << std::endl; 11 | std::cout << a % b << std::endl; // modulo 12 | 13 | double c = 14, d = 4; 14 | 15 | std::cout << c / d << std::endl; 16 | std::cout << a / d << std::endl; 17 | std::cout << c / b << std::endl; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/06-compoundAssignment.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | int a = 5; 6 | a = a + 10; 7 | 8 | int b = 5; 9 | b += 10; 10 | 11 | std::cout << a << " " << b << std::endl; 12 | 13 | // Similarly, these operators also exist: -=, *=, /=, %= 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/07-incrementAndDecrement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | int a = 5; 6 | a = a + 1; 7 | 8 | int b = 5; 9 | b += 1; 10 | 11 | int c = 5; 12 | c++; 13 | 14 | int d = 5; 15 | ++d; 16 | 17 | std::cout << a << " " << b << " " << c << " " << d << std::endl; 18 | 19 | int x1, y1, x2, y2; 20 | x1 = 3; 21 | y1 = ++x1; 22 | // x1 = x1 + 1; 23 | // y1 = x1; 24 | 25 | x2 = 3; 26 | y2 = x2++; 27 | // y2 = x2; 28 | // x2 = x2 + 1; 29 | 30 | std::cout << x1 << " " << y1 << std::endl; 31 | std::cout << x2 << " " << y2 << std::endl; 32 | 33 | // Similarly, these operators also exist: --x, x-- 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/08-relationalAndComparisonOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | std::cout << std::boolalpha << (5 == 5) << std::endl; 6 | std::cout << std::boolalpha << (7 != 5) << std::endl; 7 | std::cout << std::boolalpha << (6 > 5) << std::endl; 8 | std::cout << std::boolalpha << (3 < 5) << std::endl; 9 | std::cout << std::boolalpha << (6 >= 5) << std::endl; 10 | std::cout << std::boolalpha << (5 <= 5) << std::endl; 11 | 12 | int a = 5, b; 13 | std::cout << std::boolalpha << ((b = 5) == 5) << std::endl; 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/09-logicalOperators.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | bool a = ((5 < 7) && (5 >= 2)); 6 | bool b = ((4 > 6) || (1 == 1)); 7 | 8 | std::cout << std::boolalpha << a << " " << b << std::endl; 9 | 10 | bool c = (!(2 >= 3) && (8 < 10)); 11 | 12 | std::cout << std::boolalpha << c << std::endl; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/10-ternaryOperator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () 4 | { 5 | int a, b; 6 | 7 | std::cout << "Enter a value for 'a': "; 8 | std::cin >> a; 9 | 10 | std::cout << "Enter a value for 'b': "; 11 | std::cin >> b; 12 | 13 | // ? : 14 | int max = (a >= b) ? a : b; 15 | 16 | std::cout << "The bigger number is " << ((a >= b) ? "'a'" : "'b'") << ". The value is: " << max << std::endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/11-climits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | std::cout << "The maximum value for an int is: " << INT_MAX << std::endl; 6 | std::cout << "The minimum value for an int is: " << INT_MIN << std::endl; 7 | std::cout << "The number of bits in a byte is: " << CHAR_BIT << std::endl; 8 | std::cout << "The maximum value for a char is: " << CHAR_MAX << std::endl; 9 | std::cout << "The minimum value for a char is: " << CHAR_MIN << std::endl; 10 | std::cout << "The maximum value for a short is: " << SHRT_MAX << std::endl; 11 | std::cout << "The minimum value for a short is: " << SHRT_MIN << std::endl; 12 | std::cout << "The maximum value for a long is: " << LONG_MAX << std::endl; 13 | std::cout << "The minimum value for a long is: " << LONG_MIN << std::endl; 14 | std::cout << "The maximum value for a long long is: " << LLONG_MAX << std::endl; 15 | std::cout << "The minimum value for a long long is: " << LLONG_MIN << std::endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/12-cmath.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | 6 | float a = INFINITY; // Infinity for floats 7 | float b = NAN; // Not a number 8 | 9 | // Min and Max 10 | int first = 5, second = 28; 11 | std::cout << "min: " << std::min(first, second) << " " << "max: " << std::max(first, second) << std::endl; 12 | 13 | // Square root 14 | std::cout << "sqrt(2) = " << sqrt(2) << std::endl; 15 | 16 | // Power 17 | std::cout << "pow(2, 3) = " << pow(2, 3) << std::endl; 18 | 19 | // Absolute value 20 | std::cout << "abs(-2) = " << abs(-2) << std::endl; 21 | 22 | // Rounding 23 | std::cout << "round(2.3) = " << round(2.3) << std::endl; 24 | 25 | // Floor 26 | std::cout << "floor(2.3) = " << floor(2.3) << std::endl; 27 | 28 | // Ceiling 29 | std::cout << "ceil(2.3) = " << ceil(2.3) << std::endl; 30 | 31 | // Logarithm 32 | std::cout << "log(2.3) = " << log(2.3) << std::endl; 33 | 34 | // Log10 35 | std::cout << "log10(2.3) = " << log10(2.3) << std::endl; 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Practicum/Week 01/snippets/13-iomanip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | // Set precision to 2 decimal places 6 | std::cout << std::fixed << std::setprecision(2); 7 | 8 | // Output floating point numbers with padding and alignment 9 | std::cout << std::setw(10) << std::left << 3.14159 << std::endl; 10 | std::cout << std::setw(10) << std::right << 2.71828 << std::endl; 11 | 12 | // Output integers with padding and leading zeros 13 | std::cout << std::setw(10) << std::setfill('0') << std::left << 42 << std::endl; 14 | std::cout << std::setw(10) << std::setfill('0') << std::right << 123 << std::endl; 15 | 16 | // Output boolean values as "true" or "false" (otherwise printed as 1 or 0) 17 | std::cout << std::boolalpha << true << std::endl; 18 | std::cout << std::boolalpha << false << std::endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char letter; 6 | std::cin >> letter; 7 | 8 | char uppercase = letter - 'a' + 'A'; 9 | std::cout << uppercase << std::endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, c; 6 | std::cin >> a >> b >> c; 7 | 8 | std::cout << std::boolalpha << (a % 3 == 0) << std::endl; // !(a % 3) е алтернативен вариант 9 | std::cout << std::boolalpha << (b >= -9 && b <= 13) << std::endl; 10 | std::cout << std::boolalpha << (c < 0 || c > 8) << std::endl; 11 | std::cout << std::boolalpha << (a < 0 || b < 0 || c < 0) << std::endl; 12 | std::cout << std::boolalpha << (a == b && b == c) << std::endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num; 6 | std::cin >> num; 7 | 8 | int firstDigit = num / 100, 9 | secondDigit = (num / 10) % 10, 10 | thirdDigit = num % 10; 11 | 12 | std::cout << std::boolalpha << (firstDigit % 2 && secondDigit % 2 && thirdDigit % 2) << std::endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task4_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | double a, b, c; 7 | std::cin >> a >> b >> c; 8 | 9 | double p = (a + b + c) / 2; 10 | std::cout << "S = " << sqrt(p * (p - a) * (p - b) * (p - c)) << std::endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task4_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const double PI = 3.14159; 6 | 7 | double r; 8 | std::cin >> r; 9 | 10 | std::cout << "S = " << PI * r * r << std::endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task5_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const double PI = 3.14159; 6 | 7 | double r; 8 | std::cin >> r; 9 | 10 | std::cout << "V = " << (4 * PI * r * r * r) / 3 << std::endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task5_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | const double PI = 3.14159; 7 | 8 | double r, h; 9 | std::cin >> r >> h; 10 | 11 | std::cout << "V = " << (PI * r * r * h) / 3 << std::endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Practicum/Week 01/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, c; 6 | std::cin >> a >> b >> c; 7 | 8 | std::cout << std::boolalpha << (std::min(a, c) < b && b < std::max(a, c)) << std::endl; 9 | 10 | std::cout << std::boolalpha << ((a - c < 0 && a < b && b < c) || // a < c 11 | (a - c > 0 && c < b && b < a)) // a > c 12 | << std::endl; 13 | 14 | // Bonus task 15 | std::cout << std::boolalpha << ((a - b) * (c - b) < 0) << std::endl; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Practicum/Week 02/snippets/01-absTypes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | double x = -3.14; 7 | 8 | // Using abs from cmath 9 | std::cout << "abs(" << x << ") = " << abs(x) << '\n'; 10 | 11 | // Using abs from std 12 | std::cout << "abs(" << x << ") = " << std::abs(x) << '\n'; 13 | 14 | // Using fabs from cstdlib 15 | std::cout << "fabs(" << x << ") = " << fabs(x) << '\n'; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Practicum/Week 02/snippets/02-switchFallThroughs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num = 2; 5 | int num2 = 0; 6 | 7 | std::cout << "Switch one:\n"; 8 | switch(num) { 9 | case 1: 10 | std::cout << "Number is 1" << std::endl; 11 | case 2: 12 | std::cout << "Number is 2" << std::endl; 13 | case 3: 14 | std::cout << "Number is 3" << std::endl; 15 | default: 16 | std::cout << "Number is not 1, 2, or 3" << std::endl; 17 | } 18 | 19 | std::cout << "Switch two:\n"; 20 | switch(num) { 21 | default: 22 | std::cout << "Number is not 1, 2, or 3" << std::endl; 23 | case 1: 24 | std::cout << "Number is 1" << std::endl; 25 | case 2: 26 | std::cout << "Number is 2" << std::endl; 27 | case 3: 28 | std::cout << "Number is 3" << std::endl; 29 | } 30 | 31 | std::cout << "Switch three:\n"; 32 | switch(num2) { 33 | default: 34 | std::cout << "Number is not 1, 2, or 3" << std::endl; 35 | case 1: 36 | std::cout << "Number is 1" << std::endl; 37 | case 2: 38 | std::cout << "Number is 2" << std::endl; 39 | case 3: 40 | std::cout << "Number is 3" << std::endl; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Practicum/Week 02/snippets/03-shortCircuitEval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int x = 5; 5 | int y = 0; 6 | 7 | if (y != 0 && (x /= 2)) { // the first expression is false, so the second one will not evaluate 8 | std::cout << "This won't print because of short circuit evaluation\n"; 9 | // because the first statement is false, because of the AND operator the entire expression can't be true 10 | } 11 | 12 | std::cout << "x = " << x << std::endl; 13 | 14 | if (y == 0 || (x /= 2)) { // the first expression is true, so the second one will not evaluate 15 | std::cout << "This will print because of short circuit evaluation\n"; 16 | // naturally if the first statement is true, then it doesn't matter what the next statements are, because of the OR operator 17 | } 18 | 19 | std::cout << "x = " << x << std::endl; 20 | 21 | if ((x /= 2) || y == 0) { 22 | std::cout << "This will print because of short circuit evaluation\n"; 23 | } 24 | 25 | std::cout << "x = " << x << std::endl; 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Practicum/Week 02/snippets/04-ifElseIf.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int num = 10; 5 | 6 | if (num > 10) { 7 | std::cout << "The number is greater than 10." << std::endl; 8 | } else if (num < 10) { 9 | std::cout << "The number is less than 10." << std::endl; 10 | } else { 11 | std::cout << "The number is equal to 10." << std::endl; 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 02/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | double alpha, beta, gamma; 6 | std::cin >> alpha >> beta >> gamma; 7 | 8 | if(alpha <= 0 || beta <= 0 || gamma <= 0) 9 | { 10 | std::cout << "Incorrect input." << std::endl; 11 | return -1; 12 | } 13 | 14 | const double EPS = 0.001, 15 | ANGLES_SUM = 180; 16 | 17 | std::cout << ( (std::abs(alpha + beta + gamma - ANGLES_SUM) < EPS) ? "These angles can form a triangle" : "These angles can't form a triangle") << std::endl; 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Practicum/Week 02/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char symbol; 6 | std::cin >> symbol; 7 | 8 | if(symbol >= '0' && symbol <= '9') 9 | { 10 | std::cout << "digit" << std::endl; 11 | } 12 | else if(symbol >= 'a' && symbol <= 'z') 13 | { 14 | std::cout << "lowercase" << std::endl; 15 | } 16 | else if(symbol >= 'A' && symbol <= 'Z') 17 | { 18 | std::cout << "uppercase" << std::endl; 19 | } 20 | else 21 | { 22 | std::cout << "The symbol is not a digit, lowercase or uppercase." << std::endl; 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Practicum/Week 02/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, c; 6 | std::cin >> a >> b >> c; 7 | 8 | if(a <= 0 || b <= 0 || c <= 0) 9 | { 10 | std::cout << "Incorrect input." << std::endl; 11 | return 1; 12 | } 13 | 14 | if(a + b <= c || b + c <= a || a + c <= b) 15 | { 16 | std::cout << "These sides can't form a triangle." << std::endl; 17 | } 18 | else if(a == b && b == c) 19 | { 20 | std::cout << "These sides can form a triangle. The triangle is equilateral." << std::endl; 21 | } 22 | else if(a == b || b == c || a == c) 23 | { 24 | std::cout << "These sides can form a triangle. The triangle is isosceles." << std::endl; 25 | } 26 | else 27 | { 28 | std::cout << "These sides can form a triangle. The triangle is scalene." << std::endl; 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Practicum/Week 02/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | int kilos; 6 | char destination; 7 | 8 | std::cin >> kilos >> destination; 9 | switch (destination) 10 | { 11 | case 'A': 12 | std::cout << kilos * 5 << std::endl; 13 | break; 14 | case 'B': 15 | std::cout << kilos * 7 << std:: endl; 16 | break; 17 | case 'C': 18 | std::cout << kilos * 10 << std::endl; 19 | break; 20 | } 21 | } -------------------------------------------------------------------------------- /Practicum/Week 02/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int day, month, year; 6 | std::cin >> year >> month >> day; 7 | 8 | if (month == 1 || month == 2) { 9 | 10 | month += 12; 11 | year -= 1; 12 | 13 | } 14 | 15 | int K = year % 100; 16 | int J = year / 100; 17 | int day_of_week = (day + 13 * (month + 1) / 5 + K + K / 4 + J / 4 - 2 * J) % 7; 18 | 19 | std::cout << "The day of the week for " << day << "/" << month << "/" << year << " is "; 20 | 21 | switch(day_of_week){ 22 | 23 | case 0: 24 | std::cout << "Saturday\n"; 25 | break; 26 | 27 | case 1: 28 | std::cout << "Sunday\n"; 29 | break; 30 | 31 | case 2: 32 | std::cout << "Monday\n"; 33 | break; 34 | 35 | case 3: 36 | std::cout << "Tuesday\n"; 37 | break; 38 | 39 | case 4: 40 | std::cout << "Wednesday\n"; 41 | break; 42 | 43 | case 5: 44 | std::cout << "Thursday\n"; 45 | break; 46 | 47 | case 6: 48 | std::cout << "Friday\n"; 49 | break; 50 | 51 | } 52 | 53 | return 0; 54 | 55 | } -------------------------------------------------------------------------------- /Practicum/Week 03/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned short x, n; 6 | std::cin >> x >> n; 7 | 8 | if(x > 255 || n > 7) 9 | { 10 | std::cout << "Incorrect input." << std::endl; 11 | return 1; 12 | } 13 | 14 | //change bit with index n to 1 15 | x |= (1 << n); // x = x | (1 << n) 16 | std::cout << x << std::endl; 17 | 18 | //change bit with index n to 0 19 | x &= ~(1 << n); // x = x & ~(1 << n) 20 | std::cout << x << std::endl; 21 | 22 | //toggle bit with index n 23 | x ^= (1 << n); // x = x ^ (1 << n) 24 | std::cout << x << std::endl; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Practicum/Week 03/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | short a; 6 | std::cin >> a; 7 | 8 | if(a < 0 || a > 255){ 9 | 10 | std::cout << "Invalid value\n"; 11 | return 0; 12 | 13 | } 14 | 15 | int toggledBitsCount = 0; 16 | toggledBitsCount += !!(a & 1 << 0); 17 | toggledBitsCount += !!(a & 1 << 1); 18 | toggledBitsCount += !!(a & 1 << 2); 19 | toggledBitsCount += !!(a & 1 << 3); 20 | toggledBitsCount += !!(a & 1 << 4); 21 | toggledBitsCount += !!(a & 1 << 5); 22 | toggledBitsCount += !!(a & 1 << 6); 23 | toggledBitsCount += !!(a & 1 << 7); 24 | 25 | std::cout << "The number: " << a << " has " << (toggledBitsCount % 2 ? "odd" : "even") << " number of bits set.\n"; 26 | 27 | } -------------------------------------------------------------------------------- /Practicum/Week 03/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x, y; 6 | std::cin >> x >> y; 7 | 8 | std::cout<< std::boolalpha << ((x ^ y) < 0) << std::endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Practicum/Week 03/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | int num; 6 | std::cin >> num; 7 | 8 | int mask = num >> (sizeof(int) * 8 - 1); 9 | int absVal = (num + mask) ^ mask; 10 | 11 | std::cout << "Absolute value: " << absVal << '\n'; 12 | 13 | return 0; 14 | 15 | } -------------------------------------------------------------------------------- /Practicum/Week 03/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | bool number1_bit0, number1_bit1, number1_bit2, number1_bit3; 6 | bool number2_bit0, number2_bit1, number2_bit2, number2_bit3; 7 | bool carry_over = 0; 8 | bool sum_bit0, sum_bit1, sum_bit2, sum_bit3, sum_bit4; 9 | 10 | std::cin >> number1_bit3 >> number1_bit2 >> number1_bit1 >> number1_bit0; 11 | std::cin >> number2_bit3 >> number2_bit2 >> number2_bit1 >> number2_bit0; 12 | 13 | sum_bit0 = number1_bit0 ^ number2_bit0; 14 | carry_over = number1_bit0 & number2_bit0; 15 | 16 | sum_bit1 = number1_bit1 ^ number2_bit1 ^ carry_over; 17 | carry_over = (number1_bit1 & number2_bit1) | (number1_bit1 & carry_over) | (number2_bit1 & carry_over); 18 | 19 | sum_bit2 = number1_bit2 ^ number2_bit2 ^ carry_over; 20 | carry_over = (number1_bit2 & number2_bit2) | (number1_bit2 & carry_over) | (number2_bit2 & carry_over); 21 | 22 | sum_bit3 = number1_bit3 ^ number2_bit3 ^ carry_over; 23 | carry_over = (number1_bit3 & number2_bit3) | (number1_bit3 & carry_over) | (number2_bit3 & carry_over); 24 | 25 | sum_bit4 = carry_over; 26 | 27 | std::cout << sum_bit4 << sum_bit3 << sum_bit2 << sum_bit1 << sum_bit0 << std::endl; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Practicum/Week 03/solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x = 1; 6 | 7 | x <<= 12; // x = x << 12 8 | x |= 8; 9 | x |= (1 << 5); 10 | x |= (1 << 6); 11 | x |= (1 << 8); 12 | x |= (1 << 10); 13 | x >>= 3; // x = x >> 3 14 | x ^= (1 << 2); // x = x ^ (1 << 2) 15 | 16 | std::cout << x << std::endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Practicum/Week 04/README.md: -------------------------------------------------------------------------------- 1 |

Цикли и оператори за цикли - while, do-while, for

2 | 3 | ## Задачи 4 | 5 | ### Задача 1: 6 | Да се прочете от конзолата цяло число и да се отпечатат цифрите му в обратен ред. 7 | 8 | **Пример:** 9 | ``` 10 | Вход: 12345 11 | Изход: 54321 12 | ``` 13 | 14 | ### Задача 2: 15 | Да се прочете от конзолата цяло число n и да се намери стойността на **n!**. 16 | 17 | **Пример:** 18 | ``` 19 | Вход: 5 20 | Изход: 120 21 | ``` 22 | 23 | ### Задача 3: 24 | Да се прочетат от конзолата цяло число **x** и цифра **n** и да се намери колко пъти цифрата **n** се среща в числото **x**. 25 | 26 | **Пример:** 27 | ``` 28 | Вход: 32345 3 29 | Изход: 2 30 | ``` 31 | 32 | ### Задача 4: 33 | Да се прочете цяло число от конзолата и да се провери дали то е **палиндром**. 34 | 35 | **Пример:** 36 | ``` 37 | Вход: 12321 38 | Изход: true 39 | 40 | Вход: 12345 41 | Изход: false 42 | ``` 43 | 44 | ### Задача 5: 45 | Да се прочете от конзолата цяло число и да се изведат на екрана всички негови делители. 46 | 47 | **Пример:** 48 | ``` 49 | Вход: 10 50 | Изход: 1 2 5 10 51 | ``` 52 | 53 | ### Задача 6: 54 | Да се прочете от конзолата цяло число **n** и след това **n** на брой цели числа. Да се намерят минимум, максимум и средно аритметично на тези числа ***(за целта не е необходимо да се използва масив)***. 55 | 56 | **Пример:** 57 | ``` 58 | Вход: 5 1 2 3 4 5 59 | Изход: min = 1, max = 5, avg = 3 60 | ``` 61 | 62 | ### Задача 7: 63 | Да се прочете от конзолата цяло число **n** и да се отпечатат на екрана първите **n** числа на Фибоначи. 64 | 65 | **Пример:** 66 | ``` 67 | Вход: 5 68 | Изход: 1 1 2 3 5 69 | ``` 70 | 71 | ### Задача 8 (за домашно): 72 | Да се напише програма, която проверява дали дадено число е *"нарцистично"*. Едно число се нарича *"нарцистично"*, ако сумата от цифрите му, повдигнати на степен броя на цифрите му, е равна на самото число. 73 | 74 | **Пример:** 75 | ``` 76 | Вход: 153 77 | Изход: true 78 | Обяснение: 153 = 1^3 + 5^3 + 3^3 79 | 80 | Вход: 123 81 | Изход: false 82 | Обяснение: 123 != 1^3 + 2^3 + 3^3 83 | 84 | Вход: 370 85 | Изход: true 86 | Обяснение: 370 = 3^3 + 7^3 + 0^3 87 | ``` 88 | 89 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | while(n != 0) 9 | { 10 | std::cout << n % 10; 11 | n /= 10; // n = n / 10; 12 | } 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | unsigned int fact = 1; 9 | 10 | for(int i = 2; i <= n; i++) 11 | { 12 | fact *= i; // fact = fact * i; 13 | } 14 | 15 | std::cout << fact << std::endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int x, n; 6 | std::cin >> x >> n; 7 | 8 | unsigned short count = 0; 9 | 10 | while(x != 0) 11 | { 12 | if(x % 10 == n) 13 | { 14 | count++; 15 | } 16 | x /= 10; // x = x / 10 17 | } 18 | 19 | std::cout << count << std::endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | unsigned int reversed = 0, copy = n; 9 | 10 | while(copy != 0) 11 | { 12 | reversed *= 10; 13 | reversed += copy % 10; 14 | copy /= 10; 15 | } 16 | 17 | std::cout << std::boolalpha << (n == reversed) << std::endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | for(int i = 1; i <= n; i++) 9 | { 10 | if(n % i == 0) 11 | { 12 | std::cout << i << ' '; 13 | } 14 | } 15 | 16 | std::cout << std::endl; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | int min = INT16_MAX, max = INT16_MIN, sum = 0; 9 | unsigned int copy = n; 10 | 11 | while(copy != 0) 12 | { 13 | int curr; 14 | std::cin >> curr; 15 | 16 | if(curr < min) 17 | { 18 | min = curr; 19 | } 20 | 21 | if(curr > max) 22 | { 23 | max = curr; 24 | } 25 | 26 | sum += curr; 27 | copy--; 28 | } 29 | 30 | std::cout << "min = " << min << ", " 31 | << "max = " << max << ", " 32 | << "avg = " << (double)sum / n 33 | << std::endl; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | int a = 1, b = 0, c; 9 | for(int i = 0; i < n ; i++) 10 | { 11 | c = a + b; 12 | std::cout << c << ' '; 13 | 14 | a = b; 15 | b = c; 16 | } 17 | 18 | std::cout << std::endl; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Practicum/Week 04/solutions/task8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | unsigned int n; 7 | std::cin >> n; 8 | 9 | unsigned int count = 0, sum = 0, copy = n; 10 | 11 | while(copy != 0) 12 | { 13 | count++; 14 | copy /= 10; 15 | } 16 | 17 | copy = n; 18 | 19 | while(copy != 0) 20 | { 21 | sum += pow(copy % 10, count); 22 | copy /= 10; 23 | } 24 | 25 | std::cout << std::boolalpha << (sum == n) << std::endl; 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ReadArray(int arr[], const unsigned int n) 4 | { 5 | for(unsigned i = 0; i < n; i++) 6 | { 7 | std::cin >> arr[i]; 8 | } 9 | } 10 | 11 | void PrintReverse(const int arr[], const unsigned int n) 12 | { 13 | for(int i = n - 1; i >= 0; i--) 14 | { 15 | std::cout << arr[i] << ' '; 16 | } 17 | std::cout << std::endl; 18 | } 19 | 20 | int main() 21 | { 22 | const unsigned int MAX_SIZE = 100; 23 | int arr[MAX_SIZE]; 24 | 25 | unsigned int n; 26 | do 27 | { 28 | std::cout << "Enter n: "; 29 | std::cin >> n; 30 | } while(n == 0 || n > MAX_SIZE); 31 | 32 | ReadArray(arr, n); 33 | PrintReverse(arr, n); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int ARRAY_SIZE = 5; 4 | 5 | int scalar_product(int arr1[], int arr2[]) { 6 | int result = 0; 7 | for (int i = 0; i < ARRAY_SIZE; i++) { 8 | result += arr1[i] * arr2[i]; 9 | } 10 | return result; 11 | } 12 | 13 | int main() { 14 | int arr1[ARRAY_SIZE] = {1, 2, 3, 4, 5}; 15 | int arr2[ARRAY_SIZE] = {5, 4, 3, 2, 1}; 16 | int result = scalar_product(arr1, arr2); 17 | std::cout << "Scalar product: " << result << std::endl; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | const unsigned int MAX_SIZE = 10; 6 | unsigned int count[MAX_SIZE] = {0,}; 7 | 8 | unsigned int k; 9 | std::cout << "Enter k: "; 10 | std::cin >> k; 11 | 12 | for(unsigned i = 0; i < k; i++) 13 | { 14 | unsigned short digit; 15 | do 16 | { 17 | std::cin >> digit; 18 | } while(digit > 9); 19 | 20 | count[digit]++; 21 | } 22 | 23 | for(unsigned i = 0; i < MAX_SIZE; i++) 24 | { 25 | std::cout << '[' << i << "]: " << count[i] << std::endl; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int _MAX_SIZE = 100; 4 | 5 | void printSubsequences(int array[], int size) { 6 | int prev = 0; 7 | for (size_t i = 0; i < size; i++) 8 | { 9 | if (array[i] <= prev) 10 | std::cout << '\n'; 11 | 12 | std::cout << array[i] << " "; 13 | prev = array[i]; 14 | } 15 | std::cout << std::endl; 16 | } 17 | 18 | int main() { 19 | int array[_MAX_SIZE]; 20 | int size, input; 21 | std::cin >> size; 22 | for (size_t i = 0; i < size; i++) 23 | { 24 | std::cin >> input; 25 | array[i] = input; 26 | } 27 | 28 | printSubsequences(array, size); 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task5_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | void ReadArray(int arr[], const unsigned int n) 6 | { 7 | for(unsigned i = 0; i < n; i++) 8 | { 9 | std::cin >> arr[i]; 10 | } 11 | } 12 | 13 | void ReverseWithHelperArray(int arr[], const unsigned int n) 14 | { 15 | int helper[MAX_SIZE]; 16 | 17 | for(unsigned i = 0; i < n; i++) 18 | { 19 | helper[i] = arr[n - i - 1]; 20 | } 21 | 22 | for(unsigned i = 0; i < n; i++) 23 | { 24 | arr[i] = helper[i]; 25 | } 26 | } 27 | 28 | void Print(const int arr[], const unsigned int n) 29 | { 30 | for(unsigned i = 0; i < n; i++) 31 | { 32 | std::cout << arr[i] << ' '; 33 | } 34 | std::cout << std::endl; 35 | } 36 | 37 | int main() 38 | { 39 | int arr[MAX_SIZE]; 40 | 41 | unsigned int n; 42 | do 43 | { 44 | std::cout << "Enter n: "; 45 | std::cin >> n; 46 | } while(n == 0 || n > MAX_SIZE); 47 | 48 | ReadArray(arr, n); 49 | ReverseWithHelperArray(arr, n); 50 | Print(arr, n); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task5_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ReadArray(int arr[], const unsigned int n) 4 | { 5 | for(unsigned i = 0; i < n; i++) 6 | { 7 | std::cin >> arr[i]; 8 | } 9 | } 10 | 11 | void Swap(int& a, int& b) 12 | { 13 | int temp = a; 14 | a = b; 15 | b = temp; 16 | } 17 | 18 | void ReverseWithoutHelperArray(int arr[], const unsigned int n) 19 | { 20 | int middle = (n % 2 == 0) ? n / 2 : n / 2 + 1; 21 | for(int i = 0; i < middle; i++) 22 | { 23 | Swap(arr[i], arr[n - i - 1]); 24 | } 25 | } 26 | 27 | void Print(const int arr[], const unsigned int n) 28 | { 29 | for(unsigned i = 0; i < n; i++) 30 | { 31 | std::cout << arr[i] << ' '; 32 | } 33 | std::cout << std::endl; 34 | } 35 | 36 | int main() 37 | { 38 | const unsigned int MAX_SIZE = 100; 39 | int arr[MAX_SIZE]; 40 | 41 | unsigned int n; 42 | do 43 | { 44 | std::cout << "Enter n: "; 45 | std::cin >> n; 46 | } while(n == 0 || n > MAX_SIZE); 47 | 48 | ReadArray(arr, n); 49 | ReverseWithoutHelperArray(arr, n); 50 | Print(arr, n); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int &a, int &b) { 4 | int temp = a; 5 | a = b; 6 | b = temp; 7 | } 8 | 9 | void remove_shift(int arr[], int &size, int remove_at_index) { // preserves order 10 | if (remove_at_index < 0 || remove_at_index >= size) { 11 | std::cout << "Invalid index!" << std::endl; 12 | return; 13 | } 14 | 15 | for (int i = remove_at_index; i < size - 1; i++) { 16 | arr[i] = arr[i + 1]; 17 | } 18 | --size; 19 | } 20 | 21 | void remove_swap(int arr[], int &size, int remove_at_index) { // does not preserve order, but its faster 22 | if (remove_at_index < 0 || remove_at_index >= size) { 23 | std::cout << "Invalid index!" << std::endl; 24 | return; 25 | } 26 | arr[remove_at_index] = arr[size - 1]; 27 | --size; 28 | } 29 | 30 | void removeEven(int arr[], int &size) { 31 | int i = 0; 32 | while (i < size) { 33 | if (arr[i] % 2 == 0) { 34 | // remove_swap(arr, size, i); 35 | remove_shift(arr, size, i); 36 | continue; 37 | } 38 | ++i; 39 | } 40 | } 41 | 42 | int main() { 43 | int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 44 | const int capacity = sizeof(arr) / sizeof(arr[0]); 45 | int size = capacity; 46 | 47 | std::cout << "Original array: "; 48 | for (int i = 0; i < size; i++) { 49 | std::cout << arr[i] << " "; 50 | } 51 | std::cout << std::endl; 52 | 53 | removeEven(arr, size); 54 | 55 | std::cout << "Array with even numbers removed: "; 56 | for (int i = 0; i < size; i++) { 57 | std::cout << arr[i] << " "; 58 | } 59 | std::cout << std::endl; 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ReadArray(int arr[], const unsigned int n) 4 | { 5 | std::cout << "Enter array: "; 6 | for(unsigned i = 0; i < n; i++) 7 | { 8 | std::cin >> arr[i]; 9 | } 10 | } 11 | 12 | void InsertIntoSortedArray(int sortedArr[], unsigned int& n, const int num) 13 | { 14 | bool inserted = false; 15 | 16 | for(unsigned i = 0; i < n && !inserted; i++) 17 | { 18 | if(sortedArr[i] > num) 19 | { 20 | for(int j = n; j > i; j--) 21 | { 22 | sortedArr[j] = sortedArr[j - 1]; 23 | } 24 | 25 | sortedArr[i] = num; 26 | n++; 27 | inserted = true; 28 | } 29 | } 30 | 31 | if(!inserted) 32 | { 33 | sortedArr[n] = num; 34 | n++; 35 | } 36 | } 37 | 38 | void Print(const int arr[], const unsigned int n) 39 | { 40 | for(unsigned i = 0; i < n; i++) 41 | { 42 | std::cout << arr[i] << ' '; 43 | } 44 | std::cout << std::endl; 45 | } 46 | 47 | int main() 48 | { 49 | const unsigned int MAX_SIZE = 100; 50 | int sortedArr[MAX_SIZE]; 51 | 52 | unsigned int n; 53 | do 54 | { 55 | std::cout << "Enter n: "; 56 | std::cin >> n; 57 | } while(n == 0 || n > MAX_SIZE); 58 | 59 | ReadArray(sortedArr, n); 60 | 61 | unsigned int k; 62 | do 63 | { 64 | std::cout << "\nEnter k: "; 65 | std::cin >> k; 66 | } while(k == 0 || k + n > MAX_SIZE); 67 | 68 | std::cout << "Enter k numbers: "; 69 | for(unsigned i = 0; i < k; i++) 70 | { 71 | int num; 72 | std::cin >> num; 73 | 74 | InsertIntoSortedArray(sortedArr, n, num); 75 | } 76 | 77 | std::cout << std::endl; 78 | Print(sortedArr, n); 79 | return 0; 80 | } -------------------------------------------------------------------------------- /Practicum/Week 06/solutions/task8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_SIZE = 100; 4 | 5 | void printPairs(int arr[], int size, int m) { 6 | for (size_t i = 0; i < size; i++) 7 | { 8 | for (size_t j = i + 1; j < size; j++) 9 | { 10 | if (arr[i] + arr[j] == m) 11 | std::cout << arr[i] << " " << arr[j] << std::endl; 12 | } 13 | } 14 | } 15 | 16 | int main() { 17 | int arr[MAX_SIZE]; 18 | int size, m; 19 | std::cin >> size >> m; 20 | 21 | for (int i = 0; i < size; i++) 22 | { 23 | std::cin >> arr[i]; 24 | } 25 | 26 | printPairs(arr, size, m); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Practicum/Week 07/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int &a, int &b){ 4 | 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | 9 | } 10 | 11 | void Sort(int arr[], size_t const size){ 12 | 13 | size_t min_index; 14 | 15 | for(size_t i = 0; i < size - 1; i++){ 16 | 17 | min_index = i; 18 | 19 | for(size_t j = i + 1; j < size; j++) 20 | if(arr[min_index] > arr[j]) 21 | min_index = j; 22 | 23 | if(min_index != i) Swap(arr[i], arr[min_index]); 24 | 25 | } 26 | 27 | } 28 | 29 | int GetKthElementInArray(int arr[], size_t const size, size_t const k){ 30 | 31 | Sort(arr, size); 32 | return arr[k - 1]; 33 | 34 | } 35 | 36 | int main(){ 37 | 38 | size_t const CAPACITY = 1024; 39 | int arr[CAPACITY]; 40 | size_t size, k; 41 | 42 | std::cin >> size; 43 | if(size >= CAPACITY){ 44 | 45 | std::cout << "Invalid input\n"; 46 | return -1; 47 | 48 | } 49 | 50 | for(size_t i = 0; i < size; ++i) std::cin >> arr[i]; 51 | 52 | std::cin >> k; 53 | if(k >= size){ 54 | 55 | std::cout << "Invalid input\n"; 56 | return -1; 57 | 58 | } 59 | 60 | std::cout << GetKthElementInArray(arr, size, k) << '\n'; 61 | 62 | return 0; 63 | 64 | } -------------------------------------------------------------------------------- /Practicum/Week 07/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | double const EPSILON = 0.001; 4 | 5 | bool BinarySearch(double const arr[], size_t const size, double const toBeSearched){ 6 | 7 | int start = 0, end = size - 1, mid; 8 | 9 | while(start <= end){ 10 | 11 | mid = (start + end) / 2; 12 | 13 | if(std::abs(arr[mid] - toBeSearched) < EPSILON) return true; 14 | 15 | if(toBeSearched < arr[mid]) end = mid - 1; 16 | else start = mid + 1; 17 | 18 | } 19 | 20 | return false; 21 | 22 | } 23 | 24 | int main(){ 25 | 26 | size_t const CAPACITY = 1024; 27 | double arr[CAPACITY], k; 28 | size_t size; 29 | 30 | std::cin >> size; 31 | if(size >= CAPACITY){ 32 | 33 | std::cout << "Invalid input\n"; 34 | return -1; 35 | 36 | } 37 | 38 | for(size_t i = 0; i < size; ++i) std::cin >> arr[i]; 39 | std::cin >> k; 40 | 41 | std::cout << std::boolalpha << BinarySearch(arr, size, k) << '\n'; 42 | 43 | return 0; 44 | 45 | } -------------------------------------------------------------------------------- /Practicum/Week 07/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void SpaceElementsByOne(int arr[], size_t &size){ 4 | 5 | size = 2 * size; 6 | for(size_t i = size - 2; i > 0; i -= 2) 7 | arr[i] = arr[i / 2]; 8 | 9 | } 10 | 11 | void FillArray(int arr[], size_t &size){ 12 | 13 | SpaceElementsByOne(arr, size); 14 | arr[1] = arr[0]; 15 | for(size_t i = 3; i < size; i += 2) 16 | arr[i] = arr[i - 1] + arr[i - 2]; 17 | 18 | } 19 | 20 | int main(){ 21 | 22 | size_t const CAPACITY = 1024; 23 | int arr[CAPACITY]; 24 | size_t size, k, index; 25 | 26 | std::cin >> size; 27 | if(size >= CAPACITY){ 28 | 29 | std::cout << "Invalid input\n"; 30 | return -1; 31 | 32 | } 33 | 34 | for(size_t i = 0; i < size; ++i) std::cin >> arr[i]; 35 | 36 | FillArray(arr, size); 37 | 38 | std::cin >> k; 39 | 40 | for(size_t i = 0; i < k; ++i){ 41 | 42 | std::cin >> index; 43 | 44 | if(2 * index + 1 >= size){ 45 | 46 | std::cout << "\nInvalid input\n"; 47 | return -1; 48 | 49 | } 50 | 51 | std::cout << arr[2 * index + 1] << ' '; 52 | 53 | } 54 | 55 | std::cout << '\n'; 56 | 57 | return 0; 58 | 59 | } -------------------------------------------------------------------------------- /Practicum/Week 07/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool IsSorted(unsigned const arr[], size_t const size){ 4 | 5 | bool isAscending = true, isDescending = true; 6 | for(size_t i = 0; i < size - 1 && (isAscending || isDescending); ++i){ 7 | 8 | isAscending = isAscending && arr[i] > arr[i + 1]; 9 | isDescending = isDescending && arr[i] < arr[i + 1]; 10 | 11 | } 12 | 13 | return isAscending || isDescending; 14 | 15 | } 16 | 17 | void FindMostCommonElementWithoutExtraSpace(unsigned const arr[], size_t const size){ 18 | 19 | unsigned mostCommonElementOccurences = 0, mostCommonElementIndex = 0, currentOccurences = 1, currentIndex = 0; 20 | for(size_t i = 1; i < size; ++i){ 21 | 22 | if(arr[i] == arr[i - 1]) ++currentOccurences; 23 | else if(mostCommonElementOccurences < currentOccurences){ 24 | 25 | mostCommonElementIndex = currentIndex; 26 | mostCommonElementOccurences = currentOccurences; 27 | 28 | } 29 | 30 | } 31 | 32 | std::cout << arr[mostCommonElementIndex] << ' ' << mostCommonElementOccurences << '\n'; 33 | 34 | } 35 | 36 | void FindMostCommonElementWithExtraSpace(unsigned const arr[], size_t const size){ 37 | 38 | size_t const MAX_ELEMENT = 1000; 39 | unsigned histogram[MAX_ELEMENT + 1] = { 0, }; 40 | 41 | unsigned mostCommonElementOccurences = 0, mostCommonElement; 42 | 43 | for(size_t i = 0; i < size; ++i) ++histogram[arr[i]]; 44 | 45 | for(size_t i = 0; i < MAX_ELEMENT + 1; ++i) 46 | if(histogram[i] > mostCommonElementOccurences){ 47 | 48 | mostCommonElementOccurences = histogram[i]; 49 | mostCommonElement = i; 50 | 51 | } 52 | 53 | std::cout << mostCommonElement << ' ' << mostCommonElementOccurences << '\n'; 54 | 55 | } 56 | 57 | int main(){ 58 | 59 | size_t const CAPACITY = 1024; 60 | unsigned arr[CAPACITY]; 61 | size_t size; 62 | 63 | std::cin >> size; 64 | if(size == 0 || size >= CAPACITY){ 65 | 66 | std::cout << "Invalid input\n"; 67 | return -1; 68 | 69 | } 70 | 71 | for(size_t i = 0; i < size; ++i) std::cin >> arr[i]; 72 | 73 | if(IsSorted(arr, size)) FindMostCommonElementWithoutExtraSpace(arr, size); 74 | else FindMostCommonElementWithExtraSpace(arr, size); 75 | 76 | return 0; 77 | 78 | } -------------------------------------------------------------------------------- /Practicum/Week 07/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool BinarySearch(int const arr[], size_t start, size_t end, int const toBeSearched){ 4 | 5 | int mid; 6 | 7 | while(start <= end){ 8 | 9 | mid = (start + end) / 2; 10 | 11 | if(arr[mid] == toBeSearched) return true; 12 | 13 | if(toBeSearched < arr[mid]) end = mid - 1; 14 | else start = mid + 1; 15 | 16 | } 17 | 18 | return false; 19 | 20 | } 21 | 22 | void TwoSum(int const arr[], size_t const size, int const sum){ 23 | 24 | for(size_t i = 0; i < size; ++i) 25 | if(BinarySearch(arr, i + 1, size - 1, sum - arr[i])) 26 | std::cout << arr[i] << ' ' << sum - arr[i] << '\n'; 27 | 28 | } 29 | 30 | int main(){ 31 | 32 | size_t const CAPACITY = 1024; 33 | int arr[CAPACITY], sum; 34 | size_t size; 35 | 36 | std::cin >> size; 37 | if(size >= CAPACITY){ 38 | 39 | std::cout << "Invalid input\n"; 40 | return -1; 41 | 42 | } 43 | 44 | for(size_t i = 0; i < size; ++i) std::cin >> arr[i]; 45 | std::cin >> sum; 46 | 47 | TwoSum(arr, size, sum); 48 | 49 | return 0; 50 | 51 | } -------------------------------------------------------------------------------- /Practicum/Week 07/solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void ReadArray(unsigned int arr[], const unsigned int n) 4 | { 5 | for(unsigned i = 0; i < n; i++) 6 | { 7 | std::cin >> arr[i]; 8 | } 9 | } 10 | 11 | void Modify(unsigned int arr[], const unsigned int n) 12 | { 13 | for(unsigned i = 1; i < n; i++) 14 | { 15 | arr[i] += arr[i - 1]; 16 | } 17 | } 18 | 19 | void PrintArray(const unsigned int arr[], const unsigned int n) 20 | { 21 | for(unsigned i = 0; i < n; i++) 22 | { 23 | std::cout << arr[i] << ' '; 24 | } 25 | std::cout << std::endl; 26 | } 27 | 28 | int main() 29 | { 30 | const unsigned int MAX_SIZE = 100; 31 | unsigned int arr[MAX_SIZE]; 32 | 33 | unsigned int n; 34 | do 35 | { 36 | std::cout << "Enter n: "; 37 | std::cin >> n; 38 | } while(n == 0 || n > MAX_SIZE); 39 | 40 | ReadArray(arr, n); 41 | Modify(arr, n); 42 | PrintArray(arr, n); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while(size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for(unsigned i = 0; i < n; i++) 19 | { 20 | for(unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void PrintMatrix(const int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 28 | { 29 | for(unsigned i = 0; i < n; i++) 30 | { 31 | for(unsigned j = 0; j < m; j++) 32 | { 33 | std::cout << matrix[i][j] << ' '; 34 | } 35 | std::cout << std::endl; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | int matrix[MAX_SIZE][MAX_SIZE]; 42 | 43 | unsigned int n = ReadSize(), 44 | m = ReadSize(); 45 | 46 | ReadMatrix(matrix, n, m); 47 | std::cout << std::endl; 48 | PrintMatrix(matrix, n, m); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while(size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for(unsigned i = 0; i < n; i++) 19 | { 20 | for(unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void PrintMax(const int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 28 | { 29 | for(unsigned i = 0; i < n; i++) 30 | { 31 | int max = INT_MIN; 32 | for(unsigned j = 0; j < m; j++) 33 | { 34 | if(matrix[i][j] > max) 35 | { 36 | max = matrix[i][j]; 37 | } 38 | } 39 | std::cout << max << std::endl; 40 | } 41 | } 42 | 43 | int main() 44 | { 45 | int matrix[MAX_SIZE][MAX_SIZE]; 46 | 47 | unsigned int n = ReadSize(), 48 | m = ReadSize(); 49 | 50 | ReadMatrix(matrix, n, m); 51 | std::cout << std::endl; 52 | PrintMax(matrix, n, m); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while(size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for(unsigned i = 0; i < n; i++) 19 | { 20 | for(unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void PrintMin(const int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 28 | { 29 | for(unsigned i = 0; i < m; i++) 30 | { 31 | int min = INT_MAX; 32 | for(unsigned j = 0; j < n; j++) 33 | { 34 | if(matrix[j][i] < min) 35 | { 36 | min = matrix[j][i]; 37 | } 38 | } 39 | std::cout << min << std::endl; 40 | } 41 | } 42 | 43 | int main() 44 | { 45 | int matrix[MAX_SIZE][MAX_SIZE]; 46 | 47 | unsigned int n = ReadSize(), 48 | m = ReadSize(); 49 | 50 | ReadMatrix(matrix, n, m); 51 | std::cout << std::endl; 52 | PrintMin(matrix, n, m); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while(size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for(unsigned i = 0; i < n; i++) 19 | { 20 | for(unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void CountEvenAndOdd(const int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 28 | { 29 | unsigned int even = 0, odd = 0, 30 | limit = n < m ? n : m; 31 | 32 | for(unsigned i = 0; i < limit; i++) 33 | { 34 | for(unsigned j = 0; j < i; j++) 35 | { 36 | if(matrix[i][j] % 2 == 0) 37 | { 38 | even++; 39 | } 40 | } 41 | } 42 | std::cout << "Even: " << even << std::endl; 43 | 44 | for(unsigned i = 0; i < n; i++) 45 | { 46 | for(unsigned j = i + 1; j < m; j++) 47 | { 48 | if(matrix[i][j] % 2 != 0) 49 | { 50 | odd++; 51 | } 52 | } 53 | } 54 | std::cout << "Odd: " << odd << std::endl; 55 | } 56 | 57 | int main() 58 | { 59 | int matrix[MAX_SIZE][MAX_SIZE]; 60 | 61 | unsigned int n = ReadSize(), 62 | m = ReadSize(); 63 | 64 | ReadMatrix(matrix, n, m); 65 | std::cout << std::endl; 66 | CountEvenAndOdd(matrix, n, m); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while(size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for(unsigned i = 0; i < n; i++) 19 | { 20 | for(unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void CountEvenAndOdd(const int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 28 | { 29 | unsigned int even = 0, odd = 0, 30 | limit = n < m ? n : m; 31 | 32 | for(unsigned i = 0; i < limit; i++) 33 | { 34 | for(unsigned j = 0; j < m - i - 1; j++) 35 | { 36 | if(matrix[i][j] % 2 == 0) 37 | { 38 | even++; 39 | } 40 | } 41 | } 42 | std::cout << "Even: " << even << std::endl; 43 | 44 | for(unsigned i = 0; i < limit; i++) 45 | { 46 | for(unsigned j = m - i; j < m; j++) 47 | { 48 | if(matrix[i][j] % 2 != 0) 49 | { 50 | odd++; 51 | } 52 | } 53 | } 54 | std::cout << "Odd: " << odd << std::endl; 55 | } 56 | 57 | int main() 58 | { 59 | int matrix[MAX_SIZE][MAX_SIZE]; 60 | 61 | unsigned int n = ReadSize(), 62 | m = ReadSize(); 63 | 64 | ReadMatrix(matrix, n, m); 65 | std::cout << std::endl; 66 | CountEvenAndOdd(matrix, n, m); 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while (size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for (unsigned i = 0; i < n; i++) 19 | { 20 | for (unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void PrintMatrix(const int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 28 | { 29 | for (unsigned i = 0; i < n; i++) 30 | { 31 | for (unsigned j = 0; j < m; j++) 32 | { 33 | std::cout << matrix[i][j] << ' '; 34 | } 35 | std::cout << std::endl; 36 | } 37 | } 38 | 39 | void TransformMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 40 | { 41 | int j; 42 | for (unsigned i = 0; i < n; i++) 43 | { 44 | j = 0; 45 | while (matrix[i][j] != 1 && j < m) 46 | { 47 | j++; 48 | } 49 | if (j == m) 50 | { 51 | continue; 52 | } 53 | for (unsigned k = 0; k < m; k++) 54 | { 55 | matrix[i][k] = 1; 56 | } 57 | } 58 | } 59 | 60 | int main() 61 | { 62 | int matrix[MAX_SIZE][MAX_SIZE]; 63 | 64 | unsigned int n = ReadSize(), 65 | m = ReadSize(); 66 | 67 | ReadMatrix(matrix, n, m); 68 | 69 | std::cout << std::endl; 70 | 71 | TransformMatrix(matrix, n, m); 72 | PrintMatrix(matrix, n, m); 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Practicum/Week 08/solutions/task8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cin >> size; 11 | } while (size == 0 || size > MAX_SIZE); 12 | 13 | return size; 14 | } 15 | 16 | void ReadMatrix(int matrix[][MAX_SIZE], const unsigned int n, const unsigned int m) 17 | { 18 | for (unsigned i = 0; i < n; i++) 19 | { 20 | for (unsigned j = 0; j < m; j++) 21 | { 22 | std::cin >> matrix[i][j]; 23 | } 24 | } 25 | } 26 | 27 | void fillWithFalse(bool arr[], int size) 28 | { 29 | for (int i = 0; i < size; i++) 30 | { 31 | arr[i] = 0; 32 | } 33 | } 34 | 35 | bool isValidSudoku(int matrix[][MAX_SIZE]) 36 | { 37 | bool usedNumbers[10]; 38 | 39 | // Check rows 40 | for (unsigned i = 0; i < 9; i++) 41 | { 42 | fillWithFalse(usedNumbers, 10); 43 | for (unsigned j = 0; j < 9; j++) 44 | { 45 | if (usedNumbers[matrix[i][j]]) 46 | return false; 47 | usedNumbers[matrix[i][j]] = true; 48 | } 49 | } 50 | 51 | // Check columns 52 | for (unsigned i = 0; i < 9; i++) 53 | { 54 | fillWithFalse(usedNumbers, 10); 55 | for (unsigned j = 0; j < 9; j++) 56 | { 57 | if (usedNumbers[matrix[j][i]]) 58 | return false; 59 | usedNumbers[matrix[j][i]] = true; 60 | } 61 | } 62 | 63 | // Check small squares 64 | for (unsigned x = 0; x < 3; x++) 65 | { 66 | for (unsigned y = 0; y < 3; y++) 67 | { 68 | fillWithFalse(usedNumbers, 10); 69 | for (unsigned i = 0; i < 3; i++) 70 | { 71 | for (unsigned j = 0; j < 3; j++) 72 | { 73 | if (usedNumbers[matrix[j][i]]) 74 | return false; 75 | usedNumbers[matrix[j][i]] = true; 76 | } 77 | } 78 | } 79 | } 80 | return true; 81 | } 82 | 83 | int main() 84 | { 85 | int matrix[MAX_SIZE][MAX_SIZE]; 86 | 87 | ReadMatrix(matrix, 9, 9); 88 | 89 | std::cout << std::endl 90 | << isValidSudoku(matrix); 91 | return 0; 92 | } -------------------------------------------------------------------------------- /Practicum/Week 09/README.md: -------------------------------------------------------------------------------- 1 |

Задачи за референции, указатели и динамична памет

2 | 3 | ## Задачи 4 | 5 | ### Задача 1: 6 | Напишете функция `swap` чрез подаване на аргументите по указател. 7 | 8 | ### Задача 2: 9 | Инициализирайте целочислена променлива с произволна стойност, насочете указател към нея и изведете на екрана стойността на променливата, нейния адрес, стойността на указателя, адресът на указателя и стойността, пазена в адреса, към който указателят сочи. 10 | 11 | ### Задача 3: 12 | Създайте функция `int* CreateArray(const size_t size)`, която създава масив от цели числа по въведена от клавиатурата стойност **n** и връща указател към първия елемент. Ако заделянето на памет е неуспешно, върнете `nullptr`. 13 | 14 | ### Задача 4: 15 | Създайте функция `void PrintArray(const int* arr, const size_t size)`, която отпечатва масив от цели числа. Използвайте *указателна аритметика*. 16 | 17 | ### Задача 5: 18 | Да се състави програма, в която се въвеждат естествени цели числа от клавиатурата. Размерът на масива да се въвежда от клавиатурата и да се заделя динамично памет за него. Програмата да разменя симетричните елементи спрямо средата, като по-малкото число остава в началото на масива, а по-голямото в края. 19 | ``` 20 | Вход 21 | 7 22 | 189 123 145 196 1204 0 112 23 | 24 | Изход 25 | 112 0 145 196 1204 123 189 26 | ``` 27 | 28 | 29 | ### Задача 6: 30 | Да се състави програма, която прочита **n** естествени числа от клавиатурата, заделяйки динамична памет за тях, сортира първата половина в нарастващ ред, a втората половина в намаляващ ред и ги отпечатва. Всяко от действията да бъде отделна функция, като съответно подадете половината масив на едната сортираща функция и другата половина от масива на другата. Използвайте *указателна аритметика*. 31 | ``` 32 | Вход 33 | 7 34 | 189 123 145 196 1000 0 112 35 | 36 | Изход 37 | 123 145 189 1000 196 112 0 38 | ``` 39 | 40 | ### Задача 7: 41 | Създайте 2 функции първата да бъде `int** AllocateMatrix(const size_t rows, const size_t cols)`, която да заделя памет за една матрица, а втората да бъде `void Deallocate(int **matrix, const size_t rows)`, която да освобождава заделената памет за матрицата. 42 | 43 | ##### Забележка, ако при заделянето се появи грешка, първата функция да връща nullptr и да се погрижи да няма изтекла памет. 44 | 45 | ### Задача 8: 46 | Да се напише функция, която измества нулите от всеки ред на целочислена матрица най-отзад, подредбата на останалите елементите да се запази. Употребата на оператора за индексиране не е позволена. Да не се използва помощна матрица. -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int* const a, int* const b) 4 | { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | int main() 11 | { 12 | int a = 3, b = 5; 13 | Swap(&a, &b); 14 | std::cout << "a = " << a << ", " 15 | << "b = " << b << std::endl; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int var = 10; 6 | int* ptr = &var; 7 | 8 | std::cout << "Value of var: " << var << std::endl; 9 | std::cout << "Address of var: " << &var << std::endl; 10 | std::cout << "Value of ptr: " << ptr << std::endl; 11 | std::cout << "Address of ptr: " << &ptr << std::endl; 12 | std::cout << "Pointer value: " << *ptr << std::endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int* CreateArray(const size_t size) 4 | { 5 | return new(std::nothrow) int[size]; 6 | } 7 | 8 | void ReadArray(int* const array, const size_t size) 9 | { 10 | for(size_t i = 0; i < size; i++) 11 | { 12 | std::cin >> array[i]; 13 | } 14 | } 15 | 16 | void PrintArray(const int* const array, const size_t size) 17 | { 18 | for(size_t i = 0; i < size; i++) 19 | { 20 | std::cout << array[i] << ' '; 21 | } 22 | std::cout << std::endl; 23 | } 24 | 25 | int main() 26 | { 27 | size_t n; 28 | std::cin >> n; 29 | 30 | int* array = CreateArray(n); 31 | if(!array) 32 | { 33 | std::cout << "Memory problem!" << std::endl; 34 | return -1; 35 | } 36 | 37 | ReadArray(array, n); 38 | PrintArray(array, n); 39 | 40 | delete[] array; 41 | array = nullptr; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int* CreateArray(const size_t size) 4 | { 5 | return new(std::nothrow) int[size]; 6 | } 7 | 8 | void ReadArray(int* const array, const size_t size) 9 | { 10 | for(size_t i = 0; i < size; i++) 11 | { 12 | std::cin >> array[i]; 13 | } 14 | } 15 | 16 | void PrintArray(const int* const array, const size_t size) 17 | { 18 | for(size_t i = 0; i < size; i++) 19 | { 20 | std::cout << *(array + i) << ' '; 21 | } 22 | std::cout << std::endl; 23 | } 24 | 25 | int main() 26 | { 27 | size_t n; 28 | std::cin >> n; 29 | 30 | int* array = CreateArray(n); 31 | if(!array) 32 | { 33 | std::cout << "Memory problem!" << std::endl; 34 | return -1; 35 | } 36 | 37 | ReadArray(array, n); 38 | PrintArray(array, n); 39 | 40 | delete[] array; 41 | array = nullptr; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(unsigned int* a, unsigned int* b) 4 | { 5 | unsigned int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | unsigned int* CreateArray(const size_t size) 11 | { 12 | return new(std::nothrow) unsigned int[size]; 13 | } 14 | 15 | void ReadArray(unsigned int* const array, const size_t size) 16 | { 17 | for(size_t i = 0; i < size; i++) 18 | { 19 | std::cin >> array[i]; 20 | } 21 | } 22 | 23 | void PrintArray(const unsigned int* const array, const size_t size) 24 | { 25 | for(size_t i = 0; i < size; i++) 26 | { 27 | std::cout << array[i] << ' '; 28 | } 29 | std::cout << std::endl; 30 | } 31 | 32 | void Modify(unsigned int* const array, const size_t size) 33 | { 34 | for(size_t i = 0; i < size / 2; i++) 35 | { 36 | if(array[i] > array[size - i - 1]) 37 | { 38 | Swap(&array[i], &array[size - i - 1]); 39 | } 40 | } 41 | } 42 | 43 | int main() 44 | { 45 | size_t n; 46 | std::cin >> n; 47 | 48 | unsigned int* array = CreateArray(n); 49 | if(!array) 50 | { 51 | std::cout << "Memory problem!" << std::endl; 52 | return -1; 53 | } 54 | 55 | ReadArray(array, n); 56 | Modify(array, n); 57 | PrintArray(array, n); 58 | 59 | delete[] array; 60 | array = nullptr; 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int* CreateArray(const size_t size) 4 | { 5 | return new(std::nothrow) unsigned int[size]; 6 | } 7 | 8 | void ReadArray(unsigned int* const array, const size_t size) 9 | { 10 | for(size_t i = 0; i < size; i++) 11 | { 12 | std::cin >> array[i]; 13 | } 14 | } 15 | 16 | void Swap(unsigned int* const a, unsigned int* const b) 17 | { 18 | int temp = *a; 19 | *a = *b; 20 | *b = temp; 21 | } 22 | 23 | // selection sort -> asc 24 | void SortAsc(unsigned int* const array, const size_t from, const size_t to) 25 | { 26 | for(unsigned i = from; i < to; i++) 27 | { 28 | unsigned minIndex = i; 29 | for(unsigned j = i + 1; j < to; j++) 30 | { 31 | if (array[j] < array[minIndex]) 32 | { 33 | minIndex = j; 34 | } 35 | } 36 | 37 | if(minIndex != i) 38 | { 39 | Swap(&array[minIndex], &array[i]); 40 | } 41 | } 42 | } 43 | 44 | 45 | // selection sort -> desc 46 | void SortDesc(unsigned int* const array, const size_t from, const size_t to) 47 | { 48 | for(unsigned i = from; i < to; i++) 49 | { 50 | unsigned maxIndex = i; 51 | for(unsigned j = i + 1; j < to; j++) 52 | { 53 | if(array[j] > array[maxIndex]) 54 | { 55 | maxIndex = j; 56 | } 57 | } 58 | 59 | if(maxIndex != i) 60 | { 61 | Swap(&array[maxIndex], &array[i]); 62 | } 63 | } 64 | } 65 | 66 | void Sort(unsigned int* const array, const size_t size) 67 | { 68 | SortAsc(array, 0, size / 2); 69 | SortDesc(array, size / 2, size); 70 | } 71 | 72 | void PrintArray(const unsigned int* const array, const size_t size) 73 | { 74 | for(size_t i = 0; i < size; i++) 75 | { 76 | std::cout << array[i] << ' '; 77 | } 78 | std::cout << std::endl; 79 | } 80 | 81 | int main() 82 | { 83 | size_t n; 84 | std::cin >> n; 85 | 86 | unsigned int* array = CreateArray(n); 87 | if(!array) 88 | { 89 | std::cout << "Memory problem!" << std::endl; 90 | return -1; 91 | } 92 | 93 | ReadArray(array, n); 94 | Sort(array, n); 95 | PrintArray(array, n); 96 | 97 | delete[] array; 98 | array = nullptr; 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /Practicum/Week 09/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Deallocate(int const * const * const matrix, const size_t rows) 4 | { 5 | for(size_t i = 0; i < rows; i++) 6 | { 7 | delete[] matrix[i]; 8 | } 9 | delete[] matrix; 10 | } 11 | 12 | int** AllocateMatrix(const size_t rows, const size_t cols) 13 | { 14 | int** matrix = new(std::nothrow) int*[rows]; 15 | if(!matrix) 16 | { 17 | return nullptr; 18 | } 19 | 20 | for(size_t i = 0; i < rows; i++) 21 | { 22 | matrix[i] = new(std::nothrow) int[cols]; 23 | if(!matrix[i]) 24 | { 25 | Deallocate(matrix, i); 26 | return nullptr; 27 | } 28 | } 29 | 30 | return matrix; 31 | } 32 | 33 | void ReadMatrix(int* const * const matrix, const size_t rows, const size_t cols) 34 | { 35 | for(size_t i = 0; i < rows; i++) 36 | { 37 | for(size_t j = 0; j < cols; j++) 38 | { 39 | std::cin >> matrix[i][j]; 40 | } 41 | } 42 | } 43 | 44 | void PrintMatrix(int const * const * const matrix, const size_t rows, const size_t cols) 45 | { 46 | for(size_t i = 0; i < rows; i++) 47 | { 48 | for(size_t j = 0; j < cols; j++) 49 | { 50 | std::cout << matrix[i][j] << ' '; 51 | } 52 | std::cout << std::endl; 53 | } 54 | } 55 | 56 | int main() 57 | { 58 | size_t rows, cols; 59 | std::cin >> rows >> cols; 60 | 61 | int** matrix = AllocateMatrix(rows, cols); 62 | if(!matrix) 63 | { 64 | std::cout << "Memory problem!" << std::endl; 65 | return -1; 66 | } 67 | 68 | ReadMatrix(matrix, rows, cols); 69 | 70 | std::cout << std::endl; 71 | PrintMatrix(matrix, rows, cols); 72 | 73 | Deallocate(matrix, rows); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const size_t MAX_SIZE = 100; 5 | 6 | char* ReverseStr(char const * const s) 7 | { 8 | size_t const size = strlen(s); 9 | char *reversed = new(std::nothrow) char[size]; 10 | 11 | if(!reversed) 12 | { 13 | return nullptr; 14 | } 15 | 16 | for (int i = 0; i < size / 2; i++) 17 | { 18 | reversed[i] = reversed[size - i - 1]; 19 | } 20 | 21 | return reversed; 22 | } 23 | 24 | int main() 25 | { 26 | char str[MAX_SIZE]; 27 | std::cin.getline(str, MAX_SIZE); 28 | std::cout << ReverseStr(str) << std::endl; 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t strlen(const char* const str) 4 | { 5 | unsigned int len = 0; 6 | while(str[len]) // str[len] != '\0' 7 | { 8 | ++len; 9 | } 10 | return len; 11 | } 12 | 13 | int strcmp(const char* str1, const char* str2) 14 | { 15 | while(*str1 && *str2 && *str1 == *str2) 16 | { 17 | ++str1; 18 | ++str2; 19 | } 20 | return *str1 - *str2; 21 | } 22 | 23 | // The destination array must be large enough to append the contents of 24 | // source array (including the terminating null character). 25 | void strcat(char* const destination, const char* const source) 26 | { 27 | size_t destLen = strlen(destination), 28 | srcLen = strlen(source); 29 | 30 | for(size_t i = 0; i < srcLen; i++) 31 | { 32 | destination[destLen + i] = source[i]; 33 | } 34 | destination[destLen + srcLen] = '\0'; 35 | } 36 | 37 | // The destination array must be large enough to contain the same string 38 | // as source (including the terminating null character). 39 | void strcpy(char* const destination, const char* const source) 40 | { 41 | size_t srcLen = strlen(source); 42 | for(size_t i = 0; i < srcLen; i++) 43 | { 44 | destination[i] = source[i]; 45 | } 46 | destination[srcLen] = '\0'; 47 | } 48 | 49 | int main() 50 | { 51 | const size_t MAX_SIZE = 256; 52 | char str1[MAX_SIZE], str2[MAX_SIZE]; 53 | 54 | std::cout << "str1: "; 55 | std::cin.getline(str1, MAX_SIZE); 56 | 57 | std::cout << "str2: "; 58 | std::cin.getline(str2, MAX_SIZE); 59 | 60 | size_t str1Len = strlen(str1), str2Len = strlen(str2); 61 | std::cout << "\nstr1 length: " << str1Len 62 | << "\nstr2 length: " << str2Len 63 | << std::endl; 64 | 65 | std::cout << "\nstring comparation: " << strcmp(str1, str2) << std::endl; 66 | 67 | if(str1Len + str2Len + 1 <= MAX_SIZE) 68 | { 69 | strcat(str1, str2); 70 | std::cout << "\nconcatenation: " << str1 << std::endl; 71 | str1Len = strlen(str1); 72 | } 73 | 74 | if(str1Len + 1 <= MAX_SIZE) 75 | { 76 | strcpy(str2, str1); 77 | std::cout << "\ncopy str1 to str2: " << str2 << std::endl; 78 | } 79 | return 0; 80 | } 81 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char* LargestPrefix(const char* const str1, const char* const str2, const char* const str3) 5 | { 6 | size_t minlen = std::min(strlen(str1), strlen(str2)), prefixLen = 0; 7 | minlen = std::min(minlen, strlen(str3)); 8 | 9 | while(prefixLen < minlen && str1[prefixLen] == str2[prefixLen] && str1[prefixLen] == str3[prefixLen]) 10 | { 11 | ++prefixLen; 12 | } 13 | 14 | char* result = new(std::nothrow) char[prefixLen + 1]; 15 | if(!result) 16 | { 17 | return nullptr; 18 | } 19 | 20 | for(size_t i = 0; i < prefixLen; i++) 21 | { 22 | result[i] = str1[i]; 23 | } 24 | 25 | result[prefixLen] = '\0'; 26 | return result; 27 | } 28 | 29 | int main() 30 | { 31 | size_t n1, n2, n3; 32 | std::cin >> n1; 33 | std::cin.ignore(); 34 | 35 | char* str1 = new(std::nothrow) char[n1]; 36 | if(!str1) 37 | { 38 | std::cout << "Memory problem!" << std::endl; 39 | return -1; 40 | } 41 | 42 | std::cin.getline(str1, n1 + 1); 43 | 44 | std::cin >> n2; 45 | std::cin.ignore(); 46 | 47 | char* str2 = new(std::nothrow) char[n2]; 48 | if(!str2) 49 | { 50 | std::cout << "Memory problem!" << std::endl; 51 | 52 | delete[] str1; 53 | str1 = nullptr; 54 | 55 | return -1; 56 | } 57 | 58 | std::cin.getline(str2, n2 + 1); 59 | 60 | std::cin >> n3; 61 | std::cin.ignore(); 62 | 63 | char* str3 = new(std::nothrow) char[n3]; 64 | if(!str3) 65 | { 66 | std::cout << "Memory problem!" << std::endl; 67 | 68 | delete[] str1; 69 | str1 = nullptr; 70 | 71 | delete[] str2; 72 | str2 = nullptr; 73 | 74 | return -1; 75 | } 76 | 77 | std::cin.getline(str3, n3 + 1); 78 | 79 | char* pref = LargestPrefix(str1, str2, str3); 80 | if(pref) 81 | { 82 | std::cout << '\n' << pref << std::endl; 83 | } 84 | 85 | delete[] pref; 86 | pref = nullptr; 87 | 88 | delete[] str1; 89 | str1 = nullptr; 90 | 91 | delete[] str2; 92 | str2 = nullptr; 93 | 94 | delete[] str3; 95 | str3 = nullptr; 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int Find(const char* const s, const char* const s1) 5 | { 6 | size_t const sLen = strlen(s), s1Len = strlen(s1); 7 | 8 | for(size_t i = 0; i < sLen; i++) 9 | { 10 | size_t j = 0; 11 | while(s[i] && s1[j] && s[i] == s1[j]) 12 | { 13 | i++; 14 | j++; 15 | } 16 | 17 | i -= j; 18 | 19 | if(j - 1 == s1Len) 20 | { 21 | return i; 22 | } 23 | } 24 | 25 | return -1; 26 | } 27 | 28 | int main() 29 | { 30 | const size_t MAX_SIZE = 256; 31 | char s[MAX_SIZE], s1[MAX_SIZE]; 32 | 33 | std::cout << "s: "; 34 | std::cin.getline(s, MAX_SIZE); 35 | 36 | std::cout << "s1: "; 37 | std::cin.getline(s1, MAX_SIZE); 38 | 39 | std::cout << Find(s, s1) << std::endl; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const size_t MAX_SIZE = 100; 5 | const size_t MAX_LEN = 11; 6 | 7 | void Read(char matrix[][MAX_SIZE][MAX_LEN], const size_t n) 8 | { 9 | for(size_t i = 0; i < n; i++) 10 | { 11 | for(size_t j = 0; j < n; j++) 12 | { 13 | std::cin >> matrix[i][j]; 14 | } 15 | } 16 | } 17 | 18 | bool IsSymetrical(const char matrix[][MAX_SIZE][MAX_LEN], const size_t n) 19 | { 20 | for (size_t i = 0; i < n; i++) 21 | { 22 | for (size_t j = 0; j < i; j++) 23 | { 24 | if(strcmp(matrix[i][j], matrix[j][i])) 25 | { 26 | return false; 27 | } 28 | } 29 | } 30 | return true; 31 | } 32 | 33 | int main() 34 | { 35 | char matrix[MAX_SIZE][MAX_SIZE][MAX_LEN]; 36 | size_t n; 37 | do 38 | { 39 | std::cout << "Enter n: "; 40 | std::cin >> n; 41 | } while (n > MAX_SIZE); 42 | 43 | Read(matrix, n); 44 | std::cout << std::boolalpha << IsSymetrical(matrix, n) << std::endl; 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const unsigned int MAX_SIZE = 100, MAX_LEN = 11; 5 | 6 | void ReadArray(char array[][MAX_LEN], const unsigned int n) 7 | { 8 | for(unsigned i = 0; i < n; i++) 9 | { 10 | std::cin >> array[i]; 11 | } 12 | } 13 | 14 | void Swap(char* const a, char* const b) 15 | { 16 | char temp[MAX_LEN]; 17 | strcpy(temp, a); 18 | strcpy(a, b); 19 | strcpy(b, temp); 20 | } 21 | 22 | // selection sort -> asc 23 | void Sort(char array[][MAX_LEN], const unsigned int n) 24 | { 25 | for(unsigned i = 0; i < n; i++) 26 | { 27 | unsigned minIndex = i; 28 | for(unsigned j = i + 1; j < n; j++) 29 | { 30 | if(strlen(array[j]) < strlen(array[minIndex])) 31 | { 32 | minIndex = j; 33 | } 34 | } 35 | 36 | if(minIndex != i) 37 | { 38 | Swap(array[minIndex], array[i]); 39 | } 40 | } 41 | } 42 | 43 | void PrintArray(const char array[][MAX_LEN], const unsigned int n) 44 | { 45 | for(unsigned i = 0; i < n; i++) 46 | { 47 | std::cout << array[i] << ' '; 48 | if((i + 1) % 3 == 0) 49 | { 50 | std::cout << std::endl; 51 | } 52 | } 53 | } 54 | 55 | int main() 56 | { 57 | char array[MAX_SIZE][MAX_LEN]; 58 | unsigned int n; 59 | do 60 | { 61 | std::cout << "Enter n: "; 62 | std::cin >> n; 63 | } while(n > MAX_SIZE); 64 | 65 | ReadArray(array, n); 66 | Sort(array, n); 67 | 68 | std::cout << std::endl; 69 | PrintArray(array, n); 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const size_t MAX_SIZE = 100, LETTERS = 26; 5 | 6 | bool IsValid(const char* const str) 7 | { 8 | unsigned int len = strlen(str); 9 | for(unsigned i = 0; i < len; i++) 10 | { 11 | if(!(str[i] >= 'a' && str[i] <= 'z')) 12 | { 13 | return false; 14 | } 15 | } 16 | return true; 17 | } 18 | 19 | char* RemoveDuplicates(const char* const str) 20 | { 21 | unsigned int letters[LETTERS] = { 0, }, 22 | len = strlen(str); 23 | 24 | for(unsigned i = 0; i < len; i++) 25 | { 26 | letters[str[i] - 'a']++; 27 | } 28 | 29 | unsigned int length = 0; 30 | for(unsigned i = 0; i < LETTERS; i++) 31 | { 32 | length += (letters[i] == 1); 33 | } 34 | 35 | char *result = new(std::nothrow) char[length + 1]; 36 | if(!result) 37 | { 38 | std::cout << "Memory problem!" << std::endl; 39 | return nullptr; 40 | } 41 | 42 | unsigned int index = 0; 43 | for(unsigned i = 0; i < len; i++) 44 | { 45 | if(letters[str[i] - 'a'] == 1) 46 | { 47 | result[index++] = str[i]; 48 | } 49 | } 50 | result[length] = '\0'; 51 | return result; 52 | } 53 | 54 | int main() 55 | { 56 | char input[MAX_SIZE]; 57 | std::cin.getline(input, MAX_SIZE); 58 | 59 | if(!IsValid(input)) 60 | { 61 | std::cout << "Incorrect input!" << std::endl; 62 | return -1; 63 | } 64 | 65 | char* result = RemoveDuplicates(input); 66 | std::cout << result << std::endl; 67 | 68 | delete[] result; 69 | result = nullptr; 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Practicum/Week 10/solutions/task8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const unsigned int MAX_SIZE = 100, MAX_LEN = 20; 5 | 6 | void ReadArray(char array[][MAX_LEN], const unsigned int n) 7 | { 8 | for(unsigned i = 0; i < n; i++) 9 | { 10 | std::cin >> array[i]; 11 | } 12 | } 13 | 14 | void ClearArray(char array[][MAX_LEN], unsigned int& n) 15 | { 16 | unsigned int read = 0, write = 0; 17 | while(read < n) 18 | { 19 | if(strlen(array[read]) < 4) 20 | { 21 | read++; 22 | } 23 | else 24 | { 25 | strcpy(array[write++], array[read++]); 26 | } 27 | } 28 | n -= read - write; 29 | } 30 | 31 | void PrintArray(const char array[][MAX_LEN], const unsigned int n) 32 | { 33 | for(unsigned i = 0; i < n; i++) 34 | { 35 | std::cout << array[i] << std::endl; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | char array[MAX_SIZE][MAX_LEN]; 42 | unsigned int n; 43 | do 44 | { 45 | std::cout << "Enter n: "; 46 | std::cin >> n; 47 | } while(n > MAX_SIZE); 48 | 49 | ReadArray(array, n); 50 | ClearArray(array, n); 51 | 52 | std::cout << std::endl; 53 | PrintArray(array, n); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Practicum/Week 11/README.md: -------------------------------------------------------------------------------- 1 |

Допълнителни задачи за указатели и динамична памет

2 | 3 | ## Задачи 4 | 5 | ### Задача 1: 6 | Да се напише функция, която приема символен низ и връща нов символен низ с **ТОЧНА ГОЛЕМИНА**, в който всички цифри са цензурирани. (Всяко число е заменено с '*'). 7 | 8 | ### Задача 2: 9 | Да се въведе низ състоящ се от думи, на своя страна състоящи се от цифри и латински букви, разделени с интервали. Да се обходи низът и в нов масив да се запишат всички числа, които могат да бъдат преобразувани (не е задължително да е с точна големина). 10 | 11 | **Пример:** 12 | ``` 13 | Вход: 14 | 1345 asd 2 dwe 12! 15 | 16 | Изход: 17 | 1345 2 12 18 | ``` 19 | 20 | ### Задача 3: 21 | Заделете символен низ, чиято дължина четете от конзолата. Коригирайте символния низ така, че да съдържа по един интервал след всяка дума (ако се срещат повече да се редуцират до 1 интервал). Думата е съставена от букви, цифри и “_“. Ако има пунктуационен знак (“,“ “.“ “?“ “!“), то интервалът трябва да стои сред него. Сложността *по памет да е константна*, а *по време линейна*. 22 | 23 | **Пример:** 24 | ``` 25 | Вход: 26 | Hello ,world ! How are you? 27 | 28 | Изход: 29 | Hello, world! How are you? 30 | ``` 31 | 32 | ### Задача 4: 33 | Напишете функция, която приема като параметър два едномерни сортирани целочислени масива с елементи от тип `int` и връща като резултат трети масив (който съдържа всички елементи на двата масива и само тях), който също е сортиран. 34 | Предполага се, че входните масиви са сортирани преди да се извика написаната от вас функция. 35 | 36 | ### Задача 5: 37 | Дадена е **NxM** матрица от цели числа. Напишете функция, която връща щастливите числа във всеки ред на матрицата. Щастливо число е елемент от матрицата, който е минималният елемент в своя ред и максималният елемент в своята колона. 38 | 39 | **Пример:** 40 | ``` 41 | Вход: 42 | 3 7 8 43 | 9 11 13 44 | 15 16 17 45 | 46 | Изход: 47 | 15 48 | ``` -------------------------------------------------------------------------------- /Practicum/Week 11/solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char* Censor(const char* const str) 5 | { 6 | unsigned len = strlen(str); 7 | 8 | char* newStr = new(std::nothrow) char[len + 1]; 9 | if(!newStr) 10 | { 11 | return nullptr; 12 | } 13 | 14 | for(unsigned i = 0; i < len; i++) 15 | { 16 | if(str[i] >= '0' && str[i] <= '9') 17 | { 18 | newStr[i] = '*'; 19 | } 20 | else 21 | { 22 | newStr[i] = str[i]; 23 | } 24 | } 25 | 26 | newStr[len] = '\0'; 27 | return newStr; 28 | } 29 | 30 | int main() 31 | { 32 | const unsigned int CAPACITY = 1024; 33 | char str[CAPACITY]; 34 | std::cin.getline(str, CAPACITY); 35 | 36 | char* newStr = Censor(str); 37 | if(!newStr) 38 | { 39 | std::cout << "Memory problem!" << std::endl; 40 | return -1; 41 | } 42 | 43 | std::cout << newStr << std::endl; 44 | 45 | delete[] newStr; 46 | newStr = nullptr; 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Practicum/Week 11/solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool IsDigit(char c) 5 | { 6 | return c >= '0' && c <= '9'; 7 | } 8 | 9 | unsigned CountNumbers(const char* const str) 10 | { 11 | unsigned int count = 0, i = 0, len = strlen(str); 12 | while(i < len) 13 | { 14 | bool isNum = false; 15 | while(IsDigit(str[i])) 16 | { 17 | isNum = true; 18 | i++; 19 | } 20 | count += isNum; 21 | i += !isNum; 22 | } 23 | return count; 24 | } 25 | 26 | unsigned ToInt(const char* str, const unsigned int from, const unsigned int to) 27 | { 28 | unsigned int result = 0; 29 | for(unsigned i = from; i <= to; i++) 30 | { 31 | result *= 10; 32 | result += (str[i] - '0'); 33 | } 34 | return result; 35 | } 36 | 37 | unsigned int* GetNumbers(const char* str, const unsigned int size) 38 | { 39 | unsigned int* numbers = new(std::nothrow) unsigned int[size]; 40 | if(!numbers) 41 | { 42 | return nullptr; 43 | } 44 | 45 | unsigned int len = strlen(str), i = 0, j = 0; 46 | while(i < len && j < size) 47 | { 48 | if(IsDigit(str[i])) 49 | { 50 | unsigned int from = i, to = i; 51 | while(i + 1 < len && IsDigit(str[i + 1])) 52 | { 53 | to++; 54 | i++; 55 | } 56 | numbers[j] = ToInt(str, from, to); 57 | j++; i++; 58 | } 59 | else i++; 60 | } 61 | 62 | return numbers; 63 | } 64 | 65 | int main() 66 | { 67 | const unsigned int CAPACITY = 256; 68 | char str[CAPACITY]; 69 | std::cin.getline(str, CAPACITY); 70 | 71 | unsigned int size = CountNumbers(str); 72 | if(size == 0) 73 | { 74 | return 0; 75 | } 76 | 77 | unsigned int* numbers = GetNumbers(str, size); 78 | if(!numbers) 79 | { 80 | std::cout << "Memory problem!" << std::endl; 81 | return -1; 82 | } 83 | 84 | for(unsigned i = 0; i < size; i++) 85 | { 86 | std::cout << numbers[i] << ' '; 87 | } 88 | std::cout << std::endl; 89 | 90 | delete[] numbers; 91 | numbers = nullptr; 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /Practicum/Week 11/solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool IsWordCharacter(char const c) 5 | { 6 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || 7 | (c >= '0' && c <= '9') || 8 | c == '_'; 9 | } 10 | 11 | bool IsPunctuationMark(char const a) 12 | { 13 | return a == ',' || a == '.' || a == '?' || a == '!'; 14 | } 15 | 16 | bool IsValidString(const char* const str) 17 | { 18 | for(unsigned i = 0; str[i]; ++i) 19 | { 20 | if(!IsWordCharacter(str[i]) && !IsPunctuationMark(str[i]) && str[i] != ' ') 21 | { 22 | return false; 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | void Modify(char* const str) 29 | { 30 | unsigned len = strlen(str), read = 0, write = 0; 31 | 32 | while(read < len && str[read] == ' ') ++read; 33 | 34 | while(read < len) 35 | { 36 | if(IsWordCharacter(str[read]) || IsPunctuationMark(str[read])) 37 | { 38 | str[write++] = str[read++]; 39 | } 40 | 41 | while(str[read] == ' ' && read + 1 < len && str[read + 1] == ' ') 42 | { 43 | read++; 44 | } 45 | 46 | if(str[read] == ' ' && read + 1 < len && IsPunctuationMark(str[read + 1])) 47 | { 48 | str[write++] = str[read + 1]; 49 | 50 | if(read + 1 != len) 51 | str[write++] = ' '; 52 | 53 | read += 2; 54 | } 55 | 56 | if(str[read] == ' ' && read + 1 < len && IsWordCharacter(str[read + 1])) 57 | { 58 | if(IsWordCharacter(str[write - 1])) 59 | str[write++] = ' '; 60 | 61 | str[write++] = str[read + 1]; 62 | read += 2; 63 | } 64 | } 65 | 66 | str[write] = '\0'; 67 | } 68 | 69 | int main() 70 | { 71 | unsigned len; 72 | std::cin >> len; 73 | 74 | char* str = new(std::nothrow) char[len + 1]; 75 | if(!str) 76 | { 77 | std::cout << "Memory problem!" << std::endl; 78 | return -1; 79 | } 80 | 81 | std::cin.get(); 82 | std::cin.getline(str, len + 1); 83 | 84 | if(!IsValidString(str)) 85 | { 86 | std::cout << "Threre are invalid characters in the string!" << std::endl; 87 | 88 | delete[] str; 89 | str = nullptr; 90 | 91 | return -1; 92 | } 93 | 94 | Modify(str); 95 | std::cout << std::endl << str << std::endl; 96 | 97 | delete[] str; 98 | str = nullptr; 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /Practicum/Week 11/solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 100; 4 | 5 | unsigned int ReadSize() 6 | { 7 | unsigned int size; 8 | do 9 | { 10 | std::cout << "Enter size: "; 11 | std::cin >> size; 12 | } while (size > MAX_SIZE); 13 | return size; 14 | } 15 | 16 | void ReadArray(int array[], unsigned int const size) 17 | { 18 | std::cout << "Enter array:\n"; 19 | for(unsigned i = 0; i < size; i++) 20 | { 21 | std::cin >> array[i]; 22 | } 23 | } 24 | 25 | void PrintArray(const int array[], unsigned int const size) 26 | { 27 | for(unsigned i = 0; i < size; i++) 28 | { 29 | std::cout << array[i] << ' '; 30 | } 31 | std::cout << std::endl; 32 | } 33 | 34 | int* MergeArr(const int arr1[], const int arr2[], const unsigned int n1, const unsigned int n2) 35 | { 36 | unsigned int index1 = 0, index2 = 0; 37 | 38 | int* result = new(std::nothrow) int[n1 + n2]; 39 | if(!result) 40 | { 41 | return nullptr; 42 | } 43 | 44 | unsigned int index = 0; 45 | 46 | while(index1 < n1 || index2 < n2) 47 | { 48 | if(index1 == n1) 49 | { 50 | result[index] = arr2[index2]; 51 | index2++; 52 | } 53 | else if(index2 == n2) 54 | { 55 | result[index] = arr1[index1]; 56 | index1++; 57 | } 58 | else 59 | { 60 | if(arr1[index1] > arr2[index2]) 61 | { 62 | result[index] = arr2[index2]; 63 | index2++; 64 | } 65 | else 66 | { 67 | result[index] = arr1[index1]; 68 | index1++; 69 | } 70 | } 71 | index++; 72 | } 73 | return result; 74 | } 75 | 76 | int main() 77 | { 78 | int arr1[MAX_SIZE], arr2[MAX_SIZE]; 79 | 80 | unsigned int n1 = ReadSize(); 81 | ReadArray(arr1, n1); 82 | 83 | unsigned int n2 = ReadSize(); 84 | ReadArray(arr2, n2); 85 | 86 | int* result = MergeArr(arr1, arr2, n1, n2); 87 | if(!result) 88 | { 89 | std::cout << "Memory problem!" << std::endl; 90 | return -1; 91 | } 92 | 93 | std::cout << std::endl; 94 | PrintArray(result, n1 + n2); 95 | 96 | delete[] result; 97 | result = nullptr; 98 | 99 | return 0; 100 | } 101 | 102 | /* 103 | 5 104 | 1 3 5 7 9 105 | 6 106 | 2 4 6 8 10 12 107 | */ 108 | -------------------------------------------------------------------------------- /Practicum/Week 12/README.md: -------------------------------------------------------------------------------- 1 |

Задачи за рекурсивни функции

2 | 3 | ## Задачи 4 | 5 | ## ***За решението на всяка задача трябва да напишете рекурсивна функция.*** 6 | 7 | ### Задача 1: 8 | Да се пресметне **n!**. 9 | 10 | ### Задача 2: 11 | Да се пресметне **n**-тото число на Фибоначи 12 | 13 | ### Задача 3: 14 | Да се пресметне сумата на цифрите на дадено естествено число. 15 | 16 | ### Задача 4: 17 | Напишете рекурсивна функцията `isPrime`. 18 | 19 | ### Задача 5: 20 | Напишете рекурсивна версия на функцията `pow`. 21 | 22 | ### Задача 6: 23 | Да се провери дали число е палиндром рекурсивно 24 | 25 | ### Задача 7: 26 | Да се напише програма която рекурсивно генерира всички пермутации на **n**-символен низ без повтарящи се елементи. 27 | 28 | **Пример:** 29 | ``` 30 | Вход: 31 | 3 32 | abc 33 | 34 | Изход: 35 | abc 36 | acb 37 | bac 38 | bca 39 | cab 40 | cba 41 | 42 | ``` 43 | 44 | ### Задача 8: 45 | При дадена булева матрица **n x m** да се изведат колко “острови” има в тази матрица. Остров се наричат ненулеви стойности, които са едни до други (съседни) по хоризонтал, диагонал или вертикал. Сама единица също се брои за остров. 46 | 47 | **Пример:** 48 | 49 | 1 1 0 0
50 | 1 0 0 0
51 | 0 0 1 0 - 2 острова 52 | 53 | 1 0 0 0
54 | 0 1 0 1
55 | 0 0 1 0 - 1 остров 56 | -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | std::cout << "Hristo"; 6 | 7 | } -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | std::cout << "The sizeof(char) is: " << sizeof(char) << " bytes\n"; 6 | std::cout << "The sizeof(short) is: " << sizeof(short) << " bytes\n"; 7 | std::cout << "The sizeof(int) is: " << sizeof(int) << " bytes\n"; 8 | std::cout << "The sizeof(long) is: " << sizeof(long) << " bytes\n"; 9 | std::cout << "The sizeof(long long) is: " << sizeof(long long) << " bytes\n"; 10 | std::cout << "The sizeof(float) is: " << sizeof(float) << " bytes\n"; 11 | std::cout << "The sizeof(double) is: " << sizeof(double) << " bytes\n"; 12 | std::cout << "The sizeof(long double) is: " << sizeof(long double) << " bytes\n"; 13 | std::cout << "The sizeof(bool) is: " << sizeof(bool) << " bytes\n"; 14 | 15 | } -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | 6 | std::cout << "The maximum of int data type: " << INT_MAX << '\n'; 7 | std::cout << "The minimum of int data type: " << INT_MIN << '\n'; 8 | std::cout << "The maximum of unsigned int data type: " << UINT_MAX << '\n'; 9 | std::cout << "The maximum of long long data type: " << LONG_LONG_MAX << '\n'; 10 | std::cout << "The minimum of long long data type: " << LONG_LONG_MIN << '\n'; 11 | std::cout << "The maximum of unsigned long long data type: " << ULONG_LONG_MAX << '\n'; 12 | std::cout << "The maximum of char data type: " << CHAR_MAX << '\n'; 13 | std::cout << "The minimum of char data type: " << CHAR_MIN << '\n'; 14 | std::cout << "The maximum of signed char data type: " << SCHAR_MAX << '\n'; 15 | std::cout << "The minimum of signed char data type: " << SCHAR_MIN << '\n'; 16 | std::cout << "The maximum of unsigned char data type: " << UCHAR_MAX << '\n'; 17 | std::cout << "The minimum of short data type: " << SHRT_MIN << '\n'; 18 | std::cout << "The maximum of short data type: " << SHRT_MAX << '\n'; 19 | std::cout << "The maximum of unsigned short data type: " << USHRT_MAX << '\n'; 20 | 21 | } -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n; 6 | std::cin >> n; 7 | std::cout << !(n % 2); 8 | 9 | } -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n; 6 | std::cin >> n; 7 | int flipped = 100 * (n % 10) + 10 * ((n / 10) % 10) + n / 100; 8 | std::cout << flipped; 9 | 10 | } -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // Библиотека за математически функции(abs, sqr, sqrt...) 3 | 4 | int main(){ 5 | 6 | int n, m; 7 | std::cin >> n >> m; 8 | 9 | std::cout << n + m << '\n'; 10 | std::cout << abs(n - m) << '\n'; 11 | std::cout << n % m << '\n'; 12 | std::cout << n / m << '\n'; 13 | std::cout << n * 1.0 / m << '\n'; 14 | std::cout << floor(n * 1.0 / m) << '\n'; 15 | std::cout << ceil(n * 1.0 / m) << '\n'; 16 | std::cout << pow(n,2) + sqrt(m) << '\n'; 17 | 18 | } -------------------------------------------------------------------------------- /Seminars/Week 01/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int a = 34, b = 12; 6 | 7 | // Вариант с помощна променлива 8 | int temp; 9 | temp = a; 10 | a = b; 11 | b = temp; 12 | 13 | std::cout << a << ' ' << b << '\n'; 14 | 15 | // Вариант с аритметични операции 16 | a += b; 17 | b = a - b; 18 | a -= b; 19 | 20 | std::cout << a << ' ' << b << '\n'; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n; 6 | std::cin >> n; 7 | 8 | switch(n){ 9 | 10 | case 0: 11 | std::cout << "N is 0" << std::endl; 12 | break; 13 | case 1: 14 | std::cout << "N is 1" << std::endl; 15 | break; 16 | case 2: 17 | std::cout << "N is 2" << std::endl; 18 | break; 19 | case 3: 20 | std::cout << "N is 3" << std::endl; 21 | break; 22 | case 4: 23 | std::cout << "N is 4" << std::endl; 24 | break; 25 | case 5: 26 | std::cout << "N is 5" << std::endl; 27 | break; 28 | case 6: 29 | std::cout << "N is 6" << std::endl; 30 | break; 31 | case 7: 32 | std::cout << "N is 7" << std::endl; 33 | break; 34 | case 8: 35 | std::cout << "N is 8" << std::endl; 36 | break; 37 | case 9: 38 | std::cout << "N is 9" << std::endl; 39 | break; 40 | 41 | default: 42 | std::cout << "N is not a digit" << std::endl; 43 | break; 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n, m; 6 | std::cin >> n >> m; 7 | 8 | // Начин с if - else 9 | if(n > m) std::cout << n << std::endl; 10 | else std::cout << m << std::endl; 11 | 12 | // Начин с тернарен оператор 13 | std::cout << (n > m ? n : m) << std::endl; 14 | 15 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int points; 6 | std::cin >> points; 7 | 8 | if(points > 85 && points <= 100){ 9 | 10 | std::cout << "6" << std::endl; 11 | 12 | } 13 | else if(points > 70 && points <= 85){ 14 | 15 | std::cout << "5" << std::endl; 16 | 17 | } 18 | else if(points > 50 && points <= 70){ 19 | 20 | std::cout << "4" << std::endl; 21 | 22 | } 23 | else if(points > 30 && points <= 50){ 24 | 25 | std::cout << "3" << std::endl; 26 | 27 | } 28 | else if(points > 0 && points <= 30){ 29 | 30 | std::cout << "2" << std::endl; 31 | 32 | } 33 | else{ 34 | 35 | std::cout << "Incorrect number of points!" << std::endl; 36 | 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | if(n % 2 == 1){ 9 | 10 | std::cout << "Weird" << std::endl; 11 | 12 | } 13 | else if(n >= 2 && n <= 5){ 14 | 15 | std::cout << "Not weird" << std::endl; 16 | 17 | } 18 | else if(n >= 6 && n <= 20){ 19 | 20 | std::cout << "Weird" << std::endl; 21 | 22 | } 23 | else if(n % 2 == 0 && n >= 20){ 24 | 25 | std::cout << "Not weird" << std::endl; 26 | 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int r, x, y; 6 | std::cin >> r >> x >> y; 7 | 8 | int equation = x * x + y * y, distance = r * r; 9 | 10 | if(equation < distance){ 11 | 12 | std::cout << "Inside" << std::endl; 13 | 14 | } 15 | else if(equation == distance){ 16 | 17 | std::cout << "On the circle" << std::endl; 18 | 19 | } 20 | else std::cout << "Outside" << std::endl; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int hh, mm; 6 | std::cin >> hh >> mm; 7 | 8 | mm += 15; 9 | if(mm >= 60){ 10 | 11 | hh++; 12 | mm -= 60; 13 | 14 | } 15 | if(hh == 24) hh -= 24; 16 | 17 | std::cout << (hh < 10 ? '0' : '\0') << hh << ':' << (mm < 10 ? '0' : '\0') << mm << std::endl; 18 | 19 | } -------------------------------------------------------------------------------- /Seminars/Week 02/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n, m, l, k; 6 | std::cin >> n >> m >> l >> k; 7 | 8 | int min = n; 9 | 10 | if(min > m) min = m; 11 | if(min > l) min = l; 12 | if(min > k) min = k; 13 | 14 | std::cout << n + m + l + k - min + 1 << std::endl; 15 | 16 | } -------------------------------------------------------------------------------- /Seminars/Week 03/NumericSystemConversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 32 5 | 6 | char getCharByNumber(std::size_t const number){ 7 | 8 | if(number <= 9) return '0' + number; 9 | if(10 <= number && number <= 35) return 'A' + (number - 10); 10 | return 'a' + (number - 36); 11 | 12 | } 13 | 14 | std::size_t getNumberByChar(char const ch){ 15 | 16 | if('0' <= ch && ch <= '9') return ch - '0'; 17 | if('A' <= ch && ch <= 'Z') return 10 + ch - 'A'; 18 | return 36 + ch - 'a'; 19 | 20 | } 21 | 22 | void swap(char &a, char &b){ 23 | 24 | char temp = a; 25 | a = b; 26 | b = temp; 27 | 28 | } 29 | 30 | void convertFromDecimal(std::size_t number, char * const result, std::size_t const k){ 31 | 32 | std::size_t resultInd = 0, symbolIndex; 33 | 34 | while(number != 0){ 35 | 36 | symbolIndex = number % k; 37 | result[resultInd++] = getCharByNumber(symbolIndex); 38 | number /= k; 39 | 40 | } 41 | 42 | result[resultInd] = '\0'; 43 | 44 | for(std::size_t i = 0; i < resultInd / 2; ++i) 45 | swap(result[i], result[resultInd - 1 - i]); 46 | 47 | } 48 | 49 | std::size_t convertToDecimal(char const * const number, std::size_t const size, std::size_t const k){ 50 | 51 | std::size_t mult = 1, result = 0; 52 | 53 | for(std::size_t i = size - 1; !~i; --i){ 54 | 55 | result += getNumberByChar(number[i]) * mult; 56 | mult *= k; 57 | 58 | } 59 | 60 | return result; 61 | 62 | } 63 | 64 | void init(char * const buff, std::size_t const start){ 65 | 66 | for(std::size_t i = start; i < MAX_SIZE; i++) 67 | buff[i] = ' '; 68 | 69 | } 70 | 71 | void print(char const * const buff){ 72 | 73 | for(size_t i = 0; i < MAX_SIZE; i++) 74 | std::cout << buff[i]; 75 | std::cout << '\n'; 76 | 77 | } 78 | 79 | int main(){ 80 | 81 | std::size_t const n = 123456; 82 | 83 | { 84 | 85 | char result[MAX_SIZE]; 86 | init(result, 0); 87 | 88 | convertFromDecimal(n, result, 16); 89 | 90 | std::cout << n << " to hex: " << result << '\n'; 91 | 92 | } 93 | 94 | { 95 | 96 | char buff[MAX_SIZE] = { 'A', '1', '2', 'I' }; 97 | init(buff, 5); 98 | 99 | size_t result = convertToDecimal(buff, 4, 20); 100 | 101 | std::cout << buff << " to decimal: " << result << '\n'; 102 | 103 | } 104 | 105 | } -------------------------------------------------------------------------------- /Seminars/Week 03/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n; 6 | unsigned count = 0, mask = 1; 7 | 8 | std::cin >> n; 9 | 10 | count += !!(n & mask); 11 | mask <<= 1; 12 | count += !!(n & mask); 13 | mask <<= 1; 14 | count += !!(n & mask); 15 | mask <<= 1; 16 | count += !!(n & mask); 17 | mask <<= 1; 18 | count += !!(n & mask); 19 | mask <<= 1; 20 | count += !!(n & mask); 21 | mask <<= 1; 22 | count += !!(n & mask); 23 | mask <<= 1; 24 | count += !!(n & mask); 25 | mask <<= 1; 26 | count += !!(n & mask); 27 | mask <<= 1; 28 | count += !!(n & mask); 29 | mask <<= 1; 30 | count += !!(n & mask); 31 | mask <<= 1; 32 | count += !!(n & mask); 33 | mask <<= 1; 34 | count += !!(n & mask); 35 | mask <<= 1; 36 | count += !!(n & mask); 37 | mask <<= 1; 38 | count += !!(n & mask); 39 | mask <<= 1; 40 | count += !!(n & mask); 41 | mask <<= 1; 42 | count += !!(n & mask); 43 | mask <<= 1; 44 | count += !!(n & mask); 45 | mask <<= 1; 46 | count += !!(n & mask); 47 | mask <<= 1; 48 | count += !!(n & mask); 49 | mask <<= 1; 50 | count += !!(n & mask); 51 | mask <<= 1; 52 | count += !!(n & mask); 53 | mask <<= 1; 54 | count += !!(n & mask); 55 | mask <<= 1; 56 | count += !!(n & mask); 57 | mask <<= 1; 58 | count += !!(n & mask); 59 | mask <<= 1; 60 | count += !!(n & mask); 61 | mask <<= 1; 62 | count += !!(n & mask); 63 | mask <<= 1; 64 | count += !!(n & mask); 65 | mask <<= 1; 66 | count += !!(n & mask); 67 | mask <<= 1; 68 | count += !!(n & mask); 69 | mask <<= 1; 70 | count += !!(n & mask); 71 | mask <<= 1; 72 | count += !!(n & mask); 73 | 74 | std::cout << count << '\n'; 75 | 76 | return 0; 77 | 78 | } -------------------------------------------------------------------------------- /Seminars/Week 03/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int num1, num2, num3, num4, num5; 6 | 7 | std::cin >> num1 >> num2 >> num3 >> num4 >> num5; 8 | std::cout << (num1 ^ num2 ^ num3 ^ num4 ^ num5) << '\n'; 9 | 10 | return 0; 11 | 12 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | for(unsigned int i = 0; i < n; i++){ 9 | 10 | std::cout << i * i << ' '; 11 | 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int sum = 0, temp; 6 | 7 | do{ 8 | 9 | std::cin >> temp; 10 | sum += temp; 11 | 12 | }while(temp != 0); 13 | 14 | std::cout << sum; 15 | 16 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned int n, result = 1; 6 | std::cin >> n; 7 | 8 | while(n > 0){ 9 | 10 | result *= n; 11 | n--; 12 | 13 | } 14 | 15 | // Вариант с for цикъл 16 | // for(unsigned int i = 1; i <= n; i++){ 17 | 18 | // result *= i; 19 | 20 | // } 21 | 22 | std::cout << result << '\n'; 23 | 24 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n, answer = 0; 6 | bool negative = n < 0; 7 | std::cin >> n; 8 | 9 | while(n != 0){ 10 | 11 | answer *= 10; 12 | answer += n % 10; 13 | n /= 10; 14 | 15 | } 16 | 17 | if(negative){ 18 | 19 | answer *= -1; 20 | 21 | } 22 | 23 | std::cout << answer << '\n'; 24 | 25 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n; 6 | bool flag = true; 7 | std::cin >> n; 8 | 9 | while(n != 1){ 10 | 11 | if(n % 2){ 12 | 13 | flag = false; 14 | break; 15 | 16 | } 17 | 18 | n /= 2; 19 | 20 | } 21 | 22 | std::cout << std::boolalpha << flag << '\n'; 23 | 24 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned int a, b; 6 | std::cin >> a >> b; 7 | 8 | for(unsigned int i = a; i <= b; i++){ 9 | 10 | if (i>=0 && i <= 9){ 11 | 12 | switch(i){ 13 | 14 | case 0: 15 | std::cout << "zero\n"; 16 | break; 17 | case 1: 18 | std::cout << "one\n"; 19 | break; 20 | case 2: 21 | std::cout << "two\n"; 22 | break; 23 | case 3: 24 | std::cout << "three\n"; 25 | break; 26 | case 4: 27 | std::cout << "four\n"; 28 | break; 29 | case 5: 30 | std::cout << "five\n"; 31 | break; 32 | case 6: 33 | std::cout << "six\n"; 34 | break; 35 | case 7: 36 | std::cout << "seven\n"; 37 | break; 38 | case 8: 39 | std::cout << "eight\n"; 40 | break; 41 | case 9: 42 | std::cout << "nine\n"; 43 | break; 44 | 45 | default: 46 | break; 47 | 48 | } 49 | 50 | } 51 | else{ 52 | 53 | if(i%2 == 0) std::cout << "even\n"; 54 | else std::cout << "odd\n"; 55 | 56 | } 57 | 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /Seminars/Week 04/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | unsigned int n; 6 | std::cin >> n; 7 | 8 | for (unsigned int i = 1; i <= n; i++){ 9 | 10 | for (unsigned int j = 0; j < n - i; j++){ 11 | std::cout << " "; 12 | } 13 | 14 | for (unsigned int j = 0; j < i; j++){ 15 | std::cout << "* "; 16 | } 17 | 18 | std::cout << '\n'; 19 | 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isEven(const unsigned int &n){ 4 | 5 | return n % 2; 6 | 7 | } 8 | 9 | int main(){ 10 | 11 | unsigned int n; 12 | std::cin >> n; 13 | 14 | std::cout << std::boolalpha << isEven(n) << '\n'; 15 | 16 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int pow(const unsigned int &n, const unsigned int &pow){ 4 | 5 | unsigned int res = 1; 6 | for(unsigned int i = 0; i < pow; i++) res *= n; 7 | 8 | return res; 9 | 10 | } 11 | 12 | unsigned int numberOfDigits(const unsigned int &n){ 13 | 14 | unsigned int copyN = n, digits = 0; 15 | while(copyN != 0){ 16 | 17 | digits++; 18 | copyN /= 10; 19 | 20 | } 21 | 22 | return digits; 23 | 24 | } 25 | 26 | bool isPalindrome(const unsigned int &n){ 27 | 28 | unsigned int digits = numberOfDigits(n), biggerPow = pow(10, digits - 1), smallerPow = 1; 29 | for(unsigned int i = 0; i < digits / 2; i++){ 30 | 31 | if(n / biggerPow % 10 != n / smallerPow % 10) return false; 32 | biggerPow /= 10; 33 | smallerPow *= 10; 34 | 35 | } 36 | 37 | return true; 38 | 39 | } 40 | 41 | int main(){ 42 | 43 | unsigned int n; 44 | std::cin >> n; 45 | 46 | std::cout << std::boolalpha << isPalindrome(n) << '\n'; 47 | 48 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task3-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int pow(const unsigned int &n, const unsigned int &pow){ 4 | 5 | unsigned int res = 1; 6 | for(unsigned int i = 0; i < pow; i++) res *= n; 7 | 8 | return res; 9 | 10 | } 11 | 12 | unsigned int numberOfDigits(const unsigned int &n){ 13 | 14 | unsigned int copyN = n, digits = 0; 15 | while(copyN != 0){ 16 | 17 | digits++; 18 | copyN /= 10; 19 | 20 | } 21 | 22 | return digits; 23 | 24 | } 25 | 26 | bool isPrefix(unsigned int n, unsigned int m){ 27 | 28 | unsigned int digitsN = numberOfDigits(n), digitsM = numberOfDigits(m), 29 | powN = pow(10, digitsN - 1), powM = pow(10, digitsM - 1); 30 | 31 | while(powM != 0){ 32 | 33 | if(n / powN % 10 != m / powM % 10) return false; 34 | powN /= 10; 35 | powM /= 10; 36 | 37 | } 38 | 39 | return true; 40 | 41 | } 42 | 43 | int main(){ 44 | 45 | unsigned int n, m; 46 | std::cin >> n >> m; 47 | 48 | std::cout << std::boolalpha << isPrefix(n, m) << '\n'; 49 | 50 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task3-2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isInfix(unsigned int n, unsigned int m){ 4 | 5 | unsigned int powTen = 1; 6 | n /= 10; 7 | 8 | while(n >= 10){ 9 | 10 | if(n % 10 != m / powTen % 10) powTen = 1; 11 | powTen *= 10; 12 | n /= 10; 13 | if(powTen > m) return true; 14 | 15 | } 16 | 17 | return false; 18 | 19 | } 20 | 21 | int main(){ 22 | 23 | unsigned int n, m; 24 | std::cin >> n >> m; 25 | 26 | std::cout << std::boolalpha << isInfix(n, m) << '\n'; 27 | 28 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isSufix(unsigned int n, unsigned int m){ 4 | 5 | while(m != 0){ 6 | 7 | if(n % 10 != m % 10) return false; 8 | n /= 10; 9 | m /= 10; 10 | 11 | } 12 | 13 | return true; 14 | 15 | } 16 | 17 | int main(){ 18 | 19 | unsigned int n, m; 20 | std::cin >> n >> m; 21 | 22 | std::cout << std::boolalpha << isSufix(n, m) << '\n'; 23 | 24 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task4-1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int log_k(unsigned int n, const unsigned int &k){ 4 | 5 | unsigned int result = 0; 6 | while(n >= k){ 7 | 8 | result++; 9 | n /= k; 10 | 11 | } 12 | 13 | return result; 14 | 15 | } 16 | 17 | int main(){ 18 | 19 | unsigned int n, k; 20 | std::cin >> n >> k; 21 | 22 | std::cout << log_k(n, k) << '\n'; 23 | 24 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int pow(const unsigned int &n, const unsigned int &k){ 4 | 5 | unsigned int result = 1; 6 | for(unsigned int i = 0; i < k; i++) result *= n; 7 | 8 | return result; 9 | 10 | } 11 | 12 | int main(){ 13 | 14 | unsigned int n, k; 15 | std::cin >> n >> k; 16 | 17 | std::cout << pow(n, k) << '\n'; 18 | 19 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double distance(const int &x1, const int &y1, const int &x2, const int &y2){ 5 | 6 | double result = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 7 | 8 | unsigned int temp = result * 1000; 9 | result = temp / 1000.0; 10 | 11 | return result; 12 | 13 | } 14 | 15 | int main(){ 16 | 17 | unsigned int x1, y1, x2, y2; 18 | std::cin >> x1 >> y1 >> x2 >> y2; 19 | 20 | std::cout << distance(x1, y1, x2, y2) << '\n'; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | double distance(const int &x1, const int &y1, const int &x2, const int &y2){ 5 | 6 | double result = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 7 | 8 | unsigned int temp = result * 1000; 9 | result = temp / 1000.0; 10 | 11 | return result; 12 | 13 | } 14 | 15 | int main(){ 16 | 17 | unsigned int x1, y1, x2, y2, x3, y3; 18 | std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; 19 | 20 | std::cout << distance(x1, y1, x2, y2) + distance(x1, y1, x3, y3) + distance(x2, y2, x3, y3) << '\n'; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 05/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::cin; 5 | using std::cout; 6 | using std::sqrt; 7 | 8 | bool isPrime(int n){ 9 | 10 | for(int i = 2; i <= sqrt(n); i++){ 11 | if(n % i == 0){ 12 | return false; 13 | } 14 | } 15 | 16 | return true; 17 | 18 | } 19 | 20 | void printPairs(int n){ 21 | 22 | for(int i = 2; i <= n / 2; i++){ 23 | if(isPrime(i) && isPrime(n - i)){ 24 | std::cout << i << ' ' << n - i << '\n'; 25 | } 26 | } 27 | 28 | } 29 | 30 | int main(){ 31 | 32 | int n; 33 | std::cin >> n; 34 | printPairs(n); 35 | 36 | } -------------------------------------------------------------------------------- /Seminars/Week 06/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int findMin(int arr[], const unsigned int size){ 4 | 5 | int min = arr[0]; 6 | 7 | for(unsigned int i = 0; i < size; i++) 8 | if(arr[i] < min) min = arr[i]; 9 | 10 | return min; 11 | 12 | } 13 | 14 | int main(){ 15 | 16 | const unsigned int SIZE = 4; 17 | int arr[4] = {12, 5, 23, 9}; 18 | 19 | std::cout << findMin(arr, SIZE) << '\n'; 20 | 21 | } -------------------------------------------------------------------------------- /Seminars/Week 06/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int findSum(int arr[], const unsigned int SIZE){ 4 | 5 | int sum = 0; 6 | 7 | for(unsigned int i = 0; i < SIZE; i++) 8 | sum += arr[i]; 9 | 10 | return sum; 11 | 12 | } 13 | 14 | int main(){ 15 | 16 | const unsigned int SIZE = 5; 17 | int arr[SIZE] = {13, 0, 2, 3, 16}; 18 | 19 | std::cout << findSum(arr, SIZE) << '\n'; 20 | 21 | } -------------------------------------------------------------------------------- /Seminars/Week 06/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int min(const int a, const int b){ 4 | return (a < b) ? a : b; 5 | } 6 | 7 | int gcd(const int a, const int b){ 8 | 9 | int result = min(a, b); 10 | while(result > 0){ 11 | 12 | if(a % result == 0 && b % result == 0) break; 13 | result--; 14 | 15 | } 16 | 17 | return result; 18 | 19 | } 20 | 21 | int main(){ 22 | 23 | const unsigned int SIZE = 4; 24 | int arr[SIZE] = {9, 18, 27, 15}; 25 | int greatestCommonDenominator = gcd(arr[0], arr[1]); 26 | 27 | for(unsigned int i = 2; i < SIZE; i++) 28 | greatestCommonDenominator = gcd(greatestCommonDenominator, arr[i]); 29 | 30 | std::cout << greatestCommonDenominator << '\n'; 31 | 32 | } -------------------------------------------------------------------------------- /Seminars/Week 06/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int countNumberOfMatchingIntegers(const int arr1[], const int arr2[], const unsigned int SIZE_1, const unsigned int SIZE_2){ 4 | 5 | unsigned int counter = 0; 6 | 7 | for(unsigned i = 0; i < SIZE_1; i++) 8 | for(unsigned int j = 0; j < SIZE_2; j++) 9 | if(arr1[i] == arr2[j]){ 10 | 11 | counter++; 12 | break; 13 | 14 | } 15 | 16 | return counter; 17 | 18 | } 19 | 20 | int main(){ 21 | 22 | const unsigned int SIZE_1 = 9, SIZE_2 = 12; 23 | int arr1[SIZE_1] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[SIZE_2] = {14, 67, 23, 4, 92, 6, 10, 2, 48, 5, 19, 4}; 24 | 25 | std::cout << countNumberOfMatchingIntegers(arr1, arr2, SIZE_1, SIZE_2) << '\n'; 26 | 27 | } -------------------------------------------------------------------------------- /Seminars/Week 06/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void removeDuplicates(int arr[], unsigned int &size){ 4 | 5 | for(unsigned int i = 0; i < size - 1; i++){ 6 | 7 | if(arr[i] == arr[i + 1]){ 8 | 9 | // Брояч за броя повтарящи се числа 10 | unsigned int count = 0; 11 | 12 | // Броим колко еднакви елемнта има без първия тъй като почваме от него 13 | for(; count < size - i - 1; count++) 14 | if(arr[i + count] != arr[i + count + 1]) break; 15 | 16 | // Добавяме още едно заради първия елемент 17 | count++; 18 | 19 | // Преместваме елементите напред на мястото на повтарящите се 20 | for(unsigned k = 0; k < size - i - 1; k++) 21 | arr[i + k] = arr[i + k + count]; 22 | 23 | // Премахваме от size-а колкото елемента сме изтрили 24 | size -= count; 25 | 26 | } 27 | 28 | } 29 | 30 | } 31 | 32 | void printArrayWithoutDuplicates(int arr[], const unsigned int &size){ 33 | 34 | bool hasDuplicate = false; 35 | 36 | for(unsigned int i = 0; i < size - 1; i++){ 37 | 38 | if(arr[i] == arr[i + 1]){ 39 | 40 | hasDuplicate = true; 41 | continue; 42 | 43 | } 44 | 45 | if(!hasDuplicate) std::cout << arr[i] << ' '; 46 | hasDuplicate = false; 47 | 48 | } 49 | 50 | if(!hasDuplicate) std::cout << arr[size - 1]; 51 | std::cout << '\n'; 52 | 53 | } 54 | 55 | int main(){ 56 | 57 | unsigned int size = 10; 58 | int arr[] = {1, 2, 2, 2, 3, 4, 5, 5, 6, 7}; 59 | 60 | printArrayWithoutDuplicates(arr, size); 61 | 62 | removeDuplicates(arr, size); 63 | 64 | std::cout << size << '\n'; 65 | 66 | for(unsigned int i = 0; i < size; i++) std::cout << arr[i] << ' '; 67 | std::cout << '\n'; 68 | 69 | } -------------------------------------------------------------------------------- /Seminars/Week 06/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int minDiff(const int arr[], const int len){ 6 | 7 | int min = INT_MAX; 8 | 9 | for(int i = 0; i < len - 1; i++) 10 | for(int j = i + 1; j < len; j++) 11 | if(abs(arr[i] - arr[j]) < min) 12 | min = abs(arr[i] - arr[j]); 13 | 14 | return min; 15 | 16 | } 17 | 18 | int main(){ 19 | 20 | const int SIZE = 8; 21 | 22 | const int arr[SIZE] = {10, 2, 5, 83, 20, 17, 24, 7}; 23 | std::cout << minDiff(arr, SIZE) << '\n'; 24 | 25 | } -------------------------------------------------------------------------------- /Seminars/Week 07/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool BinarySeach(int const arr[], size_t const SIZE, int const toBeSearched){ 4 | 5 | int start = 0, end = SIZE - 1, mid; 6 | 7 | while(start <= end){ 8 | 9 | mid = (start + end) / 2; 10 | 11 | // Възможно е тук да връщаме и индекса mid, 12 | // ако задачата го изисква. 13 | if(arr[mid] == toBeSearched) return true; 14 | 15 | if(toBeSearched < arr[mid]) end = mid - 1; 16 | else start = mid + 1; 17 | 18 | } 19 | 20 | return false; 21 | 22 | } 23 | 24 | int main(){ 25 | 26 | size_t const SIZE = 10; 27 | int arr[SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 28 | 29 | std::cout << std::boolalpha << BinarySeach(arr, SIZE, 2) << '\n'; 30 | std::cout << std::boolalpha << BinarySeach(arr, SIZE, 7) << '\n'; 31 | std::cout << std::boolalpha << BinarySeach(arr, SIZE, 10) << '\n'; 32 | 33 | } -------------------------------------------------------------------------------- /Seminars/Week 07/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int &a, int &b){ 4 | 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | 9 | } 10 | 11 | void BubbleSort(int arr[], size_t const SIZE){ 12 | 13 | bool swapped; 14 | 15 | for(size_t i = 0; i < SIZE - 1; i++){ 16 | 17 | swapped = false; 18 | for(size_t j = 0; j < SIZE - i - 1; j++) 19 | if(arr[j] > arr[j + 1]){ 20 | 21 | Swap(arr[j], arr[j + 1]); 22 | swapped = true; 23 | 24 | } 25 | 26 | // Ако не се сменят нито една двойка съседни клетки, то масивът е сортиран. 27 | if(!swapped) break; 28 | 29 | } 30 | 31 | } 32 | 33 | int main(){ 34 | 35 | size_t const SIZE = 10; 36 | int arr[SIZE] = { 5, 1, 6, 8, 0, 4, 3, 2, 9, 7 }; 37 | 38 | for(size_t i = 0; i < SIZE; i++) std::cout << arr[i] << ' '; 39 | std::cout << '\n'; 40 | 41 | BubbleSort(arr, SIZE); 42 | 43 | for(size_t i = 0; i < SIZE; i++) std::cout << arr[i] << ' '; 44 | std::cout << '\n'; 45 | 46 | } -------------------------------------------------------------------------------- /Seminars/Week 07/InsertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void InsertionSort(int array[], size_t const size){ 4 | 5 | for(size_t i = 1; i < size; ++i){ 6 | 7 | size_t j = i; 8 | int x = array[i]; 9 | 10 | while(j > 0 && array[j - 1] > x){ 11 | 12 | array[j] = array[j - 1]; 13 | --j; 14 | 15 | } 16 | 17 | array[j] = x; 18 | 19 | } 20 | 21 | } 22 | 23 | int main(){ 24 | 25 | size_t const SIZE = 10; 26 | int arr[SIZE] = { 5, 1, 6, 8, 0, 4, 3, 2, 9, 7 }; 27 | 28 | for(size_t i = 0; i < SIZE; i++) std::cout << arr[i] << ' '; 29 | std::cout << '\n'; 30 | 31 | InsertionSort(arr, SIZE); 32 | 33 | for(size_t i = 0; i < SIZE; i++) std::cout << arr[i] << ' '; 34 | std::cout << '\n'; 35 | 36 | } -------------------------------------------------------------------------------- /Seminars/Week 07/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int &a, int &b){ 4 | 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | 9 | } 10 | 11 | void SelectionSort(int arr[], size_t const SIZE){ 12 | 13 | size_t min_index; 14 | 15 | for(size_t i = 0; i < SIZE - 1; i++){ 16 | 17 | min_index = i; 18 | 19 | for(size_t j = i + 1; j < SIZE; j++) 20 | if(arr[min_index] > arr[j]) 21 | min_index = j; 22 | 23 | if(min_index != i) Swap(arr[i], arr[min_index]); 24 | 25 | } 26 | 27 | } 28 | 29 | int main(){ 30 | 31 | size_t const SIZE = 10; 32 | int arr[SIZE] = { 5, 1, 6, 8, 0, 4, 3, 2, 9, 7 }; 33 | 34 | for(size_t i = 0; i < SIZE; i++) std::cout << arr[i] << ' '; 35 | std::cout << '\n'; 36 | 37 | SelectionSort(arr, SIZE); 38 | 39 | for(size_t i = 0; i < SIZE; i++) std::cout << arr[i] << ' '; 40 | std::cout << '\n'; 41 | 42 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | int BinarySearch(int const arr[], size_t const size, int const k){ 6 | 7 | int start = 0, end = size - 1, mid; 8 | 9 | while(start <= end){ 10 | 11 | mid = (start + end) / 2; 12 | if(mid == size - 1 || (arr[mid] <= k && arr[mid + 1] > k)) return arr[mid]; 13 | 14 | if(k < arr[mid]) end = mid - 1; 15 | else start = mid + 1; 16 | 17 | } 18 | 19 | return INT32_MIN; 20 | 21 | } 22 | 23 | int main(){ 24 | 25 | int arr[CAPACITY], k; 26 | size_t n; 27 | std::cin >> n; 28 | 29 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 30 | std::cin >> k; 31 | 32 | std::cout << BinarySearch(arr, n, k) << '\n'; 33 | 34 | return 0; 35 | 36 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | int FindMissingElement(int const arr[], size_t const size){ 6 | 7 | int total_sum = (size * (size + 1)) / 2; 8 | for(size_t i = 0; i < size; ++i) total_sum -= arr[i]; 9 | return total_sum; 10 | 11 | } 12 | 13 | int main(){ 14 | 15 | int arr[CAPACITY]; 16 | size_t n; 17 | std::cin >> n; 18 | 19 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 20 | std::cout << FindMissingElement(arr, n) << '\n'; 21 | 22 | return 0; 23 | 24 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | void Swap(int &a, int &b){ 6 | 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | 11 | } 12 | 13 | void SelectionSort(int arr[], size_t const size){ 14 | 15 | for(size_t i = 0; i < size - 1; i++) 16 | for(size_t j = i + 1; j < size; j++) 17 | if(arr[i] > arr[j]) 18 | Swap(arr[i], arr[j]); 19 | 20 | } 21 | 22 | void FindElementIndexes(int arr[], size_t const size, int const k){ 23 | 24 | SelectionSort(arr, size); 25 | for(size_t i = 0; i < size; ++i) 26 | if(arr[i] == k) 27 | std::cout << i << ' '; 28 | 29 | } 30 | 31 | int main(){ 32 | 33 | int arr[CAPACITY], k; 34 | size_t n; 35 | std::cin >> n; 36 | 37 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 38 | std::cin >> k; 39 | 40 | FindElementIndexes(arr, n, k); 41 | 42 | return 0; 43 | 44 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | int FindMoundIndex(int const arr[], size_t const size){ 6 | 7 | int start = 0, end = size - 1; 8 | 9 | while(start < end){ 10 | 11 | int mid = start + (end - start) / 2; 12 | 13 | if(arr[mid] > arr[mid + 1]) end = mid; 14 | else start = mid + 1; 15 | 16 | } 17 | 18 | return start; 19 | 20 | } 21 | 22 | int main(){ 23 | 24 | int arr[CAPACITY]; 25 | size_t n; 26 | std::cin >> n; 27 | 28 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 29 | std::cout << FindMoundIndex(arr, n) << '\n'; 30 | 31 | return 0; 32 | 33 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | int FindDuplicateElement(int arr[], size_t const size){ 6 | 7 | for(size_t i = 0; i < size; ++i){ 8 | 9 | int temp = std::abs(arr[i]); 10 | arr[temp - 1] = -arr[temp - 1]; 11 | if(arr[temp - 1] > 0) return temp; 12 | 13 | } 14 | 15 | return -1; 16 | 17 | } 18 | 19 | int main(){ 20 | 21 | int arr[CAPACITY]; 22 | size_t n; 23 | std::cin >> n; 24 | 25 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 26 | std::cout << FindDuplicateElement(arr, n) << '\n'; 27 | 28 | return 0; 29 | 30 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | void InsertionSort(int array[], size_t const size){ 6 | 7 | for(size_t i = 1; i < size; ++i){ 8 | 9 | size_t j = i; 10 | int x = array[i]; 11 | 12 | while(j > 0 && array[j - 1] > x){ 13 | 14 | array[j] = array[j - 1]; 15 | --j; 16 | 17 | } 18 | 19 | array[j] = x; 20 | 21 | } 22 | 23 | } 24 | 25 | int FindMaximizedSum(int arr[], size_t const size){ 26 | 27 | int result = 0; 28 | 29 | InsertionSort(arr, size); 30 | for(size_t i = 0; i < size; i += 2) 31 | result += arr[i]; 32 | 33 | return result; 34 | 35 | } 36 | 37 | int main(){ 38 | 39 | int arr[CAPACITY]; 40 | size_t n; 41 | std::cin >> n; 42 | 43 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 44 | std::cout << FindMaximizedSum(arr, n) << '\n'; 45 | 46 | return 0; 47 | 48 | } -------------------------------------------------------------------------------- /Seminars/Week 07/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 1024; 4 | 5 | int CountOfPairSumsSmallerThanK(int const arr[], size_t const size, int const k){ 6 | 7 | int result = 0; 8 | 9 | for(int i = 0; i < size - 1; ++i) 10 | for(int j = i + 1; j < size; ++j) 11 | result += arr[i] + arr[j] < k; 12 | 13 | return result; 14 | 15 | } 16 | 17 | int main(){ 18 | 19 | int arr[CAPACITY], k; 20 | size_t n; 21 | std::cin >> n >> k; 22 | 23 | for(size_t i = 0; i < n; ++i) std::cin >> arr[i]; 24 | std::cout << CountOfPairSumsSmallerThanK(arr, n, k) << '\n'; 25 | 26 | return 0; 27 | 28 | } -------------------------------------------------------------------------------- /Seminars/Week 08/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const size_t CAPACITY = 100; 4 | 5 | bool IsValidIndex(size_t const i, size_t const j, size_t const sizeX, size_t const sizeY){ 6 | return i < sizeY && j < sizeX; 7 | } 8 | 9 | void PrintInSpiral(int matrix[][CAPACITY], size_t const sizeX, size_t const sizeY){ 10 | 11 | bool visited[CAPACITY][CAPACITY]; 12 | 13 | size_t indexRow = 0, indexColomn = 0; 14 | int directionX = 1, directionY = 0; 15 | 16 | for(size_t i = 0; i < sizeX * sizeY; ++i){ 17 | 18 | std::cout << matrix[indexRow][indexColomn] << ' '; 19 | visited[indexRow][indexColomn] = true; 20 | 21 | if(!IsValidIndex(indexRow + directionY, indexColomn + directionX, sizeX, sizeY) 22 | || visited[indexRow + directionY][indexColomn + directionX]){ 23 | 24 | if(directionX != 0){ 25 | 26 | directionY = directionX; 27 | directionX = 0; 28 | 29 | } 30 | else if(directionY != 0){ 31 | 32 | directionX = -directionY; 33 | directionY = 0; 34 | 35 | } 36 | 37 | } 38 | 39 | indexRow += directionY; 40 | indexColomn += directionX; 41 | 42 | } 43 | 44 | } 45 | 46 | int main(){ 47 | 48 | int matrix[CAPACITY][CAPACITY]; 49 | size_t sizeX, sizeY; 50 | std::cin >> sizeX >> sizeY; 51 | 52 | if(sizeX >= CAPACITY || sizeY >= CAPACITY){ 53 | 54 | std::cout << "Invalid input\n"; 55 | return -1; 56 | 57 | } 58 | 59 | for(size_t i = 0; i < sizeY; ++i) 60 | for(size_t j = 0; j < sizeX; ++j) 61 | std::cin >> matrix[i][j]; 62 | 63 | PrintInSpiral(matrix, sizeX, sizeY); 64 | 65 | return 0; 66 | 67 | } -------------------------------------------------------------------------------- /Seminars/Week 08/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 100; 4 | 5 | bool IsMagicSquare(int matrix[][CAPACITY], size_t const n){ 6 | 7 | int diagonal1 = 0, diagonal2 = 0; 8 | for(size_t i = 0; i < n; ++i){ 9 | 10 | diagonal1 += matrix[i][i]; 11 | diagonal2 += matrix[i][n - i - 1]; 12 | 13 | } 14 | 15 | // Двата диагонала имат различна сума 16 | if(diagonal1 != diagonal2) return false; 17 | 18 | int rowSum, colSum; 19 | for(size_t i = 0; i < n; ++i){ 20 | 21 | rowSum = colSum = 0; 22 | for(size_t j = 0; j < n; ++j){ 23 | 24 | rowSum += matrix[i][j]; 25 | colSum += matrix[j][i]; 26 | 27 | } 28 | 29 | // Или сумата на ред и колона е различна или на колоната с диагонала 30 | if(rowSum != colSum || colSum != diagonal1) return false; 31 | 32 | } 33 | 34 | return true; 35 | 36 | } 37 | 38 | int main(){ 39 | 40 | int matrix[CAPACITY][CAPACITY]; 41 | size_t n; 42 | std::cin >> n; 43 | 44 | if(n >= CAPACITY){ 45 | 46 | std::cout << "Invalid input\n"; 47 | return -1; 48 | 49 | } 50 | 51 | for(size_t i = 0; i < n; ++i) 52 | for(size_t j = 0; j < n; ++j) 53 | std::cin >> matrix[i][j]; 54 | 55 | std::cout << std::boolalpha << IsMagicSquare(matrix, n) << '\n'; 56 | 57 | return 0; 58 | 59 | } -------------------------------------------------------------------------------- /Seminars/Week 08/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 100; 4 | 5 | bool IsLowerTriangle(int matrix[][CAPACITY], size_t const n){ 6 | 7 | for(size_t i = 0; i < n - 1; ++i) 8 | for(size_t j = i + 1; j < n; ++j) 9 | if(matrix[i][j] != 0) return false; 10 | 11 | return true; 12 | 13 | } 14 | 15 | bool IsUpperTriangle(int matrix[][CAPACITY], size_t const n){ 16 | 17 | for(size_t i = 1; i < n; ++i) 18 | for(size_t j = 0; j < i; ++j) 19 | if(matrix[i][j] != 0) return false; 20 | 21 | return true; 22 | 23 | } 24 | 25 | void CheckIfMatrixIsTriangle(int matrix[][CAPACITY], size_t const n){ 26 | 27 | if(IsLowerTriangle(matrix, n)){ 28 | 29 | std::cout << "This is a lower triangular matrix\n"; 30 | return; 31 | 32 | } 33 | 34 | if(IsUpperTriangle(matrix, n)){ 35 | 36 | std::cout << "This is an upper triangular matrix\n"; 37 | return; 38 | 39 | } 40 | 41 | std::cout << "This is neither an upper, nor a lower triangular matrix\n"; 42 | 43 | } 44 | 45 | int main(){ 46 | 47 | int matrix[CAPACITY][CAPACITY]; 48 | size_t n; 49 | std::cin >> n; 50 | 51 | if(n >= CAPACITY){ 52 | 53 | std::cout << "Invalid input\n"; 54 | return -1; 55 | 56 | } 57 | 58 | for(size_t i = 0; i < n; ++i) 59 | for(size_t j = 0; j < n; ++j) 60 | std::cin >> matrix[i][j]; 61 | 62 | CheckIfMatrixIsTriangle(matrix, n); 63 | 64 | return 0; 65 | 66 | } -------------------------------------------------------------------------------- /Seminars/Week 08/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 100; 4 | 5 | void FindSum(int A[][CAPACITY], int B[][CAPACITY], size_t const n, size_t const m){ 6 | 7 | for(size_t i = 0; i < n; ++i){ 8 | 9 | for(size_t j = 0; j < m; ++j) 10 | std::cout << A[i][j] + B[i][j] << ' '; 11 | std::cout << '\n'; 12 | 13 | } 14 | 15 | } 16 | 17 | void FindDifference(int A[][CAPACITY], int B[][CAPACITY], size_t const n, size_t const m){ 18 | 19 | for(size_t i = 0; i < n; ++i){ 20 | 21 | for(size_t j = 0; j < m; ++j) 22 | std::cout << A[i][j] - B[i][j] << ' '; 23 | std::cout << '\n'; 24 | 25 | } 26 | 27 | } 28 | 29 | int main(){ 30 | 31 | int A[CAPACITY][CAPACITY], B[CAPACITY][CAPACITY]; 32 | size_t n, m; 33 | std::cin >> n >> m; 34 | 35 | if(n >= CAPACITY || m >= CAPACITY){ 36 | 37 | std::cout << "Invalid input\n"; 38 | return -1; 39 | 40 | } 41 | 42 | for(size_t i = 0; i < n; ++i) 43 | for(size_t j = 0; j < m; ++j) 44 | std::cin >> A[i][j]; 45 | 46 | for(size_t i = 0; i < n; ++i) 47 | for(size_t j = 0; j < m; ++j) 48 | std::cin >> B[i][j]; 49 | 50 | std::cout << '\n'; 51 | FindSum(A, B, n, m); 52 | std::cout << '\n'; 53 | FindDifference(A, B, n, m); 54 | 55 | return 0; 56 | 57 | } -------------------------------------------------------------------------------- /Seminars/Week 08/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 100; 4 | 5 | void Swap(int &a, int &b){ 6 | 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | 11 | } 12 | 13 | void ReverseRows(int matrix[][CAPACITY], size_t const n){ 14 | 15 | for(size_t i = 0; i < n; i++) 16 | for(size_t j = 0, k = n - 1; j < k; j++, k--) 17 | Swap(matrix[i][j], matrix[i][k]); 18 | 19 | } 20 | 21 | void Transpose(int matrix[][CAPACITY], size_t const n){ 22 | 23 | for (int i = 0; i < n; i++) 24 | for (int j = i; j < n; j++) 25 | Swap(matrix[i][j], matrix[j][i]); 26 | 27 | } 28 | 29 | void Rotate90(int matrix[][CAPACITY], size_t const n){ 30 | 31 | Transpose(matrix, n); 32 | ReverseRows(matrix, n); 33 | 34 | } 35 | 36 | void Print(int matrix[][CAPACITY], size_t const n){ 37 | 38 | for(size_t i = 0; i < n; ++i){ 39 | 40 | for(size_t j = 0; j < n; ++j) 41 | std::cout << matrix[i][j] << ' '; 42 | std::cout << '\n'; 43 | 44 | } 45 | 46 | } 47 | 48 | int main(){ 49 | 50 | int matrix[CAPACITY][CAPACITY]; 51 | size_t n; 52 | std::cin >> n; 53 | 54 | if(n >= CAPACITY){ 55 | 56 | std::cout << "Invalid input\n"; 57 | return -1; 58 | 59 | } 60 | 61 | for(size_t i = 0; i < n; ++i) 62 | for(size_t j = 0; j < n; ++j) 63 | std::cin >> matrix[i][j]; 64 | 65 | Rotate90(matrix, n); 66 | Print(matrix, n); 67 | 68 | return 0; 69 | 70 | } -------------------------------------------------------------------------------- /Seminars/Week 08/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | size_t const CAPACITY = 100; 4 | 5 | void Swap(int &a, int &b){ 6 | 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | 11 | } 12 | 13 | int CalculateDeterminant(int matrix[][CAPACITY], size_t const n){ 14 | 15 | int temp, det = 1, index, total = 1; 16 | 17 | for(size_t i = 0; i < n; ++i){ 18 | 19 | index = i; 20 | 21 | while(index < n && !matrix[index][i]) ++index; 22 | 23 | if(index == n) continue; 24 | 25 | if(index != i){ 26 | 27 | for(size_t j = 0; j < n; ++j) 28 | Swap(matrix[index][j], matrix[i][j]); 29 | det *= (index - i) % 2 ? -1 : 1; 30 | 31 | } 32 | 33 | for(size_t j = i + 1; j < n; ++j){ 34 | 35 | temp = matrix[j][i]; 36 | 37 | for(size_t k = 0; k < n; ++k) 38 | matrix[j][k] = (matrix[i][i] * matrix[j][k]) - (temp * matrix[i][k]); 39 | 40 | total *= matrix[i][i]; 41 | 42 | } 43 | 44 | } 45 | 46 | for(size_t i = 0; i < n; ++i) det *= matrix[i][i]; 47 | 48 | return det / total; 49 | 50 | } 51 | 52 | int main(){ 53 | 54 | int matrix[CAPACITY][CAPACITY]; 55 | size_t n; 56 | std::cin >> n; 57 | 58 | if(n >= CAPACITY){ 59 | 60 | std::cout << "Invalid input\n"; 61 | return -1; 62 | 63 | } 64 | 65 | for(size_t i = 0; i < n; ++i) 66 | for(size_t j = 0; j < n; ++j) 67 | std::cin >> matrix[i][j]; 68 | 69 | std::cout << CalculateDeterminant(matrix, n) << '\n'; 70 | 71 | return 0; 72 | 73 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int FindMajorityNumber(int const * const array, size_t const size){ 4 | 5 | unsigned timesFound, majorityFound = 0; 6 | int result; 7 | 8 | for(size_t i = 0; i < size - majorityFound; ++i){ 9 | 10 | timesFound = 1; 11 | 12 | for(size_t j = i + 1; j < size; ++j) 13 | if(array[i] == array[j]) ++timesFound; 14 | 15 | if(timesFound > majorityFound){ 16 | 17 | majorityFound = timesFound; 18 | result = array[i]; 19 | 20 | } 21 | 22 | } 23 | 24 | return result; 25 | 26 | } 27 | 28 | int main(){ 29 | 30 | size_t n; 31 | std::cin >> n; 32 | 33 | int *array = new(std::nothrow) int[n]; 34 | 35 | if(!array){ 36 | 37 | std::cout << "Memory error\n"; 38 | return -1; 39 | 40 | } 41 | 42 | for(size_t i = 0; i < n; ++i) std::cin >> array[i]; 43 | 44 | std::cout << FindMajorityNumber(array, n) << '\n'; 45 | 46 | delete[] array; 47 | 48 | return 0; 49 | 50 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int &a, int &b){ 4 | 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | 9 | } 10 | 11 | unsigned CountTurnedBits(int number){ 12 | 13 | unsigned count = 0; 14 | 15 | for(unsigned i = 0; i < sizeof(number) * 8; ++i) 16 | count += !!(number & 1 << i); 17 | 18 | return count; 19 | 20 | } 21 | 22 | void Sort(int * const array, size_t const size){ 23 | 24 | for(size_t i = 0; i < size - 1; ++i) 25 | for(size_t j = i + 1; j < size; ++j) 26 | if(CountTurnedBits(array[i]) > CountTurnedBits(array[j])) Swap(array[i], array[j]); 27 | else if(array[i] > array[j]) Swap(array[i], array[j]); 28 | 29 | } 30 | 31 | int main(){ 32 | 33 | size_t n; 34 | std::cin >> n; 35 | 36 | int *array = new(std::nothrow) int[n]; 37 | 38 | if(!array){ 39 | 40 | std::cout << "Memory error\n"; 41 | return -1; 42 | 43 | } 44 | 45 | for(size_t i = 0; i < n; ++i) std::cin >> array[i]; 46 | 47 | Sort(array, n); 48 | 49 | for(size_t i = 0; i < n; ++i) std::cout << array[i] << ' '; 50 | std::cout << '\n'; 51 | 52 | delete[] array; 53 | 54 | return 0; 55 | 56 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int FindMiddleIndex(int const * const nums, size_t const size){ 4 | 5 | int totalSum = 0, leftSum = 0; 6 | 7 | for(size_t i = 0; i < size; ++i) totalSum += nums[i]; 8 | 9 | for(size_t i = 0; i < size; ++i){ 10 | 11 | if(leftSum == totalSum - nums[i]) return i; 12 | leftSum += nums[i]; 13 | totalSum -= nums[i]; 14 | 15 | } 16 | 17 | return -1; 18 | 19 | } 20 | 21 | int main(){ 22 | 23 | size_t n; 24 | std::cin >> n; 25 | 26 | int *nums = new(std::nothrow) int[n]; 27 | 28 | if(!nums){ 29 | 30 | std::cout << "Memory error\n"; 31 | return -1; 32 | 33 | } 34 | 35 | for(size_t i = 0; i < n; ++i) std::cin >> nums[i]; 36 | 37 | std::cout << FindMiddleIndex(nums, n) << '\n'; 38 | 39 | delete[] nums; 40 | 41 | return 0; 42 | 43 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int FindRichestAmount(int const * const * const accounts, size_t const n, size_t const m){ 4 | 5 | int richestAmount = INT_MIN, currentAmount; 6 | 7 | for(size_t i = 0; i < n; ++i){ 8 | 9 | currentAmount = 0; 10 | 11 | for(size_t j = 0; j < m; ++j) 12 | currentAmount += accounts[i][j]; 13 | 14 | richestAmount = std::max(currentAmount, richestAmount); 15 | 16 | } 17 | 18 | return richestAmount; 19 | 20 | } 21 | 22 | int main(){ 23 | 24 | size_t n, m; 25 | std::cin >> n >> m; 26 | 27 | int **accounts = new(std::nothrow) int *[n]; 28 | 29 | if(!accounts){ 30 | 31 | std::cout << "Memory error\n"; 32 | return -1; 33 | 34 | } 35 | 36 | for(size_t i = 0; i < n; ++i){ 37 | 38 | accounts[i] = new(std::nothrow) int[m]; 39 | if(!accounts[i]){ 40 | 41 | for(size_t j = 0; j < i; ++j) 42 | delete[] accounts[j]; 43 | 44 | std::cout << "Memory error\n"; 45 | return -1; 46 | 47 | } 48 | 49 | for(size_t j = 0; j < m; ++j) std::cin >> accounts[i][j]; 50 | 51 | } 52 | 53 | 54 | std::cout << FindRichestAmount(accounts, n, m) << '\n'; 55 | 56 | for(size_t i = 0; i < n; ++i) delete[] accounts[i]; 57 | delete[] accounts; 58 | 59 | return 0; 60 | 61 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int &a, int &b){ 4 | 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | 9 | } 10 | 11 | void Sort(int * const array, size_t const size){ 12 | 13 | for(size_t i = 0; i < size - 1; ++i) 14 | for(size_t j = i + 1; j < size; ++j) 15 | if(array[i] > array[j]) 16 | Swap(array[i], array[j]); 17 | 18 | } 19 | 20 | bool CanSplitIntoNPairs(int * const nums, size_t size){ 21 | 22 | Sort(nums, size); 23 | 24 | for(size_t i = 0; i < size; i += 2) 25 | if(nums[i] != nums[i + 1]) 26 | return false; 27 | 28 | return true; 29 | 30 | } 31 | 32 | int main(){ 33 | 34 | size_t n; 35 | std::cin >> n; 36 | 37 | if(n % 2 != 0){ 38 | 39 | std::cout << "Incorrect size\n"; 40 | return -1; 41 | 42 | } 43 | 44 | int *nums = new(std::nothrow) int[n]; 45 | 46 | if(!nums){ 47 | 48 | std::cout << "Memory error\n"; 49 | return -1; 50 | 51 | } 52 | 53 | for(size_t i = 0; i < n; ++i) std::cin >> nums[i]; 54 | 55 | std::cout << std::boolalpha << CanSplitIntoNPairs(nums, n) << '\n'; 56 | 57 | delete[] nums; 58 | 59 | return 0; 60 | 61 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int *PrintSumDiffs(int * const array, size_t const size){ 4 | 5 | int *answer = new(std::nothrow) int[size], total; 6 | 7 | if(!answer) return nullptr; 8 | 9 | for(size_t i = 1; i < size; ++i) 10 | array[i] += array[i - 1]; 11 | 12 | total = array[size - 1]; 13 | 14 | answer[0] = total - array[0]; 15 | 16 | for(size_t i = 1; i < size; ++i) 17 | answer[i] = std::abs(total - array[i] - array[i - 1]); 18 | 19 | return answer; 20 | 21 | } 22 | 23 | int main(){ 24 | 25 | size_t n; 26 | std::cin >> n; 27 | 28 | int *array = new(std::nothrow) int[n]; 29 | 30 | if(!array){ 31 | 32 | std::cout << "Memory error\n"; 33 | return -1; 34 | 35 | } 36 | 37 | for(size_t i = 0; i < n; ++i) std::cin >> array[i]; 38 | 39 | int *result = PrintSumDiffs(array, n); 40 | 41 | if(!result){ 42 | 43 | std::cout << "Memory error\n"; 44 | delete[] array; 45 | return -1; 46 | 47 | } 48 | 49 | for(size_t i = 0; i < n; ++i) std::cout << result[i] << ' '; 50 | std::cout << '\n'; 51 | 52 | delete[] array; 53 | 54 | return 0; 55 | 56 | } -------------------------------------------------------------------------------- /Seminars/Week 09/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void Swap(int &a, int &b){ 4 | 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | 9 | } 10 | 11 | void Reorder(int * const array, size_t const n, int const k){ 12 | 13 | size_t index = 0; 14 | 15 | for(size_t i = 0; i < n; ++i) 16 | if(array[i] < k){ 17 | 18 | Swap(array[i], array[index++]); 19 | if(array[i] == k) Swap(array[i], array[index]); 20 | 21 | } 22 | 23 | } 24 | 25 | void Print(int const * const array, size_t const n){ 26 | 27 | for(size_t i = 0; i < n; ++i) 28 | std::cout << array[i] << ' '; 29 | std::cout << '\n'; 30 | 31 | } 32 | 33 | int main(){ 34 | 35 | size_t n; 36 | std::cin >> n; 37 | 38 | int *array = new(std::nothrow) int[n], k; 39 | 40 | if(!array){ 41 | 42 | std::cout << "Memory error\n"; 43 | return -1; 44 | 45 | } 46 | 47 | for(size_t i = 0; i < n; ++i) std::cin >> array[i]; 48 | std::cin >> k; 49 | 50 | Reorder(array, n, k); 51 | Print(array, n); 52 | 53 | delete[] array; 54 | 55 | return 0; 56 | 57 | } -------------------------------------------------------------------------------- /Seminars/Week 09/showcasing_debug_new.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "debug_new.h" 3 | 4 | // Not using std::nothrow, because it breaks the program. 5 | // My code will not throw an error anyways. 6 | 7 | int main(){ 8 | 9 | int a = 10; 10 | int *ptr = &a; 11 | 12 | printList(); 13 | 14 | std::cout << ptr << ' ' << *ptr << '\n'; 15 | 16 | ptr = new(std::nothrow) int(5); 17 | 18 | std::cout << ptr << ' ' << *ptr << '\n'; 19 | 20 | 21 | std::cout << "Print 1:\n"; 22 | printList(); 23 | 24 | delete ptr; 25 | 26 | std::cout << "Print 2:\n"; 27 | printList(); 28 | 29 | int **matrix = new(std::nothrow) int*[10]; 30 | 31 | for(size_t i = 0; i < 10; ++i) 32 | matrix[i] = new(std::nothrow) int[7]; 33 | 34 | std::cout << "Print 3:\n"; 35 | printList(); 36 | 37 | for(size_t i = 0; i < 10; ++i) 38 | delete[] matrix[i]; 39 | 40 | std::cout << "Print 4:\n"; 41 | printList(); 42 | 43 | delete[] matrix; 44 | 45 | std::cout << "Print 5:\n"; 46 | checkMemoryLeaks(); 47 | 48 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int countRepeats(const char str[], const unsigned int size, const char c){ 4 | 5 | unsigned int count = 0; 6 | for(unsigned int i = 0; i < size; i++) 7 | count += str[i] == c; 8 | return count; 9 | 10 | } 11 | 12 | int main(){ 13 | 14 | char str[] = "ababbbccba", c; 15 | std::cin >> c; 16 | std::cout << countRepeats(str, sizeof(str) - 1, c) << '\n'; 17 | 18 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printInfo(const char str[], const unsigned int size){ 4 | 5 | unsigned int upper = 0, lower = 0, digits = 0; 6 | for(unsigned int i = 0; i < size; i++) 7 | if(str[i] >= 'A' && str[i] <= 'Z') upper++; 8 | else if(str[i] >= 'a' && str[i] <= 'z') lower++; 9 | else if(str[i] >= '0' && str[i] <= '9') digits++; 10 | 11 | std::cout << "Uppercase: " << upper << "; Lowercase: " << lower << "; Digits: " << digits << ";\n"; 12 | 13 | } 14 | 15 | int main(){ 16 | 17 | char str[] = "Aaab1Ba5Bab2b"; 18 | printInfo(str, sizeof(str) - 1); 19 | 20 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const unsigned int MAX_SIZE = 1024; 5 | 6 | void flipCase(char str[]){ 7 | 8 | for(unsigned int i = 0; str[i] != '\0'; i++) 9 | if(str[i] >= 'A' && str[i] <= 'Z') str[i] = std::tolower(str[i]); 10 | else if(str[i] >= 'a' && str[i] <= 'z') str[i] = std::toupper(str[i]); 11 | 12 | } 13 | 14 | int main(){ 15 | 16 | char str[MAX_SIZE]; 17 | std::cin >> str; 18 | flipCase(str); 19 | 20 | std::cout << str << '\n'; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned int maxDistanceBetweenTwoUpperCase(const char str[], const unsigned int size){ 4 | 5 | bool count = false; 6 | unsigned int maxDistance, currDistance; 7 | maxDistance = currDistance = 0; 8 | 9 | for(unsigned int i = 0; i < size; i++){ 10 | 11 | currDistance += count; 12 | if(str[i] >= 'A' && str[i] <= 'Z'){ 13 | 14 | if(count) currDistance--; 15 | if(currDistance > maxDistance) maxDistance = currDistance; 16 | currDistance = 0; 17 | count = !count; 18 | 19 | } 20 | 21 | } 22 | 23 | return maxDistance; 24 | 25 | } 26 | 27 | int main(){ 28 | 29 | char str[] = "jwislaowAkslawofOlsaQlkasG"; 30 | std::cout << maxDistanceBetweenTwoUpperCase(str, sizeof(str) - 1) << '\n'; 31 | 32 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void removeEmptySpaces(char str[], unsigned int &size){ 4 | 5 | unsigned int readIndex, writeIndex; 6 | readIndex = writeIndex = 0; 7 | 8 | for(unsigned int i = 0; i < size; i++) 9 | if(str[i] == ' ') ++readIndex; 10 | else str[writeIndex++] = str[readIndex++]; 11 | 12 | size -= readIndex - writeIndex; 13 | str[size] = '\0'; 14 | 15 | 16 | } 17 | 18 | int main(){ 19 | 20 | char str[] = "Hello there my friends it's nice to meet you."; 21 | unsigned int size = sizeof(str) - 1; 22 | removeEmptySpaces(str, size); 23 | std::cout << str << '\n'; 24 | 25 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isPalindrome(const char str[], const unsigned int size){ 4 | 5 | for(unsigned int i = 0; i < size / 2; i++) 6 | if(str[i] != str[size - 1 - i]) return false; 7 | return true; 8 | 9 | } 10 | 11 | int main(){ 12 | 13 | char str[] = "skjwiaonsse"; 14 | std::cout << std::boolalpha << isPalindrome(str, sizeof(str) - 1) << '\n'; 15 | 16 | } -------------------------------------------------------------------------------- /Seminars/Week 10/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned int MAX_SIZE = 1024; 4 | 5 | void strcpy(char copied[], const char toBeCopied[], const unsigned int size){ 6 | 7 | for(unsigned int i = 0; i < size; i++) copied[i] = toBeCopied[i]; 8 | copied[size] = '\0'; 9 | 10 | } 11 | 12 | void findLongestAndShortestWord(const char str[], const unsigned int size){ 13 | 14 | unsigned int maxLen = 0, shortestLen = MAX_SIZE, currLen = 0; 15 | char longest[MAX_SIZE], shortest[MAX_SIZE], curr[MAX_SIZE]; 16 | 17 | for(unsigned int i = 0; i < size; i++) 18 | if(str[i] == ' '){ 19 | 20 | if(currLen < shortestLen){ 21 | 22 | strcpy(shortest, curr, currLen); 23 | shortestLen = currLen; 24 | 25 | } 26 | 27 | if(currLen > maxLen){ 28 | 29 | strcpy(longest, curr, currLen); 30 | maxLen = currLen; 31 | 32 | } 33 | 34 | currLen = 0; 35 | 36 | } 37 | else curr[currLen++] = str[i]; 38 | 39 | std::cout << longest << ' ' << shortest << '\n'; 40 | 41 | } 42 | 43 | int main(){ 44 | 45 | char str[] = "Hello there my friends it's nice to meet you."; 46 | findLongestAndShortestWord(str, sizeof(str) - 1); 47 | 48 | } -------------------------------------------------------------------------------- /Seminars/Week 11/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char FindFirstUppercase(char const * const str){ 5 | 6 | if(*str == '\0') return '\0'; 7 | if('A' <= *str && *str <= 'Z') return *str; 8 | return FindFirstUppercase(str + 1); 9 | 10 | } 11 | 12 | int main(){ 13 | 14 | size_t const SIZE = 256; 15 | char str[SIZE]; 16 | std::cin.getline(str, SIZE); 17 | 18 | std::cout << FindFirstUppercase(str) << '\n'; 19 | 20 | return 0; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 11/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintPiramid(unsigned const n, unsigned const i, unsigned row){ 4 | 5 | if(i == n * (n + 1) / 2) return; 6 | if(i == row * (row + 1) / 2){ 7 | 8 | ++row; 9 | std::cout << '\n'; 10 | 11 | } 12 | std::cout << i + 1 << ' '; 13 | PrintPiramid(n, i + 1, row); 14 | 15 | } 16 | 17 | int main(){ 18 | 19 | unsigned int n; 20 | std::cin >> n; 21 | 22 | PrintPiramid(n, 0, 1); 23 | 24 | return 0; 25 | 26 | } -------------------------------------------------------------------------------- /Seminars/Week 11/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void ReduceArray(int * const array, size_t &n){ 6 | 7 | if(n == 1) return; 8 | for(size_t i = 0; i < n - 1; ++i) 9 | array[i] += array[i + 1]; 10 | ReduceArray(array, --n); 11 | 12 | } 13 | 14 | int main(){ 15 | 16 | size_t n; 17 | std::cin >> n; 18 | 19 | int *array = new(std::nothrow) int[n]; 20 | 21 | if(!array){ 22 | 23 | std::cout << "Memory error\n"; 24 | return -1; 25 | 26 | } 27 | 28 | for(size_t i = 0; i < n; ++i) std::cin >> array[i]; 29 | 30 | ReduceArray(array, n); 31 | std::cout << array[0] << '\n'; 32 | 33 | delete[] array; 34 | 35 | return 0; 36 | 37 | } -------------------------------------------------------------------------------- /Seminars/Week 11/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool HasConsecotiveOnes(unsigned const n){ 4 | 5 | unsigned mask = 1; 6 | 7 | while(mask <= n){ 8 | 9 | if(n & mask && n & (mask << 1)) return true; 10 | mask <<= 1; 11 | 12 | } 13 | 14 | return false; 15 | 16 | } 17 | 18 | void PrintSpecifiedNumbers(unsigned const numberOfBits, unsigned const n = 1){ 19 | 20 | if(n == 1 << numberOfBits) return; 21 | if(!HasConsecotiveOnes(n)) std::cout << n << ' '; 22 | PrintSpecifiedNumbers(numberOfBits, n + 1); 23 | 24 | } 25 | 26 | int main(){ 27 | 28 | unsigned numberOfBits; 29 | std::cin >> numberOfBits; 30 | 31 | PrintSpecifiedNumbers(numberOfBits); 32 | std::cout << '\n'; 33 | 34 | return 0; 35 | 36 | } -------------------------------------------------------------------------------- /Seminars/Week 11/stack_size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void){ 5 | 6 | struct rlimit limit; 7 | 8 | getrlimit(RLIMIT_STACK, &limit); 9 | 10 | // rlimit.rlim_cur -> size in bytes 11 | // Alternative "ulimit -s" -> size in KB 12 | printf("\nStack Limit = %ld bytes and %ld bytes max\n", limit.rlim_cur, limit.rlim_max); 13 | 14 | } -------------------------------------------------------------------------------- /Seminars/Week 12/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned MinTiles(unsigned const n, unsigned const m){ 4 | 5 | if(n == 0 || m == 0) return 0; 6 | if(n % 2 == 0 && m % 2 == 0) return MinTiles(n / 2, m / 2); 7 | if(n % 2 == 0 && m % 2 == 1) return (n + MinTiles(n / 2, m / 2)); 8 | if(n % 2 == 1 && m % 2 == 0) return (m + MinTiles(n / 2, m / 2)); 9 | return (n + m - 1 + MinTiles(n / 2, m / 2)); 10 | 11 | } 12 | 13 | int main(){ 14 | 15 | unsigned n, m; 16 | std::cin >> n >> m; 17 | 18 | std::cout << MinTiles(n, m) << '\n'; 19 | 20 | return 0; 21 | 22 | } -------------------------------------------------------------------------------- /Seminars/Week 12/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void GenerateAllStrings(size_t const n, char * const str, size_t const index = 1){ 6 | 7 | if(index == n){ 8 | 9 | str[index] = '\0'; 10 | std::cout << str << ' '; 11 | return; 12 | 13 | } 14 | 15 | str[index] = '0'; 16 | GenerateAllStrings(n, str, index + 1); 17 | 18 | if(str[index - 1] == '0'){ 19 | 20 | str[index] = '1'; 21 | GenerateAllStrings(n, str, index + 1); 22 | 23 | } 24 | 25 | } 26 | 27 | int main(){ 28 | 29 | size_t n; 30 | std::cin >> n; 31 | 32 | char *str = new(std::nothrow) char[n]; 33 | if(!str){ 34 | 35 | std::cout << "Memory error\n"; 36 | return -1; 37 | 38 | } 39 | 40 | str[0] = '0'; 41 | GenerateAllStrings(n, str); 42 | 43 | str[0] = '1'; 44 | GenerateAllStrings(n, str); 45 | 46 | delete[] str; 47 | 48 | return 0; 49 | 50 | } -------------------------------------------------------------------------------- /Seminars/Week 12/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool IsValidMove(bool **map, size_t const n, size_t const x, size_t const y){ 6 | return x < n && y < n && map[x][y] == 0; 7 | } 8 | 9 | bool HasPath(bool **map, size_t const n, size_t const sx, size_t const sy, size_t const ex, size_t const ey){ 10 | 11 | if(!IsValidMove(map, n, sx, sy)) return false; 12 | if(sx == ex && sy == ey) return true; 13 | 14 | map[sx][sy] = true; 15 | 16 | bool result = HasPath(map, n, sx + 1, sy, ex, ey) 17 | || HasPath(map, n, sx, sy + 1, ex, ey) 18 | || HasPath(map, n, sx - 1, sy, ex, ey) 19 | || HasPath(map, n, sx, sy - 1, ex, ey); 20 | 21 | map[sx][sy] = false; 22 | 23 | return result; 24 | 25 | } 26 | 27 | void FreeMatrix(bool const * const * const map, size_t const rows){ 28 | 29 | for(size_t i = 0; i < rows; ++i) 30 | delete[] map[i]; 31 | delete[] map; 32 | 33 | } 34 | 35 | int main(){ 36 | 37 | size_t n; 38 | std::cin >> n; 39 | 40 | bool **map = new(std::nothrow) bool*[n]; 41 | 42 | if(!map){ 43 | 44 | std::cout << "Memory error\n"; 45 | return -1; 46 | 47 | } 48 | 49 | for(size_t i = 0; i < n; ++i){ 50 | 51 | map[i] = new(std::nothrow) bool[n]; 52 | 53 | if(!map[i]){ 54 | 55 | std::cout << "Memory error\n"; 56 | FreeMatrix(map, i); 57 | return -1; 58 | 59 | } 60 | 61 | } 62 | 63 | for(size_t i = 0; i < n; ++i) 64 | for(size_t j = 0; j < n; ++j) 65 | std::cin >> map[i][j]; 66 | 67 | size_t sx, sy, ex, ey; 68 | std::cin >> sx >> sy >> ex >> ey; 69 | 70 | std::cout << std::boolalpha << HasPath(map, n, sx, sy, ex, ey) << '\n'; 71 | 72 | FreeMatrix(map, n); 73 | 74 | return 0; 75 | 76 | } -------------------------------------------------------------------------------- /Seminars/Week 12/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | size_t const POSSIBLE_MOVES = 8; 7 | int const X_MOVES[POSSIBLE_MOVES] = { 2, 1, -1, -2, -2, -1, 1, 2 }; 8 | int const Y_MOVES[POSSIBLE_MOVES] = { 1, 2, 2, 1, -1, -2, -2, -1 }; 9 | 10 | int isSafe(int const * const * const sol, size_t const n, size_t const x, size_t const y){ 11 | return x < n && y < n && sol[x][y] == -1; 12 | } 13 | 14 | void printSolution(int const * const * const sol, size_t const n){ 15 | 16 | for(size_t i = 0; i < n; i++){ 17 | 18 | for(size_t j = 0; j < n; j++) 19 | std::cout << std::setw(2) << sol[i][j] << ' '; 20 | std::cout << '\n'; 21 | 22 | } 23 | 24 | } 25 | 26 | bool solve(int * const * const sol, size_t const n, size_t const x, size_t const y, unsigned const movei){ 27 | 28 | if(movei == n * n) return true; 29 | size_t next_x, next_y; 30 | 31 | for(size_t k = 0; k < POSSIBLE_MOVES; k++){ 32 | 33 | next_x = x + X_MOVES[k]; 34 | next_y = y + Y_MOVES[k]; 35 | 36 | if(isSafe(sol, n, next_x, next_y)){ 37 | 38 | sol[next_x][next_y] = movei; 39 | if(solve(sol, n, next_x, next_y, movei + 1)) return true; 40 | sol[next_x][next_y] = -1; // Backtracking 41 | 42 | } 43 | 44 | } 45 | 46 | return false; 47 | 48 | } 49 | 50 | void FreeMatrix(int const * const * const matrix, size_t const rows){ 51 | 52 | for(size_t i = 0; i < rows; ++i) 53 | delete[] matrix[i]; 54 | delete[] matrix; 55 | 56 | } 57 | 58 | int main(){ 59 | 60 | size_t n; 61 | std::cin >> n; 62 | 63 | int **board = new(std::nothrow) int *[n]; 64 | 65 | if(!board){ 66 | 67 | std::cout << "Memory error\n"; 68 | return -1; 69 | 70 | } 71 | 72 | for(size_t i = 0; i < n; ++i){ 73 | 74 | board[i] = new(std::nothrow) int[n]; 75 | 76 | if(!board[i]){ 77 | 78 | std::cout << "Memory error\n"; 79 | FreeMatrix(board, i); 80 | return -1; 81 | 82 | } 83 | 84 | } 85 | 86 | for(size_t i = 0; i < n; ++i) 87 | for(size_t j = 0; j < n; ++j) 88 | board[i][j] = -1; 89 | 90 | board[0][0] = 1; 91 | 92 | if(solve(board, n, 0, 0, 1)) printSolution(board, n); 93 | else std::cout << "Solution does not exist\n"; 94 | 95 | FreeMatrix(board, n); 96 | 97 | return 0; 98 | 99 | } -------------------------------------------------------------------------------- /Seminars/Week 13/Solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef int(*OperationType)(int const); 4 | 5 | int AddOne(int const x){ 6 | return x + 1; 7 | } 8 | 9 | int Double(int const x){ 10 | return 2 * x; 11 | } 12 | 13 | void Map(int * const array, size_t const size, OperationType operation){ 14 | 15 | if(!size) return; 16 | 17 | array[size - 1] = operation(array[size - 1]); 18 | Map(array, size - 1, operation); 19 | 20 | // Алтернативен вариант 21 | // *array = operation(*array); 22 | // Map(array + 1, size - 1, operation); 23 | 24 | } 25 | 26 | int main(){ 27 | 28 | size_t const SIZE = 5; 29 | int array[SIZE] = {1, 2, 3, 4, 5}; 30 | 31 | Map(array, SIZE, AddOne); 32 | 33 | for(size_t i = 0; i < SIZE; ++i) 34 | std::cout << array[i] << ' '; 35 | std::cout << '\n'; 36 | 37 | Map(array, SIZE, Double); 38 | 39 | for(size_t i = 0; i < SIZE; ++i) 40 | std::cout << array[i] << ' '; 41 | std::cout << '\n'; 42 | 43 | return 0; 44 | 45 | } -------------------------------------------------------------------------------- /Seminars/Week 13/Solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef bool(*PredicatFunction)(int const); 6 | 7 | int *Filter(int const * const array, size_t const size, size_t &newSize, PredicatFunction predicat){ 8 | 9 | newSize = 0; 10 | size_t counter = 0; 11 | for(size_t i = 0; i < size; ++i) 12 | if(predicat(array[i])) 13 | counter++; 14 | 15 | int *result = new(std::nothrow) int[counter]; 16 | if(!result) return nullptr; 17 | 18 | for(size_t i = 0; i < size; ++i) 19 | if(predicat(array[i])) 20 | result[newSize++] = array[i]; 21 | 22 | return result; 23 | 24 | } 25 | 26 | bool IsEven(int const x){ 27 | return !(x % 2); 28 | } 29 | 30 | bool IsOdd(int const x){ 31 | return x % 2; 32 | } 33 | 34 | int main(){ 35 | 36 | size_t const SIZE = 5; 37 | size_t newSize; 38 | int array[SIZE] = {1, 2, 3, 4, 5}; 39 | int *result = Filter(array, SIZE, newSize, IsEven); 40 | 41 | if(!result){ 42 | 43 | std::cout << "Memory error\n"; 44 | return -1; 45 | 46 | } 47 | 48 | for(size_t i = 0; i < newSize; ++i) 49 | std::cout << result[i] << ' '; 50 | std::cout << '\n'; 51 | 52 | delete[] result; 53 | 54 | result = Filter(array, SIZE, newSize, IsOdd); 55 | 56 | if(!result){ 57 | 58 | std::cout << "Memory error\n"; 59 | return -1; 60 | 61 | } 62 | 63 | for(size_t i = 0; i < newSize; ++i) 64 | std::cout << result[i] << ' '; 65 | std::cout << '\n'; 66 | 67 | delete[] result; 68 | 69 | return 0; 70 | 71 | } -------------------------------------------------------------------------------- /Seminars/Week 13/Solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef bool(*PredicateFunction)(int const, int const); 5 | 6 | void Sort(int * const arr, size_t const size, PredicateFunction predicate){ 7 | 8 | for(size_t i = 0; i < size - 1; i++){ 9 | size_t min_index = i; 10 | for(size_t j = i + 1; j < size; j++){ 11 | if(predicate(arr[min_index], arr[j])){ 12 | min_index = j; 13 | } 14 | } 15 | if(min_index != i){ 16 | std::swap(arr[i], arr[min_index]); 17 | } 18 | } 19 | 20 | } 21 | 22 | bool EvenBeforeOdd(int const a, int const b){ 23 | return a % 2 && !(b % 2); 24 | } 25 | 26 | bool Ascending(int const a, int const b){ 27 | return a > b; 28 | } 29 | 30 | bool Descending(int const a, int const b){ 31 | return a < b; 32 | } 33 | 34 | int main(){ 35 | 36 | size_t const SIZE = 10; 37 | int arr[SIZE] = { 5, 1, 6, 8, 0, 4, 3, 2, 9, 7 }; 38 | 39 | Sort(arr, SIZE, EvenBeforeOdd); 40 | 41 | for(size_t i = 0; i < SIZE; ++i) 42 | std::cout << arr[i] << ' '; 43 | std::cout << '\n'; 44 | 45 | Sort(arr, SIZE, Ascending); 46 | 47 | for(size_t i = 0; i < SIZE; ++i) 48 | std::cout << arr[i] << ' '; 49 | std::cout << '\n'; 50 | 51 | Sort(arr, SIZE, Descending); 52 | 53 | for(size_t i = 0; i < SIZE; ++i) 54 | std::cout << arr[i] << ' '; 55 | std::cout << '\n'; 56 | 57 | return 0; 58 | 59 | } --------------------------------------------------------------------------------