├── .gitignore ├── IS ├── README.md ├── Sem.01 │ ├── Pract.01 │ │ ├── README.md │ │ ├── task01.cpp │ │ ├── task02.cpp │ │ ├── task03.cpp │ │ ├── task04.cpp │ │ ├── task05.cpp │ │ ├── task06.cpp │ │ ├── task07.cpp │ │ ├── task08.cpp │ │ ├── task09.cpp │ │ ├── task10.cpp │ │ ├── task11.cpp │ │ ├── task12.cpp │ │ ├── task13.cpp │ │ └── task14.cpp │ └── README.md ├── Sem.02 │ ├── Pract.02 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ └── Task08.cpp │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ └── Task3.cpp ├── Sem.03 │ ├── Pract.03 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ ├── Task09.cpp │ │ ├── Task10.cpp │ │ ├── Task11.cpp │ │ ├── Task12.cpp │ │ ├── Task13.cpp │ │ └── Task14.cpp │ ├── README.md │ └── Solutions │ │ ├── GCD.cpp │ │ ├── GCDEuclideanAlgorithm.cpp │ │ ├── PrimeNumber.cpp │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ └── Task3.cpp ├── Sem.04 │ ├── Pract.04 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ ├── Task09.cpp │ │ ├── Task10.cpp │ │ ├── Task11.cpp │ │ ├── Task12.cpp │ │ ├── Task13.cpp │ │ ├── Task14.cpp │ │ └── Task15.cpp │ ├── README.md │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp ├── Sem.05 │ ├── BinarySearch.cpp │ ├── EratosthenesSieve.cpp │ ├── InputArray.cpp │ ├── LinearSerach.cpp │ ├── Pract.05 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ └── TicTacToeGame.cpp │ ├── PrintArray.cpp │ ├── README.md │ └── ReverseArray.cpp ├── Sem.06 │ ├── BitwiseSimpleExamples.cpp │ ├── NumerialSystemsExample.cpp │ ├── Playground.cpp │ ├── Pract.06 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ └── Task07.cpp │ └── README.md ├── Sem.07 │ ├── Pract.07 │ │ ├── References │ │ │ ├── README.md │ │ │ ├── Task01.cpp │ │ │ ├── Task02.cpp │ │ │ └── Task09.cpp │ │ └── Strings │ │ │ ├── README.md │ │ │ ├── Task04.cpp │ │ │ ├── Task07.cpp │ │ │ ├── Task08.cpp │ │ │ ├── Task11.cpp │ │ │ ├── Task12.cpp │ │ │ └── task06.cpp │ ├── README.md │ ├── Solutions │ │ ├── getCharOccurrencesCount.cpp │ │ ├── myAtoi.cpp │ │ ├── myStrCat.cpp │ │ ├── myStrCmp.cpp │ │ ├── myStrCpy.cpp │ │ ├── myStrLen.cpp │ │ └── searchInText.cpp │ └── images │ │ ├── example1.png │ │ ├── example2.png │ │ ├── right-to-left.jfif │ │ └── spiral-rule.jfif ├── Sem.08 │ ├── Pract.08 │ │ ├── FileExamples.cpp │ │ ├── ProjectHelperFunctions.cpp │ │ └── README.md │ ├── README.md │ ├── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ └── Task6.cpp │ ├── images │ │ ├── Memory.png │ │ └── exampleMemoryLeak.png │ └── memoryLeak.cpp ├── Sem.09 │ ├── Pract.09 │ │ ├── Bonus │ │ │ ├── Factoriel.cpp │ │ │ ├── FibonacciWithWrapperFunc.cpp │ │ │ └── FibonacciWithoutWrapperFunc.cpp │ │ ├── README copy.md │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ └── Task06.cpp │ ├── README.md │ └── Solutions │ │ ├── BinarySearch.cpp │ │ ├── BubbleSort.cpp │ │ ├── LinearSearch.cpp │ │ ├── Pow.cpp │ │ └── SearchInText.cpp └── Sem.10 │ ├── README.md │ ├── examples │ ├── example1.cpp │ ├── example2.cpp │ ├── example3.cpp │ └── example4.cpp │ └── img │ ├── Function-pointer-declaration.png │ ├── Lambda-syntax.png │ └── lambda-extended-syntax.gif ├── README.md ├── SI ├── README.md ├── Sem.01 │ ├── Pract.01 │ │ ├── README.md │ │ ├── task01.cpp │ │ ├── task02.cpp │ │ ├── task03.cpp │ │ ├── task04.cpp │ │ ├── task05.cpp │ │ ├── task06.cpp │ │ ├── task07.cpp │ │ ├── task08.cpp │ │ ├── task09.cpp │ │ ├── task10.cpp │ │ ├── task11.cpp │ │ ├── task12.cpp │ │ ├── task13.cpp │ │ └── task14.cpp │ └── README.md ├── Sem.02 │ ├── Pract.02 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ └── Task08.cpp │ ├── README.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ └── Task3.cpp ├── Sem.03 │ ├── Pract.03 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ ├── Task09.cpp │ │ ├── Task10.cpp │ │ ├── Task11.cpp │ │ ├── Task12.cpp │ │ ├── Task13.cpp │ │ └── Task14.cpp │ ├── README.md │ └── Solutions │ │ ├── GCD.cpp │ │ ├── GCDEuclideanAlgorithm.cpp │ │ ├── PrimeNumber.cpp │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ └── Task3.cpp ├── Sem.04 │ ├── Pract.04 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ ├── Task09.cpp │ │ ├── Task10.cpp │ │ ├── Task11.cpp │ │ ├── Task12.cpp │ │ ├── Task13.cpp │ │ ├── Task14.cpp │ │ └── Task15.cpp │ ├── README.md │ ├── task1.cpp │ ├── task2.cpp │ └── task3.cpp ├── Sem.05 │ ├── BinarySearch.cpp │ ├── EratosthenesSieve.cpp │ ├── InputArray.cpp │ ├── LinearSerach.cpp │ ├── Pract.05 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ └── TicTacToeGame.cpp │ ├── PrintArray.cpp │ ├── README.md │ └── ReverseArray.cpp ├── Sem.06 │ ├── BitwiseSimpleExamples.cpp │ ├── NumerialSystemsExample.cpp │ ├── Playground.cpp │ ├── Pract.06 │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ └── Task07.cpp │ └── README.md ├── Sem.07 │ ├── Pract.07 │ │ ├── References │ │ │ ├── README.md │ │ │ ├── Task01.cpp │ │ │ ├── Task02.cpp │ │ │ └── Task09.cpp │ │ └── Strings │ │ │ ├── README.md │ │ │ ├── Task04.cpp │ │ │ ├── Task07.cpp │ │ │ ├── Task08.cpp │ │ │ ├── Task11.cpp │ │ │ ├── Task12.cpp │ │ │ └── task06.cpp │ ├── README.md │ ├── Solutions │ │ ├── getCharOccurrencesCount.cpp │ │ ├── myAtoi.cpp │ │ ├── myStrCat.cpp │ │ ├── myStrCmp.cpp │ │ ├── myStrCpy.cpp │ │ ├── myStrLen.cpp │ │ └── searchInText.cpp │ └── images │ │ ├── example1.png │ │ ├── example2.png │ │ ├── right-to-left.jfif │ │ └── spiral-rule.jfif ├── Sem.08 │ ├── Pract.08 │ │ ├── FileExamples.cpp │ │ ├── ProjectHelperFunctions.cpp │ │ └── README.md │ ├── README.md │ ├── Solutions │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.cpp │ │ └── Task6.cpp │ ├── images │ │ ├── Memory.png │ │ └── exampleMemoryLeak.png │ └── memoryLeak.cpp ├── Sem.09 │ ├── Algorithms │ │ └── README.md │ ├── Pract.09 │ │ ├── Bonus │ │ │ ├── Factoriel.cpp │ │ │ ├── FibonacciWithWrapperFunc.cpp │ │ │ └── FibonacciWithoutWrapperFunc.cpp │ │ ├── README.md │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ └── Task06.cpp │ ├── README.md │ ├── Solutions │ │ ├── BinarySearch.cpp │ │ ├── BubbleSort.cpp │ │ ├── CountingSort.cpp │ │ ├── LinearSearch.cpp │ │ ├── Pow.cpp │ │ ├── SearchInText.cpp │ │ └── SelectionSort.cpp │ └── images │ │ ├── Head-recursion.jpg │ │ ├── Indirect-direct-recursion.png │ │ ├── Nested-recursion.jpg │ │ ├── Recursion-simpsons.gif │ │ ├── SumRange.gif │ │ ├── Tail-recursion.jpg │ │ ├── Tree-recursion.jpg │ │ ├── factorial-formula.png │ │ ├── factorial.gif │ │ ├── fib.gif │ │ ├── fib.png │ │ └── gcd-definition.png └── Sem.10 │ ├── README.md │ ├── examples │ ├── example1.cpp │ ├── example2.cpp │ ├── example3.cpp │ └── example4.cpp │ └── img │ ├── Function-pointer-declaration.png │ ├── Lambda-syntax.png │ └── lambda-extended-syntax.gif ├── Utils ├── Consultations │ ├── IS-13.01 │ │ ├── Tasks │ │ │ ├── README.md │ │ │ ├── Task01.cpp │ │ │ ├── Task02.cpp │ │ │ ├── Task03.cpp │ │ │ └── Task04.cpp │ │ └── Theory │ │ │ ├── MemoryQuestions.cpp │ │ │ ├── RecursionFibonacciProblem.cpp │ │ │ └── RecursionQuestions.cpp │ ├── IS-6.11 │ │ ├── README.md │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ └── Task3.cpp │ ├── SI-12.11 │ │ ├── README.md │ │ ├── Task1.cpp │ │ └── Task2.cpp │ └── SI-28.11 │ │ ├── Problems.md │ │ ├── README.md │ │ ├── Task1.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ └── Task4.cpp └── Tests │ ├── IS-Test-1 │ ├── README.md │ ├── Task1.cpp │ ├── Task2.cpp │ ├── Task3 - a.cpp │ └── Task3 - b.cpp │ ├── IS-Test-2 │ └── README.md │ ├── Inf-Test-1 │ ├── README.md │ ├── Task1.cpp │ ├── Task2.cpp │ └── Task3.cpp │ ├── Inf-Test-2 │ └── README.md │ └── SI-Test-2 │ ├── README.md │ ├── Task1.cpp │ └── Task2.cpp └── images ├── fetch-origin.png ├── repository-fork-settings.png ├── repository-settings-menu.png └── star-the-repo.png /.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 | -------------------------------------------------------------------------------- /IS/README.md: -------------------------------------------------------------------------------- 1 | ## Материали от семинарите по "Увод в програмирането" 2 | #### зимен семестър 2022/2023, спец. Информационни системи 3 | 4 | - [Тема 1:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.01) Увод. Примитивни типове данни. Преобразуване между типове. Оператори. 5 | - [Тема 2:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.02) Условни конструкции - if, switch и тернарен оператор. 6 | - [Тема 3:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.03) Цикли. Оператори break и continue. 7 | - [Тема 4:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.04) Функции. 8 | - [Тема 5:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.05) Масиви. Основни операции, обхождане на елементите, търсене. Многомерни масиви. 9 | - [Тема 6:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.06) Бройни системи и побитови операции. 10 | - [Тема 7:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.07) Референция. Указатели. Символни низове. 11 | - [Тема 8:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.08) Динамична памет. 12 | - [Тема 9:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.09) Рекурсия. 13 | - [Тема 10:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.10) Функции от по-висок ред. -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | cout << "Oh what\na happy day!\nOh yes,\nwhat a happy day!" << endl; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double a = 5.4; 8 | double b = 7.9; 9 | 10 | cout << "P = " << 2 * (a + b) << endl; 11 | cout << "S = " << a * b << endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int amount; 8 | cout << "Enter amount in lv:"; 9 | cin >> amount; 10 | cout << "This amount is " << amount * 0.5 << " in USD and " << amount * 0.51 << " in EUR." << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double x, y; 8 | 9 | cout << "First side: "; 10 | cin >> x; 11 | 12 | cout << "Second side: "; 13 | cin >> y; 14 | 15 | cout << "Parameter: " << 2 * (x + y) << endl; 16 | cout << "Area: " << x * y << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double a, b; 8 | 9 | cout << "First number: "; 10 | cin >> a; 11 | 12 | cout << "Second number: "; 13 | cin >> b; 14 | 15 | cout << boolalpha << (a < b) << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | 9 | cout << "Dividend: "; 10 | cin >> a; 11 | cout << "Divisor: "; 12 | cin >> b; 13 | 14 | cout << "The quotient of the division is : " << a / b << endl; 15 | cout << "The remainder of the division is : " << a % b << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int apples, pears, bananas; 8 | 9 | cout << "Apples: "; 10 | cin >> apples; 11 | cout << "Pears: "; 12 | cin >> pears; 13 | cout << "Bananas: "; 14 | cin >> bananas; 15 | 16 | cout << "Pesho, don't forget to buy " << apples << " apples, " 17 | << pears << " pears and " << bananas << " bananas!"; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const double PI = 3.14159265; 6 | 7 | int main() 8 | { 9 | double radius; 10 | 11 | cout << "Radius: "; 12 | cin >> radius; 13 | 14 | cout << "Perimeter: " << 2 * radius * PI << endl; 15 | cout << "Area: " << radius * PI * PI << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int a, b, c; 9 | 10 | cout << "a = "; 11 | cin >> a; 12 | cout << "b = "; 13 | cin >> b; 14 | cout << "c = "; 15 | cin >> c; 16 | 17 | double d = sqrt(b * b - 4 * a * c); 18 | double x1 = (-b + d) / (2 * a); 19 | double x2 = (-b - d) / (2 * a); 20 | 21 | cout << "x1 = " << x1 << ", x2 = " << x2; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | cout << "a: "; 9 | cin >> a; 10 | cout << "b: "; 11 | cin >> b; 12 | 13 | int temp = a; 14 | a = b; 15 | b = temp; 16 | 17 | cout << "a: " << a << endl; 18 | cout << "b: " << b << endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | cout << "a: "; 9 | cin >> a; 10 | cout << "b: "; 11 | cin >> b; 12 | 13 | bool isFirstHigher = a > b; // 0 or 1 14 | 15 | cout << "Max: " << a * isFirstHigher + b * !isFirstHigher << endl; 16 | cout << "Min: " << a * !isFirstHigher + b * isFirstHigher << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int inputSeconds; 8 | 9 | cout << "Seconds: "; 10 | cin >> inputSeconds; 11 | 12 | int days = inputSeconds / (60 * 60 * 24); 13 | int hours = (inputSeconds / (60 * 60)) % 24; 14 | int minutes = (inputSeconds / 60) % 60; 15 | int seconds = inputSeconds % 60; 16 | 17 | cout << days << " days, " 18 | << hours << " hours, " 19 | << minutes << " minutes and " 20 | << seconds << " seconds"; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x1, x2, y1, y2; 9 | cout << "x1: "; 10 | cin >> x1; 11 | cout << "y1: "; 12 | cin >> y1; 13 | cout << "x2: "; 14 | cin >> x2; 15 | cout << "y2: "; 16 | cin >> y2; 17 | 18 | double distance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 19 | 20 | cout << "Distance: " << fixed << setprecision(2) << distance; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /IS/Sem.01/Pract.01/task14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long cardNumber; 8 | cin >> cardNumber; 9 | 10 | cout << "XXXXXXXXXXXX" << cardNumber % 10000; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int year = 0; 5 | 6 | std::cout << "Enter a year: "; 7 | std::cin >> year; 8 | 9 | if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { 10 | std::cout << "Entered year is leap.\n"; 11 | } else { 12 | std::cout << "Entered year is not leap.\n"; 13 | } 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char inputCharacter; 5 | 6 | std::cin >> inputCharacter; 7 | 8 | if ((inputCharacter >= 'A' && inputCharacter <= 'Z') || (inputCharacter >= 'a' && inputCharacter <= 'z')) { 9 | switch (inputCharacter) { 10 | case 'A': 11 | case 'E': 12 | case 'I': 13 | case 'O': 14 | case 'U': 15 | case 'Y': 16 | case 'a': 17 | case 'e': 18 | case 'i': 19 | case 'o': 20 | case 'u': 21 | case 'y': 22 | std::cout << "Entered letter is a vowel.\n"; 23 | break; 24 | default: 25 | std::cout << "Entered letter is not a vowel.\n"; 26 | break; 27 | } 28 | } else { 29 | std::cout << "The symbol is not part of the latin alphabet." << std::endl; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double number1, number2; 5 | 6 | std::cout << "Enter first number value: "; 7 | std::cin >> number1; 8 | std::cout << "Enter second number value: "; 9 | std::cin >> number2; 10 | 11 | if (number1 == number2) { 12 | std::cout << "Both numbers are equal." << std::endl; 13 | return 0; 14 | } 15 | 16 | std::cout << (number1 > number2 ? "The first number is larger than the second number." : "The second number is larger than the first number.") << std::endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int percentage, grade; 5 | 6 | std::cout << "Result: "; 7 | std::cin >> percentage; 8 | 9 | if (percentage >= 0 && percentage <= 100) { 10 | bool isSixPlus = false; 11 | 12 | if (percentage >= 90) { 13 | grade = 6; 14 | isSixPlus = true; 15 | } else if (percentage >= 80) { 16 | grade = 6; 17 | } else if (percentage >= 70) { 18 | grade = 5; 19 | } else if (percentage >= 60) { 20 | grade = 4; 21 | } else if (percentage >= 40) { 22 | grade = 3; 23 | } else { 24 | grade = 2; 25 | } 26 | 27 | std::cout << "Grade: " << grade << (isSixPlus ? "+\n" : "\n"); 28 | } else { 29 | std::cout << "Entered percentage is not valid!\n"; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double sideA, sideB, sideC; 5 | 6 | std::cout << "Enter value for side a: "; 7 | std::cin >> sideA; 8 | std::cout << "Enter value for side b: "; 9 | std::cin >> sideB; 10 | std::cout << "Enter value for side c: "; 11 | std::cin >> sideC; 12 | 13 | if ((sideA + sideB > sideC) && (sideA + sideC > sideB) && (sideB + sideC > sideA)) { 14 | std::cout << "The triangle is valid.\n"; 15 | } else { 16 | std::cout << "The triangle is invalid.\n"; 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | cout << "Finding the root of the equation a * x^2 + b * x + c = 0\n"; 9 | 10 | double a; 11 | cout << "Enter the value of a: "; 12 | cin >> a; 13 | 14 | if (a < 0.00000001) 15 | { 16 | cout << "If a is 0, then this is not a quadratic equation!\n"; 17 | return 1; 18 | } 19 | 20 | double b, c; 21 | cout << "Enter the value of b: "; 22 | cin >> b; 23 | 24 | cout << "Enter the value of c: "; 25 | cin >> c; 26 | 27 | double discriminant; 28 | discriminant = (b * b) - (4 * a * c); 29 | 30 | double root1, root2, imaginary; 31 | if (discriminant > 0) { 32 | root1 = (-b + sqrt(discriminant)) / (2 * a); 33 | root2 = (-b - sqrt(discriminant)) / (2 * a); 34 | 35 | cout << "Two distinct and real roots exist: " << root1 << " and " << root2 << endl; 36 | } else if (discriminant == 0) { 37 | root1 = root2 = -b / (2 * a); 38 | 39 | cout << "Two equal and real roots exist: " << root1 << " and " << root2 << endl; 40 | } else if (discriminant < 0) { 41 | root1 = root2 = -b / (2 * a); 42 | imaginary = sqrt(-discriminant) / (2 * a); 43 | 44 | cout << "Two distinct complex roots exist: " << root1 << "+" << imaginary << "i and " << root2 << "-" << imaginary << "i\n"; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char symbol; 5 | std::cin >> symbol; 6 | 7 | if (symbol >= 'A' && symbol <= 'Z') { 8 | std::cout << (symbol += 32) << std::endl; 9 | } else if (symbol >= 'a' && symbol <= 'z') { 10 | std::cout << (symbol -= 32) << std::endl; 11 | } else { 12 | std::cout << "Invalid character" << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /IS/Sem.02/Pract.02/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int arabicNumber = 0; 5 | 6 | std::cout << "Arabic numeral: "; 7 | std::cin >> arabicNumber; 8 | 9 | if (arabicNumber / 10 == 0 && arabicNumber != 0) { 10 | std::cout << "Roman numerals: "; 11 | 12 | switch (arabicNumber) { 13 | case 1: 14 | std::cout << "I"; 15 | break; 16 | case 2: 17 | std::cout << "II"; 18 | break; 19 | case 3: 20 | std::cout << "III"; 21 | break; 22 | case 4: 23 | std::cout << "IV"; 24 | break; 25 | case 5: 26 | std::cout << "V"; 27 | break; 28 | case 6: 29 | std::cout << "VI"; 30 | break; 31 | case 7: 32 | std::cout << "VII"; 33 | break; 34 | case 8: 35 | std::cout << "VIII"; 36 | break; 37 | case 9: 38 | std::cout << "IX"; 39 | break; 40 | default: 41 | break; 42 | } 43 | 44 | std::cout << std::endl; 45 | } else { 46 | std::cout << "Entered number contains more than one digit or is 0." << std::endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /IS/Sem.02/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int n; 7 | cin >> n; 8 | 9 | if (n < 100) { 10 | cout << "Less than 100" << endl; 11 | } else if (n <= 200) { 12 | cout << "Between 100 and 200" << endl; 13 | } else { 14 | cout << "More than 200" << endl; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.02/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | double speed; 7 | cin >> speed; 8 | 9 | if (speed <= 10) { 10 | cout << "Slow" << endl; 11 | } else if (speed < 50) { 12 | cout << "Average" << endl; 13 | } else if (speed < 150) { 14 | cout << "Fast" << endl; 15 | } else if (speed < 1000) { 16 | cout << "Ultra fast" << endl; 17 | } else { 18 | cout << "Extremely fast" << endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /IS/Sem.02/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, c, d, e; 6 | std::cin >> a >> b >> c >> d >> e; 7 | 8 | std::cout << ((a <= b && b >= c && c <= d && d >= e) 9 | || (a >= b && b <= c && c >= d && d <= e)) 10 | ? "Yes" : "No"; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | for (int i = 1; i <= n; i++) { 11 | for (int j = 1; j <= i; j++) { 12 | cout << j; 13 | } 14 | 15 | if (i != n) { 16 | cout << endl; 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | for (int i = 1; i < n; i++) { 11 | int x = i; 12 | int y = n - x; 13 | 14 | cout << "x = " << x << " y = " << y << endl; 15 | } 16 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | int reversed = 0; 11 | n = n * n; 12 | 13 | while (n != 0) { 14 | reversed = reversed * 10 + n % 10; 15 | n /= 10; 16 | } 17 | 18 | cout << reversed; 19 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, m; 7 | cin >> n >> m; 8 | 9 | if(n <= 0 || m <= 0) { 10 | cout << "Invalid input." << endl; 11 | return 0; 12 | } 13 | 14 | for (int i = 1; i <= n * m; i++) { 15 | cout << i << " "; 16 | 17 | if (i % m == 0) { 18 | cout << endl; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int target = 0; 7 | 8 | cout << "Enter a number between 0 and 100: "; 9 | cin >> target; 10 | 11 | if (target < 0 || target > 100) { 12 | cout << "Entered number invalid." << endl; 13 | return 0; 14 | } 15 | 16 | int counter = 0, currentGuess; 17 | while (true) { 18 | cin >> currentGuess; 19 | counter++; 20 | 21 | if (currentGuess == target) { 22 | break; 23 | } 24 | 25 | cout << (currentGuess > target ? "Higher" : "Lower") << endl; 26 | } 27 | 28 | cout << "Congratulations, you guessed right! In only took you " << counter << " tries to do so. :)" << endl; 29 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | int targetSum = 0; 10 | for (int i = 1; i <= n; i++) { 11 | targetSum += i; 12 | } 13 | 14 | for (int i = 0; i < n - 1; i++) { 15 | int currentNumber; 16 | cin >> currentNumber; 17 | targetSum -= currentNumber; 18 | } 19 | 20 | cout << targetSum; 21 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = i; j <= n; j++) { 11 | for (int k = j; k <= n; k++) { 12 | if (i * i + j * j == k * k) { 13 | cout << i << " " << j << " " << k << endl; 14 | } 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | char s; 8 | cin >> n >> s; 9 | 10 | if (n < 2 || n > 9) { 11 | cout << "Invalid input." << endl; 12 | return 0; 13 | } 14 | 15 | int numberOfRows = 2 * n - 1; 16 | 17 | for (int i = 1; i <= numberOfRows; i++) { 18 | int numberOfEmptySpaces = abs(n - i); 19 | int numberOfSymbols = numberOfRows - 2 * numberOfEmptySpaces; 20 | 21 | for (int j = 0; j < numberOfEmptySpaces; j++) { 22 | cout << ' '; 23 | } 24 | for (int j = 0; j < numberOfSymbols; j++) { 25 | cout << s; 26 | } 27 | 28 | cout << endl; 29 | } 30 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | if (n <= 3) { 10 | cout << "Invalid input." << endl; 11 | return 0; 12 | } 13 | 14 | int num1 = 1, num2 = 1; 15 | cout << num1 << " " << num2 << " "; 16 | 17 | while (num1 + num2 < n) { 18 | cout << num1 + num2 << " "; 19 | int temp = num1; 20 | num1 = num2; 21 | num2 = num1 + temp; 22 | } 23 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int number, digits = 0; 7 | cin >> number; 8 | 9 | int temp = number; //copy of number 10 | while (temp != 0) 11 | { 12 | temp /= 10; 13 | digits++; 14 | } 15 | 16 | int newNumber = 0; 17 | if (digits > 2) 18 | { 19 | int Multiplier = 1; 20 | const int centralDigit = digits / 2 + 1; 21 | 22 | for (int i = 1; i <= digits; i++) 23 | { 24 | if(digits % 2 == 0 && i == centralDigit - 1) 25 | { 26 | number /= 10; 27 | continue; 28 | } 29 | 30 | if (i == centralDigit) 31 | { 32 | number /= 10; 33 | continue; 34 | } 35 | 36 | newNumber += number % 10 * Multiplier; 37 | Multiplier *= 10; 38 | number /= 10; 39 | } 40 | } 41 | 42 | cout << newNumber << ", "; 43 | cout << ++newNumber << endl; 44 | } 45 | -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a = 0, b = 0; 7 | bool isPrime = false; 8 | 9 | cin >> a >> b; 10 | 11 | if (a < 0 || b < 0) { 12 | cout << "Invalid input." << endl; 13 | return 0; 14 | } 15 | 16 | if (a > b) { 17 | int temp = a; 18 | a = b; 19 | b = temp; 20 | } 21 | 22 | for (int i = a; i <= b; i++) { 23 | if (i == 2) { 24 | cout << i << " "; 25 | continue; 26 | } 27 | 28 | if (i == 0 || i == 1 || i % 2 == 0) { 29 | continue; 30 | } 31 | 32 | isPrime = true; 33 | 34 | int squareRoot = sqrt(i); 35 | for (int j = 3; j <= squareRoot; j += 2) { 36 | if (i % j == 0) { 37 | isPrime = false; 38 | break; 39 | } 40 | } 41 | 42 | if (isPrime) { 43 | cout << i << " "; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | if (n < 1) { 10 | cout << "false"; 11 | return 0; 12 | } 13 | 14 | bool isMultipleOfTwo = true; 15 | while (n / 2 != 0) { 16 | if (n % 2 != 0) { 17 | isMultipleOfTwo = false; 18 | break; 19 | } 20 | 21 | n /= 2; 22 | } 23 | 24 | cout << boolalpha << isMultipleOfTwo << endl; 25 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | while (n != 0) { 10 | int k = 1; 11 | int pow = 0; 12 | 13 | while (k * 2 <= n) { 14 | k *= 2; 15 | pow++; 16 | } 17 | 18 | n -= k; 19 | cout << "2^" << pow; 20 | 21 | if (n != 0) { 22 | cout << "+"; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /IS/Sem.03/Pract.03/Task14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b, c, d, e; 7 | cin >> a >> b >> c >> d >> e; 8 | 9 | for (int i = -100; i <= 100; i++) { 10 | if (a * pow(i, 4) + b * pow(i,3) + c * pow(i, 2) + d * i + e >= 0) { 11 | cout << i << " "; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /IS/Sem.03/Solutions/GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | int minNumber = a > b ? b : a; 10 | 11 | int gcd = 1; 12 | 13 | for (size_t i = 2; i <= minNumber; i++) { 14 | if (a % i == 0 && b % i == 0) 15 | gcd = i; 16 | } 17 | 18 | cout << gcd; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /IS/Sem.03/Solutions/GCDEuclideanAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | if (a < b) { 10 | int temp = a; 11 | a = b; 12 | b = temp; 13 | } 14 | 15 | while (b != 0) { 16 | int r = a % b; 17 | 18 | a = b; 19 | b = r; 20 | } 21 | 22 | cout << a; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /IS/Sem.03/Solutions/PrimeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a; 7 | cin >> a; 8 | 9 | if (a == 2) { 10 | cout << "Prime"; 11 | return 0; 12 | } 13 | 14 | if (a % 2 == 0 || a == 1) { 15 | cout << "Not Prime"; 16 | return 0; 17 | } 18 | 19 | double squareRoot = sqrt(a); 20 | for (int i = 3; i <= squareRoot; i += 2) { 21 | if (a % i == 0) { 22 | cout << "Not Prime"; 23 | return 0; 24 | } 25 | } 26 | 27 | cout << "Prime"; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /IS/Sem.03/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int sum = 0; 8 | int n; 9 | do 10 | { 11 | cin >> n; 12 | sum += n; 13 | } while (n != 0); 14 | 15 | cout << sum; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.03/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | int reversed = 0; 9 | 10 | while (n != 0) { 11 | reversed = reversed * 10 + n % 10; 12 | n /= 10; 13 | } 14 | 15 | cout << reversed + 1; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.03/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | cin >> a >> b; 9 | 10 | int min = a > b ? b : a; 11 | 12 | for (int i = min; i > 0; i--) { 13 | if (a % i == 0 && b % i == 0) 14 | cout << i << " "; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task01.cpp: -------------------------------------------------------------------------------- 1 | int Sum(int a, int b) 2 | { 3 | return a + b; 4 | } 5 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task02.cpp: -------------------------------------------------------------------------------- 1 | bool IsEven(int num) 2 | { 3 | return num % 2 == 0; 4 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task03.cpp: -------------------------------------------------------------------------------- 1 | bool IsPositive(int num) 2 | { 3 | return (num > 0); 4 | } 5 | 6 | bool IsNegative(int num) 7 | { 8 | return (num < 0); 9 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task04.cpp: -------------------------------------------------------------------------------- 1 | int abs(int num1, int num2) 2 | { 3 | return ((num1 - num2) >= 0 ? (num1 - num2) : (num2 - num1)); 4 | } 5 | 6 | double fabs(double &num) 7 | { 8 | return num > 0.0 ? num : -num; 9 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task05.cpp: -------------------------------------------------------------------------------- 1 | void print(int N, char S = ' ') 2 | { 3 | for (size_t i = 0; i < N; i++) { 4 | cout << S; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task06.cpp: -------------------------------------------------------------------------------- 1 | bool isCapitalLetter(char S) 2 | { 3 | return 'A' <= S && S <= 'Z'; 4 | } 5 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task07.cpp: -------------------------------------------------------------------------------- 1 | bool isNumberInInterval(long long num, long long lowerBound, long long upperBound) 2 | { 3 | return interval_lower_bound <= num && num <= interval_upper_bound 4 | } 5 | 6 | long long askUser(long long from, long long to) 7 | { 8 | long long number; 9 | while (std::cin >> number) 10 | { 11 | if (isNumberInInterval(number, from, to)) { 12 | return number; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task08.cpp: -------------------------------------------------------------------------------- 1 | long long pow(int num, unsigned N) 2 | { 3 | long long result = 1; 4 | 5 | for (size_t i = 0; i < N; i++) { 6 | result *= num; 7 | } 8 | 9 | return result; 10 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task09.cpp: -------------------------------------------------------------------------------- 1 | int calculate(int a, int b, char action = '+') 2 | { 3 | switch (action) 4 | { 5 | case '+': return a + b; 6 | 7 | case '-': return a - b; 8 | 9 | case '*': return a * b; 10 | 11 | case '/': 12 | if(b == 0) { 13 | std::cout << "Cannot divide by 0\n"; 14 | return -1; 15 | } 16 | return a / b; 17 | 18 | case '%': 19 | if(b == 0) { 20 | std::cout << "Cannot divide by 0\n"; 21 | return -1; 22 | } 23 | return a % b; 24 | 25 | default: 26 | std::cout << "Error\n"; 27 | return -1; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task10.cpp: -------------------------------------------------------------------------------- 1 | unsigned long long toBinary(unsigned number) 2 | { 3 | unsigned long long toBinary = 0; 4 | unsigned long long temp = 1; 5 | 6 | while (number > 0) { 7 | toBinary += (number % 2) * temp; 8 | temp *= 10; 9 | number /= 2; 10 | } 11 | 12 | return toBinary; 13 | } 14 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int Gcd(int x, int y) 4 | { 5 | while (x != y) 6 | { 7 | if (x > y) { 8 | x -= y; 9 | } else { 10 | y -= x; 11 | } 12 | } 13 | 14 | return x; 15 | } 16 | 17 | int main() 18 | { 19 | int a, b, c, d; 20 | 21 | std::cout << "a, b, c, d= \n"; 22 | std::cin >> a >> b >> c >> d; 23 | if (!cin || a < 1 || b < 1 || c < 1 || d < 1) { 24 | std::cout << "Error! \n"; 25 | return 1; 26 | } 27 | 28 | int r = Gcd(a, b); 29 | std::cout << "gcd{" << a << ", " << b << "}= " << r << "\n"; 30 | 31 | int s = Gcd(c, d); 32 | std::cout << "gcd{" << c << ", " << d << "}= " << s << "\n"; 33 | 34 | std::cout << "gcd{" << a << ", " << b << ", " << c << ", " << d << "}= " << Gcd(r, s) << "\n"; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int LowerBound = 100; 4 | const int UpperBound = 1000000; 5 | 6 | int CheckInput() { 7 | int number; 8 | do { 9 | std::cout << "Enter a valid number: "; 10 | std::cin >> number; 11 | } while (!std::cin || number < LowerBound || number > UpperBound); 12 | 13 | return number; 14 | } 15 | 16 | int FindTheNearest(int input) { 17 | int result = 64; // The nearest possible value 18 | int lower, higher; 19 | while (result <= input) { 20 | lower = result; 21 | result *= 2; 22 | higher = result; 23 | }; 24 | 25 | result = ((higher - input) > (input - lower)) ? lower : higher; 26 | return result; 27 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //Function to check whether a number is prime 5 | bool IsPrime(int number) 6 | { 7 | int size = sqrt(number); 8 | 9 | for (int i = 2; i <= size; i++) { 10 | if (number % i == 0) { 11 | return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | 18 | //Print all prime numbers that are smaller than N 19 | void PrintAllPrimeToNumber(int number) //Interval [1 ; N] 20 | { 21 | std::cout << "The primes are: \n"; 22 | 23 | for (int i = 2; i < number; i++) { 24 | if (IsPrime(i)) { 25 | cout << i << endl; 26 | } 27 | } 28 | } 29 | 30 | //Function whether the number N can be expressed as a sum of two prime numbers 31 | bool CheckSumOfTwoPrimes(int number) 32 | { 33 | int size = number / 2; 34 | for (int i = 2; i <= size; i++) { 35 | if (IsPrime(i) && IsPrime(number - i)) { 36 | return true; 37 | } 38 | } 39 | return false; 40 | } -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task14.cpp: -------------------------------------------------------------------------------- 1 | long long SumOfDigits(long long n) { 2 | long long sumOfDigits = 0; 3 | while (n != 0) { 4 | sumOfDigits += n % 10; 5 | n /= 10; 6 | } 7 | return sumOfDigits; 8 | } 9 | 10 | int main() 11 | { 12 | long long n; 13 | std::cout << "Enter a number: " << std::endl; 14 | std::cin >> n; 15 | 16 | while (n >= 10) { 17 | std::cout << n << " -> "; 18 | n = sumOfDigits(n); 19 | } 20 | std::cout << n << std::endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /IS/Sem.04/Pract.04/Task15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Function to check whether a number is prime 4 | bool IsPrime(int number) 5 | { 6 | int size = sqrt(number); 7 | 8 | for (int i = 2; i <= size; i++) 9 | { 10 | if (number % i == 0) { 11 | return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | 18 | //PrimeFactorization 19 | void PrimeFactor(int number) 20 | { 21 | for (int i = 2; i <= number; i++) { 22 | if (IsPrime(i) && number % i == 0) { 23 | if (i == number) { //last step, no space 24 | std::cout << i; 25 | } else { 26 | std::cout << i << " "; 27 | } 28 | 29 | number /= i; 30 | i--; 31 | } 32 | } 33 | std::cout << std::endl; 34 | } -------------------------------------------------------------------------------- /IS/Sem.04/task1.cpp: -------------------------------------------------------------------------------- 1 | int Reverse(unsigned int n) 2 | { 3 | unsigned int result; 4 | short lastDigit; 5 | 6 | while (n != 0) 7 | { 8 | lastDigit = n % 10; 9 | (result *= 10) += lastDigit; 10 | n /= 10; 11 | } 12 | 13 | return result; 14 | } -------------------------------------------------------------------------------- /IS/Sem.04/task2.cpp: -------------------------------------------------------------------------------- 1 | int Reverse(unsigned int n) 2 | { 3 | unsigned int result; 4 | short lastDigit; 5 | 6 | while (n != 0) 7 | { 8 | lastDigit = n % 10; 9 | (result *= 10) += lastDigit; 10 | n /= 10; 11 | } 12 | 13 | return result; 14 | } 15 | 16 | bool IsPalindrome(unsigned int n) 17 | { 18 | return n == reverse(n); 19 | } -------------------------------------------------------------------------------- /IS/Sem.04/task3.cpp: -------------------------------------------------------------------------------- 1 | bool AreDigitsSortedAscending(unsigned int n) 2 | { 3 | while (n >= 10) 4 | { 5 | unsigned int lastDigit = n % 10; 6 | unsigned int prevLastDigit = (n / 10) % 10; 7 | 8 | if (prevLastDigit > lastDigit) { 9 | return false; 10 | } 11 | 12 | n /= 10; 13 | } 14 | 15 | return true; 16 | } 17 | 18 | bool AreDigitsSortedDescending(unsigned int n) 19 | { 20 | while (n >= 10) 21 | { 22 | unsigned int lastDigit = n % 10; 23 | iunsigned intnt prevLastDigit = (n / 10) % 10; 24 | 25 | if (prevLastDigit < lastDigit) { 26 | return false; 27 | } 28 | 29 | n /= 10; 30 | } 31 | 32 | return true; 33 | } 34 | 35 | bool AreDigitsSorted(unsigned int n) 36 | { 37 | return AreDigitsSortedDescending(n) || AreDigitsSortedAscending(n); 38 | } -------------------------------------------------------------------------------- /IS/Sem.05/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * A iterative binary search function. 3 | * It returns location of x in given array arr[l..r] if present, 4 | * otherwise -1. 5 | */ 6 | 7 | /* 8 | * int l - left side index 9 | * int r - right side index 10 | * int m - middle index 11 | * int x - number 'x' we are searching for 12 | */ 13 | int BinarySearch(const int *arr, int size, int x) 14 | { 15 | int l = 0, r = size - 1, m; 16 | while (l <= r) 17 | { 18 | int m = l + (r - l) / 2; 19 | if (arr[m] == x) { // Check if x is present at mid 20 | return m; 21 | } else if (arr[m] < x) { // If x greater, ignore left half 22 | l = m + 1; 23 | } else { // If x is smaller, ignore right half 24 | r = m - 1; 25 | } 26 | } 27 | return -1; // if we reach here, then the element was not present 28 | } -------------------------------------------------------------------------------- /IS/Sem.05/EratosthenesSieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned MaxSize = 1000; 4 | 5 | void InitArray(bool* arr, unsigned size, bool value) 6 | { 7 | for (unsigned i = 0; i < size; i++) { 8 | arr[i] = value; 9 | } 10 | } 11 | 12 | void EratosthenesSieve(bool* arr, unsigned size) 13 | { 14 | arr[0] = arr[1] = false; 15 | 16 | for (unsigned i = 2; i < sqrt(size); i++) 17 | { 18 | if (!arr[i]) { //ако числото е задраскано 19 | continue; 20 | } 21 | 22 | for (unsigned j = i * i; j < size; j += i) { 23 | arr[j] = false; //задраскваме 24 | } 25 | } 26 | } 27 | 28 | void PrintSieve(const bool* arr, unsigned size) 29 | { 30 | for (unsigned i = 0; i < size; i++) 31 | { 32 | if (arr[i]) { 33 | std::cout << i << " "; 34 | } 35 | } 36 | } 37 | 38 | int main() 39 | { 40 | bool buffer[MaxSize]; 41 | 42 | int n; 43 | std::cin >> n; 44 | 45 | InitArray(buffer, n, true); 46 | EratosthenesSieve(buffer, n); 47 | PrintSieve(buffer, n); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /IS/Sem.05/InputArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void InputArray(int* arr,const int size) 4 | { 5 | for (int i = 0; i < size; i++) { 6 | std::cin >> arr[i]; 7 | } 8 | } -------------------------------------------------------------------------------- /IS/Sem.05/LinearSerach.cpp: -------------------------------------------------------------------------------- 1 | int LinearSearch(const int* arr, int size, int number) 2 | { 3 | for(int i = 0; i < size; i++) { 4 | if(arr[i] == number) { 5 | return i; 6 | } 7 | } 8 | 9 | return -1; // if we reach here, then the element was not present 10 | } -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/README.md: -------------------------------------------------------------------------------- 1 | ## Задачи 2 | 3 | 1. Напишете програма, която намира първия повтарящ се елемент в редица от N числа.
4 | 5 | 2. Напишете програма, която подрежда елементите на редица от N числа, като първите елементи са четните, вторите - нечетните.
6 | Бонус: Сортирайте четните във възходящ ред,а нечетните в низходящ ред(отново четните са първи,а нечетните - втори).
7 | 8 | 3. Напишете програма, която превръща дадено естествено число в двоична бройна система.
9 | 10 | 4. Напишете програма, която извежда дали едно число е палиндром.
11 | 12 | 5. Напишете функция, която по два въведени масива принтира в конзолата тяхното сечение.
13 | **Бонус:** Напишете функция, която принтира в конзолата сечението на два масива, които са предварително сортирани. Функцията трябва да прави най-много по едно обхождане на всеки от масивите! 14 | 15 | 6. Напишете програма, която подрежда елементите на редица от N числа по следния начин:
16 | а1 > а2 < а3 > а4 <(...)
17 | 18 | 7. Напишете функция, която приема матрица NxN и транспонира матрицата. 19 | 20 | 8. Напишете функция, която приема матрица NxM и матрица MxK и умножава първата матрица по втората. 21 | 22 | ## Tic-Tac-Toe 23 | 24 | UI | CLI 25 | :-------------------------:|:-------------------------: 26 | ![tic-tac-toe-UI](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1jp4/fe212fc1d9d90b12907f5b6b04fa9280/android-ios-tic-tac-toe.png?fm=avif&w=500&h=600&fit=) | ![tic-tac-toe-CLI](https://user-images.githubusercontent.com/44736064/62582421-bd23a580-b882-11e9-92d8-ba0dced4bfb4.png) 27 | 28 | ### [Онлайн версия на играта](https://playtictactoe.org) 29 |
30 | 31 | Да се реализира в **конзолна** играта **Тic-Tac-Toe**(морски шах).
32 | Играта започва с празна дъска, като на всяка стъпка: 33 | - единият играч **въвежда две числа** - **ред и колона**, които са неговия ход на дъската(Например: **1 3**). 34 | - след всеки ход се актуализира конкретното състояние на играта. 35 | 36 | Накрая се показва кой е спечелил играта. 37 | -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | void arrInpur(int *arr, unsigned size); 6 | 7 | int search (int *arr, unsigned size, int number); 8 | 9 | int main() 10 | { 11 | int num; 12 | int arr[5]; 13 | arrInput(arr, 5); 14 | 15 | cin >> num; 16 | cout << search(arr, 5, num); 17 | 18 | return 0; 19 | } 20 | 21 | 22 | void arrInput(int* arr, unsigned size) 23 | { 24 | for (int i = 0; i < size; i++) { 25 | cin >> arr[i]; 26 | } 27 | } 28 | 29 | int search(int* arr, unsigned size, int number) 30 | { 31 | for (int i = 0; i < size; i++) { 32 | if (arr[i] == number) { 33 | return 1; 34 | } 35 | } 36 | return -1; 37 | } 38 | -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task02.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 | void ResetChanges(int &index, bool &changesOccured) 11 | { 12 | index = -1; 13 | changesOccured = false; 14 | } 15 | 16 | bool ChangeCurrentIndex(int index, int ¤tIndex) 17 | { 18 | return currentIndex = index; 19 | } 20 | 21 | void SortNumbers(int *arr, size_t size) 22 | { 23 | int counter = NULL, min, max, currentIndex; 24 | bool sortingEvenNumbers = true, changeOccured; 25 | 26 | do 27 | { 28 | if (sortingEvenNumbers) { 29 | min = INT_MAX; //reset min 30 | } else { 31 | max = INT_MIN; //reset max 32 | } 33 | 34 | ResetChanges(currentIndex, changeOccured);//reset change flag & index 35 | 36 | for (size_t i = counter; i < size; i++) { 37 | if (sortingEvenNumbers) { 38 | if (arr[i] % 2 == 0 && arr[i] < min) { 39 | min = arr[i]; 40 | changeOccured = ChangeCurrentIndex(i, currentIndex); 41 | } 42 | } else { 43 | if (arr[i] % 2 != 0 && arr[i] > max) { 44 | max = arr[i]; 45 | changeOccured = ChangeCurrentIndex(i, currentIndex); 46 | } 47 | } 48 | } 49 | 50 | if (!changeOccured && sortingEvenNumbers) { 51 | sortingEvenNumbers = !sortingEvenNumbers; 52 | changeOccured = true; 53 | } else if(changeOccured) { 54 | Swap(arr[counter], arr[currentIndex]); 55 | counter++; 56 | } 57 | } while(changeOccured); 58 | } 59 | -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task03.cpp: -------------------------------------------------------------------------------- 1 | long long ToBinary(unsigned number) 2 | { 3 | int buffer[MAX_POWTWO_SIZE]; 4 | int size = 0; 5 | 6 | while (number > 0) { 7 | buffer[size] = number % 2; 8 | number /= 2; 9 | size++; 10 | } 11 | 12 | long long convertedToBinary = 0; 13 | long long multiply = 1; 14 | for (unsigned i = 0; i < size; i++) { 15 | convertedToBinary += buffer[i] * multiply; 16 | multiply *= 10; 17 | } 18 | 19 | return convertedToBinary; 20 | } 21 | -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void arrInput(int *arr, unsigned size); 4 | bool isPolindrom(int *arr, unsigned size); 5 | int main() 6 | { 7 | int arr[5]; 8 | arrInput(arr, 5); 9 | cout << (isPolindrom(arr, 5) ? "True" : "False") << endl; 10 | 11 | return 0; 12 | } 13 | 14 | void arrInput(int *arr, unsigned size) 15 | { 16 | for(int i = 0; i < size; i++) 17 | { 18 | cin >> arr[i]; 19 | } 20 | } 21 | 22 | bool isPolindrom(int *arr, unsigned size) 23 | { 24 | for(int i = 0; i < size; i++) 25 | { 26 | if(arr[i] != arr[size - 1 - i]) 27 | { 28 | return false; 29 | } 30 | } 31 | return true; 32 | } -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void arrInput(int *arr, unsigned size); 4 | void arrCut(int *arr1, int *arr2, unsigned size1, unsigned size2); 5 | int main() 6 | { 7 | int arr1[5]; 8 | int arr2[5]; 9 | arrInput(arr1, 5); 10 | arrInput(arr2, 5); 11 | arrCut(arr1, arr2, 5, 5); 12 | 13 | return 0; 14 | } 15 | 16 | void arrInput(int *arr, unsigned size) 17 | { 18 | for(int i = 0; i < size; i++) 19 | { 20 | cin >> arr[i]; 21 | } 22 | } 23 | 24 | void arrCut(int *arr1, int *arr2, unsigned size1, unsigned size2) 25 | { 26 | for(int i = 0; i < size1; i++) 27 | { 28 | for(int j = 0; j < size1; j++) 29 | { 30 | if(arr1[i] == arr2[j]) 31 | { 32 | cout << arr1[i] << " "; 33 | } 34 | } 35 | } 36 | cout << endl; 37 | } -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void arrInput(int *arr, unsigned size); 4 | void swap(int &num1, int &num2); 5 | void sort(int *arr, unsigned size); 6 | void arrOutput(int *arr, unsigned size); 7 | 8 | int main() 9 | { 10 | int arr[5]; 11 | arrInput(arr, 5); 12 | sort(arr, 5); 13 | arrOutput(arr, 5); 14 | return 0; 15 | } 16 | 17 | void arrInput(int *arr, unsigned size) 18 | { 19 | for(int i = 0; i < size; i++) 20 | { 21 | cin >> arr[i]; 22 | } 23 | } 24 | 25 | void swap(int &num1, int &num2) 26 | { 27 | int num3 = num2; 28 | num2 = num1; 29 | num1 = num3; 30 | } 31 | 32 | void sort(int *arr, unsigned size) 33 | { 34 | for(int i = 0; i < size - 1; i++) 35 | { 36 | if(i % 2 == 0) 37 | { 38 | if(arr[i] < arr[i + 1]) 39 | { 40 | swap(arr[i], arr[i + 1]); 41 | } 42 | } 43 | else 44 | { 45 | if(arr[i] > arr[i+1]) 46 | { 47 | swap(arr[i], arr[i + 1]); 48 | } 49 | } 50 | } 51 | } 52 | 53 | void arrOutput(int *arr, unsigned size) 54 | { 55 | for(int i = 0; i < size; i++) 56 | { 57 | cout << arr[i] << " "; 58 | } 59 | cout << endl; 60 | } 61 | -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void Transponate(int matrix[][3], int rows, int cols); 3 | int main() 4 | { 5 | const int rows = 3; 6 | const int cols = 3; 7 | int matrix[rows][cols]{ {1,2,3}, {4,5,6}, {7,8,9} }; 8 | 9 | Transponate(matrix, rows, cols); 10 | 11 | //print 12 | for (int i = 0; i < rows; i++) 13 | { 14 | for (int j = 0; j < cols; j++) 15 | { 16 | std::cout << matrix[i][j] << " "; 17 | } 18 | std::cout << std::endl; 19 | } 20 | 21 | } 22 | //how to pass correctly a matrix as a parameter 23 | void Transponate(int matrix[][3], int rows, int cols) 24 | { 25 | for (int i = 0; i < rows - 1; i++) 26 | { 27 | for (int j = i + 1; j < cols; j++) 28 | { 29 | int tmp = matrix[i][j]; 30 | matrix[i][j] = matrix[j][i]; 31 | matrix[j][i] = tmp; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /IS/Sem.05/Pract.05/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void MultiplyMatrixs(int matrixA[][3], int matrixB[][4], int matrixRezult[][4], int N, int M, int K); 3 | 4 | int main() 5 | { 6 | const int N = 2; 7 | const int M = 3; 8 | const int K = 4; 9 | int matrixA[N][M]{ {1,2,3}, {4,5,6} }; 10 | int matrixB[M][K]{ {5,7,3,4}, {7,5,6,6},{1,2,5,3} }; 11 | int rezult[N][K]; 12 | MultiplyMatrixs(matrixA,matrixB,rezult, N,M,K); 13 | 14 | //print 15 | for (int i = 0; i < N; i++) 16 | { 17 | for (int j = 0; j < K; j++) 18 | { 19 | std::cout << rezult[i][j]<<" "; 20 | } 21 | std::cout << std::endl; 22 | } 23 | } 24 | //how to set M and K here??? 25 | void MultiplyMatrixs(int matrixA[][3], int matrixB[][4], int matrixRezult[][4], int N, int M, int K) 26 | { 27 | for (int i = 0; i < N; i++) 28 | { 29 | for (int j = 0; j < K; j++) 30 | { 31 | int currElement = 0; 32 | for (int l = 0; l < M; l++) 33 | { 34 | currElement += matrixA[i][l] * matrixB[l][j]; 35 | } 36 | matrixRezult[i][j] = currElement; 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /IS/Sem.05/PrintArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printArray(const int* arr, int size) 4 | { 5 | for (int i = 0; i < size; i++) { 6 | std::cout << arr[i] << std::endl; 7 | } 8 | } -------------------------------------------------------------------------------- /IS/Sem.05/ReverseArray.cpp: -------------------------------------------------------------------------------- 1 | void Swap(int &f, int &s) 2 | { 3 | int temp = f; 4 | f = s; 5 | s = temp; 6 | } 7 | 8 | void ReverseArray(int* arr, int size) 9 | { 10 | for (int i = 0; i < size / 2; i++) { 11 | Swap(arr[i], arr[size - 1 - i]); 12 | } 13 | } -------------------------------------------------------------------------------- /IS/Sem.06/BitwiseSimpleExamples.cpp: -------------------------------------------------------------------------------- 1 | bool isEven(unsigned n) 2 | { 3 | return !(n & 1); 4 | } 5 | 6 | unsigned powerOfTwo(unsigned n) 7 | { 8 | return (1 << n); 9 | } -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int SIZE = 20; 4 | const char mask[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 5 | 6 | int convertFromDecimalAndGiveLength(int num, int systemCode, char* buffer) { 7 | int count = 0; 8 | 9 | while (num != 0) { 10 | buffer[count++] = mask[num % systemCode];; 11 | num /= systemCode; 12 | } 13 | 14 | return count; 15 | } 16 | 17 | int convertToDecimal(int number, int systemCode) { 18 | int sum = 0; 19 | int power = 0; 20 | while (number != 0) { 21 | int remainder = number % 10; 22 | number /= 10; 23 | sum = sum + remainder * std::pow(systemCode, power++); 24 | } 25 | 26 | return sum; 27 | } 28 | 29 | void printNumbers(char* arrayNums, int length) { 30 | for (int i = length - 1; i >= 0; i--) { 31 | std::cout << (char)arrayNums[i]; 32 | } 33 | } 34 | 35 | void convertToASystem(int number, int currentCode, int desiredCode, char* buffer) { 36 | int numberDecimal = convertToDecimal(number, currentCode); 37 | int lengthOfBuffer = convertFromDecimalAndGiveLength(numberDecimal, desiredCode, buffer); 38 | printNumbers(buffer, lengthOfBuffer); 39 | } 40 | 41 | int main() 42 | { 43 | char buffer[SIZE]; 44 | int number = 1311; 45 | convertToASystem(number, 10, 16, buffer); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task02.cpp: -------------------------------------------------------------------------------- 1 | //Solution by Georgi Terziev 2 | 3 | int countOnes(int number) { 4 | int x = 1; 5 | 6 | while (!(number & x)) { 7 | x <<= 1; 8 | } 9 | 10 | return number ^ x; 11 | } 12 | -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int GetNum(int n) 4 | { 5 | return n & (n - 1); 6 | } 7 | 8 | int main() 9 | { 10 | int n; 11 | std::cin >> n; 12 | std::cout << GetNum(n); 13 | } 14 | -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task04.cpp: -------------------------------------------------------------------------------- 1 | //Solution by Dejan Svetoslavov 2 | 3 | int find_last_k_bits_number(int number, int last_k_bits) { 4 | const int bit_one = 1; 5 | int bit_mask = 0; 6 | while (last_k_bits > 0) { 7 | bit_mask <<= 1; 8 | bit_mask = bit_mask | bit_one; 9 | last_k_bits--; 10 | } 11 | 12 | return number & bit_mask; 13 | } -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task05.cpp: -------------------------------------------------------------------------------- 1 | //Solution by Georgi Terziev 2 | 3 | int f(int n, int k) { 4 | while (n != 0) { 5 | if ((n & k) == k) { 6 | return true; 7 | } 8 | 9 | n >>= 1; 10 | } 11 | 12 | return false; 13 | } 14 | -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task06.cpp: -------------------------------------------------------------------------------- 1 | int checkUniqueNumber(const int* arr, int size) 2 | { 3 | int uniqueNum = 0; 4 | for (int i = 0; i < size; i++){ 5 | uniqueNum ^= arr[i]; 6 | } 7 | return uniqueNum; 8 | } 9 | -------------------------------------------------------------------------------- /IS/Sem.06/Pract.06/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int powerOfTwo(int exp) 4 | { 5 | return (1 << exp); 6 | } 7 | 8 | void getSubNumber(const int* arr, int size, int mask) 9 | { 10 | for (int i = 0; i < size; i++) { 11 | if (mask & 1) { 12 | std::cout << arr[i] << " "; 13 | } 14 | mask >>= 1; 15 | } 16 | } 17 | 18 | void printSubNumbers(const int* arr, int size) 19 | { 20 | int length = powerOfTwo(size); 21 | for (int i = 0; i < length; i++) { 22 | std::cout << "[ "; 23 | getSubNumber(arr, size, i); 24 | std::cout << "]"<< " "; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/References/Task01.cpp: -------------------------------------------------------------------------------- 1 | void Swap(int& a, int& b) 2 | { 3 | int temp = a; 4 | a = b; 5 | b = temp; 6 | } 7 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/References/Task02.cpp: -------------------------------------------------------------------------------- 1 | void ToUpper(char& letter) 2 | { 3 | letter -= 'a' - 'A'; 4 | } 5 | 6 | void ToLower(char& letter) 7 | { 8 | letter += 'a' - 'A'; 9 | } 10 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/References/Task09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int inverse(int num) { 4 | int inversedNum = 0; 5 | while (num != 0) { 6 | (inversedNum *= 10) += num % 10; 7 | num /= 10; 8 | } 9 | return inversedNum; 10 | } 11 | int generateLast(int& num, int suffixLen) { 12 | int suffix = 0; 13 | for (int i = 0; i < suffixLen; i++) { 14 | (suffix *= 10) += num % 10; 15 | num /= 10; 16 | } 17 | suffix = inverse(suffix); 18 | return suffix; 19 | } 20 | 21 | void concat(int a, int& b) { 22 | int bCopy = b; 23 | int bLen = 0; 24 | while (b != 0) { 25 | bLen++; 26 | b /= 10; 27 | } 28 | for (int i = 0; i < bLen; i++) { 29 | a *= 10; 30 | } 31 | b = a + bCopy; 32 | } 33 | int main() { 34 | int a, b, k; 35 | std::cin >> a >> b >> k; 36 | int lastk = generateLast(a, k); 37 | concat(lastk, b); 38 | std::cout << a << " " << b; 39 | } -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/Strings/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void replaceSymbols(char* arr, char x, char a, char b) 5 | { 6 | int index = 0; 7 | int occurances = 0; 8 | 9 | while(arr[index] != '\0') 10 | { 11 | if (arr[index] == x) 12 | { 13 | (occurances % 2 == 0) ? arr[index] = a : arr[index] = b; 14 | occurances++; 15 | } 16 | 17 | index++; 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | char arr[] = "H_ll_ my fri_nd"; 24 | replaceSymbols(arr, '_', 'e', 'o'); 25 | 26 | cout << arr; 27 | } 28 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/Strings/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int SIZE = 1024; 3 | int compareStrings(const char* firstString, const char* secondString) 4 | { 5 | size_t index = 0; 6 | while (firstString[index] == secondString[index]) 7 | { 8 | if (firstString[index] == '\0') 9 | { 10 | return 0; 11 | } 12 | index++; 13 | } 14 | return firstString[index] - secondString[index] < 0 ? -1 : 1; 15 | } 16 | 17 | int main() 18 | { 19 | char firstString[SIZE]; 20 | char secondString[SIZE]; 21 | std::cin >> firstString >> secondString; 22 | std::cout << compareStrings(firstString, secondString); 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/Strings/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int MAX_SIZE = 100; 3 | const int SIZE_SYMBOLS = 128; 4 | 5 | void PrintWithoutRepeating(char* str) 6 | { 7 | int symbolsCount[SIZE_SYMBOLS] = {}; 8 | 9 | char newStr[MAX_SIZE] = {}; 10 | int indexForNewStr = 0; 11 | 12 | for (int i = 0; i < MAX_SIZE; i++) 13 | { 14 | if (str[i] == '\0') 15 | { 16 | break; 17 | } 18 | if (symbolsCount[str[i]] == -1) 19 | { 20 | continue; 21 | } 22 | newStr[indexForNewStr] = str[i]; 23 | symbolsCount[str[i]] = -1; 24 | indexForNewStr++; 25 | } 26 | 27 | std::cout << newStr; 28 | } 29 | 30 | int main() 31 | { 32 | char str[MAX_SIZE]; 33 | std::cin >> str; 34 | 35 | PrintWithoutRepeating(str); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/Strings/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MaxLength = 1024; 4 | const int UpperCaseDifferance = 'A' - 'a'; 5 | 6 | int GetLenght(const char* str) 7 | { 8 | size_t index = 0; 9 | while (str[index] != '\0') { 10 | index++; 11 | } 12 | 13 | return index; 14 | } 15 | 16 | void Replace(char* str, size_t start, size_t end) 17 | { 18 | for (size_t i = start; i < end; i++) { 19 | str[i] = '*'; 20 | } 21 | } 22 | 23 | bool FindSubstring(const char* text,const char* substring, size_t substringLength, size_t currentLetter) 24 | { 25 | for (size_t j = 0; j < substringLength; j++) { 26 | int textDiferance = substring[j] - text[currentLetter + j]; 27 | 28 | if (textDiferance != 0 && textDiferance != UpperCaseDifferance && textDiferance != -UpperCaseDifferance) { 29 | return false; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | void ReplacedSubstringInText(char* text,const char* substring, size_t textLength, size_t substringLength) { 36 | for (size_t i = 0; i < textLength; i++) { 37 | int textDiferance = text[i] - substring[0]; 38 | if (textDiferance == 0 || textDiferance == UpperCaseDifferance || textDiferance == -UpperCaseDifferance) { 39 | if (FindSubstring(text, substring, substringLength, i)) { 40 | Replace(text, i, i + substringLength); 41 | i += substringLength - 1; 42 | } 43 | } 44 | } 45 | } 46 | 47 | int main() 48 | { 49 | char substring[MaxLength]; 50 | char text[MaxLength]; 51 | std::cin.getline(text, MaxLength); 52 | std::cin.getline(substring, MaxLength); 53 | int substringLength = GetLenght(substring); 54 | int textLength = GetLenght(text); 55 | ReplacedSubstringInText(text, substring, textLength, substringLength); 56 | std::cout << text; 57 | } 58 | -------------------------------------------------------------------------------- /IS/Sem.07/Pract.07/Strings/task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_SIZE = 1024; 4 | 5 | void findMaxPrefix(const char *first, const char *second, const char *third, char *result) { 6 | int index = 0; 7 | while(first[index] == second[index] && second[index] == third[index]) { 8 | index++; 9 | } 10 | for(int i = 0; i < index; i++) { 11 | result[i] = first[i]; 12 | } 13 | result[index] = '\0'; 14 | } 15 | 16 | int main() { 17 | char first[MAX_SIZE], second[MAX_SIZE], third[MAX_SIZE]; 18 | std::cin.getline(first, MAX_SIZE); 19 | std::cin.getline(second, MAX_SIZE); 20 | std::cin.getline(third, MAX_SIZE); 21 | 22 | char result[MAX_SIZE]; 23 | findMaxPrefix(first, second, third, result); 24 | std::cout << result << std::endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/getCharOccurrencesCount.cpp: -------------------------------------------------------------------------------- 1 | size_t getCharOccurrencesCount(const char* text, char symbol) { 2 | size_t occurrences = 0; 3 | size_t index = 0; 4 | while (text[index] != '\0') { 5 | if (text[index] == symbol) { 6 | occurrences++; 7 | } 8 | index++; 9 | } 10 | 11 | return occurrences; 12 | } -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/myAtoi.cpp: -------------------------------------------------------------------------------- 1 | bool isDigit(char symbol) { 2 | return '0' <= symbol && symbol <= '9'; 3 | } 4 | 5 | bool isValidNumber(const char* number) { 6 | size_t length = myStrLen(number); 7 | 8 | if (length == 0) { 9 | return false; 10 | } 11 | 12 | if (length == 1 && number[0] == '-') { 13 | return false; 14 | } 15 | 16 | size_t index = number[0] == '-' ? 1 : 0; 17 | 18 | while (index < length) 19 | { 20 | if (!isDigit(number[index])) { 21 | return false; 22 | } 23 | 24 | index++; 25 | } 26 | 27 | return true; 28 | } 29 | 30 | // without strlen 31 | bool isValidNumberV2(const char* number) { 32 | if (number[0] == '\0') { 33 | return false; 34 | } 35 | 36 | if (number[0] == '-' && number[1] == '\0') { 37 | return false; 38 | } 39 | 40 | size_t index = number[0] == '-' ? 1 : 0; 41 | 42 | while (number[index] != '\0') { 43 | if (!isDigit(number[index])) { 44 | return false; 45 | } 46 | 47 | index++; 48 | } 49 | 50 | return true; 51 | } 52 | 53 | int myAtoi(const char* number) { 54 | if (!isValidNumber(number)) { 55 | return 0; 56 | } 57 | 58 | bool isNegative = number[0] == '-'; 59 | size_t index = isNegative ? 1 : 0; 60 | int result = 0; 61 | 62 | while (number[index] != '\0') { 63 | result = result * 10 + (number[index] - '0'); 64 | index++; 65 | } 66 | 67 | return isNegative ? -result : result; 68 | } -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/myStrCat.cpp: -------------------------------------------------------------------------------- 1 | // Version 1 2 | // Concatenation done in additional array 3 | void myStrCat(const char* str1, const char* str2, char* result) { 4 | size_t str1Length = myStrLen(str1); 5 | size_t str2Length = myStrLen(str2); 6 | 7 | for (size_t i = 0; i < str1Length; i++) { 8 | result[i] = str1[i]; 9 | } 10 | 11 | for (size_t i = 0; i < str2Length; i++) { 12 | result[str1Length + i] = str2[i]; 13 | } 14 | 15 | result[str1Length + str2Length] = '\0'; 16 | } 17 | 18 | // Version 2 19 | // Concatenation done in destination (as in stl strcat) 20 | void my_strcat(char* dest, const char* source) 21 | { 22 | size_t destLength = myStrLen(dest); 23 | size_t sourceLength = myStrLen(source); 24 | 25 | for (size_t i = 0; i < sourceLength; i++) { 26 | dest[destLength + i] = source[i]; 27 | } 28 | 29 | dest[destLength + sourceLength] = '\0'; 30 | } 31 | 32 | // Version 1 - without strLen 33 | // Concatenation done in additional array 34 | void myStrCatV2(const char* str1, const char* str2, char* result) { 35 | size_t resultIndex = 0, sourceIndex = 0; 36 | while (str1[sourceIndex] != '\0') { 37 | result[resultIndex++] = str1[sourceIndex++]; 38 | } 39 | 40 | sourceIndex = 0; 41 | while (str2[sourceIndex] != '\0') { 42 | result[resultIndex++] = str2[sourceIndex++]; 43 | } 44 | 45 | result[resultIndex] = '\0'; 46 | } 47 | 48 | // Version 2 - without strLen 49 | // Concatenation done in destination (as in stl strcat) 50 | void my_strcatV2(char* dest, const char* source) 51 | { 52 | size_t destIndex = 0; 53 | while(dest[destIndex] != '\0') { 54 | destIndex++; 55 | } 56 | 57 | size_t sourceIndex = 0; 58 | while (source[sourceIndex] != '\0') { 59 | dest[destIndex++] = source[sourceIndex++]; 60 | } 61 | 62 | dest[destIndex] = '\0'; 63 | } 64 | -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/myStrCmp.cpp: -------------------------------------------------------------------------------- 1 | int myStrCmp(const char* str1, const char* str2) { 2 | size_t i = 0; 3 | 4 | while (str1[i] == str2[i]) { 5 | if (str1[i] == '\0'/* && str2[i] == '\0'*/) { 6 | return 0; 7 | } 8 | 9 | i++; 10 | } 11 | 12 | return str1[i] - str2[i] > 0 ? 1 : -1; 13 | } -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/myStrCpy.cpp: -------------------------------------------------------------------------------- 1 | void myStrCopy(char* dest, const char* source) { 2 | size_t index = 0; 3 | while (source[index] != '\0') { 4 | dest[index] = source[index]; 5 | index++; 6 | } 7 | 8 | dest[index] = '\0'; 9 | } -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/myStrLen.cpp: -------------------------------------------------------------------------------- 1 | size_t myStrLen(const char* str) { 2 | size_t index = 0; 3 | 4 | while (str[index] != '\0') { 5 | index++; 6 | } 7 | 8 | return index; 9 | } -------------------------------------------------------------------------------- /IS/Sem.07/Solutions/searchInText.cpp: -------------------------------------------------------------------------------- 1 | bool searchInText(const char text[], const char word[]) { 2 | size_t textIndex = 0; 3 | 4 | while (text[textIndex] != '\0') { 5 | if (text[textIndex] == word[0]) { 6 | textIndex++; 7 | size_t wordIndex = 1; 8 | bool contains = true; 9 | while (word[wordIndex] != '\0') { 10 | if (text[textIndex] != word[wordIndex]) { 11 | contains = false; 12 | break; 13 | } 14 | 15 | textIndex++; 16 | wordIndex++; 17 | } 18 | 19 | if (contains) { 20 | return true; 21 | } 22 | } 23 | else { 24 | textIndex++; 25 | } 26 | } 27 | 28 | return false; 29 | } -------------------------------------------------------------------------------- /IS/Sem.07/images/example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.07/images/example1.png -------------------------------------------------------------------------------- /IS/Sem.07/images/example2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.07/images/example2.png -------------------------------------------------------------------------------- /IS/Sem.07/images/right-to-left.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.07/images/right-to-left.jfif -------------------------------------------------------------------------------- /IS/Sem.07/images/spiral-rule.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.07/images/spiral-rule.jfif -------------------------------------------------------------------------------- /IS/Sem.08/Pract.08/FileExamples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const char UPPER_FOLDER[] = "../test.txt"; 5 | 6 | const char FILE_NAME[] = "PractFiles.cpp"; 7 | 8 | const int BUFFER_SIZE = 1024; 9 | 10 | // read the source code from your cpp and print in console 11 | void printSourceCode() { 12 | std::ifstream in(FILE_NAME); 13 | 14 | if (!in.is_open()) { 15 | std::cout << "Error"; 16 | return -1; 17 | } 18 | 19 | while (!in.eof()) { 20 | char buffer[BUFFER_SIZE]; 21 | in.getline(buffer, BUFFER_SIZE); 22 | std::cout << buffer; 23 | } 24 | 25 | in.close(); 26 | } 27 | 28 | void readCharArrayWithSpaces() { 29 | std::ofstream out(FILE_NAME); 30 | 31 | char test[] = "abc de"; 32 | out << test; 33 | 34 | out.close(); 35 | 36 | std::ifstream in(FILE_NAME); 37 | 38 | char test2[BUFFER_SIZE]; 39 | in.getline(test2, BUFFER_SIZE); 40 | 41 | in.close(); 42 | } 43 | -------------------------------------------------------------------------------- /IS/Sem.08/Pract.08/ProjectHelperFunctions.cpp: -------------------------------------------------------------------------------- 1 | // Clear console 2 | void clearConsole() { 3 | cout << "\033[;H"; // Moves cursor to the top left 4 | cout << "\033[J"; // Clears the console 5 | } 6 | 7 | // Remove all error flags and characters from the input buffer 8 | void clearInputBuffer() { 9 | // because of using both getline and cin we have to cin.ignore; 10 | // cin leaves the newline character in the stream which will be read as input from the getline 11 | 12 | cin.clear(); // clears errors flags from the cin 13 | cin.sync(); // discard unread characters from the input buffer 14 | cin.ignore(); // discard characters from the input buffer 15 | } -------------------------------------------------------------------------------- /IS/Sem.08/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int* CreateArray(size_t N) 4 | { 5 | int* arr = new int[N]; 6 | 7 | for (size_t i = 0; i < N; i++) { 8 | arr[i] = NULL; 9 | } 10 | 11 | return arr; 12 | } 13 | 14 | void Initialize(int* arr, size_t size) 15 | { 16 | for (size_t i = 0; i < size; i++) { 17 | cin >> arr[i]; 18 | } 19 | } 20 | 21 | void Print(const int* arr, size_t size) 22 | { 23 | for (size_t i = 0; i < size; i++) { 24 | std::cout << arr[i] << ' '; 25 | } 26 | } 27 | 28 | void Free(int* arr) 29 | { 30 | delete[] arr; 31 | } 32 | 33 | int main() 34 | { 35 | int N; cin >> N; 36 | int *arr = CreateArray(N); 37 | Initialize(arr, N); 38 | Print(arr, N); 39 | Free(arr); 40 | } -------------------------------------------------------------------------------- /IS/Sem.08/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int** CreateMatrix(size_t rows, size_t columns) 4 | { 5 | int** matrix = new int*[rows]; 6 | 7 | for (size_t i = 0; i < rows; i++) { 8 | matrix[i] = new int[columns]; 9 | } 10 | 11 | for (size_t i = 0; i < rows; i++) { 12 | for (size_t y = 0; y < columns; y++) { 13 | matrix[i][y] = NULL; //default initialization 14 | } 15 | } 16 | 17 | return matrix; 18 | } 19 | 20 | void Initialize(int** matrix, size_t rows, size_t columns) 21 | { 22 | for (size_t i = 0; i < rows; i++) { 23 | for (size_t y = 0; y < columns; y++) { 24 | std::cin >> matrix[i][y]; 25 | } 26 | } 27 | } 28 | void Print(int** matrix, size_t rows, size_t columns) 29 | { 30 | for (size_t i = 0; i < rows; i++) { 31 | for (size_t y = 0; y < columns; y++) { 32 | std::cout << matrix[i][y] << " "; 33 | } 34 | std::cout << std::endl; 35 | } 36 | } 37 | 38 | void Free(int** matrix, size_t rows) 39 | { 40 | for (size_t i = 0; i < rows; i++) { 41 | delete[] matrix[i]; 42 | } 43 | 44 | delete[] matrix; 45 | } -------------------------------------------------------------------------------- /IS/Sem.08/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int N, M, Q; 6 | std::cin >> N >> M >> Q; 7 | 8 | int*** matrix = new int** [N]; 9 | 10 | for (int i = 0; i < N; i++) { 11 | matrix[i] = new int* [M]; 12 | 13 | for (int j = 0; j < M; j++) { 14 | matrix[i][j] = new int[Q]; 15 | } 16 | } 17 | 18 | for (int i = 0; i < N; i++) { 19 | for (int j = 0; j < M; j++) { 20 | for (int k = 0; k < Q; k++) { 21 | std::cin >> matrix[i][j][k]; 22 | } 23 | } 24 | } 25 | 26 | for (int i = 0; i < N; i++) { 27 | for (int j = 0; j < M; j++) { 28 | delete[] matrix[i][j]; 29 | } 30 | delete[] matrix[i]; 31 | } 32 | delete[] matrix; 33 | } -------------------------------------------------------------------------------- /IS/Sem.08/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int** CreateMatrix(size_t size) 4 | { 5 | int** matrix = new int*[size]; 6 | 7 | for (size_t i = 0; i < size; i++) { 8 | matrix[i] = new int[size]; 9 | } 10 | 11 | for (size_t i = 0; i < size; i++) { 12 | for (size_t y = 0; y < size; y++) { 13 | std::cin >> matrix[i][y]; 14 | } 15 | } 16 | 17 | return matrix; 18 | } 19 | 20 | int SumAboveMainDiagonal(int** matrix, size_t size) 21 | { 22 | int sum = 0; 23 | for (size_t i = 0; i < size - 1; i++) { 24 | for (size_t j = i + 1; j < size; j++) { 25 | sum += matrix[i][j]; 26 | } 27 | } 28 | 29 | return sum; 30 | } 31 | 32 | int SumOfMainDiagonal(int **matrix, size_t size) 33 | { 34 | int sum = 0; 35 | for (size_t i = 0; i < size; i++) { 36 | sum += matrix[i][i]; 37 | } 38 | return sum; 39 | } -------------------------------------------------------------------------------- /IS/Sem.08/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | int** Transpose(int** matrix, size_t mRows, size_t mCol) 2 | { 3 | int rows = mRows; 4 | int columns = mCol; 5 | 6 | int** result = new int*[rows]; //result = transposition of matrix (columnsxrows) 7 | 8 | for (size_t i = 0; i < rows; i++) { 9 | result[i] = new int[columns]; 10 | } 11 | 12 | for (size_t i = 0; i < rows; i++) { 13 | for (size_t y = 0; y < columns; y++) { 14 | result[i][y] = matrix[y][i]; 15 | } 16 | } 17 | 18 | return result; 19 | } -------------------------------------------------------------------------------- /IS/Sem.08/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | int** CreateMatrix(size_t rows, size_t columns) 2 | { 3 | int** matrix = new int*[rows]; 4 | 5 | for (size_t i = 0; i < rows; i++) { 6 | matrix[i] = new int[columns]; 7 | } 8 | 9 | for (size_t i = 0; i < rows; i++) { 10 | for (size_t y = 0; y < columns; y++) { 11 | matrix[i][y] = NULL; //default initialization 12 | } 13 | } 14 | 15 | return matrix; 16 | } 17 | 18 | int** multiplyMatrix(int **A,size_t a_rows,size_t a_col, int **B,size_t b_rows,size_t b_col) //A && B are matrixes 19 | { 20 | int** multiply = CreateMatrix(a_rows, b_col); //a_rows x b_col -> all Zero's 21 | 22 | for (size_t i = 0; i < a_rows; i++) { 23 | for (size_t y = 0; y < b_col; y++) { 24 | for (size_t k = 0; k < a_rows; k++) { 25 | multiply[i][y] += A[i][k] * B[k][y]; 26 | } 27 | } 28 | } 29 | 30 | return multiply; 31 | } -------------------------------------------------------------------------------- /IS/Sem.08/images/Memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.08/images/Memory.png -------------------------------------------------------------------------------- /IS/Sem.08/images/exampleMemoryLeak.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.08/images/exampleMemoryLeak.png -------------------------------------------------------------------------------- /IS/Sem.08/memoryLeak.cpp: -------------------------------------------------------------------------------- 1 | //Line 1-8 are included in all .cpp files to check for memory leaks 2 | #define _CRTDBG_MAP_ALLOC 3 | #include 4 | #include 5 | 6 | #ifdef _DEBUG 7 | #define new new ( _NORMAL_BLOCK , __FILE__ , __LINE__ ) 8 | #endif 9 | 10 | int main() 11 | { 12 | _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //first line in main function 13 | } -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Bonus/Factoriel.cpp: -------------------------------------------------------------------------------- 1 | // What will be the result of each function? 2 | 3 | // the result will always be 0 4 | // because when N is 1 we should calculate fact with N * fact(--N) 5 | // --N will give us 0 6 | // => we have to calculate fact(0) so we return 1 7 | // but N * 1 is actually 0 * 1, because N is already decremented 8 | size_t fact(size_t N) { 9 | if (N == 0) { 10 | return 1; 11 | } 12 | 13 | return N * fact(--N); 14 | } 15 | 16 | // Infinite loop - we will always call fact with N 17 | // Because N-- decrements the number after we use it 18 | size_t fact(size_t N) { 19 | if (N == 0) { 20 | return 1; 21 | } 22 | 23 | return N * fact(N--); 24 | } 25 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Bonus/FibonacciWithWrapperFunc.cpp: -------------------------------------------------------------------------------- 1 | size_t _fibonacci(unsigned int n, size_t* cache = nullptr) { 2 | if (n == 0 || n == 1) { 3 | return n; 4 | } 5 | 6 | if (cache[n - 1] == 0) { 7 | cache[n - 1] = _fibonacci(n - 1, cache); 8 | } 9 | 10 | if (cache[n - 2] == 0) { 11 | cache[n - 2] = _fibonacci(n - 2, cache); 12 | } 13 | 14 | return cache[n - 1] + cache[n - 2]; 15 | } 16 | 17 | size_t fibonacci(unsigned int n) { 18 | size_t* cache = new size_t[n](); 19 | size_t result = _fibonacci(n, cache); 20 | delete[] cache; 21 | 22 | return result; 23 | } -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Bonus/FibonacciWithoutWrapperFunc.cpp: -------------------------------------------------------------------------------- 1 | size_t fibonacci(unsigned int n, size_t* cache = nullptr) { 2 | if (n == 0 || n == 1) { 3 | return n; 4 | } 5 | 6 | // create array only on the first function call of the recursion 7 | bool arrayCreationIteration = false; 8 | if (!cache) { 9 | cache = new size_t[n](); 10 | arrayCreationIteration = true; 11 | } 12 | 13 | if (cache[n - 1] == 0) { 14 | cache[n - 1] = fibonacci(n - 1, cache); 15 | } 16 | 17 | if (cache[n - 2] == 0) { 18 | cache[n - 2] = fibonacci(n - 2, cache); 19 | } 20 | 21 | size_t result = cache[n - 1] + cache[n - 2]; 22 | 23 | if (arrayCreationIteration) { 24 | delete[] cache; 25 | } 26 | 27 | return result; 28 | } 29 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Task01.cpp: -------------------------------------------------------------------------------- 1 | bool Exist_digit(unsigned int val, unsigned int digit) 2 | { 3 | if (val == 0) { 4 | return false; 5 | } 6 | 7 | if (val % 10 == digit) { 8 | return true; 9 | } 10 | 11 | return Exist_digit(val / 10, digit) 12 | } 13 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Task02.cpp: -------------------------------------------------------------------------------- 1 | unsigned power(unsigned x, unsigned n) 2 | { 3 | if (n == 0) { 4 | return 1; 5 | } 6 | 7 | if (n % 2 == 1) { 8 | return x * power(x, n - 1); 9 | } 10 | 11 | unsigned temp = power(x, n / 2); 12 | return temp * temp; 13 | } 14 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printReverse(int num) 4 | { 5 | if (num == 0) { 6 | return; 7 | } 8 | 9 | std::cout << num % 10 << " "; 10 | printReverse(num / 10); 11 | } 12 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Task04.cpp: -------------------------------------------------------------------------------- 1 | bool IsRising(const float* seq, int size) 2 | { 3 | if (size < 2) { 4 | return true; 5 | } 6 | 7 | if (*seq > *(seq+1)) { //Same as if(seq[0]>seq[1]) 8 | return false; 9 | } 10 | 11 | return IsRising(++seq, size - 1); 12 | } 13 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Task05.cpp: -------------------------------------------------------------------------------- 1 | bool isPalindrome(const char* array, int leftIndx, int rightIndx) 2 | { 3 | if (leftIndx >= rightIndx) { 4 | return true; 5 | } 6 | 7 | if (array[leftIndx] != array[rightIndx]) { 8 | return false; 9 | } 10 | 11 | return isPalindrome(array, leftIndx + 1, rightIndx - 1); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /IS/Sem.09/Pract.09/Task06.cpp: -------------------------------------------------------------------------------- 1 | void Resize(int*& array, int& arraySize, int element, int index) 2 | { 3 | int* newArray = new int[++arraySize]; 4 | 5 | for (size_t i = 0; i < index; i++) { 6 | newArray[i] = array[i]; 7 | } 8 | 9 | newArray[index] = element; 10 | 11 | for (size_t i = index + 1; i < arraySize; i++) { 12 | newArray[i] = array[i - 1]; 13 | } 14 | 15 | delete[] array; 16 | array = newArray; 17 | } 18 | 19 | void AddElement(int*& array, int& arraySize, int element, int index = 0) 20 | { 21 | if (arraySize == 0 || element >= array[arraySize - 1]) { 22 | Resize(array, arraySize, element, arraySize); 23 | return; 24 | } 25 | 26 | if (element < array[index]) { 27 | Resize(array, arraySize, element, index); 28 | return; 29 | } 30 | 31 | AddElement(array, arraySize, element, index + 1); 32 | } 33 | -------------------------------------------------------------------------------- /IS/Sem.09/Solutions/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Binary search - way1 3 | * with argument size 4 | */ 5 | bool BinarySearch(const int* arr, unsigned size, int num) 6 | { 7 | if (size == 0) { 8 | return false; 9 | } 10 | 11 | unsigned mid = size / 2; 12 | 13 | if (arr[mid] == num) { 14 | return true; 15 | } 16 | 17 | if (arr[mid] > num) { 18 | return BinarySearch(arr, mid, num); 19 | } 20 | 21 | return BinarySearch(arr + mid + 1, size - mid - 1, num); 22 | } 23 | 24 | //=========================================================== 25 | 26 | /* Binary search - way2 27 | * int l = left - first element 28 | * int r = right - last element 29 | */ 30 | bool BinarySearch(const int* arr, int l, int r, int num) 31 | { 32 | int mid = l + (r - 1) / 2; 33 | 34 | if (arr[mid] == num) { //found the desired element 35 | return true; 36 | } 37 | 38 | if (arr[mid] > num) { 39 | return BinarySearch(arr, l, mid - 1, num); 40 | } 41 | 42 | //arr[mid] < num; 43 | return BinarySearch(arr, mid + 1, r, num); 44 | } 45 | -------------------------------------------------------------------------------- /IS/Sem.09/Solutions/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | // An optimized version of Bubble Sort 2 | void BubbleSort(int* arr, int n) 3 | { 4 | bool swapped; 5 | 6 | for (size_t i = 0; i < n-1; i++) { 7 | swapped = false; 8 | for (size_t j = 0; j < n-i-1; j++) { 9 | if (arr[j] > arr[j+1]) { 10 | swap(&arr[j], &arr[j+1]); 11 | swapped = true; 12 | } 13 | 14 | // If no two elements were swapped by inner loop, then break 15 | if (swapped == false) { 16 | break; 17 | } 18 | } 19 | } 20 | } 21 | 22 | // Bubble sort - recursive 23 | void BubbleSort(int* arr, int n) 24 | { 25 | if (n == 1) { 26 | return; 27 | } 28 | 29 | int count = 0; 30 | // One pass of bubble sort. After this pass, the largest element is moved (or bubbled) to end. 31 | for (size_t i = 0; i < n-1; i++) { 32 | if (arr[i] > arr[i+1]) { 33 | swap(&arr[i], &arr[i+1]); 34 | count++; 35 | } 36 | } 37 | 38 | // Check if any recursion happens or not. If any recursion is not happen then return 39 | if (count == 0) { 40 | return; 41 | } 42 | 43 | // Largest element is fixed, recur for remaining array 44 | BubbleSort(arr, n-1); 45 | } 46 | -------------------------------------------------------------------------------- /IS/Sem.09/Solutions/LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | //Way - 1 2 | bool linearSearch(const int* arr, unsigned size, int num) 3 | { 4 | if (size == 0) { 5 | return false; 6 | } 7 | 8 | return *arr == num || linearSearch(arr + 1, size - 1, num); 9 | } 10 | 11 | //============================================================== 12 | 13 | //Way - 2 14 | bool linearSearch(const int *arr, int size, int number) 15 | { 16 | if (size == 0) { 17 | return false; 18 | } 19 | 20 | if (arr[size - 1] == number) { 21 | return true; 22 | } 23 | 24 | return linearSearch(arr, size - 1, number); 25 | } 26 | -------------------------------------------------------------------------------- /IS/Sem.09/Solutions/Pow.cpp: -------------------------------------------------------------------------------- 1 | int Power(int a, int n) 2 | { 3 | if (n == 0) { 4 | return 1; 5 | } 6 | 7 | if (n & 1 == 0) { 8 | return power(a * a, n / 2); 9 | } 10 | 11 | return a * power(a * a, n / 2); 12 | } 13 | -------------------------------------------------------------------------------- /IS/Sem.09/Solutions/SearchInText.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isPrefix(const char* text, const char* pattern) 4 | { 5 | if (*pattern == '\0') { 6 | return true; 7 | } 8 | 9 | return *text == *pattern && isPrefix(text + 1, pattern + 1); 10 | } 11 | 12 | bool searchInText(const char* text, const char* pattern) 13 | { 14 | if (*text == '\0') { 15 | return *pattern == '\0'; 16 | } 17 | 18 | return isPrefix(text, pattern) || searchInText(text + 1, pattern); 19 | } 20 | 21 | int main() 22 | { 23 | char str[] = "hello friends how are you"; 24 | char pattern[] = "friends!"; 25 | 26 | std::cout << searchInText(str, pattern); 27 | } 28 | -------------------------------------------------------------------------------- /IS/Sem.10/examples/example1.cpp: -------------------------------------------------------------------------------- 1 | //Credits for example to Angel Dimitriev 2 | 3 | #include 4 | 5 | int get_something_from_array(const int* arr, size_t size, int result, int (*func)(int,int) ) 6 | { 7 | if (size == 0) return result; 8 | return get_something_from_array(arr + 1, size - 1, func(result, *arr), func); 9 | } 10 | 11 | int max(int a, int b) 12 | { 13 | return a > b ? a : b; 14 | } 15 | 16 | int getMaxFromArray(const int* arr, size_t size) 17 | { 18 | return get_something_from_array(arr, size, INT_MIN, max); 19 | } 20 | 21 | int sum(int x, int y) 22 | { 23 | return x + y; 24 | } 25 | 26 | int getSumOnArray(const int* arr, size_t size) 27 | { 28 | return get_something_from_array(arr, size, 0, sum); 29 | } 30 | 31 | int main() 32 | { 33 | const size_t SIZE = 4; 34 | int arr[SIZE] = { 1, 2, 3, 4 }; 35 | 36 | std::cout << getSumOnArray(arr, SIZE) << std::endl 37 | << getMaxFromArray(arr, SIZE) << std::endl; 38 | } -------------------------------------------------------------------------------- /IS/Sem.10/examples/example2.cpp: -------------------------------------------------------------------------------- 1 | //Credits for example to Angel Dimitriev 2 | 3 | #include 4 | using namespace std; 5 | 6 | bool isCapital(char ch) 7 | { 8 | return ch >= 'A' && ch <= 'Z'; 9 | } 10 | 11 | bool isLower(char ch) 12 | { 13 | return ch >= 'a' && ch <= 'z'; 14 | } 15 | 16 | bool isDigit(char ch) 17 | { 18 | return ch >= '0' && ch <= '9'; 19 | } 20 | 21 | unsigned getCharCountCondition(const char* str, bool (*pred)(char)) 22 | { 23 | int count = 0; 24 | while(*str) 25 | { 26 | if(pred(*str)) 27 | count++; 28 | str++; 29 | } 30 | return count; 31 | } 32 | 33 | unsigned getLowerCount(const char* str) 34 | { 35 | return getCharCountCondition(str, isLower); 36 | } 37 | 38 | 39 | unsigned getCapitalCount(const char* str) 40 | { 41 | return getCharCountCondition(str, isCapital); 42 | } 43 | 44 | unsigned getDigitsCount(const char* str) 45 | { 46 | return getCharCountCondition(str, isDigit); 47 | } 48 | 49 | unsigned getWhitespacesCount(const char* str) 50 | { 51 | return getCharCountCondition(str, [](char ch) {return ch == ' ';} ); 52 | } 53 | 54 | int main() 55 | { 56 | char str[] = "Hello World"; 57 | 58 | cout << getWhitespacesCount(str); 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /IS/Sem.10/examples/example3.cpp: -------------------------------------------------------------------------------- 1 | // Credits for example to Elena Tuparova 2 | 3 | #include 4 | 5 | typedef int num; 6 | typedef num (*next) (num); 7 | 8 | int multiply2(int n) { 9 | return n * 2; 10 | } 11 | 12 | double sumFunc(int n, double (*func) (double), next nextI) { 13 | double sum = 0; 14 | for (int i = 1; i <= n; i = nextI(i)) { 15 | sum += func(i); 16 | } 17 | return sum; 18 | } 19 | 20 | int main() { 21 | 22 | num i = 1; 23 | 24 | std::cout << sumFunc(16, sin, multiply2) << std::endl; 25 | std::cout << sumFunc(16, sqrt, [](int n){ return n + 1; }) << std::endl;// example for lambda 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /IS/Sem.10/examples/example4.cpp: -------------------------------------------------------------------------------- 1 | //Credits for example to Elena Tuparova 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int i = 4; 8 | 9 | [=]() mutable { // here *mutable* is needed to allow the captured-by-value variable to be changed inside the lambda 10 | i++; 11 | std::cout << "Inside first lambda: " << i << std::endl; 12 | } (); // don't forget the brackets in order to call the function! 13 | std::cout << "After first lambda: " << i << std::endl; // however, i is not changed after the scope of the lambda 14 | 15 | [&] { i++; } (); 16 | std::cout << "After second lambda: " << i << std::endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /IS/Sem.10/img/Function-pointer-declaration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.10/img/Function-pointer-declaration.png -------------------------------------------------------------------------------- /IS/Sem.10/img/Lambda-syntax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.10/img/Lambda-syntax.png -------------------------------------------------------------------------------- /IS/Sem.10/img/lambda-extended-syntax.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/IS/Sem.10/img/lambda-extended-syntax.gif -------------------------------------------------------------------------------- /SI/README.md: -------------------------------------------------------------------------------- 1 | ## Материали от семинарите и практикумите по "Увод в програмирането" 2 | #### зимен семестър 2022/2023, спец. Софтуерно инженерство 3 | 4 | - [Тема 1:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.01) Увод. Примитивни типове данни. Преобразуване между типове. Оператори. 5 | - [Тема 2:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.02) Условни конструкции - if, switch и тернарен оператор. 6 | - [Тема 3:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.03) Цикли. Оператори break и continue. 7 | - [Тема 4:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.04) Функции. 8 | - [Тема 5:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.05) Масиви. Основни операции, обхождане на елементите, търсене. Многомерни масиви. 9 | - [Тема 6:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.06) Бройни системи и побитови операции. 10 | - [Тема 7:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.07) Референция. Указатели. Символни низове. 11 | - [Тема 8:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.08) Динамична памет. 12 | - [Тема 9:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.09) Рекурсия. 13 | - [Тема 10:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.10) Функции от по-висок ред. 14 | -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | cout << "Oh what\na happy day!\nOh yes,\nwhat a happy day!" << endl; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double a = 5.4; 8 | double b = 7.9; 9 | 10 | cout << "P = " << 2 * (a + b) << endl; 11 | cout << "S = " << a * b << endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int amount; 8 | cout << "Enter amount in lv:"; 9 | cin >> amount; 10 | cout << "This amount is " << amount * 0.5 << " in USD and " << amount * 0.51 << " in EUR." << endl; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double x, y; 8 | 9 | cout << "First side: "; 10 | cin >> x; 11 | 12 | cout << "Second side: "; 13 | cin >> y; 14 | 15 | cout << "Parameter: " << 2 * (x + y) << endl; 16 | cout << "Area: " << x * y << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | double a, b; 8 | 9 | cout << "First number: "; 10 | cin >> a; 11 | 12 | cout << "Second number: "; 13 | cin >> b; 14 | 15 | cout << boolalpha << (a < b) << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | 9 | cout << "Dividend: "; 10 | cin >> a; 11 | cout << "Divisor: "; 12 | cin >> b; 13 | 14 | cout << "The quotient of the division is : " << a / b << endl; 15 | cout << "The remainder of the division is : " << a % b << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int apples, pears, bananas; 8 | 9 | cout << "Apples: "; 10 | cin >> apples; 11 | cout << "Pears: "; 12 | cin >> pears; 13 | cout << "Bananas: "; 14 | cin >> bananas; 15 | 16 | cout << "Pesho, don't forget to buy " << apples << " apples, " 17 | << pears << " pears and " << bananas << " bananas!"; 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const double PI = 3.14159265; 6 | 7 | int main() 8 | { 9 | double radius; 10 | 11 | cout << "Radius: "; 12 | cin >> radius; 13 | 14 | cout << "Perimeter: " << 2 * radius * PI << endl; 15 | cout << "Area: " << radius * PI * PI << endl; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int a, b, c; 9 | 10 | cout << "a = "; 11 | cin >> a; 12 | cout << "b = "; 13 | cin >> b; 14 | cout << "c = "; 15 | cin >> c; 16 | 17 | double d = sqrt(b * b - 4 * a * c); 18 | double x1 = (-b + d) / (2 * a); 19 | double x2 = (-b - d) / (2 * a); 20 | 21 | cout << "x1 = " << x1 << ", x2 = " << x2; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | cout << "a: "; 9 | cin >> a; 10 | cout << "b: "; 11 | cin >> b; 12 | 13 | int temp = a; 14 | a = b; 15 | b = temp; 16 | 17 | cout << "a: " << a << endl; 18 | cout << "b: " << b << endl; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | cout << "a: "; 9 | cin >> a; 10 | cout << "b: "; 11 | cin >> b; 12 | 13 | bool isFirstHigher = a > b; // 0 or 1 14 | 15 | cout << "Max: " << a * isFirstHigher + b * !isFirstHigher << endl; 16 | cout << "Min: " << a * !isFirstHigher + b * isFirstHigher << endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int inputSeconds; 8 | 9 | cout << "Seconds: "; 10 | cin >> inputSeconds; 11 | 12 | int days = inputSeconds / (60 * 60 * 24); 13 | int hours = (inputSeconds / (60 * 60)) % 24; 14 | int minutes = (inputSeconds / 60) % 60; 15 | int seconds = inputSeconds % 60; 16 | 17 | cout << days << " days, " 18 | << hours << " hours, " 19 | << minutes << " minutes and " 20 | << seconds << " seconds"; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int x1, x2, y1, y2; 9 | cout << "x1: "; 10 | cin >> x1; 11 | cout << "y1: "; 12 | cin >> y1; 13 | cout << "x2: "; 14 | cin >> x2; 15 | cout << "y2: "; 16 | cin >> y2; 17 | 18 | double distance = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 19 | 20 | cout << "Distance: " << fixed << setprecision(2) << distance; 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /SI/Sem.01/Pract.01/task14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | long long cardNumber; 8 | cin >> cardNumber; 9 | 10 | cout << "XXXXXXXXXXXX" << cardNumber % 10000; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int year = 0; 5 | 6 | std::cout << "Enter a year: "; 7 | std::cin >> year; 8 | 9 | if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { 10 | std::cout << "Entered year is leap.\n"; 11 | } else { 12 | std::cout << "Entered year is not leap.\n"; 13 | } 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char inputCharacter; 5 | 6 | std::cin >> inputCharacter; 7 | 8 | if ((inputCharacter >= 'A' && inputCharacter <= 'Z') || (inputCharacter >= 'a' && inputCharacter <= 'z')) { 9 | switch (inputCharacter) { 10 | case 'A': 11 | case 'E': 12 | case 'I': 13 | case 'O': 14 | case 'U': 15 | case 'Y': 16 | case 'a': 17 | case 'e': 18 | case 'i': 19 | case 'o': 20 | case 'u': 21 | case 'y': 22 | std::cout << "Entered letter is a vowel.\n"; 23 | break; 24 | default: 25 | std::cout << "Entered letter is not a vowel.\n"; 26 | break; 27 | } 28 | } else { 29 | std::cout << "The symbol is not part of the latin alphabet." << std::endl; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double number1, number2; 5 | 6 | std::cout << "Enter first number value: "; 7 | std::cin >> number1; 8 | std::cout << "Enter second number value: "; 9 | std::cin >> number2; 10 | 11 | if (number1 == number2) { 12 | std::cout << "Both numbers are equal." << std::endl; 13 | return 0; 14 | } 15 | 16 | std::cout << (number1 > number2 ? "The first number is larger than the second number." : "The second number is larger than the first number.") << std::endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int percentage, grade; 5 | 6 | std::cout << "Result: "; 7 | std::cin >> percentage; 8 | 9 | if (percentage >= 0 && percentage <= 100) { 10 | bool isSixPlus = false; 11 | 12 | if (percentage >= 90) { 13 | grade = 6; 14 | isSixPlus = true; 15 | } else if (percentage >= 80) { 16 | grade = 6; 17 | } else if (percentage >= 70) { 18 | grade = 5; 19 | } else if (percentage >= 60) { 20 | grade = 4; 21 | } else if (percentage >= 40) { 22 | grade = 3; 23 | } else { 24 | grade = 2; 25 | } 26 | 27 | std::cout << "Grade: " << grade << (isSixPlus ? "+\n" : "\n"); 28 | } else { 29 | std::cout << "Entered percentage is not valid!\n"; 30 | } 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double sideA, sideB, sideC; 5 | 6 | std::cout << "Enter value for side a: "; 7 | std::cin >> sideA; 8 | std::cout << "Enter value for side b: "; 9 | std::cin >> sideB; 10 | std::cout << "Enter value for side c: "; 11 | std::cin >> sideC; 12 | 13 | if ((sideA + sideB > sideC) && (sideA + sideC > sideB) && (sideB + sideC > sideA)) { 14 | std::cout << "The triangle is valid.\n"; 15 | } else { 16 | std::cout << "The triangle is invalid.\n"; 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | cout << "Finding the root of the equation a * x^2 + b * x + c = 0\n"; 9 | 10 | double a; 11 | cout << "Enter the value of a: "; 12 | cin >> a; 13 | 14 | if (a < 0.00000001) 15 | { 16 | cout << "If a is 0, then this is not a quadratic equation!\n"; 17 | return 1; 18 | } 19 | 20 | double b, c; 21 | cout << "Enter the value of b: "; 22 | cin >> b; 23 | 24 | cout << "Enter the value of c: "; 25 | cin >> c; 26 | 27 | double discriminant; 28 | discriminant = (b * b) - (4 * a * c); 29 | 30 | double root1, root2, imaginary; 31 | if (discriminant > 0) { 32 | root1 = (-b + sqrt(discriminant)) / (2 * a); 33 | root2 = (-b - sqrt(discriminant)) / (2 * a); 34 | 35 | cout << "Two distinct and real roots exist: " << root1 << " and " << root2 << endl; 36 | } else if (discriminant == 0) { 37 | root1 = root2 = -b / (2 * a); 38 | 39 | cout << "Two equal and real roots exist: " << root1 << " and " << root2 << endl; 40 | } else if (discriminant < 0) { 41 | root1 = root2 = -b / (2 * a); 42 | imaginary = sqrt(-discriminant) / (2 * a); 43 | 44 | cout << "Two distinct complex roots exist: " << root1 << "+" << imaginary << "i and " << root2 << "-" << imaginary << "i\n"; 45 | } 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char symbol; 5 | std::cin >> symbol; 6 | 7 | if (symbol >= 'A' && symbol <= 'Z') { 8 | std::cout << (symbol += 32) << std::endl; 9 | } else if (symbol >= 'a' && symbol <= 'z') { 10 | std::cout << (symbol -= 32) << std::endl; 11 | } else { 12 | std::cout << "Invalid character" << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /SI/Sem.02/Pract.02/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int arabicNumber = 0; 5 | 6 | std::cout << "Arabic numeral: "; 7 | std::cin >> arabicNumber; 8 | 9 | if (arabicNumber / 10 == 0 && arabicNumber != 0) { 10 | std::cout << "Roman numerals: "; 11 | 12 | switch (arabicNumber) { 13 | case 1: 14 | std::cout << "I"; 15 | break; 16 | case 2: 17 | std::cout << "II"; 18 | break; 19 | case 3: 20 | std::cout << "III"; 21 | break; 22 | case 4: 23 | std::cout << "IV"; 24 | break; 25 | case 5: 26 | std::cout << "V"; 27 | break; 28 | case 6: 29 | std::cout << "VI"; 30 | break; 31 | case 7: 32 | std::cout << "VII"; 33 | break; 34 | case 8: 35 | std::cout << "VIII"; 36 | break; 37 | case 9: 38 | std::cout << "IX"; 39 | break; 40 | default: 41 | break; 42 | } 43 | 44 | std::cout << std::endl; 45 | } else { 46 | std::cout << "Entered number contains more than one digit or is 0." << std::endl; 47 | } 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /SI/Sem.02/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int n; 7 | cin >> n; 8 | 9 | if (n < 100) { 10 | cout << "Less than 100" << endl; 11 | } else if (n <= 200) { 12 | cout << "Between 100 and 200" << endl; 13 | } else { 14 | cout << "More than 200" << endl; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.02/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | double speed; 7 | cin >> speed; 8 | 9 | if (speed <= 10) { 10 | cout << "Slow" << endl; 11 | } else if (speed < 50) { 12 | cout << "Average" << endl; 13 | } else if (speed < 150) { 14 | cout << "Fast" << endl; 15 | } else if (speed < 1000) { 16 | cout << "Ultra fast" << endl; 17 | } else { 18 | cout << "Extremely fast" << endl; 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /SI/Sem.02/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, c, d, e; 6 | std::cin >> a >> b >> c >> d >> e; 7 | 8 | std::cout << ((a <= b && b >= c && c <= d && d >= e) 9 | || (a >= b && b <= c && c >= d && d <= e)) 10 | ? "Yes" : "No"; 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | for (int i = 1; i <= n; i++) { 11 | for (int j = 1; j <= i; j++) { 12 | cout << j; 13 | } 14 | 15 | if (i != n) { 16 | cout << endl; 17 | } 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | for (int i = 1; i < n; i++) { 11 | int x = i; 12 | int y = n - x; 13 | 14 | cout << "x = " << x << " y = " << y << endl; 15 | } 16 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | int reversed = 0; 11 | n = n * n; 12 | 13 | while (n != 0) { 14 | reversed = reversed * 10 + n % 10; 15 | n /= 10; 16 | } 17 | 18 | cout << reversed; 19 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, m; 7 | cin >> n >> m; 8 | 9 | if(n <= 0 || m <= 0) { 10 | cout << "Invalid input." << endl; 11 | return 0; 12 | } 13 | 14 | for (int i = 1; i <= n * m; i++) { 15 | cout << i << " "; 16 | 17 | if (i % m == 0) { 18 | cout << endl; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int target = 0; 7 | 8 | cout << "Enter a number between 0 and 100: "; 9 | cin >> target; 10 | 11 | if (target < 0 || target > 100) { 12 | cout << "Entered number invalid." << endl; 13 | return 0; 14 | } 15 | 16 | int counter = 0, currentGuess; 17 | while (true) { 18 | cin >> currentGuess; 19 | counter++; 20 | 21 | if (currentGuess == target) { 22 | break; 23 | } 24 | 25 | cout << (currentGuess > target ? "Higher" : "Lower") << endl; 26 | } 27 | 28 | cout << "Congratulations, you guessed right! In only took you " << counter << " tries to do so. :)" << endl; 29 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | int targetSum = 0; 10 | for (int i = 1; i <= n; i++) { 11 | targetSum += i; 12 | } 13 | 14 | for (int i = 0; i < n - 1; i++) { 15 | int currentNumber; 16 | cin >> currentNumber; 17 | targetSum -= currentNumber; 18 | } 19 | 20 | cout << targetSum; 21 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | for (int i = 1; i <= n; i++) { 10 | for (int j = i; j <= n; j++) { 11 | for (int k = j; k <= n; k++) { 12 | if (i * i + j * j == k * k) { 13 | cout << i << " " << j << " " << k << endl; 14 | } 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | char s; 8 | cin >> n >> s; 9 | 10 | if (n < 2 || n > 9) { 11 | cout << "Invalid input." << endl; 12 | return 0; 13 | } 14 | 15 | int numberOfRows = 2 * n - 1; 16 | 17 | for (int i = 1; i <= numberOfRows; i++) { 18 | int numberOfEmptySpaces = abs(n - i); 19 | int numberOfSymbols = numberOfRows - 2 * numberOfEmptySpaces; 20 | 21 | for (int j = 0; j < numberOfEmptySpaces; j++) { 22 | cout << ' '; 23 | } 24 | for (int j = 0; j < numberOfSymbols; j++) { 25 | cout << s; 26 | } 27 | 28 | cout << endl; 29 | } 30 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | if (n <= 3) { 10 | cout << "Invalid input." << endl; 11 | return 0; 12 | } 13 | 14 | int num1 = 1, num2 = 1; 15 | cout << num1 << " " << num2 << " "; 16 | 17 | while (num1 + num2 < n) { 18 | cout << num1 + num2 << " "; 19 | int temp = num1; 20 | num1 = num2; 21 | num2 = num1 + temp; 22 | } 23 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int number, digits = 0; 7 | cin >> number; 8 | 9 | int temp = number; //copy of number 10 | while (temp != 0) 11 | { 12 | temp /= 10; 13 | digits++; 14 | } 15 | 16 | int newNumber = 0; 17 | if (digits > 2) 18 | { 19 | int Multiplier = 1; 20 | const int centralDigit = digits / 2 + 1; 21 | 22 | for (int i = 1; i <= digits; i++) 23 | { 24 | if(digits % 2 == 0 && i == centralDigit - 1) 25 | { 26 | number /= 10; 27 | continue; 28 | } 29 | 30 | if (i == centralDigit) 31 | { 32 | number /= 10; 33 | continue; 34 | } 35 | 36 | newNumber += number % 10 * Multiplier; 37 | Multiplier *= 10; 38 | number /= 10; 39 | } 40 | } 41 | 42 | cout << newNumber << ", "; 43 | cout << ++newNumber << endl; 44 | } 45 | -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a = 0, b = 0; 7 | bool isPrime = false; 8 | 9 | cin >> a >> b; 10 | 11 | if (a < 0 || b < 0) { 12 | cout << "Invalid input." << endl; 13 | return 0; 14 | } 15 | 16 | if (a > b) { 17 | int temp = a; 18 | a = b; 19 | b = temp; 20 | } 21 | 22 | for (int i = a; i <= b; i++) { 23 | if (i == 2) { 24 | cout << i << " "; 25 | continue; 26 | } 27 | 28 | if (i == 0 || i == 1 || i % 2 == 0) { 29 | continue; 30 | } 31 | 32 | isPrime = true; 33 | 34 | int squareRoot = sqrt(i); 35 | for (int j = 3; j <= squareRoot; j += 2) { 36 | if (i % j == 0) { 37 | isPrime = false; 38 | break; 39 | } 40 | } 41 | 42 | if (isPrime) { 43 | cout << i << " "; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | if (n < 1) { 10 | cout << "false"; 11 | return 0; 12 | } 13 | 14 | bool isMultipleOfTwo = true; 15 | while (n / 2 != 0) { 16 | if (n % 2 != 0) { 17 | isMultipleOfTwo = false; 18 | break; 19 | } 20 | 21 | n /= 2; 22 | } 23 | 24 | cout << boolalpha << isMultipleOfTwo << endl; 25 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | while (n != 0) { 10 | int k = 1; 11 | int pow = 0; 12 | 13 | while (k * 2 <= n) { 14 | k *= 2; 15 | pow++; 16 | } 17 | 18 | n -= k; 19 | cout << "2^" << pow; 20 | 21 | if (n != 0) { 22 | cout << "+"; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /SI/Sem.03/Pract.03/Task14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b, c, d, e; 7 | cin >> a >> b >> c >> d >> e; 8 | 9 | for (int i = -100; i <= 100; i++) { 10 | if (a * pow(i, 4) + b * pow(i,3) + c * pow(i, 2) + d * i + e >= 0) { 11 | cout << i << " "; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /SI/Sem.03/Solutions/GCD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | int minNumber = a > b ? b : a; 10 | 11 | int gcd = 1; 12 | 13 | for (size_t i = 2; i <= minNumber; i++) { 14 | if (a % i == 0 && b % i == 0) 15 | gcd = i; 16 | } 17 | 18 | cout << gcd; 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /SI/Sem.03/Solutions/GCDEuclideanAlgorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | if (a < b) { 10 | int temp = a; 11 | a = b; 12 | b = temp; 13 | } 14 | 15 | while (b != 0) { 16 | int r = a % b; 17 | 18 | a = b; 19 | b = r; 20 | } 21 | 22 | cout << a; 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /SI/Sem.03/Solutions/PrimeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a; 7 | cin >> a; 8 | 9 | if (a == 2) { 10 | cout << "Prime"; 11 | return 0; 12 | } 13 | 14 | if (a % 2 == 0 || a == 1) { 15 | cout << "Not Prime"; 16 | return 0; 17 | } 18 | 19 | double squareRoot = sqrt(a); 20 | for (int i = 3; i <= squareRoot; i += 2) { 21 | if (a % i == 0) { 22 | cout << "Not Prime"; 23 | return 0; 24 | } 25 | } 26 | 27 | cout << "Prime"; 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /SI/Sem.03/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int sum = 0; 8 | int n; 9 | do 10 | { 11 | cin >> n; 12 | sum += n; 13 | } while (n != 0); 14 | 15 | cout << sum; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.03/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | int reversed = 0; 9 | 10 | while (n != 0) { 11 | reversed = reversed * 10 + n % 10; 12 | n /= 10; 13 | } 14 | 15 | cout << reversed + 1; 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.03/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a, b; 8 | cin >> a >> b; 9 | 10 | int min = a > b ? b : a; 11 | 12 | for (int i = min; i > 0; i--) { 13 | if (a % i == 0 && b % i == 0) 14 | cout << i << " "; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task01.cpp: -------------------------------------------------------------------------------- 1 | int Sum(int a, int b) 2 | { 3 | return a + b; 4 | } 5 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task02.cpp: -------------------------------------------------------------------------------- 1 | bool IsEven(int num) 2 | { 3 | return num % 2 == 0; 4 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task03.cpp: -------------------------------------------------------------------------------- 1 | bool IsPositive(int num) 2 | { 3 | return (num > 0); 4 | } 5 | 6 | bool IsNegative(int num) 7 | { 8 | return (num < 0); 9 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task04.cpp: -------------------------------------------------------------------------------- 1 | int abs(int num1, int num2) 2 | { 3 | return ((num1 - num2) >= 0 ? (num1 - num2) : (num2 - num1)); 4 | } 5 | 6 | double fabs(double &num) 7 | { 8 | return num > 0.0 ? num : -num; 9 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task05.cpp: -------------------------------------------------------------------------------- 1 | void print(int N, char S = ' ') 2 | { 3 | for (size_t i = 0; i < N; i++) { 4 | cout << S; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task06.cpp: -------------------------------------------------------------------------------- 1 | bool isCapitalLetter(char S) 2 | { 3 | return 'A' <= S && S <= 'Z'; 4 | } 5 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task07.cpp: -------------------------------------------------------------------------------- 1 | bool isNumberInInterval(long long num, long long lowerBound, long long upperBound) 2 | { 3 | return interval_lower_bound <= num && num <= interval_upper_bound 4 | } 5 | 6 | long long askUser(long long from, long long to) 7 | { 8 | long long number; 9 | while (std::cin >> number) 10 | { 11 | if (isNumberInInterval(number, from, to)) { 12 | return number; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task08.cpp: -------------------------------------------------------------------------------- 1 | long long pow(int num, unsigned N) 2 | { 3 | long long result = 1; 4 | 5 | for (size_t i = 0; i < N; i++) { 6 | result *= num; 7 | } 8 | 9 | return result; 10 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task09.cpp: -------------------------------------------------------------------------------- 1 | int calculate(int a, int b, char action = '+') 2 | { 3 | switch (action) 4 | { 5 | case '+': return a + b; 6 | 7 | case '-': return a - b; 8 | 9 | case '*': return a * b; 10 | 11 | case '/': 12 | if(b == 0) { 13 | std::cout << "Cannot divide by 0\n"; 14 | return -1; 15 | } 16 | return a / b; 17 | 18 | case '%': 19 | if(b == 0) { 20 | std::cout << "Cannot divide by 0\n"; 21 | return -1; 22 | } 23 | return a % b; 24 | 25 | default: 26 | std::cout << "Error\n"; 27 | return -1; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task10.cpp: -------------------------------------------------------------------------------- 1 | unsigned long long toBinary(unsigned number) 2 | { 3 | unsigned long long toBinary = 0; 4 | unsigned long long temp = 1; 5 | 6 | while (number > 0) { 7 | toBinary += (number % 2) * temp; 8 | temp *= 10; 9 | number /= 2; 10 | } 11 | 12 | return toBinary; 13 | } 14 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int Gcd(int x, int y) 4 | { 5 | while (x != y) 6 | { 7 | if (x > y) { 8 | x -= y; 9 | } else { 10 | y -= x; 11 | } 12 | } 13 | 14 | return x; 15 | } 16 | 17 | int main() 18 | { 19 | int a, b, c, d; 20 | 21 | std::cout << "a, b, c, d= \n"; 22 | std::cin >> a >> b >> c >> d; 23 | if (!cin || a < 1 || b < 1 || c < 1 || d < 1) { 24 | std::cout << "Error! \n"; 25 | return 1; 26 | } 27 | 28 | int r = Gcd(a, b); 29 | std::cout << "gcd{" << a << ", " << b << "}= " << r << "\n"; 30 | 31 | int s = Gcd(c, d); 32 | std::cout << "gcd{" << c << ", " << d << "}= " << s << "\n"; 33 | 34 | std::cout << "gcd{" << a << ", " << b << ", " << c << ", " << d << "}= " << Gcd(r, s) << "\n"; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int LowerBound = 100; 4 | const int UpperBound = 1000000; 5 | 6 | int CheckInput() { 7 | int number; 8 | do { 9 | std::cout << "Enter a valid number: "; 10 | std::cin >> number; 11 | } while (!std::cin || number < LowerBound || number > UpperBound); 12 | 13 | return number; 14 | } 15 | 16 | int FindTheNearest(int input) { 17 | int result = 64; // The nearest possible value 18 | int lower, higher; 19 | while (result <= input) { 20 | lower = result; 21 | result *= 2; 22 | higher = result; 23 | }; 24 | 25 | result = ((higher - input) > (input - lower)) ? lower : higher; 26 | return result; 27 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //Function to check whether a number is prime 5 | bool IsPrime(int number) 6 | { 7 | int size = sqrt(number); 8 | 9 | for (int i = 2; i <= size; i++) { 10 | if (number % i == 0) { 11 | return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | 18 | //Print all prime numbers that are smaller than N 19 | void PrintAllPrimeToNumber(int number) //Interval [1 ; N] 20 | { 21 | std::cout << "The primes are: \n"; 22 | 23 | for (int i = 2; i < number; i++) { 24 | if (IsPrime(i)) { 25 | cout << i << endl; 26 | } 27 | } 28 | } 29 | 30 | //Function whether the number N can be expressed as a sum of two prime numbers 31 | bool CheckSumOfTwoPrimes(int number) 32 | { 33 | int size = number / 2; 34 | for (int i = 2; i <= size; i++) { 35 | if (IsPrime(i) && IsPrime(number - i)) { 36 | return true; 37 | } 38 | } 39 | return false; 40 | } -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task14.cpp: -------------------------------------------------------------------------------- 1 | long long SumOfDigits(long long n) { 2 | long long sumOfDigits = 0; 3 | while (n != 0) { 4 | sumOfDigits += n % 10; 5 | n /= 10; 6 | } 7 | return sumOfDigits; 8 | } 9 | 10 | int main() 11 | { 12 | long long n; 13 | std::cout << "Enter a number: " << std::endl; 14 | std::cin >> n; 15 | 16 | while (n >= 10) { 17 | std::cout << n << " -> "; 18 | n = sumOfDigits(n); 19 | } 20 | std::cout << n << std::endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /SI/Sem.04/Pract.04/Task15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Function to check whether a number is prime 4 | bool IsPrime(int number) 5 | { 6 | int size = sqrt(number); 7 | 8 | for (int i = 2; i <= size; i++) 9 | { 10 | if (number % i == 0) { 11 | return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | 18 | //PrimeFactorization 19 | void PrimeFactor(int number) 20 | { 21 | for (int i = 2; i <= number; i++) { 22 | if (IsPrime(i) && number % i == 0) { 23 | if (i == number) { //last step, no space 24 | std::cout << i; 25 | } else { 26 | std::cout << i << " "; 27 | } 28 | 29 | number /= i; 30 | i--; 31 | } 32 | } 33 | std::cout << std::endl; 34 | } -------------------------------------------------------------------------------- /SI/Sem.04/task1.cpp: -------------------------------------------------------------------------------- 1 | int Reverse(unsigned int n) 2 | { 3 | unsigned int result; 4 | short lastDigit; 5 | 6 | while (n != 0) 7 | { 8 | lastDigit = n % 10; 9 | (result *= 10) += lastDigit; 10 | n /= 10; 11 | } 12 | 13 | return result; 14 | } 15 | -------------------------------------------------------------------------------- /SI/Sem.04/task2.cpp: -------------------------------------------------------------------------------- 1 | int Reverse(unsigned int n) 2 | { 3 | unsigned int result; 4 | short lastDigit; 5 | 6 | while (n != 0) 7 | { 8 | lastDigit = n % 10; 9 | (result *= 10) += lastDigit; 10 | n /= 10; 11 | } 12 | 13 | return result; 14 | } 15 | 16 | bool IsPalindrome(unsigned int n) 17 | { 18 | return n == reverse(n); 19 | } -------------------------------------------------------------------------------- /SI/Sem.04/task3.cpp: -------------------------------------------------------------------------------- 1 | bool AreDigitsSortedAscending(unsigned int n) 2 | { 3 | while (n >= 10) 4 | { 5 | unsigned int lastDigit = n % 10; 6 | unsigned int prevLastDigit = (n / 10) % 10; 7 | 8 | if (prevLastDigit > lastDigit) { 9 | return false; 10 | } 11 | 12 | n /= 10; 13 | } 14 | 15 | return true; 16 | } 17 | 18 | bool AreDigitsSortedDescending(unsigned int n) 19 | { 20 | while (n >= 10) 21 | { 22 | unsigned int lastDigit = n % 10; 23 | iunsigned intnt prevLastDigit = (n / 10) % 10; 24 | 25 | if (prevLastDigit < lastDigit) { 26 | return false; 27 | } 28 | 29 | n /= 10; 30 | } 31 | 32 | return true; 33 | } 34 | 35 | bool AreDigitsSorted(unsigned int n) 36 | { 37 | return AreDigitsSortedDescending(n) || AreDigitsSortedAscending(n); 38 | } -------------------------------------------------------------------------------- /SI/Sem.05/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * A iterative binary search function. 3 | * It returns location of x in given array arr[l..r] if present, 4 | * otherwise -1. 5 | */ 6 | 7 | /* 8 | * int l - left side index 9 | * int r - right side index 10 | * int m - middle index 11 | * int x - number 'x' we are searching for 12 | */ 13 | int BinarySearch(const int *arr, int size, int x) 14 | { 15 | int l = 0, r = size - 1, m; 16 | while (l <= r) 17 | { 18 | int m = l + (r - l) / 2; 19 | if (arr[m] == x) { // Check if x is present at mid 20 | return m; 21 | } else if (arr[m] < x) { // If x greater, ignore left half 22 | l = m + 1; 23 | } else { // If x is smaller, ignore right half 24 | r = m - 1; 25 | } 26 | } 27 | return -1; // if we reach here, then the element was not present 28 | } -------------------------------------------------------------------------------- /SI/Sem.05/EratosthenesSieve.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const unsigned MaxSize = 1000; 4 | 5 | void InitArray(bool* arr, unsigned size, bool value) 6 | { 7 | for (unsigned i = 0; i < size; i++) { 8 | arr[i] = value; 9 | } 10 | } 11 | 12 | void EratosthenesSieve(bool* arr, unsigned size) 13 | { 14 | arr[0] = arr[1] = false; 15 | 16 | for (unsigned i = 2; i < sqrt(size); i++) { 17 | if (!arr[i]) { //ако числото е задраскано 18 | continue; 19 | } 20 | 21 | for (unsigned j = i * i; j < size; j += i) { 22 | arr[j] = false; //задраскваме 23 | } 24 | } 25 | } 26 | 27 | void PrintSieve(const bool* arr, unsigned size) 28 | { 29 | for (unsigned i = 0; i < size; i++) { 30 | if (arr[i]) { 31 | std::cout << i << " "; 32 | } 33 | } 34 | } 35 | 36 | int main() 37 | { 38 | bool buffer[MaxSize]; 39 | 40 | int n; 41 | std::cin >> n; 42 | 43 | InitArray(buffer, n, true); 44 | EratosthenesSieve(buffer, n); 45 | PrintSieve(buffer, n); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /SI/Sem.05/InputArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void InputArray(int *arr,const int size) 4 | { 5 | for (int i = 0; i < size; i++) { 6 | std::cin >> arr[i]; 7 | } 8 | } -------------------------------------------------------------------------------- /SI/Sem.05/LinearSerach.cpp: -------------------------------------------------------------------------------- 1 | int LinearSearch(const int* arr, int size, int number) 2 | { 3 | for(int i = 0; i < size; i++) { 4 | if(arr[i] == number) { 5 | return i; 6 | } 7 | } 8 | 9 | return -1; // if we reach here, then the element was not present 10 | } -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 20; 5 | 6 | int FirstDuplicateInArray(const int *arr, int size) 7 | { 8 | for (int i = 0; i < size - 1; i++) { 9 | for (int j = i + 1; j < size; j++) { 10 | if (arr[i] == arr[j]) { 11 | return arr[i]; 12 | } 13 | } 14 | } 15 | 16 | return -1; 17 | } 18 | 19 | void PutNumbersInArray(int* arr, int size) 20 | { 21 | for (int i = 0; i < size; i++) { 22 | cin >> arr[i]; 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | int arr[MAX_SIZE]; 29 | int size = 0; 30 | 31 | while (size < 2 || size > 20) { 32 | cout << "Enter how many numbers would you want (between 2 and 20): "; 33 | cin >> size; 34 | } 35 | 36 | PutNumbersInArray(arr, size); 37 | FirstDuplicateInArray(arr, size); 38 | } 39 | -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task02.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 | void ResetChanges(int &index, bool &changesOccured) 11 | { 12 | index = -1; 13 | changesOccured = false; 14 | } 15 | 16 | bool ChangeCurrentIndex(int index, int ¤tIndex) 17 | { 18 | return currentIndex = index; 19 | } 20 | 21 | void SortNumbers(int *arr, size_t size) 22 | { 23 | int counter = NULL, min, max, currentIndex; 24 | bool sortingEvenNumbers = true, changeOccured; 25 | 26 | do 27 | { 28 | if (sortingEvenNumbers) { 29 | min = INT_MAX; //reset min 30 | } else { 31 | max = INT_MIN; //reset max 32 | } 33 | 34 | ResetChanges(currentIndex, changeOccured);//reset change flag & index 35 | 36 | for (size_t i = counter; i < size; i++) { 37 | if (sortingEvenNumbers) { 38 | if (arr[i] % 2 == 0 && arr[i] < min) { 39 | min = arr[i]; 40 | changeOccured = ChangeCurrentIndex(i, currentIndex); 41 | } 42 | } else { 43 | if (arr[i] % 2 != 0 && arr[i] > max) { 44 | max = arr[i]; 45 | changeOccured = ChangeCurrentIndex(i, currentIndex); 46 | } 47 | } 48 | } 49 | 50 | if (!changeOccured && sortingEvenNumbers) { 51 | sortingEvenNumbers = !sortingEvenNumbers; 52 | changeOccured = true; 53 | } else if(changeOccured) { 54 | Swap(arr[counter], arr[currentIndex]); 55 | counter++; 56 | } 57 | } while(changeOccured); 58 | } 59 | -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task03.cpp: -------------------------------------------------------------------------------- 1 | long long ToBinary(unsigned number) 2 | { 3 | int buffer[MAX_POWTWO_SIZE]; 4 | int size = 0; 5 | 6 | while (number > 0) { 7 | buffer[size] = number % 2; 8 | number /= 2; 9 | size++; 10 | } 11 | 12 | long long convertedToBinary = 0; 13 | long long multiply = 1; 14 | for (unsigned i = 0; i < size; i++) { 15 | convertedToBinary += buffer[i] * multiply; 16 | multiply *= 10; 17 | } 18 | 19 | return convertedToBinary; 20 | } 21 | -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task04.cpp: -------------------------------------------------------------------------------- 1 | bool isPalindrom(const int* arr, int size) 2 | { 3 | for (int i = 0; i < size / 2; i++) 4 | { 5 | if (arr[i] != arr[size - i - 1]) { 6 | return false; 7 | } 8 | } 9 | return true; 10 | } 11 | 12 | bool isPalindrom(int number) 13 | { 14 | int buffer[MAX_SIZE]; 15 | int size = 0; 16 | while (number > 0) { 17 | buffer[size] = number % 10; 18 | number /= 10; 19 | size++; 20 | } 21 | 22 | return isPalindrom(buffer, size); 23 | }} 24 | -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int& a, int& b) 4 | { 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | } 9 | int SortArrayAscending(int arr[], int size) 10 | { 11 | for (int i = 0; i < size; i++) 12 | { 13 | for (int j = i + 1; j < size; j++) 14 | { 15 | if (arr[i] > arr[j]) 16 | { 17 | swap(arr[i], arr[j]); 18 | } 19 | } 20 | } 21 | return arr[size]; 22 | } 23 | void printSection(int* arr1, int* arr2, int size1, int size2) 24 | { 25 | SortArrayAscending(arr1, size1); 26 | SortArrayAscending(arr2, size2); 27 | for (int i = 0, j = 0; i < size1 && j < size2; ) 28 | { 29 | if (arr1[i] == arr2[j]) 30 | { 31 | cout << arr1[i] << " "; 32 | i++; 33 | j++; 34 | } 35 | else if (arr1[i] < arr2[j]) 36 | { 37 | i++; 38 | } 39 | else if (arr1[i] > arr2[j]) 40 | { 41 | j++; 42 | } 43 | } 44 | } 45 | int main() 46 | { 47 | const int Size1 = 5; 48 | const int Size2 = 4; 49 | int arr1[Size1] = { 2, 5, 6, 14, 9 }; 50 | int arr2[Size2] = { 4, 2, 5, 14 }; 51 | printSection(arr1, arr2, Size1, Size2); 52 | return 0; 53 | } -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swap(int& a, int& b) 4 | { 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | } 9 | void sort(int* arr, int size) 10 | { 11 | for (int i = 0; i < size-1; i++) 12 | { 13 | if (i % 2 == 0 && arr[i] < arr[i+1]) 14 | { 15 | swap(arr[i], arr[i + 1]); 16 | } 17 | else if (i % 2 != 0 && arr[i] > arr[i + 1]) 18 | { 19 | swap(arr[i], arr[i + 1]); 20 | } 21 | } 22 | } 23 | void printArray(int* arr, int size) 24 | { 25 | for (int i = 0; i < size; i++) 26 | { 27 | cout << arr[i] << " "; 28 | } 29 | cout << endl; 30 | } 31 | int main() 32 | { 33 | const int Size = 6; 34 | int arr[Size] = { 2, 6, 7, 88, 1, 20 }; 35 | sort(arr, Size); 36 | printArray(arr, Size); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void Transponate(int matrix[][3], int rows, int cols); 3 | int main() 4 | { 5 | const int rows = 3; 6 | const int cols = 3; 7 | int matrix[rows][cols]{ {1,2,3}, {4,5,6}, {7,8,9} }; 8 | 9 | Transponate(matrix, rows, cols); 10 | 11 | //print 12 | for (int i = 0; i < rows; i++) 13 | { 14 | for (int j = 0; j < cols; j++) 15 | { 16 | std::cout << matrix[i][j] << " "; 17 | } 18 | std::cout << std::endl; 19 | } 20 | 21 | } 22 | //how to pass correctly a matrix as a parameter 23 | void Transponate(int matrix[][3], int rows, int cols) 24 | { 25 | for (int i = 0; i < rows - 1; i++) 26 | { 27 | for (int j = i + 1; j < cols; j++) 28 | { 29 | int tmp = matrix[i][j]; 30 | matrix[i][j] = matrix[j][i]; 31 | matrix[j][i] = tmp; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /SI/Sem.05/Pract.05/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | void MultiplyMatrixs(int matrixA[][3], int matrixB[][4], int matrixRezult[][4], int N, int M, int K); 3 | 4 | int main() 5 | { 6 | const int N = 2; 7 | const int M = 3; 8 | const int K = 4; 9 | int matrixA[N][M]{ {1,2,3}, {4,5,6} }; 10 | int matrixB[M][K]{ {5,7,3,4}, {7,5,6,6},{1,2,5,3} }; 11 | int rezult[N][K]; 12 | MultiplyMatrixs(matrixA,matrixB,rezult, N,M,K); 13 | 14 | //print 15 | for (int i = 0; i < N; i++) 16 | { 17 | for (int j = 0; j < K; j++) 18 | { 19 | std::cout << rezult[i][j]<<" "; 20 | } 21 | std::cout << std::endl; 22 | } 23 | } 24 | //how to set M and K here??? 25 | void MultiplyMatrixs(int matrixA[][3], int matrixB[][4], int matrixRezult[][4], int N, int M, int K) 26 | { 27 | for (int i = 0; i < N; i++) 28 | { 29 | for (int j = 0; j < K; j++) 30 | { 31 | int currElement = 0; 32 | for (int l = 0; l < M; l++) 33 | { 34 | currElement += matrixA[i][l] * matrixB[l][j]; 35 | } 36 | matrixRezult[i][j] = currElement; 37 | } 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /SI/Sem.05/PrintArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintArray(const int *arr, int size) 4 | { 5 | for (int i = 0; i < size; i++) { 6 | std::cout << arr[i] << std::endl; 7 | } 8 | } -------------------------------------------------------------------------------- /SI/Sem.05/ReverseArray.cpp: -------------------------------------------------------------------------------- 1 | void Swap(int &f, int &s) 2 | { 3 | int temp = f; 4 | f = s; 5 | s = temp; 6 | } 7 | 8 | void ReverseArray(int* arr, int size) 9 | { 10 | for (int i = 0; i < size / 2; i++) { 11 | Swap(arr[i], arr[size - 1 - i]); 12 | } 13 | } -------------------------------------------------------------------------------- /SI/Sem.06/BitwiseSimpleExamples.cpp: -------------------------------------------------------------------------------- 1 | bool isEven(unsigned n) 2 | { 3 | return !(n & 1); 4 | } 5 | 6 | unsigned powerOfTwo(unsigned n) 7 | { 8 | return (1 << n); 9 | } -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int SIZE = 20; 4 | const char mask[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 5 | 6 | int convertFromDecimalAndGiveLength(int num, int systemCode, char* buffer) { 7 | int count = 0; 8 | 9 | while (num != 0) { 10 | buffer[count++] = mask[num % systemCode];; 11 | num /= systemCode; 12 | } 13 | 14 | return count; 15 | } 16 | 17 | int convertToDecimal(int number, int systemCode) { 18 | int sum = 0; 19 | int power = 0; 20 | while (number != 0) { 21 | int remainder = number % 10; 22 | number /= 10; 23 | sum = sum + remainder * std::pow(systemCode, power++); 24 | } 25 | 26 | return sum; 27 | } 28 | 29 | void printNumbers(char* arrayNums, int length) { 30 | for (int i = length - 1; i >= 0; i--) { 31 | std::cout << (char)arrayNums[i]; 32 | } 33 | } 34 | 35 | void convertToASystem(int number, int currentCode, int desiredCode, char* buffer) { 36 | int numberDecimal = convertToDecimal(number, currentCode); 37 | int lengthOfBuffer = convertFromDecimalAndGiveLength(numberDecimal, desiredCode, buffer); 38 | printNumbers(buffer, lengthOfBuffer); 39 | } 40 | 41 | int main() 42 | { 43 | char buffer[SIZE]; 44 | int number = 1311; 45 | convertToASystem(number, 10, 16, buffer); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task02.cpp: -------------------------------------------------------------------------------- 1 | int countOnes(int number) { 2 | int numberOfOnes = 0; 3 | while (number != 0) { 4 | if ((number & 1) == 1) { 5 | numberOfOnes++; 6 | } 7 | number >>= 1; 8 | } 9 | 10 | return numberOfOnes; 11 | } 12 | -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int GetNum(int n) 4 | { 5 | return n & (n - 1); 6 | } 7 | 8 | int main() 9 | { 10 | int n; 11 | std::cin >> n; 12 | std::cout << GetNum(n); 13 | } 14 | -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task04.cpp: -------------------------------------------------------------------------------- 1 | //Solution by Georgi Teziev 2 | 3 | int findLastKBitsNumber(int number, int k) { 4 | int x = 1; 5 | 6 | for (int i = 0; i < k; i++) { 7 | x <<= 1; 8 | } 9 | x--; // make all ones 10 | 11 | return number & a; 12 | } -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task05.cpp: -------------------------------------------------------------------------------- 1 | bool is_second_number_bits_part_of_first_number(int first_number, int second_number) { 2 | return (first_number & second_number) == second_number; 3 | } -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task06.cpp: -------------------------------------------------------------------------------- 1 | int checkUniqueNumber(const int* arr, int size) 2 | { 3 | int uniqueNum = 0; 4 | for (int i = 0; i < size; i++){ 5 | uniqueNum ^= arr[i]; 6 | } 7 | return uniqueNum; 8 | } 9 | -------------------------------------------------------------------------------- /SI/Sem.06/Pract.06/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int powerOfTwo(int exp) 4 | { 5 | return (1 << exp); 6 | } 7 | 8 | void getSubNumber(const int* arr, int size, int mask) 9 | { 10 | for (int i = 0; i < size; i++) { 11 | if (mask & 1) { 12 | std::cout << arr[i] << " "; 13 | } 14 | mask >>= 1; 15 | } 16 | } 17 | 18 | void printSubNumbers(const int* arr, int size) 19 | { 20 | int length = powerOfTwo(size); 21 | for (int i = 0; i < length; i++) { 22 | std::cout << "[ "; 23 | getSubNumber(arr, size, i); 24 | std::cout << "]"<< " "; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/References/Task01.cpp: -------------------------------------------------------------------------------- 1 | void Swap(int& a, int& b) 2 | { 3 | int temp = a; 4 | a = b; 5 | b = temp; 6 | } 7 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/References/Task02.cpp: -------------------------------------------------------------------------------- 1 | void ToUpper(char& letter) 2 | { 3 | letter -= 'a' - 'A'; 4 | } 5 | 6 | void ToLower(char& letter) 7 | { 8 | letter += 'a' - 'A'; 9 | } 10 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/References/Task09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int inverse(int num) { 4 | int inversedNum = 0; 5 | while (num != 0) { 6 | (inversedNum *= 10) += num % 10; 7 | num /= 10; 8 | } 9 | return inversedNum; 10 | } 11 | int generateLast(int& num, int suffixLen) { 12 | int suffix = 0; 13 | for (int i = 0; i < suffixLen; i++) { 14 | (suffix *= 10) += num % 10; 15 | num /= 10; 16 | } 17 | suffix = inverse(suffix); 18 | return suffix; 19 | } 20 | 21 | void concat(int a, int& b) { 22 | int bCopy = b; 23 | int bLen = 0; 24 | while (b != 0) { 25 | bLen++; 26 | b /= 10; 27 | } 28 | for (int i = 0; i < bLen; i++) { 29 | a *= 10; 30 | } 31 | b = a + bCopy; 32 | } 33 | int main() { 34 | int a, b, k; 35 | std::cin >> a >> b >> k; 36 | int lastk = generateLast(a, k); 37 | concat(lastk, b); 38 | std::cout << a << " " << b; 39 | } -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/Strings/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void replaceSymbols(char* arr, char x, char a, char b) 5 | { 6 | int index = 0; 7 | int occurances = 0; 8 | 9 | while(arr[index] != '\0') 10 | { 11 | if (arr[index] == x) 12 | { 13 | (occurances % 2 == 0) ? arr[index] = a : arr[index] = b; 14 | occurances++; 15 | } 16 | 17 | index++; 18 | } 19 | } 20 | 21 | int main() 22 | { 23 | char arr[] = "H_ll_ my fri_nd"; 24 | replaceSymbols(arr, '_', 'e', 'o'); 25 | 26 | cout << arr; 27 | } 28 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/Strings/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int SIZE = 1024; 3 | int compareStrings(const char* firstString, const char* secondString) 4 | { 5 | size_t index = 0; 6 | while (firstString[index] == secondString[index]) 7 | { 8 | if (firstString[index] == '\0') 9 | { 10 | return 0; 11 | } 12 | index++; 13 | } 14 | return firstString[index] - secondString[index] < 0 ? -1 : 1; 15 | } 16 | 17 | int main() 18 | { 19 | char firstString[SIZE]; 20 | char secondString[SIZE]; 21 | std::cin >> firstString >> secondString; 22 | std::cout << compareStrings(firstString, secondString); 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/Strings/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | const int MAX_SIZE = 100; 3 | const int SIZE_SYMBOLS = 128; 4 | 5 | void PrintWithoutRepeating(char* str) 6 | { 7 | int symbolsCount[SIZE_SYMBOLS] = {}; 8 | 9 | char newStr[MAX_SIZE] = {}; 10 | int indexForNewStr = 0; 11 | 12 | for (int i = 0; i < MAX_SIZE; i++) 13 | { 14 | if (str[i] == '\0') 15 | { 16 | break; 17 | } 18 | if (symbolsCount[str[i]] == -1) 19 | { 20 | continue; 21 | } 22 | newStr[indexForNewStr] = str[i]; 23 | symbolsCount[str[i]] = -1; 24 | indexForNewStr++; 25 | } 26 | 27 | std::cout << newStr; 28 | } 29 | 30 | int main() 31 | { 32 | char str[MAX_SIZE]; 33 | std::cin >> str; 34 | 35 | PrintWithoutRepeating(str); 36 | } 37 | 38 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/Strings/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MaxLength = 1024; 4 | const int UpperCaseDifferance = 'A' - 'a'; 5 | 6 | int GetLenght(const char* str) 7 | { 8 | size_t index = 0; 9 | while (str[index] != '\0') { 10 | index++; 11 | } 12 | 13 | return index; 14 | } 15 | 16 | void Replace(char* str, size_t start, size_t end) 17 | { 18 | for (size_t i = start; i < end; i++) { 19 | str[i] = '*'; 20 | } 21 | } 22 | 23 | bool FindSubstring(const char* text,const char* substring, size_t substringLength, size_t currentLetter) 24 | { 25 | for (size_t j = 0; j < substringLength; j++) { 26 | int textDiferance = substring[j] - text[currentLetter + j]; 27 | 28 | if (textDiferance != 0 && textDiferance != UpperCaseDifferance && textDiferance != -UpperCaseDifferance) { 29 | return false; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | void ReplacedSubstringInText(char* text,const char* substring, size_t textLength, size_t substringLength) { 36 | for (size_t i = 0; i < textLength; i++) { 37 | int textDiferance = text[i] - substring[0]; 38 | if (textDiferance == 0 || textDiferance == UpperCaseDifferance || textDiferance == -UpperCaseDifferance) { 39 | if (FindSubstring(text, substring, substringLength, i)) { 40 | Replace(text, i, i + substringLength); 41 | i += substringLength - 1; 42 | } 43 | } 44 | } 45 | } 46 | 47 | int main() 48 | { 49 | char substring[MaxLength]; 50 | char text[MaxLength]; 51 | std::cin.getline(text, MaxLength); 52 | std::cin.getline(substring, MaxLength); 53 | int substringLength = GetLenght(substring); 54 | int textLength = GetLenght(text); 55 | ReplacedSubstringInText(text, substring, textLength, substringLength); 56 | std::cout << text; 57 | } 58 | -------------------------------------------------------------------------------- /SI/Sem.07/Pract.07/Strings/task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int MAX_SIZE = 1024; 4 | 5 | void findMaxPrefix(const char *first, const char *second, const char *third, char *result) { 6 | int index = 0; 7 | while(first[index] == second[index] && second[index] == third[index]) { 8 | index++; 9 | } 10 | for(int i = 0; i < index; i++) { 11 | result[i] = first[i]; 12 | } 13 | result[index] = '\0'; 14 | } 15 | 16 | int main() { 17 | char first[MAX_SIZE], second[MAX_SIZE], third[MAX_SIZE]; 18 | std::cin.getline(first, MAX_SIZE); 19 | std::cin.getline(second, MAX_SIZE); 20 | std::cin.getline(third, MAX_SIZE); 21 | 22 | char result[MAX_SIZE]; 23 | findMaxPrefix(first, second, third, result); 24 | std::cout << result << std::endl; 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/getCharOccurrencesCount.cpp: -------------------------------------------------------------------------------- 1 | size_t getCharOccurrencesCount(const char* text, char symbol) { 2 | size_t occurrences = 0; 3 | size_t index = 0; 4 | while (text[index] != '\0') { 5 | if (text[index] == symbol) { 6 | occurrences++; 7 | } 8 | index++; 9 | } 10 | 11 | return occurrences; 12 | } -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/myAtoi.cpp: -------------------------------------------------------------------------------- 1 | bool isDigit(char symbol) { 2 | return '0' <= symbol && symbol <= '9'; 3 | } 4 | 5 | bool isValidNumber(const char* number) { 6 | size_t length = myStrLen(number); 7 | 8 | if (length == 0) { 9 | return false; 10 | } 11 | 12 | if (length == 1 && number[0] == '-') { 13 | return false; 14 | } 15 | 16 | size_t index = number[0] == '-' ? 1 : 0; 17 | 18 | while (index < length) 19 | { 20 | if (!isDigit(number[index])) { 21 | return false; 22 | } 23 | 24 | index++; 25 | } 26 | 27 | return true; 28 | } 29 | 30 | // without strlen 31 | bool isValidNumberV2(const char* number) { 32 | if (number[0] == '\0') { 33 | return false; 34 | } 35 | 36 | if (number[0] == '-' && number[1] == '\0') { 37 | return false; 38 | } 39 | 40 | size_t index = number[0] == '-' ? 1 : 0; 41 | 42 | while (number[index] != '\0') { 43 | if (!isDigit(number[index])) { 44 | return false; 45 | } 46 | 47 | index++; 48 | } 49 | 50 | return true; 51 | } 52 | 53 | int myAtoi(const char* number) { 54 | if (!isValidNumber(number)) { 55 | return 0; 56 | } 57 | 58 | bool isNegative = number[0] == '-'; 59 | size_t index = isNegative ? 1 : 0; 60 | int result = 0; 61 | 62 | while (number[index] != '\0') { 63 | result = result * 10 + (number[index] - '0'); 64 | index++; 65 | } 66 | 67 | return isNegative ? -result : result; 68 | } -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/myStrCat.cpp: -------------------------------------------------------------------------------- 1 | // Version 1 2 | // Concatenation done in additional array 3 | void myStrCat(const char* str1, const char* str2, char* result) { 4 | size_t str1Length = myStrLen(str1); 5 | size_t str2Length = myStrLen(str2); 6 | 7 | for (size_t i = 0; i < str1Length; i++) { 8 | result[i] = str1[i]; 9 | } 10 | 11 | for (size_t i = 0; i < str2Length; i++) { 12 | result[str1Length + i] = str2[i]; 13 | } 14 | 15 | result[str1Length + str2Length] = '\0'; 16 | } 17 | 18 | // Version 2 19 | // Concatenation done in destination (as in stl strcat) 20 | void my_strcat(char* dest, const char* source) 21 | { 22 | size_t destLength = myStrLen(dest); 23 | size_t sourceLength = myStrLen(source); 24 | 25 | for (size_t i = 0; i < sourceLength; i++) { 26 | dest[destLength + i] = source[i]; 27 | } 28 | 29 | dest[destLength + sourceLength] = '\0'; 30 | } 31 | 32 | // Version 1 - without strLen 33 | // Concatenation done in additional array 34 | void myStrCatV2(const char* str1, const char* str2, char* result) { 35 | size_t resultIndex = 0, sourceIndex = 0; 36 | while (str1[sourceIndex] != '\0') { 37 | result[resultIndex++] = str1[sourceIndex++]; 38 | } 39 | 40 | sourceIndex = 0; 41 | while (str2[sourceIndex] != '\0') { 42 | result[resultIndex++] = str2[sourceIndex++]; 43 | } 44 | 45 | result[resultIndex] = '\0'; 46 | } 47 | 48 | // Version 2 - without strLen 49 | // Concatenation done in destination (as in stl strcat) 50 | void my_strcatV2(char* dest, const char* source) 51 | { 52 | size_t destIndex = 0; 53 | while(dest[destIndex] != '\0') { 54 | destIndex++; 55 | } 56 | 57 | size_t sourceIndex = 0; 58 | while (source[sourceIndex] != '\0') { 59 | dest[destIndex++] = source[sourceIndex++]; 60 | } 61 | 62 | dest[destIndex] = '\0'; 63 | } 64 | -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/myStrCmp.cpp: -------------------------------------------------------------------------------- 1 | int myStrCmp(const char* str1, const char* str2) { 2 | size_t i = 0; 3 | 4 | while (str1[i] == str2[i]) { 5 | if (str1[i] == '\0'/* && str2[i] == '\0'*/) { 6 | return 0; 7 | } 8 | 9 | i++; 10 | } 11 | 12 | return str1[i] - str2[i] > 0 ? 1 : -1; 13 | } -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/myStrCpy.cpp: -------------------------------------------------------------------------------- 1 | void myStrCopy(char* dest, const char* source) { 2 | size_t index = 0; 3 | while (source[index] != '\0') { 4 | dest[index] = source[index]; 5 | index++; 6 | } 7 | 8 | dest[index] = '\0'; 9 | } -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/myStrLen.cpp: -------------------------------------------------------------------------------- 1 | size_t myStrLen(const char* str) { 2 | size_t index = 0; 3 | 4 | while (str[index] != '\0') { 5 | index++; 6 | } 7 | 8 | return index; 9 | } -------------------------------------------------------------------------------- /SI/Sem.07/Solutions/searchInText.cpp: -------------------------------------------------------------------------------- 1 | bool searchInText(const char text[], const char word[]) { 2 | size_t textIndex = 0; 3 | 4 | while (text[textIndex] != '\0') { 5 | if (text[textIndex] == word[0]) { 6 | textIndex++; 7 | size_t wordIndex = 1; 8 | bool contains = true; 9 | while (word[wordIndex] != '\0') { 10 | if (text[textIndex] != word[wordIndex]) { 11 | contains = false; 12 | break; 13 | } 14 | 15 | textIndex++; 16 | wordIndex++; 17 | } 18 | 19 | if (contains) { 20 | return true; 21 | } 22 | } 23 | else { 24 | textIndex++; 25 | } 26 | } 27 | 28 | return false; 29 | } -------------------------------------------------------------------------------- /SI/Sem.07/images/example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.07/images/example1.png -------------------------------------------------------------------------------- /SI/Sem.07/images/example2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.07/images/example2.png -------------------------------------------------------------------------------- /SI/Sem.07/images/right-to-left.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.07/images/right-to-left.jfif -------------------------------------------------------------------------------- /SI/Sem.07/images/spiral-rule.jfif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.07/images/spiral-rule.jfif -------------------------------------------------------------------------------- /SI/Sem.08/Pract.08/FileExamples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const char UPPER_FOLDER[] = "../test.txt"; 5 | 6 | const char FILE_NAME[] = "PractFiles.cpp"; 7 | 8 | const int BUFFER_SIZE = 1024; 9 | 10 | // read the source code from your cpp and print in console 11 | void printSourceCode() { 12 | std::ifstream in(FILE_NAME); 13 | 14 | if (!in.is_open()) { 15 | std::cout << "Error"; 16 | return; 17 | } 18 | 19 | while (!in.eof()) { 20 | char buffer[BUFFER_SIZE]; 21 | in.getline(buffer, BUFFER_SIZE); 22 | std::cout << buffer; 23 | } 24 | 25 | in.close(); 26 | } 27 | 28 | void readCharArrayWithSpaces() { 29 | std::ofstream out(FILE_NAME); 30 | 31 | char test[] = "abc de"; 32 | out << test; 33 | 34 | out.close(); 35 | 36 | std::ifstream in(FILE_NAME); 37 | 38 | char test2[BUFFER_SIZE]; 39 | in.getline(test2, BUFFER_SIZE); 40 | 41 | in.close(); 42 | } 43 | -------------------------------------------------------------------------------- /SI/Sem.08/Pract.08/ProjectHelperFunctions.cpp: -------------------------------------------------------------------------------- 1 | // Clear console 2 | void clearConsole() { 3 | cout << "\033[;H"; // Moves cursor to the top left 4 | cout << "\033[J"; // Clears the console 5 | } 6 | 7 | // Remove all error flags and characters from the input buffer 8 | void clearInputBuffer() { 9 | // because of using both getline and cin we have to cin.ignore; 10 | // cin leaves the newline character in the stream which will be read as input from the getline 11 | 12 | cin.clear(); // clears errors flags from the cin 13 | cin.sync(); // discard unread characters from the input buffer 14 | cin.ignore(); // discard characters from the input buffer 15 | } -------------------------------------------------------------------------------- /SI/Sem.08/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int* CreateArray(size_t N) 4 | { 5 | int* arr = new int[N]; 6 | 7 | for (size_t i = 0; i < N; i++) { 8 | arr[i] = NULL; 9 | } 10 | 11 | return arr; 12 | } 13 | 14 | void Initialize(int* arr, size_t size) 15 | { 16 | for (size_t i = 0; i < size; i++) { 17 | cin >> arr[i]; 18 | } 19 | } 20 | 21 | void Print(const int* arr, size_t size) 22 | { 23 | for (size_t i = 0; i < size; i++) { 24 | std::cout << arr[i] << ' '; 25 | } 26 | } 27 | 28 | void Free(int* arr) 29 | { 30 | delete[] arr; 31 | } 32 | 33 | int main() 34 | { 35 | int N; cin >> N; 36 | int *arr = CreateArray(N); 37 | Initialize(arr, N); 38 | Print(arr, N); 39 | Free(arr); 40 | } 41 | -------------------------------------------------------------------------------- /SI/Sem.08/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int** CreateMatrix(size_t rows, size_t columns) 4 | { 5 | int** matrix = new int*[rows]; 6 | 7 | for (size_t i = 0; i < rows; i++) { 8 | matrix[i] = new int[columns]; 9 | } 10 | 11 | for (size_t i = 0; i < rows; i++) { 12 | for (size_t y = 0; y < columns; y++) { 13 | matrix[i][y] = NULL; //default initialization 14 | } 15 | } 16 | 17 | return matrix; 18 | } 19 | 20 | void Initialize(int** matrix, size_t rows, size_t columns) 21 | { 22 | for (size_t i = 0; i < rows; i++) { 23 | for (size_t y = 0; y < columns; y++) { 24 | std::cin >> matrix[i][y]; 25 | } 26 | } 27 | } 28 | void Print(int** matrix, size_t rows, size_t columns) 29 | { 30 | for (size_t i = 0; i < rows; i++) { 31 | for (size_t y = 0; y < columns; y++) { 32 | std::cout << matrix[i][y] << " "; 33 | } 34 | std::cout << std::endl; 35 | } 36 | } 37 | 38 | void Free(int** matrix, size_t rows) 39 | { 40 | for (size_t i = 0; i < rows; i++) { 41 | delete[] matrix[i]; 42 | } 43 | 44 | delete[] matrix; 45 | } 46 | -------------------------------------------------------------------------------- /SI/Sem.08/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int N, M, Q; 6 | std::cin >> N >> M >> Q; 7 | 8 | int*** matrix = new int** [N]; 9 | 10 | for (int i = 0; i < N; i++) { 11 | matrix[i] = new int* [M]; 12 | 13 | for (int j = 0; j < M; j++) { 14 | matrix[i][j] = new int[Q]; 15 | } 16 | } 17 | 18 | for (int i = 0; i < N; i++) { 19 | for (int j = 0; j < M; j++) { 20 | for (int k = 0; k < Q; k++) { 21 | std::cin >> matrix[i][j][k]; 22 | } 23 | } 24 | } 25 | 26 | for (int i = 0; i < N; i++) { 27 | for (int j = 0; j < M; j++) { 28 | delete[] matrix[i][j]; 29 | } 30 | delete[] matrix[i]; 31 | } 32 | delete[] matrix; 33 | } -------------------------------------------------------------------------------- /SI/Sem.08/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int** CreateMatrix(size_t size) 4 | { 5 | int** matrix = new int*[size]; 6 | 7 | for (size_t i = 0; i < size; i++) { 8 | matrix[i] = new int[size]; 9 | } 10 | 11 | for (size_t i = 0; i < size; i++) { 12 | for (size_t y = 0; y < size; y++) { 13 | std::cin >> matrix[i][y]; 14 | } 15 | } 16 | 17 | return matrix; 18 | } 19 | 20 | int SumAboveMainDiagonal(int** matrix, size_t size) 21 | { 22 | int sum = 0; 23 | for (size_t i = 0; i < size - 1; i++) { 24 | for (size_t j = i + 1; j < size; j++) { 25 | sum += matrix[i][j]; 26 | } 27 | } 28 | 29 | return sum; 30 | } 31 | 32 | int SumOfMainDiagonal(int **matrix, size_t size) 33 | { 34 | int sum = 0; 35 | for (size_t i = 0; i < size; i++) { 36 | sum += matrix[i][i]; 37 | } 38 | return sum; 39 | } -------------------------------------------------------------------------------- /SI/Sem.08/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | int** Transpose(int** matrix, size_t mRows, size_t mCol) 2 | { 3 | int rows = mRows; 4 | int columns = mCol; 5 | 6 | int** result = new int*[rows]; //result = transposition of matrix (columnsxrows) 7 | 8 | for (size_t i = 0; i < rows; i++) { 9 | result[i] = new int[columns]; 10 | } 11 | 12 | for (size_t i = 0; i < rows; i++) { 13 | for (size_t y = 0; y < columns; y++) { 14 | result[i][y] = matrix[y][i]; 15 | } 16 | } 17 | 18 | return result; 19 | } -------------------------------------------------------------------------------- /SI/Sem.08/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | int** CreateMatrix(size_t rows, size_t columns) 2 | { 3 | int** matrix = new int*[rows]; 4 | 5 | for (size_t i = 0; i < rows; i++) { 6 | matrix[i] = new int[columns]; 7 | } 8 | 9 | for (size_t i = 0; i < rows; i++) { 10 | for (size_t y = 0; y < columns; y++) { 11 | matrix[i][y] = NULL; //default initialization 12 | } 13 | } 14 | 15 | return matrix; 16 | } 17 | 18 | int** multiplyMatrix(int **A,size_t a_rows,size_t a_col, int **B,size_t b_rows,size_t b_col) //A && B are matrixes 19 | { 20 | int** multiply = CreateMatrix(a_rows, b_col); //a_rows x b_col -> all Zero's 21 | 22 | for (size_t i = 0; i < a_rows; i++) { 23 | for (size_t y = 0; y < b_col; y++) { 24 | for (size_t k = 0; k < a_rows; k++) { 25 | multiply[i][y] += A[i][k] * B[k][y]; 26 | } 27 | } 28 | } 29 | 30 | return multiply; 31 | } -------------------------------------------------------------------------------- /SI/Sem.08/images/Memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.08/images/Memory.png -------------------------------------------------------------------------------- /SI/Sem.08/images/exampleMemoryLeak.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.08/images/exampleMemoryLeak.png -------------------------------------------------------------------------------- /SI/Sem.08/memoryLeak.cpp: -------------------------------------------------------------------------------- 1 | //Line 1-8 are included in all .cpp files to check for memory leaks 2 | #define _CRTDBG_MAP_ALLOC 3 | #include 4 | #include 5 | 6 | #ifdef _DEBUG 7 | #define new new ( _NORMAL_BLOCK , __FILE__ , __LINE__ ) 8 | #endif 9 | 10 | int main() 11 | { 12 | _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); //first line in main function 13 | } -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Bonus/Factoriel.cpp: -------------------------------------------------------------------------------- 1 | // What will be the result of each function? 2 | 3 | // the result will always be 0 4 | // because when N is 1 we should calculate fact with N * fact(--N) 5 | // --N will give us 0 6 | // => we have to calculate fact(0) so we return 1 7 | // but N * 1 is actually 0 * 1, because N is already decremented 8 | size_t fact(size_t N) { 9 | if (N == 0) { 10 | return 1; 11 | } 12 | 13 | return N * fact(--N); 14 | } 15 | 16 | // Infinite loop - we will always call fact with N 17 | // Because N-- decrements the number after we use it 18 | size_t fact(size_t N) { 19 | if (N == 0) { 20 | return 1; 21 | } 22 | 23 | return N * fact(N--); 24 | } 25 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Bonus/FibonacciWithWrapperFunc.cpp: -------------------------------------------------------------------------------- 1 | size_t _fibonacci(unsigned int n, size_t* cache = nullptr) { 2 | if (n == 0 || n == 1) { 3 | return n; 4 | } 5 | 6 | if (cache[n - 1] == 0) { 7 | cache[n - 1] = _fibonacci(n - 1, cache); 8 | } 9 | 10 | if (cache[n - 2] == 0) { 11 | cache[n - 2] = _fibonacci(n - 2, cache); 12 | } 13 | 14 | return cache[n - 1] + cache[n - 2]; 15 | } 16 | 17 | size_t fibonacci(unsigned int n) { 18 | size_t* cache = new size_t[n](); 19 | size_t result = _fibonacci(n, cache); 20 | delete[] cache; 21 | 22 | return result; 23 | } -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Bonus/FibonacciWithoutWrapperFunc.cpp: -------------------------------------------------------------------------------- 1 | size_t fibonacci(unsigned int n, size_t* cache = nullptr) { 2 | if (n == 0 || n == 1) { 3 | return n; 4 | } 5 | 6 | // create array only on the first function call of the recursion 7 | bool arrayCreationIteration = false; 8 | if (!cache) { 9 | cache = new size_t[n](); 10 | arrayCreationIteration = true; 11 | } 12 | 13 | if (cache[n - 1] == 0) { 14 | cache[n - 1] = fibonacci(n - 1, cache); 15 | } 16 | 17 | if (cache[n - 2] == 0) { 18 | cache[n - 2] = fibonacci(n - 2, cache); 19 | } 20 | 21 | size_t result = cache[n - 1] + cache[n - 2]; 22 | 23 | if (arrayCreationIteration) { 24 | delete[] cache; 25 | } 26 | 27 | return result; 28 | } 29 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Task01.cpp: -------------------------------------------------------------------------------- 1 | bool Exist_digit(unsigned int val, unsigned int digit) 2 | { 3 | if (val == 0) { 4 | return false; 5 | } 6 | 7 | if (val % 10 == digit) { 8 | return true; 9 | } 10 | 11 | return Exist_digit(val / 10, digit) 12 | } 13 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Task02.cpp: -------------------------------------------------------------------------------- 1 | unsigned power(unsigned x, unsigned n) 2 | { 3 | if (n == 0) { 4 | return 1; 5 | } 6 | 7 | if (n % 2 == 1) { 8 | return x * power(x, n - 1); 9 | } 10 | 11 | unsigned temp = power(x, n / 2); 12 | return temp * temp; 13 | } 14 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printReverse(int num) 4 | { 5 | if (num == 0) { 6 | return; 7 | } 8 | 9 | std::cout << num % 10 << " "; 10 | printReverse(num / 10); 11 | } 12 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Task04.cpp: -------------------------------------------------------------------------------- 1 | bool IsRising(const float* seq, int size) 2 | { 3 | if (size < 2) { 4 | return true; 5 | } 6 | 7 | if (*seq > *(seq+1)) { //Same as if(seq[0]>seq[1]) 8 | return false; 9 | } 10 | 11 | return IsRising(++seq, size - 1); 12 | } 13 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Task05.cpp: -------------------------------------------------------------------------------- 1 | bool isPalindrome(const char* array, int leftIndx, int rightIndx) 2 | { 3 | if (leftIndx >= rightIndx) { 4 | return true; 5 | } 6 | 7 | if (array[leftIndx] != array[rightIndx]) { 8 | return false; 9 | } 10 | 11 | return isPalindrome(array, leftIndx + 1, rightIndx - 1); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /SI/Sem.09/Pract.09/Task06.cpp: -------------------------------------------------------------------------------- 1 | void Resize(int*& array, int& arraySize, int element, int index) 2 | { 3 | int* newArray = new int[++arraySize]; 4 | 5 | for (size_t i = 0; i < index; i++) { 6 | newArray[i] = array[i]; 7 | } 8 | 9 | newArray[index] = element; 10 | 11 | for (size_t i = index + 1; i < arraySize; i++) { 12 | newArray[i] = array[i - 1]; 13 | } 14 | 15 | delete[] array; 16 | array = newArray; 17 | } 18 | 19 | void AddElement(int*& array, int& arraySize, int element, int index = 0) 20 | { 21 | if (arraySize == 0 || element >= array[arraySize - 1]) { 22 | Resize(array, arraySize, element, arraySize); 23 | return; 24 | } 25 | 26 | if (element < array[index]) { 27 | Resize(array, arraySize, element, index); 28 | return; 29 | } 30 | 31 | AddElement(array, arraySize, element, index + 1); 32 | } 33 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Binary search - way1 3 | * with argument size 4 | */ 5 | bool BinarySearch(const int* arr, unsigned size, int num) 6 | { 7 | if (size == 0) { 8 | return false; 9 | } 10 | 11 | unsigned mid = size / 2; 12 | 13 | if (arr[mid] == num) { 14 | return true; 15 | } 16 | 17 | if (arr[mid] > num) { 18 | return BinarySearch(arr, mid, num); 19 | } 20 | 21 | return BinarySearch(arr + mid + 1, size - mid - 1, num); 22 | } 23 | 24 | //=========================================================== 25 | 26 | /* Binary search - way2 27 | * int l = left - first element 28 | * int r = right - last element 29 | */ 30 | bool BinarySearch(const int* arr, int l, int r, int num) 31 | { 32 | int mid = l + (r - 1) / 2; 33 | 34 | if (arr[mid] == num) { //found the desired element 35 | return true; 36 | } 37 | 38 | if (arr[mid] > num) { 39 | return BinarySearch(arr, l, mid - 1, num); 40 | } 41 | 42 | //arr[mid] < num; 43 | return BinarySearch(arr, mid + 1, r, num); 44 | } 45 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/BubbleSort.cpp: -------------------------------------------------------------------------------- 1 | // An optimized version of Bubble Sort 2 | void BubbleSort(int* arr, int n) 3 | { 4 | bool swapped; 5 | 6 | for (size_t i = 0; i < n-1; i++) { 7 | swapped = false; 8 | for (size_t j = 0; j < n-i-1; j++) { 9 | if (arr[j] > arr[j+1]) { 10 | swap(&arr[j], &arr[j+1]); 11 | swapped = true; 12 | } 13 | 14 | // If no two elements were swapped by inner loop, then break 15 | if (swapped == false) { 16 | break; 17 | } 18 | } 19 | } 20 | } 21 | 22 | // Bubble sort - recursive 23 | void BubbleSort(int* arr, int n) 24 | { 25 | if (n == 1) { 26 | return; 27 | } 28 | 29 | int count = 0; 30 | // One pass of bubble sort. After this pass, the largest element is moved (or bubbled) to end. 31 | for (size_t i = 0; i < n-1; i++) { 32 | if (arr[i] > arr[i+1]) { 33 | swap(&arr[i], &arr[i+1]); 34 | count++; 35 | } 36 | } 37 | 38 | // Check if any recursion happens or not. If any recursion is not happen then return 39 | if (count == 0) { 40 | return; 41 | } 42 | 43 | // Largest element is fixed, recur for remaining array 44 | BubbleSort(arr, n-1); 45 | } 46 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/CountingSort.cpp: -------------------------------------------------------------------------------- 1 | void CountingSort(int* arr, unsigned size, int max) { 2 | int* res = new int[max+1]; 3 | for (int i = 0; i <= max; i++) { 4 | res[i] = 0; 5 | } 6 | 7 | for (unsigned i = 0; i < size; i++) { 8 | res[arr[i]]++; 9 | } 10 | 11 | for (int i = 1; i <= max; i++) { 12 | res[i] += res[i - 1]; 13 | } 14 | 15 | int* temp = new int[size]; 16 | for (unsigned i = size - 1; i >= 0; i--) { 17 | int num = arr[i]; 18 | int index = res[num] - 1; 19 | temp[index] = num; 20 | res[num]--; 21 | } 22 | 23 | for (unsigned i = 0; i < size; ++i) { 24 | arr[i] = temp[i]; 25 | } 26 | 27 | delete[] res; 28 | delete[] temp; 29 | } 30 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/LinearSearch.cpp: -------------------------------------------------------------------------------- 1 | //Way - 1 2 | bool linearSearch(const int* arr, unsigned size, int num) 3 | { 4 | if (size == 0) { 5 | return false; 6 | } 7 | 8 | return *arr == num || linearSearch(arr + 1, size - 1, num); 9 | } 10 | 11 | //============================================================== 12 | 13 | //Way - 2 14 | bool linearSearch(const int *arr, int size, int number) 15 | { 16 | if (size == 0) { 17 | return false; 18 | } 19 | 20 | if (arr[size - 1] == number) { 21 | return true; 22 | } 23 | 24 | return linearSearch(arr, size - 1, number); 25 | } 26 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/Pow.cpp: -------------------------------------------------------------------------------- 1 | int power(int a, int n) 2 | { 3 | if (n == 0) { 4 | return 1; 5 | } 6 | 7 | if (n & 1 == 0) { 8 | return power(a * a, n / 2); 9 | } 10 | 11 | return a * power(a * a, n / 2); 12 | } 13 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/SearchInText.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isPrefix(const char* text, const char* pattern) 4 | { 5 | if (*pattern == '\0') { 6 | return true; 7 | } 8 | 9 | return *text == *pattern && isPrefix(text + 1, pattern + 1); 10 | } 11 | 12 | bool searchInText(const char* text, const char* pattern) 13 | { 14 | if (*text == '\0') { 15 | return *pattern == '\0'; 16 | } 17 | 18 | return isPrefix(text, pattern) || searchInText(text + 1, pattern); 19 | } 20 | 21 | int main() 22 | { 23 | char str[] = "hello friends how are you"; 24 | char pattern[] = "friends!"; 25 | 26 | std::cout << searchInText(str, pattern); 27 | } 28 | -------------------------------------------------------------------------------- /SI/Sem.09/Solutions/SelectionSort.cpp: -------------------------------------------------------------------------------- 1 | void selectionSort(int* arr, unsigned size) { 2 | for (unsigned i = 0; i < size - 1; ++i) { 3 | int min = arr[i]; 4 | int minIndex = i; 5 | 6 | for (unsigned j = i + 1; j < size; ++j) { 7 | if (arr[j] < min) { 8 | min = arr[j]; 9 | minIndex = j; 10 | } 11 | } 12 | 13 | if (minIndex != i) { 14 | std::swap(arr[i], arr[minIndex]); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /SI/Sem.09/images/Head-recursion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/Head-recursion.jpg -------------------------------------------------------------------------------- /SI/Sem.09/images/Indirect-direct-recursion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/Indirect-direct-recursion.png -------------------------------------------------------------------------------- /SI/Sem.09/images/Nested-recursion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/Nested-recursion.jpg -------------------------------------------------------------------------------- /SI/Sem.09/images/Recursion-simpsons.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/Recursion-simpsons.gif -------------------------------------------------------------------------------- /SI/Sem.09/images/SumRange.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/SumRange.gif -------------------------------------------------------------------------------- /SI/Sem.09/images/Tail-recursion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/Tail-recursion.jpg -------------------------------------------------------------------------------- /SI/Sem.09/images/Tree-recursion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/Tree-recursion.jpg -------------------------------------------------------------------------------- /SI/Sem.09/images/factorial-formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/factorial-formula.png -------------------------------------------------------------------------------- /SI/Sem.09/images/factorial.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/factorial.gif -------------------------------------------------------------------------------- /SI/Sem.09/images/fib.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/fib.gif -------------------------------------------------------------------------------- /SI/Sem.09/images/fib.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/fib.png -------------------------------------------------------------------------------- /SI/Sem.09/images/gcd-definition.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.09/images/gcd-definition.png -------------------------------------------------------------------------------- /SI/Sem.10/examples/example1.cpp: -------------------------------------------------------------------------------- 1 | //Credits for example to Angel Dimitriev 2 | 3 | #include 4 | 5 | int get_something_from_array(const int* arr, size_t size, int result, int (*func)(int,int) ) 6 | { 7 | if (size == 0) return result; 8 | return get_something_from_array(arr + 1, size - 1, func(result, *arr), func); 9 | } 10 | 11 | int max(int a, int b) 12 | { 13 | return a > b ? a : b; 14 | } 15 | 16 | int getMaxFromArray(const int* arr, size_t size) 17 | { 18 | return get_something_from_array(arr, size, INT_MIN, max); 19 | } 20 | 21 | int sum(int x, int y) 22 | { 23 | return x + y; 24 | } 25 | 26 | int getSumOnArray(const int* arr, size_t size) 27 | { 28 | return get_something_from_array(arr, size, 0, sum); 29 | } 30 | 31 | int main() 32 | { 33 | const size_t SIZE = 4; 34 | int arr[SIZE] = { 1, 2, 3, 4 }; 35 | 36 | std::cout << getSumOnArray(arr, SIZE) << std::endl 37 | << getMaxFromArray(arr, SIZE) << std::endl; 38 | } -------------------------------------------------------------------------------- /SI/Sem.10/examples/example2.cpp: -------------------------------------------------------------------------------- 1 | //Credits for example to Angel Dimitriev 2 | 3 | #include 4 | using namespace std; 5 | 6 | bool isCapital(char ch) 7 | { 8 | return ch >= 'A' && ch <= 'Z'; 9 | } 10 | 11 | bool isLower(char ch) 12 | { 13 | return ch >= 'a' && ch <= 'z'; 14 | } 15 | 16 | bool isDigit(char ch) 17 | { 18 | return ch >= '0' && ch <= '9'; 19 | } 20 | 21 | unsigned getCharCountCondition(const char* str, bool (*pred)(char)) 22 | { 23 | int count = 0; 24 | while(*str) 25 | { 26 | if(pred(*str)) 27 | count++; 28 | str++; 29 | } 30 | return count; 31 | } 32 | 33 | unsigned getLowerCount(const char* str) 34 | { 35 | return getCharCountCondition(str, isLower); 36 | } 37 | 38 | 39 | unsigned getCapitalCount(const char* str) 40 | { 41 | return getCharCountCondition(str, isCapital); 42 | } 43 | 44 | unsigned getDigitsCount(const char* str) 45 | { 46 | return getCharCountCondition(str, isDigit); 47 | } 48 | 49 | unsigned getWhitespacesCount(const char* str) 50 | { 51 | return getCharCountCondition(str, [](char ch) {return ch == ' ';} ); 52 | } 53 | 54 | int main() 55 | { 56 | char str[] = "Hello World"; 57 | 58 | cout << getWhitespacesCount(str); 59 | 60 | return 0; 61 | } -------------------------------------------------------------------------------- /SI/Sem.10/examples/example3.cpp: -------------------------------------------------------------------------------- 1 | // Credits for example to Elena Tuparova 2 | 3 | #include 4 | 5 | typedef int num; 6 | typedef num (*next) (num); 7 | 8 | int multiply2(int n) { 9 | return n * 2; 10 | } 11 | 12 | double sumFunc(int n, double (*func) (double), next nextI) { 13 | double sum = 0; 14 | for (int i = 1; i <= n; i = nextI(i)) { 15 | sum += func(i); 16 | } 17 | return sum; 18 | } 19 | 20 | int main() { 21 | 22 | num i = 1; 23 | 24 | std::cout << sumFunc(16, sin, multiply2) << std::endl; 25 | std::cout << sumFunc(16, sqrt, [](int n){ return n + 1; }) << std::endl;// example for lambda 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /SI/Sem.10/examples/example4.cpp: -------------------------------------------------------------------------------- 1 | //Credits for example to Elena Tuparova 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int i = 4; 8 | 9 | [=]() mutable { // here *mutable* is needed to allow the captured-by-value variable to be changed inside the lambda 10 | i++; 11 | std::cout << "Inside first lambda: " << i << std::endl; 12 | } (); // don't forget the brackets in order to call the function! 13 | std::cout << "After first lambda: " << i << std::endl; // however, i is not changed after the scope of the lambda 14 | 15 | [&] { i++; } (); 16 | std::cout << "After second lambda: " << i << std::endl; 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /SI/Sem.10/img/Function-pointer-declaration.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.10/img/Function-pointer-declaration.png -------------------------------------------------------------------------------- /SI/Sem.10/img/Lambda-syntax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.10/img/Lambda-syntax.png -------------------------------------------------------------------------------- /SI/Sem.10/img/lambda-extended-syntax.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/SI/Sem.10/img/lambda-extended-syntax.gif -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Tasks/README.md: -------------------------------------------------------------------------------- 1 |  2 | # Задачи 3 | 4 | **Задача 1:** 5 | 6 | **Labyrinth.**
7 | Имате дадена матрица 5x5, тя представя лабиринт, като с 0 означаваме, че през съответната позиция от матрицата може да се премине, докато с 1 означаваме, че тази позиция е ограда и не можем да преминем през нея. Да се напише функция, която по даден лабиринт, връща дали може да се стигне от една точка до друга. 8 | 9 | **Задача 2**: 10 | 11 | Имаме калкулатор, който може да изпълнява следните операции: 12 | - Да дели число X на 2 13 | - Да дели число X на 3 14 | - Да извади 1 от числото X 15 | 16 | Открийте колко най-малко операции трябва да изпълним, за да получим 1 от числото N. 17 | 18 | **Пример**: 19 | 20 | **Вход:** 19 21 | 22 | **Изход:** 3 23 | 24 | (Обяснение към примера: 19 -> 18 -> 6 -> 2 -> 1) 25 | 26 | **Задача 3**: 27 | 28 | Да се реализира функция, която приема като параметър низ и връща указател към началото на лексикографски най-малка непразна дума в него. Дума е непрекъсната последователност от малки и големи латински букви и цифри, всички останали символи се считат за разделители. 29 | 30 | **Примери**: 31 | 32 | **Вход**: 33 | me? why always me? 34 | 35 | **Изход**: 36 | always me? 37 | 38 | **Вход**: 39 | this sentence is false. 40 | 41 | **Изход**: 42 | falsе. 43 | 44 | **Вход**: 45 | What is C++? - A 37-year old programming language. 46 | 47 | **Изход**: 48 | 37-year old… 49 | 50 | **Задача 4**: 51 | 52 | Да се реализира функция, която приема като параметър низ и връща нов низ, който се състои само от малките букви в подадения, като са сортирани. 53 | 54 | **Примери**: 55 | 56 | **Вход**: 57 | AbbaBBcb 58 | 59 | **Изход**: 60 | abbbc 61 | -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Tasks/Task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int Min(int a, int b) { 4 | return a < b ? a : b; 5 | } 6 | 7 | int Min(int a, int b, int c) { 8 | return Min(Min(a, b), c); 9 | } 10 | 11 | int GetMinOperations(size_t N, int* cache) 12 | { 13 | if (N == 1) 14 | { 15 | return 0; 16 | } 17 | 18 | if (cache[N] != 0) 19 | { 20 | return cache[N]; 21 | } 22 | 23 | int div3 = INT_MAX; 24 | int div2 = INT_MAX; 25 | int s1 = INT_MAX; 26 | 27 | if (N % 3 == 0) 28 | { 29 | div3 = GetMinOperations(N / 3, cache); 30 | } 31 | if (N % 2 == 0) 32 | { 33 | div2 = GetMinOperations(N / 2, cache); 34 | } 35 | s1 = GetMinOperations(N - 1, cache); 36 | 37 | int result = 1 + Min(div3, div2, s1); 38 | cache[N] = result; 39 | return result; 40 | } 41 | 42 | 43 | int GetMinOperations (int n) { 44 | int* cache = new int[n + 1]; 45 | for (size_t i = 0; i < n+1; i++) 46 | { 47 | cache[i] = 0; 48 | } 49 | 50 | int result = GetMinOperations(n, cache); 51 | delete[] cache; 52 | return result; 53 | } 54 | 55 | int GetMinOpNoCache(int n) { 56 | if (n == 1) 57 | { 58 | return 0; 59 | } 60 | 61 | int div3 = INT_MAX; 62 | int div2 = INT_MAX; 63 | int s1 = INT_MAX; 64 | 65 | if (n % 3 == 0) 66 | { 67 | div3 = GetMinOpNoCache(n / 3); 68 | } 69 | if (n % 2 == 0) 70 | { 71 | div2 = GetMinOpNoCache(n / 2); 72 | } 73 | s1 = GetMinOpNoCache(n - 1); 74 | 75 | int result = Min(div3, div2, s1) + 1; 76 | 77 | return result; 78 | } 79 | 80 | int main() 81 | { 82 | std::cout << GetMinOperations(19); //4 83 | } 84 | -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Tasks/Task03.cpp: -------------------------------------------------------------------------------- 1 | bool IsValidSymbol(char ch) { 2 | return ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9'; 3 | } 4 | 5 | bool IsLexLess(const char* w1, const char* w2) { 6 | 7 | while (IsValidSymbol(*w1) && IsValidSymbol(*w2)) { 8 | if (*w1 < *w2) { 9 | return true; 10 | } 11 | if (*w1 > *w2) { 12 | return false; 13 | } 14 | w1++; 15 | w2++; 16 | } 17 | 18 | return !IsValidSymbol(*w1); 19 | } 20 | 21 | const char* GetLexMin(const char* text) { 22 | const char* iter = text; 23 | const char* minIter = nullptr; 24 | 25 | while (*iter != '\0') { 26 | if (IsValidSymbol(*iter) && ((iter == text) || !IsValidSymbol(*(iter - 1)))) { 27 | if (minIter == nullptr) { 28 | minIter = iter; 29 | } 30 | else { 31 | if (IsLexLess(iter, minIter)) { 32 | minIter = iter; 33 | } 34 | } 35 | } 36 | ++iter; 37 | } 38 | 39 | return minIter; 40 | } 41 | -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Tasks/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const int ALP_SIZE = 26; 4 | 5 | bool IsLower(char ch) { 6 | return ch >= 'a' && ch <= 'z'; 7 | } 8 | 9 | void Fill(int hist[ALP_SIZE], char* result) { 10 | int i = 0; 11 | 12 | while (*result != '\0') { 13 | while (hist[i] > 0) { 14 | *result = i + 'a'; 15 | ++result; 16 | --hist[i]; 17 | } 18 | ++i; 19 | } 20 | } 21 | 22 | char* SortedLowerLetters(const char* text) { 23 | 24 | int hist[ALP_SIZE] = { 0 }; 25 | int counter = 0; 26 | 27 | while (*text != '\0') { 28 | if (IsLower(*text)) { 29 | int index = *text - 'a'; 30 | hist[index]++; 31 | counter++; 32 | } 33 | ++text; 34 | } 35 | 36 | char* result = new char[counter + 1]; 37 | result[counter] = '\0'; 38 | Fill(hist, result); 39 | return result; 40 | } 41 | 42 | int main() 43 | { 44 | char* res = SortedLowerLetters("AacBbbWb"); 45 | std::cout << res; 46 | delete[] res; 47 | } 48 | -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Theory/MemoryQuestions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int q1() { 4 | // How much memory is allocated in the following expressions and where? 5 | // we assume that the pointer takes 4 bytes 6 | short s = 1; // Stack: 2 Heap: 0 7 | int a = 2; // Stack: 4 Heap: 0 8 | bool b = true; // Stack: 1 Heap: 0 9 | bool* p = new bool; // Stack: 4 Heap: 1 10 | char c = '\0'; // Stack: 1 Heap: 0 11 | double d = 11.1; // Stack: 8 Heap: 0 12 | int arr1[40] = { 0 }; // Stack: 164 Heap: 0 13 | // arr1 is a static array => it allocates one pointer and 40 * 4 int 14 | 15 | int* arr2 = new int(40); // Stack: 4 Heap: 4 16 | int* arr3 = new int[40]; // Stack: 4 Heap: 160 17 | char test[] = "abcd"; // Stack: 9 Heap: 0 18 | int** matrix = new int*[10]; // Stack: 4 Heap: 10 * 4 19 | for (size_t i = 1; i < 10; i++) { 20 | matrix[i] = new int[i]; // new int(i) vs new int[i] 21 | } 22 | // new int(i) 23 | // Stack: 4 Heap: 10 * 4 + 9 * 4 24 | // new int[i] 25 | // Stack: 4 Heap: 10 * 4 + 4(1 + 2 .... + 9) 26 | 27 | //int arr1[40] = { 0 }; // Stack: 164 Heap: 28 | //int* arr2 = new int(40); // Stack: 4 Heap: 4 29 | //int* arr3 = new int[40]; // Stack: 4 Heap: 160 30 | // Which of the following statements is/are incorrect? 31 | // delete[] arr1; - arr1 is not a dynamic array 32 | // delete[] arr2; - arr2 is a pointer to a int, not to a int[] 33 | // delete[] arr3; - correct 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Theory/RecursionFibonacciProblem.cpp: -------------------------------------------------------------------------------- 1 | // Is there a problem with this implementation? 2 | // Answer: some of the fibonacci numbers are calculated more than once 3 | // What type of recursion is this? 4 | // Answer: Tree recursion 5 | // What will the function return if n = 50? 6 | // Answer: The function will require too much time 7 | size_t fibonacci(unsigned int n) { 8 | if (n == 0 || n == 1) { 9 | return n; 10 | } 11 | 12 | return fibonacci(n - 1) + fibonacci(n - 2); 13 | } 14 | 15 | // n - the number we are searching 16 | // cache - pointer to an array of the already calculated fibonacci numbers 17 | size_t _fibonacci(unsigned int n, size_t* cache = nullptr) { 18 | if (n == 0 || n == 1) { // default case 19 | return n; 20 | } 21 | 22 | // if we have not calculated _fibonacci(n - 1) 23 | // calculate it and then write it in the cache 24 | if (cache[n - 1] == 0) { 25 | cache[n - 1] = _fibonacci(n - 1, cache); 26 | } 27 | 28 | // if we have not calculated _fibonacci(n - 2) 29 | // calculate it and then write it in the cache 30 | if (cache[n - 2] == 0) { 31 | cache[n - 2] = _fibonacci(n - 2, cache); 32 | } 33 | 34 | // return the sum of the numbers from the cache 35 | return cache[n - 1] + cache[n - 2]; 36 | } 37 | 38 | // Wrapper function which manages the creation and the deletion of the cache 39 | size_t fibonacciFast(unsigned int n) { 40 | // allocated cache 41 | size_t* cache = new size_t[n](); 42 | size_t result = _fibonacci(n, cache); 43 | 44 | // delete the allocated cache 45 | delete[] cache; 46 | 47 | return result; 48 | } -------------------------------------------------------------------------------- /Utils/Consultations/IS-13.01/Theory/RecursionQuestions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // What type of recursion is this, how will it print the array? 4 | // Array: [1, 2, 3, 4, 5], size: 5 5 | // Answer: Head recursion => 1 2 3 4 5 6 | void printArr1(int* arr, size_t size) { 7 | if (size == 0) { 8 | return; 9 | } 10 | 11 | printArr1(arr, size - 1); 12 | std::cout << arr[size - 1] << " "; 13 | } 14 | 15 | // What type of recursion is this, how will it print the array? 16 | // Array: [1, 2, 3, 4, 5], size: 5 17 | // Answer: Tail recursion => 5 4 3 2 1 18 | void printArr2(int* arr, int size) { 19 | if (size == 0) { 20 | return; 21 | } 22 | 23 | std::cout << arr[size - 1] << " "; 24 | printArr2(arr, size - 1); 25 | } 26 | 27 | // What will the function return if n = 5? 28 | // Answer: the result will always be 0 29 | // because when N is 1 we should calculate fact with N * fact(--N) 30 | // --N will give us 0 31 | // => we have to calculate fact(0) so we return 1 32 | // but N * 1 is actually 0 * 1, because N is already decremented 33 | size_t fact1(size_t n) { 34 | if (n == 0) { 35 | return 1; 36 | } 37 | 38 | return n * fact1(--n); 39 | } 40 | 41 | // What will the function return if n = 5? 42 | // Answer: Infinite loop - we will always call fact with N 43 | // Because N-- decrements the number after we use it 44 | size_t fact2(size_t n) { 45 | if (n == 0) { 46 | return 1; 47 | } 48 | 49 | return n * fact2(n--); 50 | } -------------------------------------------------------------------------------- /Utils/Consultations/IS-6.11/README.md: -------------------------------------------------------------------------------- 1 | ## Конспект: 2 | - [Тема 1:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.01) Увод. Примитивни типове данни. Преобразуване между типове. Оператори. 3 | - [Тема 2:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.02) Условни конструкции - if, switch и тернарен оператор. 4 | - [Тема 3:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.03) Цикли. Оператори break и continue. 5 | - [Тема 4:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.04) Функции. 6 | 7 | ## Лектори 8 | - Никола Светославов 9 | - Ангел Димитриев 10 | 11 | Проведена на: 06.11.2022г. -------------------------------------------------------------------------------- /Utils/Consultations/IS-6.11/Task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Задача: 3 | * Напишете функция, която приема естествено число и връща ново число, което има същите цифри, но в сортиран вид. 4 | * 5 | * Пример: 6 | * Вход: 531261 7 | * Изход: 112356 8 | * 9 | */ 10 | 11 | int countDigitOccurences(int n, int digit) 12 | { 13 | int count = 0; 14 | while(n != 0) 15 | { 16 | if(n % 10 == digit) 17 | count++; 18 | n /= 10; 19 | } 20 | return count; 21 | } 22 | 23 | int concatAtBack(int n, int digit, int howManyTimes) 24 | { 25 | for(int i = 0; i < howManyTimes; i++) 26 | { 27 | (n *= 10) += digit; 28 | } 29 | return n; 30 | } 31 | 32 | int sortNumber(int n) 33 | { 34 | int result = 0; 35 | for(int i = 1; i <= 9; i++) 36 | { 37 | int currentDigitOccurences = countDigitOccurences(n, i); 38 | result = concatAtBack(result, i, currentDigitOccurences); 39 | } 40 | return result; 41 | } 42 | 43 | int main() 44 | { 45 | cout<< sortNumber(912332); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Utils/Consultations/IS-6.11/Task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Задача: 3 | * Да се напише функция, която приема две естествени числа и връща ново число, което е тяхната конкатенация. 4 | * 5 | * Пример: 6 | * Вход: 19 32 7 | * Изход: 1932 8 | * 9 | */ 10 | 11 | unsigned int concatDigitOnBack(unsigned int a, unsigned int digit) 12 | { 13 | if(digit > 9) 14 | return 0; 15 | return a * 10 + digit; 16 | } 17 | 18 | unsigned int reverse(unsigned int n) 19 | { 20 | unsigned reversed = 0; 21 | 22 | while(n != 0) 23 | { 24 | (reversed *= 10) += (n % 10); 25 | n/=10; 26 | } 27 | return reversed; 28 | } 29 | 30 | unsigned int concatNumbers(unsigned int a, unsigned int b) 31 | { 32 | unsigned int result = a; 33 | unsigned int reversedB = reverse(b); 34 | 35 | while(reversedB != 0) 36 | { 37 | int lastDigit = reversedB % 10; 38 | result = concatDigitOnBack(result, lastDigit); 39 | reversedB /= 10; 40 | } 41 | return result; 42 | } 43 | 44 | int main() 45 | { 46 | cout< 4 | using namespace std; 5 | 6 | int reverse(int n) 7 | { 8 | int result = 0; 9 | while(n != 0) 10 | { 11 | (result *= 10) += (n % 10); 12 | n /= 10; 13 | } 14 | return result; 15 | } 16 | 17 | bool isPalindome(int n) 18 | { 19 | return n == reverse(n); 20 | } 21 | 22 | int longestSubnumberPalindrome(int n) 23 | { 24 | int longestPalindrome = 0; 25 | while(n >= 10) 26 | { 27 | int reverseN = reverse(n); 28 | 29 | while(reverseN != 0) 30 | { 31 | if(isPalindome(reverseN) && reverseN > longestPalindrome) 32 | longestPalindrome = reverseN; 33 | reverseN /= 10; 34 | } 35 | 36 | n /= 10; 37 | } 38 | return longestPalindrome; 39 | } 40 | 41 | int main() 42 | { 43 | cout << longestSubnumberPalindrome(1234); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Utils/Consultations/SI-12.11/README.md: -------------------------------------------------------------------------------- 1 | ## Конспект: 2 | - [Тема 1:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.01) Увод. Примитивни типове данни. Преобразуване между типове. Оператори. 3 | - [Тема 2:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.02) Условни конструкции - if, switch и тернарен оператор. 4 | - [Тема 3:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.03) Цикли. Оператори break и continue. 5 | - [Тема 4:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/IS/Sem.04) Функции. 6 | 7 | ## Лектори 8 | - Никола Светославов 9 | - Ангел Димитриев 10 | 11 | Проведена на: 12.11.2022г. -------------------------------------------------------------------------------- /Utils/Consultations/SI-12.11/Task1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Задача: 3 | * K ще наричаме подчисло на N, ако мултимножеството от цифрите на K е подмножество на мултимножеството на цифрите на N. 4 | * Напише функция, която приема две естествени числа N и K и връща дали K е подчисло на N 5 | * 6 | * Примери: 7 | * Вход: 13324, 1334 8 | * Изход: true 9 | * 10 | * Вход: 1233 433 11 | * Изход: false 12 | * 13 | */ 14 | 15 | unsigned int getDigitOcurrences(unsigned int n, unsigned int digit) 16 | { 17 | if (digit > 9) 18 | return 0; ///error 19 | 20 | if (n == 0 && digit == 0) 21 | return 1; 22 | 23 | int count = 0; 24 | 25 | while (n != 0) 26 | { 27 | int lastDigit = n % 10; 28 | if (lastDigit == digit 29 | count++; 30 | n /= 10; 31 | } 32 | return count; 33 | } 34 | 35 | bool isSubset(unsigned int n, unsigned int k) 36 | { 37 | for (int digit = 0; digit <= 9; digit++) 38 | { 39 | if (getDigitOcurrences(n, digit) < getDigitOcurrences(k, digit)) 40 | return false; 41 | } 42 | return true; 43 | } -------------------------------------------------------------------------------- /Utils/Consultations/SI-12.11/Task2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Задача: 3 | * Подчисло на N ще наричаме число, което е получено чрез премахването на 0 или няколко цифри на N. 4 | * Напишете функция, която приема естествено число N и връща сумата на всички подчисла на N. 5 | * 6 | * Пример: 7 | * Вход: 123 8 | * Изход: 177 9 | * 10 | */ 11 | 12 | unsigned int getNumLength(unsigned n) 13 | { 14 | unsigned int count = 0; 15 | 16 | if (n == 0) 17 | return 1; 18 | 19 | while (n != 0) 20 | { 21 | count++; 22 | n /= 10; 23 | } 24 | return count; 25 | } 26 | 27 | unsigned int power(unsigned n, unsigned power) 28 | { 29 | unsigned res = 1; 30 | for (unsigned i = 0; i < power; i++) 31 | res *= n; 32 | return res; 33 | } 34 | 35 | unsigned int getSubnumber(unsigned n, unsigned mask) 36 | { 37 | unsigned subNumber = 0; 38 | unsigned mult = 1; 39 | 40 | while (n != 0) 41 | { 42 | if (mask % 2 == 1) 43 | { 44 | subNumber += (mult * (n % 10)); 45 | mult *= 10; 46 | } 47 | mask /= 2; 48 | n /= 10; 49 | } 50 | return subNumber; 51 | } 52 | 53 | unsigned int getSumOfSubnumbers(unsigned int n) 54 | { 55 | unsigned int len = getNumLength(n); 56 | unsigned int subNumbersCount = power(2, len); 57 | 58 | unsigned int sum = 0; 59 | 60 | for (int i = 0; i < subNumbersCount; i++) 61 | sum += getSubnumber(n, i); 62 | return sum; 63 | } -------------------------------------------------------------------------------- /Utils/Consultations/SI-28.11/Problems.md: -------------------------------------------------------------------------------- 1 | **Задача 1 :** Конкатенация на две естествени числа A и B наричаме число A ∘ B, което се получава, като към цифрите на A се долепят цифрите на B. 2 | Пример: 3 | 123 ∘ 431 = 123431 4 | Да се напише функция, която въвежда две естествени числа N и K и извежда дали съществуват естествени числа А и B, такива че **N = A ∘ K ∘ B**. 5 | В решението на задачата не се допуска използването на масиви!
6 | **Примери:**
7 | Вход Изход Обяснение
8 | N = 1234 K = 4 False
9 | N = 1234 K = 2 True N = 1 ∘ K ∘ 34
10 | N = 1234 K = 23 True N = 1 ∘ K ∘ 4
11 | 12 | **Задача 2 :** Напишете функция, която приема стринг. Функцията да премахва всички цифри от стринга и да връща произведението на числата в стринга.
13 | **Примери:**
14 | Вход Изход Обяснение
15 | “Abc23ffds3ff5” 345 Стрингът става “Abcffdsff”
16 | “12345d2” 24690 Стрингът става “d”
17 | “1345” 1345 Стрингът става “”
18 | 19 | 20 | **Задача 3 :** Да се напише функция, която приема масив и връща дали е 21 | конкатенация на два масива,които са палиндроми. 22 | 23 | Вход: [1 2 3 2 1 4 5 5 4], Изход: true 24 | 25 | Вход: [1 2 3 4 5], Изход: false 26 | 27 | Вход: [1 2 3 2 1], Изход: true 28 | 29 | Вход: [1 1 2], Изход: true // 1 1 и 2 30 | 31 | **Задача 4 :*
32 | ![image](https://user-images.githubusercontent.com/49128895/204383507-e84015c5-f159-4e48-895b-e0918d8a6f43.png) 33 | -------------------------------------------------------------------------------- /Utils/Consultations/SI-28.11/README.md: -------------------------------------------------------------------------------- 1 | ## Конспект: 2 | 3 | - [Тема 5:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.05) Масиви. Основни операции, обхождане на елементите, търсене. Многомерни масиви. 4 | - [Тема 6:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.06) Бройни системи и побитови операции. 5 | - [Тема 7:](https://github.com/Justsvetoslavov/Introduction_to_programming/tree/master/SI/Sem.07) Референция. Указатели. Символни низове. 6 | 7 | ## Лектори 8 | - Пламена Илиева 9 | - Георги Терзиев 10 | 11 | Проведена на: 28.11.2022г. 12 | -------------------------------------------------------------------------------- /Utils/Consultations/SI-28.11/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //проверява дали K е "строг" суфикс на N, т.е. е суфикс, но не съвпадат 4 | bool isSuffix(unsigned N, unsigned K) { 5 | while (N != 0 && K != 0) { 6 | if (N % 10 != K % 10) { 7 | return false; 8 | } 9 | N /= 10; 10 | K /= 10; 11 | } 12 | 13 | return N != 0; 14 | } 15 | 16 | bool isConcat(unsigned N, unsigned K) { 17 | //премахва последната цифра, защото ако краищата да съвпадат, числата не отговарят на условието 18 | N /= 10; 19 | 20 | while (N != 0) { 21 | if (isSuffix(N, K)) { 22 | return true; 23 | } 24 | N /= 10; 25 | } 26 | 27 | return false; 28 | } 29 | 30 | int main() { 31 | std::cout << isConcat(1234, 23); 32 | std::cout << isConcat(1234, 234); 33 | std::cout << isConcat(1234, 123); 34 | std::cout << isConcat(12345, 2); 35 | } 36 | -------------------------------------------------------------------------------- /Utils/Consultations/SI-28.11/Task2.cpp: -------------------------------------------------------------------------------- 1 | bool isDigit(char ch) { 2 | return ch >= '0' && ch <= '9'; 3 | } 4 | 5 | unsigned numInStr(const char* str, unsigned &get) { 6 | unsigned result = 0; 7 | while (isDigit(str[get])) { 8 | //намираме цифрата, която трябва да добавим, като от ASCII кода на цифрата извадим ASCII кода на нулата 9 | result = result * 10 + (str[get] - '0'); 10 | 11 | //променяме оригиналния индекс, от който четем, за да продължим от там четенето 12 | get++; 13 | } 14 | return result; 15 | } 16 | 17 | unsigned removeNumsAndReturnProduct(char* str) { 18 | //индекси за позицията, от която четем (get) и за позицията, на която пишем (put) 19 | unsigned get, put; 20 | get = put = 0; 21 | unsigned product = 1; 22 | 23 | while (str[get] != '\0') { 24 | if (isDigit(str[get])) { 25 | product *= numInStr(str, get); 26 | } 27 | 28 | str[put++] = str[get]; 29 | 30 | //проверяваме дали не сме стигнали до края на стринга във функцията numInStr, за да не подминем края 31 | if (str[get] == '\0') { 32 | break; 33 | } 34 | get++; 35 | } 36 | 37 | //слагаме край на стринга 38 | str[put] = '\0'; 39 | 40 | return product; 41 | } 42 | 43 | int main() 44 | { 45 | char str[] = "2abc3d45"; 46 | std::cout << removeNumsAndReturnProduct(str) << std::endl; 47 | std::cout << str; 48 | } 49 | -------------------------------------------------------------------------------- /Utils/Consultations/SI-28.11/Task3.cpp: -------------------------------------------------------------------------------- 1 | bool isPalindrome(const int* arr, size_t length) { 2 | for (size_t i = 0; i < length / 2; i++) { 3 | if (arr[i] != arr[length - 1 - i]) { 4 | return false; 5 | } 6 | } 7 | 8 | return true; 9 | } 10 | 11 | bool isPalindromeConcatenation(const int* arr, size_t length) { 12 | for (size_t i = 0; i < length; i++) { 13 | if (isPalindrome(arr, i) && isPalindrome(arr + i, length - i)) { 14 | return true; 15 | } 16 | } 17 | 18 | return false; 19 | } -------------------------------------------------------------------------------- /Utils/Tests/IS-Test-1/README.md: -------------------------------------------------------------------------------- 1 | ## Контролна работа 1 2 | Задачи от контролна работа 1 (проведена нал лист) на спец. Информационни системи 2022г. 3 | 4 | ### Задача 1 5 | Дадена е следната безкрайна редица от естествени числа: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 ...
6 | (една единица, две двойки, три тройки, четири четворки, пет петици и т.н.)
7 | Напишете програма, която по зададено число n извежда на екрана първите n елемента на тази редица.
8 | 9 | **Пример:**
10 | Вход: 7
11 | Изход: 1 2 2 3 3 3 4
12 | 13 | ### Задача 2 14 | Напишете програма, която приема естествено число и отпечатва неговото разлагане на прости множители
15 | (prime factorization, представянето на числото като произведение от само прости числа).
16 | Забележка: разделете програмния код на отделни функции по подходящ начин.
17 | 18 | **Примери:**
19 | Вход: 225, Изход: 3.3.5.5
20 | Вход: 31668, Изход: 2.2.3.7.13.29
21 | 22 | ### Задача 3 23 | Палиндром се нарича естествено число, което прочетено отляво надясно и отдясно наляво е еднакво (такова число е 121).
24 | Двоен палиндром се нарича естествено число, което е палиндром, но и неговият квадрат също е палиндром.
25 | Такова число е 11 (11 е палиндром и 112 = 121 също е палиндром).
26 | Напишете програма, която по въведено число k намира първите k такива числа, като ги извежда по двойки в следния вид: ,
27 | всяка на отделен ред. Едноцифрени числа не трябва да бъдат част от резултата.
28 | За ограничение на цикъла използвайте INT32_MAX.
29 | **Забележка 1:** разделете програмния код на отделни функции по подходящ начин.
30 | **Забележка 2:** нямате право да използвате никакви библиотеки освен библиотеката iostream.
31 | 32 | **Пример:**
33 | Вход: 5
34 | Изход:
35 | 11 121 36 | 22 484 37 | 101 10201 38 | 111 12321 39 | 121 14641 -------------------------------------------------------------------------------- /Utils/Tests/IS-Test-1/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintPositiveElements(int n) { 4 | if (n <= 0) { 5 | std::cout << "Error!\n"; 6 | return; 7 | } 8 | 9 | int counter = 1, elementCounter = 0; 10 | 11 | for (int i = 0; i < n; ++i) { 12 | for (int j = 0; j < counter && elementCounter < n; ++j, ++elementCounter) { 13 | std::cout << counter << " "; 14 | } 15 | counter++; 16 | } 17 | std::cout << std::endl; 18 | } -------------------------------------------------------------------------------- /Utils/Tests/IS-Test-1/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Function to check whether a number is prime 4 | bool IsPrime(unsigned number) 5 | { 6 | unsigned size = sqrt(number); 7 | 8 | for (unsigned i = 2; i <= size; i++) 9 | { 10 | if (number % i == 0) { 11 | return false; 12 | } 13 | } 14 | 15 | return true; 16 | } 17 | 18 | //PrimeFactorization 19 | void PrimeFactor(unsigned number) 20 | { 21 | for (unsigned i = 2; i <= number; i++) { 22 | if (IsPrime(i) && number % i == 0) { 23 | if (i == number) { //last step, no space 24 | std::cout << i; 25 | } else { 26 | std::cout << i << " "; 27 | } 28 | 29 | number /= i; 30 | i--; 31 | } 32 | } 33 | std::cout << std::endl; 34 | } 35 | -------------------------------------------------------------------------------- /Utils/Tests/IS-Test-1/Task3 - a.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned Reverse(unsigned n) 4 | { 5 | unsigned result = 0; 6 | short lastDigit; 7 | 8 | while (n != 0) 9 | { 10 | lastDigit = n % 10; 11 | (result *= 10) += lastDigit; 12 | n /= 10; 13 | } 14 | 15 | return result; 16 | } 17 | 18 | unsigned long long Reverse(unsigned long long n) 19 | { 20 | unsigned long long result = 0; 21 | short lastDigit; 22 | 23 | while (n != 0) 24 | { 25 | lastDigit = n % 10; 26 | (result *= 10) += lastDigit; 27 | n /= 10; 28 | } 29 | 30 | return result; 31 | } 32 | 33 | bool IsPalindrome(unsigned n) { 34 | return n == Reverse(n); 35 | } 36 | 37 | bool IsPalindrome(unsigned long long n) { 38 | return n == Reverse(n); 39 | } 40 | 41 | void PrintDoublePalindromes(unsigned k) { 42 | for (unsigned i = 11, counter = 0; i <= INT32_MAX && counter < k; i++) { 43 | if (IsPalindrome(i) && IsPalindrome((unsigned long long)i * i)) { 44 | std::cout << i << " " << (unsigned long long)i * i << std::endl; 45 | counter++; 46 | } 47 | } 48 | } 49 | 50 | int main() { 51 | PrintDoublePalindromes(3); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Utils/Tests/IS-Test-1/Task3 - b.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned long long Reverse(unsigned long long n) 4 | { 5 | unsigned long long result = 0; 6 | short lastDigit; 7 | 8 | while (n != 0) 9 | { 10 | lastDigit = n % 10; 11 | (result *= 10) += lastDigit; 12 | n /= 10; 13 | } 14 | 15 | return result; 16 | } 17 | 18 | bool IsPalindrome(unsigned long long n) { 19 | return n == Reverse(n); 20 | } 21 | 22 | void PrintDoublePalindromes(unsigned k) { 23 | for (unsigned long long i = 11, counter = 0; i <= INT32_MAX && counter < k; i++) { 24 | if (IsPalindrome(i) && IsPalindrome(i * i)) { 25 | std::cout << i << " " << i * i << std::endl; 26 | counter++; 27 | } 28 | } 29 | } 30 | 31 | int main() { 32 | PrintDoublePalindromes(3); 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Utils/Tests/Inf-Test-1/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int Abs(int input) 4 | { 5 | return input >= 0 ? input : -input; 6 | } 7 | 8 | void Compass(double x1, double y1, double x2, double y2) { 9 | double xDistance = Abs(x1 - x2), yDistance = Abs(y1 - y2); 10 | 11 | if (xDistance <= 0.1 && yDistance <= 0.1) { 12 | std::cout << "You're there already!"; 13 | } else { 14 | if (yDistance > 0.1) std::cout << (y1 - y2 > 0 ? 'S' : 'N'); 15 | if (xDistance > 0.1) std::cout << (x1 - x2 > 0 ? 'W' : 'E'); 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | double x1 = 0, y1 = 0, x2 = 0, y2 = 0; 22 | 23 | std::cin >> x1 >> y1 >> x2 >> y2; 24 | 25 | Compass(x1, y1, x2, y2); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Utils/Tests/Inf-Test-1/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void PrintLongestSubsequenceOfEqualNumbers(unsigned n) // It's actually subarray, not subsequence, but arrays are not studied yet 4 | { 5 | short max = 1; // The LSEN will always consist of at least one element 6 | short currentMax = 1; 7 | short digitFromMaxSubseq = n % 10; 8 | 9 | short lastDigit, preLastDigit; 10 | while (n >= 10) { 11 | lastDigit = n % 10; 12 | preLastDigit = (n / 10) % 10; 13 | 14 | if (preLastDigit == lastDigit) { 15 | currentMax++; 16 | if (currentMax > max) { 17 | max = currentMax; 18 | digitFromMaxSubseq = preLastDigit; 19 | } 20 | } else { 21 | currentMax = 1; 22 | } 23 | 24 | n /= 10; 25 | } 26 | std::cout << max << " (" << digitFromMaxSubseq << ")\n"; 27 | } 28 | 29 | int main() 30 | { 31 | PrintLongestSubsequenceOfEqualNumbers(1777444329); 32 | } -------------------------------------------------------------------------------- /Utils/Tests/Inf-Test-1/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned Reverse(unsigned n) 4 | { 5 | unsigned reversed = 0; 6 | short lastDigit; 7 | while (n != 0) { 8 | lastDigit = n % 10; 9 | (reversed *= 10) += lastDigit; 10 | n /= 10; 11 | } 12 | 13 | return reversed; 14 | } 15 | 16 | bool IsPalindrome(unsigned n) 17 | { 18 | return n == Reverse(n); 19 | } 20 | 21 | int main() 22 | { 23 | unsigned n; 24 | std::cin >> n; 25 | 26 | for (size_t i = 10, currentSuffix = 0; n != currentSuffix; i *= 10) { 27 | currentSuffix = n % i; 28 | if (IsPalindrome(currentSuffix)) { 29 | std::cout << currentSuffix << std::endl; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Utils/Tests/SI-Test-2/README.md: -------------------------------------------------------------------------------- 1 | TBA -------------------------------------------------------------------------------- /Utils/Tests/SI-Test-2/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void SubstractOne(int& n) 4 | { 5 | int m = 1; 6 | while (!(n & m)) { 7 | n ^= m; 8 | m <<= 1; 9 | } 10 | 11 | n ^= m; 12 | } 13 | 14 | void SubstractOne(unsigned long long& n) //function overload to avoid casting\ 15 | //needed for exact pow of 2 edge case 16 | { 17 | int m = 1; 18 | while (!(n & m)) { 19 | n ^= m; 20 | m <<= 1; 21 | } 22 | 23 | n ^= m; 24 | } 25 | 26 | bool IsExactPowOfTwo(unsigned long long n) //needed for exact pow of 2 edge case 27 | { 28 | unsigned long long temp = n; 29 | SubstractOne(n); 30 | return (temp & n) == 0; 31 | } 32 | 33 | void SumOfPowTwoNumber(unsigned long long n) 34 | { 35 | if (IsExactPowOfTwo(n)) { //cover edge case for exact pow of 2 36 | //NOTE: Unoptimized example for this edge case 37 | // will NOT be included in the grading! 38 | bool isFirst = true; 39 | for (int i = 39; i >= 0; SubstractOne(i)) { 40 | if (n & (unsigned long long(1) << i)) { 41 | if (isFirst) { //skip first 42 | isFirst = false; 43 | n >>= 1; 44 | continue; 45 | } 46 | std::cout << i << " " << i; 47 | break; 48 | } 49 | } 50 | } else { 51 | for (int i = 40; i >= 0; SubstractOne(i)) { 52 | if (n & (unsigned long long(1) << i)) { 53 | std::cout << i << " "; 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Utils/Tests/SI-Test-2/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const size_t MAX_SIZE = 1024; 4 | 5 | void Init(bool* arr, size_t size, bool value) 6 | { 7 | for(size_t i = 0; i < size; i++) { 8 | arr[i] = value; 9 | } 10 | } 11 | 12 | void MarkNotNeededNumbers(bool* arr, size_t size, int queryNumber) 13 | { 14 | for(size_t i = 1; i < size; i++) { 15 | if((i % queryNumber == 0) || (queryNumber % i == 0)) { 16 | arr[i] = false; 17 | } 18 | } 19 | } 20 | 21 | void PrintSolution(const bool* sieve, size_t size) 22 | { 23 | for(size_t i = 1; i <= size; i++) { 24 | if(sieve[i]) { 25 | std::cout << i << " "; 26 | } 27 | } 28 | 29 | std::cout << std::endl; 30 | } 31 | 32 | int main() 33 | { 34 | bool sieve[MAX_SIZE]; 35 | 36 | int n; 37 | cin >> n; 38 | Init(sieve, n, true); 39 | 40 | int userQuery; 41 | cin >> userQuery; 42 | while(userQuery != 0) { 43 | MarkNotNeededNumbers(sieve, n, userQuery); 44 | cin >> userQuery; 45 | } 46 | PrintSolution(sieve, n); 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /images/fetch-origin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/images/fetch-origin.png -------------------------------------------------------------------------------- /images/repository-fork-settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/images/repository-fork-settings.png -------------------------------------------------------------------------------- /images/repository-settings-menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/images/repository-settings-menu.png -------------------------------------------------------------------------------- /images/star-the-repo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Justsvetoslavov/Introduction_to_programming/8d016929d64c476078ac1924dcb7dca6f970e74e/images/star-the-repo.png --------------------------------------------------------------------------------