├── Sem. 05 ├── README.md ├── maxAndMinDigit.cpp ├── sortNumber.cpp └── bullsAndCows.cpp ├── Sem. 04 ├── Solutions │ ├── d │ ├── task 04.cpp │ └── task 09.cpp ├── example 01 - with functions.cpp ├── example 01 - no functions.cpp └── most_common_digit.cpp ├── Practicum ├── README.md ├── Week 01 │ └── Solutions │ │ ├── task_06.cpp │ │ ├── task_14.cpp │ │ ├── task_01.cpp │ │ ├── task_11.cpp │ │ ├── task_07.cpp │ │ ├── task_08.cpp │ │ ├── task_10.cpp │ │ ├── task_05.cpp │ │ ├── task_15.cpp │ │ ├── task_13.cpp │ │ ├── task_16.cpp │ │ ├── task_02.cpp │ │ ├── task_03.cpp │ │ ├── task_17.cpp │ │ ├── task_12.cpp │ │ ├── task_04.cpp │ │ └── task_09.cpp ├── Week 02 │ └── Solutions │ │ ├── task_02.cpp │ │ ├── task_01.cpp │ │ ├── task_04.cpp │ │ ├── task_05.cpp │ │ ├── task_03.cpp │ │ ├── task_07.cpp │ │ ├── task_12.cpp │ │ ├── task_14.cpp │ │ ├── task_09.cpp │ │ ├── task_08.cpp │ │ ├── task_13.cpp │ │ ├── task_05_bonus.cpp │ │ ├── task_06.cpp │ │ ├── task_10.cpp │ │ ├── task_11.cpp │ │ ├── task_11_bonus.cpp │ │ └── task_15.cpp ├── Week 04 │ ├── Solutions │ │ ├── task_01.cpp │ │ ├── task_02.cpp │ │ ├── task_05.cpp │ │ ├── task_06.cpp │ │ ├── task_11.cpp │ │ ├── task_17.cpp │ │ ├── task_14.cpp │ │ ├── task_15.cpp │ │ ├── task_07.cpp │ │ ├── task_10.cpp │ │ ├── task_04.cpp │ │ ├── task_03.cpp │ │ ├── task_16.cpp │ │ ├── task_12.cpp │ │ ├── task_09.cpp │ │ ├── task_08.cpp │ │ └── task_13.cpp │ └── MK.md ├── Week 03 │ └── Solutions │ │ ├── 2.5.cpp │ │ ├── 2.cpp │ │ ├── 5.cpp │ │ ├── 1.cpp │ │ ├── 7.cpp │ │ ├── 3.cpp │ │ ├── 3.5.cpp │ │ ├── 8.cpp │ │ ├── 4.cpp │ │ ├── 9.cpp │ │ └── 6.cpp ├── Week 05 │ └── Solutions │ │ ├── task_01.cpp │ │ ├── task_04.cpp │ │ ├── task_06.cpp │ │ ├── task_02.cpp │ │ ├── task_07.cpp │ │ ├── task_11.cpp │ │ ├── task_03.cpp │ │ ├── task_12.cpp │ │ ├── task_05.cpp │ │ ├── task_14.cpp │ │ ├── task_09.cpp │ │ ├── task_12.5.cpp │ │ ├── task_10.cpp │ │ ├── task_08.cpp │ │ └── task_13.cpp ├── Week 06 │ └── Solutions │ │ ├── task_01.cpp │ │ ├── task_02.cpp │ │ ├── task_06.cpp │ │ ├── task_09.cpp │ │ ├── task_05.cpp │ │ ├── task_03.cpp │ │ ├── task_08.cpp │ │ ├── task_04.cpp │ │ ├── task_07.cpp │ │ └── task_10.cpp ├── Week 08 │ └── Solutions │ │ ├── task_01.cpp │ │ ├── task_09.cpp │ │ ├── task_08.cpp │ │ ├── task_06.cpp │ │ ├── task_03.cpp │ │ ├── task_07.cpp │ │ ├── task_02.cpp │ │ ├── task_04.cpp │ │ └── task_10.cpp ├── Week 07 │ └── MK.md └── Week 09 │ └── README.md ├── .vscode ├── settings.json └── tasks.json ├── Retake ├── УП, КН, Поправка.pdf └── solutions.cpp ├── Sem. 13 ├── Сортиращи_алгоритми.pdf ├── readme.md ├── insertionSort.cpp ├── selectionSort.cpp └── bubbleSort.cpp ├── C examples ├── Sem03 │ ├── images │ │ ├── Loop.png │ │ ├── ExampleDrawings_Task07.png │ │ ├── ExampleDrawings_Task08.png │ │ ├── ExampleDrawings_Task10.png │ │ ├── ExampleDrawings_Task11.png │ │ ├── ExampleDrawings_Task12.png │ │ └── ExampleDrawings_Task13.png │ ├── Solutions │ │ ├── Task05.c │ │ ├── Task04.c │ │ ├── Task06.c │ │ ├── Task09.c │ │ ├── Task01.c │ │ ├── Task15.c │ │ ├── Task07.c │ │ ├── Task14.c │ │ ├── Task02.c │ │ ├── Task16.c │ │ ├── Task08.c │ │ ├── Task03.c │ │ ├── Task13.c │ │ ├── Drawings.txt │ │ ├── Task11.c │ │ ├── Task12.c │ │ └── Task10.c │ ├── example3_gcd_euclid.c │ ├── example2_gcd_naive.c │ ├── example5_prime-factors_better.c │ ├── example4_prime.c │ └── example5_prime-factors.c ├── Sem01 │ ├── images │ │ ├── Variable.png │ │ ├── DataTypes.png │ │ └── OperatorCharacteristics.png │ ├── Task02.c │ ├── Task01.c │ ├── Task03.c │ ├── Task06.c │ ├── Task07.c │ ├── Task04.c │ └── Task05.c ├── README.md ├── Sem05 │ └── bitwise.c ├── Exam Preparation │ ├── Zad1.c │ ├── Zad3.c │ ├── Exam Preparation Tasks.md │ └── Zad2.c ├── Sem04 │ └── functions.c ├── Sem07 │ └── examples.c └── Sem09 │ └── mem.c ├── Sem. 01 └── Solutions │ ├── task5.cpp │ ├── task1.cpp │ ├── task4.cpp │ ├── task8.cpp │ ├── task6.cpp │ ├── task3.cpp │ ├── task2.cpp │ └── task7.cpp ├── Sem. 14 ├── fact.cpp ├── power.cpp ├── fibb.cpp ├── sum_array.cpp ├── fast_power.cpp ├── linearSearch.cpp ├── isPalindrome.cpp ├── invertCase.cpp ├── binary_search.cpp ├── max_element_in_array.cpp ├── searchInText.cpp ├── lower_and_upper_count.cpp └── readme.md ├── Sem. 08 ├── allEvenOneOdd.cpp ├── powerOfTwo.cpp ├── bitManipulations.cpp └── README.md ├── Sem. 02 └── Solutions │ ├── 02. Between100and200.cpp │ ├── 06. Chainsaw.cpp │ ├── 05. Time+15.cpp │ ├── 03. SpeedCheck.cpp │ ├── 07. PointInCircle.cpp │ ├── 01. DaysOfWeek.cpp │ └── 04. Figures.cpp ├── Sem. 03 ├── ex2.cpp ├── ex1.cpp ├── ex5.cpp ├── ex3.cpp ├── ex4.cpp ├── ex6.cpp └── ex7.cpp ├── Sem. 15 ├── sumArrTail.cpp ├── boolVectorsGen.cpp ├── high-order-functions.cpp ├── sum_numbers_string.cpp ├── fibb_memoization.cpp ├── boolVectorsEqualOnesAndZeros.cpp ├── minOperations_memoization.cpp └── backtracking_example.cpp ├── Practicum - Informatics ├── Week 02 - Conditions │ └── Solutions │ │ ├── Task02.c │ │ ├── Task05.c │ │ ├── Task06.c │ │ ├── Task07.c │ │ ├── Task03.c │ │ ├── Task01.c │ │ ├── Task08.c │ │ ├── Task09.c │ │ ├── Task04.c │ │ └── Task10.c ├── Week 07 - Bitwise Operations And Counting Systems │ └── Week 07.md ├── Week 04 - Functions + Mini Exam │ └── Week 04 - CoPrimes Task.md ├── Week 12 - Strings │ └── Week 12.md └── Week 06 - Bitwise Operations │ └── Week 06.md ├── Sem. 07 ├── toBinaryFromDecimalWithIntegers.cpp ├── fromBinaryToDecimalWithIntegers.cpp └── readme.md ├── Sem. 06 ├── linearSearch.cpp ├── reverseArr.cpp ├── binarySearch.cpp ├── sieveErathostenes.cpp └── printArr.cpp ├── Sem. 10 ├── number_to_str.cpp ├── str_to_number.cpp ├── search_in_text.cpp └── basic_string_functions.cpp ├── README.md ├── Sem. 11 ├── smallAndUpperStrings.cpp ├── sortLower.cpp ├── sortArrayConcatOfTwoSortedArrays.cpp └── censoreNumbers.cpp ├── Sem. 09 ├── linearEquationsSolver.cpp └── tic-tac-toe.cpp └── Sem. 12 ├── 1. playersStatistics.cpp ├── 3. splitString.cpp └── 2. stringToWords.cpp /Sem. 05/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/d: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/task 04.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/task 09.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Practicum/README.md: -------------------------------------------------------------------------------- 1 | ## Увод в програмирането - Практикум -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "iostream": "cpp", 4 | "ostream": "cpp" 5 | } 6 | } -------------------------------------------------------------------------------- /Retake/УП, КН, Поправка.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/Retake/УП, КН, Поправка.pdf -------------------------------------------------------------------------------- /Sem. 13/Сортиращи_алгоритми.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/Sem. 13/Сортиращи_алгоритми.pdf -------------------------------------------------------------------------------- /C examples/Sem03/images/Loop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/Loop.png -------------------------------------------------------------------------------- /C examples/Sem01/images/Variable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem01/images/Variable.png -------------------------------------------------------------------------------- /C examples/Sem01/images/DataTypes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem01/images/DataTypes.png -------------------------------------------------------------------------------- /C examples/Sem01/images/OperatorCharacteristics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem01/images/OperatorCharacteristics.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/ExampleDrawings_Task07.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/ExampleDrawings_Task08.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/ExampleDrawings_Task10.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/ExampleDrawings_Task11.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/ExampleDrawings_Task12.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/HEAD/C examples/Sem03/images/ExampleDrawings_Task13.png -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num = 0; 6 | cin >> num; 7 | 8 | cout << (num / 10); 9 | } -------------------------------------------------------------------------------- /Sem. 01/Solutions/task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | 9 | cout << n / 10 << endl; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /C examples/Sem01/Task02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | printf("%d\n", (n % 2 == 0)); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num = 0; 6 | cin >> num; 7 | cout << ((num % 2 == 0) ? "true" : "false"); 8 | } -------------------------------------------------------------------------------- /Sem. 01/Solutions/task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | bool isEven = n % 2 == 0; 9 | cout << isEven; 10 | } 11 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | int sum = (n * (n + 1)) / 2; 10 | cout << sum; 11 | } 12 | -------------------------------------------------------------------------------- /C examples/Sem01/Task01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int grade = 0; 6 | scanf("%d", &grade); 7 | printf("%d\n", (grade >= 2 && grade <= 6)); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | bool isEven = (n % 2 == 0); 9 | cout << isEven; 10 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int absoluteValue(int number) { 5 | if (number < 0) 6 | { 7 | return -number; 8 | } 9 | else 10 | { 11 | return number; 12 | } 13 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int absoluteValue(int number) { 5 | if (number < 0) 6 | { 7 | return -number; 8 | } 9 | else 10 | { 11 | return number; 12 | } 13 | } -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int num; 7 | cin >> num; 8 | 9 | bool hasTwoDigits = (num < 10 || 99 < num) && (num %= 10); 10 | cout << num; 11 | } 12 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num = 0; 6 | cin >> num; 7 | 8 | if (num / 3 == 0) { 9 | cout << "true"; 10 | } else { 11 | cout << "false"; 12 | } 13 | } -------------------------------------------------------------------------------- /Sem. 01/Solutions/task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int grade; 7 | cin >> grade; 8 | 9 | bool isValidGrade = grade >= 2 && grade <= 6; 10 | 11 | cout << isValidGrade << endl; 12 | } 13 | -------------------------------------------------------------------------------- /C examples/Sem01/Task03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b; 6 | scanf("%d %d", &a, &b); 7 | 8 | int isFirstBigger = a > b; 9 | printf("%d\n", a * isFirstBigger + b * !isFirstBigger); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double cm = 0; 6 | cin >> cm; 7 | 8 | double mult = 0.393701; 9 | 10 | double result = cm * mult; 11 | cout << result; 12 | } -------------------------------------------------------------------------------- /Sem. 01/Solutions/task8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a; 7 | int b; 8 | cin >> a >> b; 9 | 10 | bool isFirstBigger = a > b; 11 | 12 | cout << a * isFirstBigger + b * !isFirstBigger; 13 | } 14 | -------------------------------------------------------------------------------- /Sem. 14/fact.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | unsigned fact(unsigned n) 6 | { 7 | if(n == 0) 8 | return 1; 9 | return n * fact(n - 1); 10 | } 11 | 12 | int main() 13 | { 14 | cout<< fact(10); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 0, b = 0; 6 | cin >> a >> b; 7 | 8 | int sum = a + b; 9 | int diff = a - b; 10 | 11 | int result = sum * sum * sum * sum - diff * diff; 12 | } -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int a, b; 8 | cin >> a >> b; 9 | 10 | bool isFirstBigger = (a > b); 11 | 12 | cout << (isFirstBigger * a) + (!isFirstBigger * b); 13 | } 14 | -------------------------------------------------------------------------------- /Sem. 14/power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int n, int k) 5 | { 6 | if(k == 0) 7 | return 1; 8 | return n * power(n , k - 1); 9 | } 10 | 11 | int main() 12 | { 13 | cout<< power(2, 8); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Sem. 14/fibb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fibb(int n) 5 | { 6 | if(n == 0 || n == 1) 7 | return 1; 8 | return fibb(n - 1) + fibb(n - 2); 9 | } 10 | 11 | int main() 12 | { 13 | cout<< fibb(10); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int grade = 0; 6 | cin >> grade; 7 | 8 | bool isValid = (grade == 2) || (grade == 3) || (grade == 4) || (grade == 5) || (grade == 6); 9 | 10 | cout << isValid; 11 | } -------------------------------------------------------------------------------- /Sem. 01/Solutions/task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | int expr1 = a + b; 10 | int expr2 = a - b; 11 | 12 | int res = expr1 * expr1 * expr1 * expr1 - expr2 * expr2; 13 | 14 | cout << res; 15 | 16 | } 17 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int sum = 0; 6 | int number; 7 | 8 | do 9 | { 10 | scanf("%d", &number); 11 | sum += number; 12 | } while (number); 13 | 14 | printf("%d", sum); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_15.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 | bool isFibb = (c == a + b) && 10 | (d == b + c) && 11 | (e == c + d); 12 | cout << isFibb; 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_05.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | 5 | int power(int base, unsigned int exponent) 6 | { 7 | int result = 1; 8 | while(exponent > 0) 9 | { 10 | result *= base; 11 | exponent--; 12 | } 13 | return result; 14 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/2.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 0, b = 0; 6 | cin >> a >> b; 7 | 8 | int min = a > b ? b : a; 9 | 10 | for (int i = min; i > 0; i--) { 11 | if (b % i == 0 && a % i == 0) { 12 | cout << i << " "; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /C examples/Sem01/Task06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b; 6 | scanf("%d %d", &a, &b); 7 | 8 | int expr1 = a + b; 9 | int expr2 = a - b; 10 | int res = expr1 * expr1 * expr1 * expr1 - expr2 * expr2; 11 | 12 | printf("%d\n", res); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int year = 0; 6 | cin >> year; 7 | if ((year % 400) || (year % 4 == 0 && year % 100 != 0)) { 8 | cout << "Leap year"; 9 | } else { 10 | cout << "Not a leap year"; 11 | } 12 | } -------------------------------------------------------------------------------- /Sem. 08/allEvenOneOdd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | unsigned n; 8 | std::cin >> n; 9 | 10 | unsigned res = 0; 11 | for (unsigned i = 0; i < n; i++) 12 | { 13 | int curr; 14 | cin >> curr; 15 | res ^= curr; 16 | } 17 | std::cout << res; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | char a, b, c, d, e, f; 7 | cin >> a >> b >> c >> d >> e >> f; 8 | 9 | int sumOfASCII = a + b + c + d + e + f; 10 | bool canDivByThree = (sumOfASCII % 3 == 0); 11 | 12 | cout << canDivByThree; 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getLength(int number){ 5 | 6 | if(number == 0) 7 | { 8 | return 1; 9 | } 10 | int result = 0; 11 | while(number != 0){ 12 | number/=10; 13 | result++; 14 | } 15 | return result; 16 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 0, b = 0; 6 | cin >> a >> b; 7 | 8 | if (b > a) { 9 | int temp = a; 10 | a = b; 11 | b = temp; 12 | } 13 | while (b != 0) { 14 | int mod = a % b; 15 | a = b; 16 | b = mod; 17 | } 18 | cout << a << endl; 19 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | int sum = 0; 7 | 8 | while (true) { 9 | cin >> n; 10 | if (n == 0) { 11 | break; 12 | } 13 | sum += n; 14 | } 15 | cout << sum << std::endl; 16 | } -------------------------------------------------------------------------------- /Sem. 01/Solutions/task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char ch; 7 | cin >> ch; 8 | 9 | bool isVowel = ch == 'a' || ch == 'e' || ch == 'o' || ch == 'u' || ch == 'i' 10 | || ch == 'A' || ch == 'E' || ch == 'O' || ch == 'U' || ch == 'I'; 11 | 12 | cout << isVowel << endl; 13 | } 14 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/02. Between100and200.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 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/06. Chainsaw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b, c, d, e; 7 | cin >> a >> b >> c >> d >> e; 8 | 9 | if ((a <= b && b >= c && c <= d && d >= e) 10 | || (a >= b && b <= c && c >= d && d <= e)) 11 | { 12 | cout << "Yes"; 13 | } 14 | else 15 | cout << "No"; 16 | } 17 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 0, b = 0, c = 0; 6 | cin >> a >> b >> c; 7 | 8 | int middle = 0; 9 | cout << a + b + c 10 | - (a > b ? (a > c ? a : c) : (b > c ? b : c)) 11 | - (a < b ? (a < c ? a : c) : (b < c ? b : c)); 12 | } -------------------------------------------------------------------------------- /Sem. 03/ex2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | int n, k; 7 | cin >> n >> k; 8 | 9 | if (n < k) //swap 10 | { 11 | int temp = n; 12 | n = k; 13 | k = temp; 14 | } 15 | 16 | while (k != 0) 17 | { 18 | int mod = n % k; 19 | n = k; 20 | k = mod; 21 | } 22 | 23 | cout << n << endl; 24 | } 25 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0, k = 0; 6 | cin >> n >> k; 7 | int max = n > k ? n : k; 8 | 9 | int lcmCandidate = max; 10 | 11 | while (lcmCandidate % n != 0 || lcmCandidate % k != 0) { 12 | lcmCandidate += max; 13 | } 14 | 15 | cout << lcmCandidate << endl; 16 | } -------------------------------------------------------------------------------- /Sem. 03/ex1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | unsigned int n, k; 7 | cin >> n >> k; 8 | 9 | unsigned max = n > k ? n : k; 10 | unsigned lcmCindidate = max; 11 | while (lcmCindidate % n != 0 || lcmCindidate % k != 0) 12 | { 13 | lcmCindidate += max; 14 | } 15 | 16 | cout << lcmCindidate << endl; 17 | } 18 | -------------------------------------------------------------------------------- /C examples/Sem01/Task07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | scanf("%c", &ch); 7 | 8 | int isVowel = ch == 'a' || ch == 'e' || ch == 'o' || ch == 'u' || ch == 'i' 9 | || ch == 'A' || ch == 'E' || ch == 'O' || ch == 'U' || ch == 'I'; 10 | 11 | printf("%d\n", isVowel); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Sem. 14/sum_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumArr(const int* arr, size_t size) 5 | { 6 | if(size == 0) 7 | return 0; 8 | return arr[0] + sumArr(arr + 1, size - 1); 9 | } 10 | 11 | int main() 12 | { 13 | int arr[] = {1, 2, 3, 4, 5}; 14 | 15 | cout << sumArr(arr, 5); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b, c, d; 7 | 8 | // Тук разчитаме, че входните данни са валидни интервали, т.е. нямаме интервал от вида [10, 5] 9 | cin >> a >> b >> c >> d; 10 | 11 | bool intersect = (a < c && c < b) || (a < d && d < b); 12 | cout << intersect; 13 | } 14 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | int inputNumber = n; 9 | int reversed = 0; 10 | 11 | while (n > 0) 12 | { 13 | (reversed *= 10) += n % 10; 14 | n /= 10; 15 | } 16 | 17 | printf("%d", inputNumber == reversed); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/05. Time+15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int hours, mins; 7 | cin >> hours >> mins; 8 | 9 | (mins += 15) %= 60; 10 | 11 | if (mins < 15) 12 | (hours+=1)%=24; 13 | 14 | if (hours <= 9) 15 | cout << 0; 16 | cout << hours << ":"; 17 | 18 | if (mins <= 9) 19 | cout << 0; 20 | cout << mins << endl; 21 | } 22 | -------------------------------------------------------------------------------- /Sem. 14/fast_power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fastPower(int n, int k) 5 | { 6 | if(k == 0) 7 | return 1; 8 | else if(k % 2 == 1) 9 | return n * fastPower(n , k - 1); 10 | else 11 | return fastPower(n * n , k/ 2) ; 12 | } 13 | 14 | int main() 15 | { 16 | cout<< fastPower(2, 8); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int dividend = 0, divisor = 0; 6 | cin >> dividend >> divisor; 7 | 8 | int quotient = dividend / divisor; 9 | int remainder = dividend % divisor; 10 | 11 | cout << "Divison quotient: " << quotient << endl 12 | << "Divison remainder: " << remainder; 13 | } -------------------------------------------------------------------------------- /Sem. 08/powerOfTwo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //Зад. Вход N. Да пресметнете 2^n 6 | 7 | unsigned powerOfTwo(unsigned n) 8 | { 9 | const int LIMIT = sizeof(unsigned) * 8; 10 | if(n >= LIMIT) 11 | return 0; //error 12 | 13 | return 1 << n; 14 | } 15 | 16 | 17 | int main() 18 | { 19 | cout << powerOfTwo(8); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Sem. 15/sumArrTail.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumArrTail(const int* arr, size_t size, int sum = 0) 5 | { 6 | if (size == 0) 7 | return sum; 8 | return sumArrTail(arr + 1, size - 1, sum + arr[0]); 9 | } 10 | 11 | int main() 12 | { 13 | const size_t SIZE = 4; 14 | int arr[SIZE]{1, 2, 3, 4}; 15 | cout << sumArrTail(arr, SIZE); 16 | } 17 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin>> n; 7 | 8 | if (n == 0) { 9 | cout << "False"; 10 | } 11 | else { 12 | int copyOfN = n; 13 | while (copyOfN %2 == 0) { 14 | copyOfN /= 2; 15 | } 16 | 17 | if (copyOfN == 1) { 18 | cout << "True"; 19 | } 20 | else { 21 | cout << "False"; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task02.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int number; 5 | scanf_s("%d", &number); 6 | 7 | if (number < 100) { 8 | printf("Less than 100\n"); 9 | } 10 | else if (number <= 200) { 11 | printf("Between 100 and 200\n"); 12 | } 13 | else { 14 | printf("Greater than 200\n"); 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 4; 5 | 6 | int sumOfElements(const int arr[], size_t size) { 7 | int sum = 0; 8 | 9 | for (size_t i = 0; i < size; i++) { 10 | sum += arr[i]; 11 | } 12 | 13 | return sum; 14 | } 15 | 16 | int main() { 17 | int arr[] = {5, 7, 4, 9}; 18 | cout << sumOfElements(arr, ARRAY_SIZE); 19 | } -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | int number = 0; 9 | for (size_t i = 0; i < n; i++) 10 | { 11 | int digit; 12 | scanf("%d", &digit); 13 | 14 | if (!(digit % 2)) 15 | { 16 | (number *= 10) += digit; 17 | } 18 | } 19 | 20 | printf("%d", number * 2); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /C examples/Sem03/example3_gcd_euclid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned n, m; 6 | scanf("%u %u", &n, &m); 7 | 8 | if (n < m) 9 | { 10 | unsigned temp = n; 11 | n = m; 12 | m = temp; 13 | } 14 | 15 | while (m != 0) 16 | { 17 | unsigned mod = n % m; 18 | n = m; 19 | m = mod; 20 | } 21 | printf("%u", n); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int hours, minutes; 5 | scanf_s("%d %d", &hours, &minutes); 6 | 7 | minutes += 15; 8 | 9 | if (minutes >= 60) { 10 | minutes -= 60; 11 | hours++; 12 | } 13 | 14 | if (hours >= 24) { 15 | hours = 0; 16 | } 17 | 18 | printf("%02d:%02d\n", hours, minutes); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/03. SpeedCheck.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 | -------------------------------------------------------------------------------- /Sem. 07/toBinaryFromDecimalWithIntegers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned int toBinaryFromDecimal(unsigned int n) 5 | { 6 | int result = 0; 7 | int mult = 1; 8 | while (n != 0) 9 | { 10 | if (n % 2 == 1) 11 | result += mult; 12 | mult *= 10; 13 | n /= 2; 14 | } 15 | return result; 16 | } 17 | 18 | 19 | int main() 20 | { 21 | cout<< toBinaryFromDecimal(55) << endl; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isSquareRoot(int number) { 6 | int sqrtOfN = sqrt(number); 7 | if (sqrtOfN * sqrtOfN == number) { 8 | return true; 9 | } 10 | return false; 11 | } 12 | 13 | int main() { 14 | int input = 0; 15 | cin >> input; 16 | 17 | cout << (isSquareRoot(input) ? 18 | "true" : "false") << endl; 19 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int squareRootToLower(int number) { 5 | if (number < 0) { 6 | return -1; 7 | } 8 | 9 | int x = 0; 10 | while ((x + 1) * (x + 1) <= number) { 11 | x++; 12 | } 13 | 14 | return x; 15 | } 16 | 17 | int main() { 18 | int number = 0; 19 | cin >> number; 20 | 21 | cout << squareRootToLower(number) << endl; 22 | } -------------------------------------------------------------------------------- /Sem. 02/Solutions/07. PointInCircle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int x; 7 | int y; 8 | int radius; 9 | 10 | cin >> x >> y >> radius; 11 | 12 | if (x * x + y * y < radius * radius) 13 | { 14 | cout << "In the circle"; 15 | } 16 | else if (x * x + y * y == radius * radius) 17 | { 18 | cout << "On the circle"; 19 | } 20 | else 21 | { 22 | cout << "Out of the circle"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Sem. 14/linearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool linearSearch(const int* arr, size_t size, int searched) 6 | { 7 | if(size == 0) 8 | return false; 9 | return arr[0] == searched || linearSearch(arr + 1, size - 1, searched); 10 | } 11 | 12 | int main() 13 | { 14 | int arr[] = {1, 2, 6, 3, 1}; 15 | 16 | cout << linearSearch(arr, 5, 3); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Sem. 01/Solutions/task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | 9 | int thirdDigit = n % 10; 10 | n /= 10; //remove the lastDigit; 11 | 12 | int secondDigit = n % 10; 13 | n /= 10; //remove the lastDigit; 14 | 15 | int firstDigit = n % 10; 16 | 17 | int reversed = thirdDigit * 100 + secondDigit * 10 + firstDigit; 18 | reversed++; 19 | 20 | cout << reversed << endl; 21 | } 22 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b, c, d, e; 6 | scanf("%d %d %d %d %d", &a, &b, &c, &d, &e); 7 | 8 | double fx; 9 | for (size_t x = -100; x <= 100; x++) 10 | { 11 | fx = a * x*x*x*x + b * x*x*x + c * x*x + d * x + e; 12 | if (fx >= 0) 13 | { 14 | printf("%d ", x); 15 | } 16 | } 17 | 18 | printf("\n"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task06.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b, c, d, e; 5 | scanf_s("%d %d %d %d %d", &a, &b, &c, &d, &e); 6 | 7 | if ((b > a && b > c || b < a && b < c) && 8 | (c > b && c > d || c < b && c < d) && 9 | (d > c && d > e || d < c && d < e)) { 10 | printf("yes\n"); 11 | } else { 12 | printf("no\n"); 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num = 0; 6 | cin >> num; 7 | 8 | switch (num) { 9 | case 2: cout << "Fail"; break; 10 | case 3: cout << "Poor"; break; 11 | case 4: cout << "Average"; break; 12 | case 5: cout << "Very good"; break; 13 | case 6: cout << "Excellecnt"; break; 14 | default: cout << "Invalid grade!"; 15 | } 16 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 4; 5 | 6 | bool isSortedArray(const int arr[], size_t size) { 7 | for (size_t i = 0; i < size - 1; i++) { 8 | if (arr[i] < arr[i + 1]) { 9 | return false; 10 | } 11 | } 12 | return true; 13 | } 14 | 15 | int main() { 16 | int arr[] = {5, 7, 4, 9}; 17 | cout << isSortedArray(arr, ARRAY_SIZE); 18 | } -------------------------------------------------------------------------------- /Sem. 07/fromBinaryToDecimalWithIntegers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned int fromBinaryToDecimal(unsigned int n) 5 | { 6 | unsigned int result = 0; 7 | unsigned int mult = 1; //2^0 8 | while (n != 0) 9 | { 10 | result += ((n % 10) * mult); 11 | mult *= 2; 12 | n /= 10; 13 | } 14 | return result; 15 | } 16 | 17 | 18 | int main() 19 | { 20 | cout<< fromBinaryToDecimal(10111) << endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isDivisableByValue(int number, int divisor) { 5 | if (divisor == 0) { 6 | return false; 7 | } 8 | 9 | int division = number / divisor; 10 | return (divisor * division == number); 11 | } 12 | 13 | int main() { 14 | int number = 0, divisor = 0; 15 | cin >> number >> divisor; 16 | 17 | cout << isDivisableByValue(number, divisor) << endl; 18 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getRemainder(int first, int second) { 5 | if (second == 0) { 6 | return 0; 7 | } 8 | 9 | int quotient = first / second; 10 | int remainder = first - quotient * second; 11 | 12 | return remainder; 13 | } 14 | 15 | int main() { 16 | int first = 0, second = 0; 17 | cin >> first >> second; 18 | 19 | cout << getRemainder(first, second); 20 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_01.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | 6 | void swap(int& a, int& b) { 7 | 8 | int temp = a; 9 | a = b; 10 | b = temp; 11 | } 12 | int main() { 13 | 14 | int a, b; 15 | cin >> a >> b; 16 | cout << "Numbers before swap" << endl; 17 | cout << a <<" "<< b << endl; 18 | swap(a, b); 19 | cout << "Number after swap"<< endl; 20 | cout << a << " " << b << endl; 21 | 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int num = 0; 6 | cin >> num; 7 | 8 | int reversed = num % 10; // third digit 9 | reversed = reversed * 10; 10 | num = num / 10; 11 | 12 | reversed = reversed + (num % 10); // second digit 13 | reversed = reversed * 10; 14 | num = num / 10; 15 | 16 | reversed = reversed + (num % 10); // first digit 17 | 18 | cout << reversed; 19 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int x = 0, y = 0; 6 | cin >> x >> y; 7 | 8 | if (x == 0 && y == 0) { 9 | cout << "Center"; 10 | } else if (x > 0 && y > 0) { 11 | cout << "First"; 12 | } else if (x < 0 && y > 0) { 13 | cout << "Second"; 14 | } else if (x < 0 && y < 0) { 15 | cout << "Third"; 16 | } else { 17 | cout << "Fourth"; 18 | } 19 | } -------------------------------------------------------------------------------- /Sem. 03/ex5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | 11 | for (int i = 2; i <= n; i++) 12 | { 13 | 14 | //check how many time i devides n 15 | int count = 0; 16 | while (n % i == 0) 17 | { 18 | count++; 19 | n /= i; 20 | } 21 | 22 | if (count >= 1) 23 | { 24 | cout << i; 25 | if(count >= 2) 26 | cout << "^" << count << " "; 27 | } 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Sem. 14/isPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isPalinromeRec(const char* str, size_t len) 6 | { 7 | if(len <= 1) 8 | return true; 9 | 10 | return str[0] == str[len - 1] && isPalinromeRec(str + 1, len - 2); 11 | } 12 | bool isPalinrome(const char* str) 13 | { 14 | return isPalinromeRec(str, strlen(str)); 15 | } 16 | int main() 17 | { 18 | cout< 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | int countOfLeapYears = 0; 9 | 10 | for (size_t i = 0; i < n; i++) 11 | { 12 | int year; 13 | scanf("%d", &year); 14 | 15 | if ((!(year % 4) && (year % 100)) || !(year % 400)) 16 | { 17 | countOfLeapYears++; 18 | } 19 | } 20 | 21 | printf("%d", countOfLeapYears); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int lcm(int first, int second){ 5 | if(first == 0 || second == 0) 6 | { 7 | return 0; 8 | } 9 | int lcmCandidate = (first > second)? first: second; 10 | 11 | 12 | while(true){ 13 | if(lcmCandidate% first != 0 || lcmCandidate % second != 0) 14 | { 15 | return lcmCandidate; 16 | } 17 | lcmCandidate ++; 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /C examples/Sem03/example2_gcd_naive.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned n, m; 6 | scanf("%u %u", &n, &m); 7 | 8 | unsigned minNum = n < m ? n : m; 9 | unsigned gcd = 1; 10 | 11 | while (minNum >= 1) 12 | { 13 | if (n % minNum == 0 && m % minNum == 0) 14 | { 15 | gcd = minNum; 16 | break; 17 | } 18 | minNum--; 19 | } 20 | 21 | printf("%u", gcd); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Sem. 14/invertCase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void invertChar(char& ch) 5 | { 6 | if (islower(ch)) 7 | ch = toupper(ch); 8 | else if (isupper(ch)) 9 | ch = tolower(ch); 10 | } 11 | void invertCaseRec(char* str) 12 | { 13 | if (!*str) 14 | return; 15 | 16 | invertChar(*str); 17 | invertCaseRec(str + 1); 18 | } 19 | 20 | int main() 21 | { 22 | char str[] = "qBc"; 23 | invertCaseRec(str); 24 | std::cout << str; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double a = 0, b = 0; 6 | char operation = ' '; 7 | cin >> a >> operation >> b; 8 | 9 | switch (operation) { 10 | case '+': cout << a + b << endl; break; 11 | case '-': cout << a - b << endl; break; 12 | case '*': cout << a * b << endl; break; 13 | case '/': cout << a / b << endl; break; 14 | default: cout << "Invalid operation!" << endl; 15 | } 16 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | if (n <= 1) { 9 | cout << "Not prime"; 10 | } 11 | 12 | else { 13 | bool isPrime = true; 14 | double sqrtOfN = sqrt(n); 15 | for (int i = 2; i <= sqrtOfN; i++) { 16 | if (n % i == 0) { 17 | isPrime = false; 18 | break; 19 | } 20 | } 21 | if (isPrime) { 22 | cout << "Prime"; 23 | } 24 | else { 25 | cout << "Not prime"; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /C examples/Sem03/example5_prime-factors_better.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | unsigned n; 6 | scanf("%u", &n); 7 | 8 | for (size_t i = 2; i <= n; i++) 9 | { 10 | // check how many times i divides n 11 | unsigned count = 0; 12 | while (n % i == 0) 13 | { 14 | count++; 15 | n /= i; 16 | } 17 | if (count >= 1) 18 | { 19 | printf("%u^%u ", i, count); 20 | } 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 07 - Bitwise Operations And Counting Systems/Week 07.md: -------------------------------------------------------------------------------- 1 | 2 | ## По-трудни задачи върху побитови операции: 3 | [Task 5 to 10] (https://github.com/Angeld55/Introduction_to_programming_FMI/blob/main/Practicum%20-%20Informatics/Week%2006%20-%20Bitwise%20Operations/Week%2006.md) 4 | 5 | ## По-трудни задачи върху бройни системи: 6 | [Task 4] (https://github.com/Angeld55/Introduction_to_programming_FMI/blob/main/Practicum%20-%20Informatics/Week%2005%20-%20Hard%20Problems%20Over%20Weeks%2001%20to%2005/Week%2005.md) 7 | -------------------------------------------------------------------------------- /Sem. 06/linearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int GRADES_COUNT = 5; 5 | 6 | int linearSearch(const int arr[], int len, int element) 7 | { 8 | for (int i = 0; i < len; i++) 9 | { 10 | if (arr[i] == element) 11 | return i; 12 | } 13 | return -1; 14 | } 15 | 16 | int main() 17 | { 18 | constexpr int ARR_SIZE = 5; 19 | int arr[ARR_SIZE] = { 5, 4, 3, 2, 1 }; 20 | 21 | bool containsTwo = linearSearch(arr, ARR_SIZE, 2) >= 0; 22 | 23 | cout << containsTwo; 24 | 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int seconds; 7 | std::cin >> seconds; 8 | 9 | int days = seconds / (24 * 3600); 10 | seconds %= (24 * 3600); 11 | 12 | int hours = seconds / 3600; 13 | seconds %= 3600; 14 | 15 | int minutes = seconds / 60; 16 | seconds = seconds % 60; 17 | 18 | std::cout << days << " days, " << hours << " hours, " 19 | << minutes << " minutes, " << seconds << " seconds"; 20 | } 21 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/3.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 0, b = 0; 6 | cin >> a >> b; 7 | 8 | if (b < a) { 9 | int temp = a; 10 | a = b; 11 | b = temp; 12 | } 13 | 14 | for (int i = a; i < b; i++) { 15 | bool isPrime = true; 16 | double sqrtOfI = sqrt(i); 17 | for (int j = 2; j <= sqrtOfI; j++) { 18 | if (i % j == 0) { 19 | isPrime = false; 20 | break; 21 | } 22 | 23 | } 24 | if (isPrime) { 25 | cout << i << " "; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Sem. 03/ex3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | unsigned int n; 8 | cin >> n; 9 | 10 | if(n <= 1) 11 | { 12 | cout << "Not prime" << endl; 13 | return 0; 14 | } 15 | 16 | bool isPrime = true; 17 | double sqrtFromNumberToCheck = sqrt(n); 18 | for (int i = 2; i <= sqrtFromNumberToCheck; i++) 19 | { 20 | if (n % i == 0) 21 | { 22 | isPrime = false; 23 | break; 24 | } 25 | } 26 | if(isPrime) 27 | cout << "Prime"; 28 | else 29 | cout << "Not prime"; 30 | } 31 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, k; 6 | scanf("%d %d", &n, &k); 7 | 8 | if (n == 0) 9 | { 10 | printf("Invalid number!\n"); 11 | return 0; 12 | } 13 | if (k == 0 || k == 1) 14 | { 15 | printf("Invalid base!\n"); 16 | return 0; 17 | } 18 | 19 | int log = 0; 20 | 21 | while (n >= k) 22 | { 23 | n /= k; 24 | log++; 25 | } 26 | 27 | printf("%d\n", log); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Sem. 06/reverseArr.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | 6 | void swap(int& a, int& b) 7 | { 8 | int temp = a; 9 | a = b; 10 | b = temp; 11 | } 12 | void reverse(int arr[], size_t size) 13 | { 14 | for(int i = 0; i < size / 2; i++) 15 | swap(arr[i], arr[size - i - 1]); 16 | } 17 | int main() 18 | { 19 | constexpr size_t size = 4; 20 | int arr[size] = {1, 2, 3, 4}; 21 | reverse(arr, size); 22 | for(int i = 0; i < size; i++) 23 | cout << arr[i] << " "; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double r, x, y; 5 | scanf_s("%lf %lf %lf", &r, &x, &y); 6 | 7 | double distanceSquared = x * x + y * y; 8 | double radiusSquared = r * r; 9 | 10 | if (distanceSquared < radiusSquared) { 11 | printf("In the circle\n"); 12 | } else if (distanceSquared == radiusSquared) { 13 | printf("On the circle\n"); 14 | } else { 15 | printf("Out of the circle\n"); 16 | } 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 8; 5 | 6 | bool isPalindrome(const int arr[], size_t size) { 7 | int lhs = 0; 8 | int rhs = size - 1; 9 | 10 | while (lhs < rhs) { 11 | if (arr[lhs] != arr[rhs]) { 12 | return false; 13 | } 14 | lhs++; 15 | rhs--; 16 | } 17 | return true; 18 | } 19 | 20 | int main() { 21 | int arr[] = {1, 2, 3, 4, 4, 3, 2, 1}; 22 | cout << isPalindrome(arr, ARRAY_SIZE) << endl; 23 | } -------------------------------------------------------------------------------- /C examples/Sem01/Task04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | short thirdDigit = n % 10; // get the last digit 9 | n /= 10; // remove the last digit 10 | 11 | short secondDigit = n % 10; // get the second digit 12 | n /= 10; // remove the second digit 13 | 14 | short firstDigit = n % 10; // get the first digit 15 | 16 | int reversed = thirdDigit * 100 + secondDigit * 10 + firstDigit; 17 | reversed++; 18 | 19 | printf("%d\n", reversed); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double speed; 5 | scanf_s("%lf", &speed); 6 | 7 | if (speed <= 10) { 8 | printf("slow\n"); 9 | } 10 | else if (speed <= 50) { 11 | printf("average\n"); 12 | } 13 | else if (speed <= 150) { 14 | printf("fast\n"); 15 | } 16 | else if (speed <= 1000) { 17 | printf("ultra fast\n"); 18 | } 19 | else { 20 | printf("extremely fast\n"); 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | b += a; 10 | (a -= b) *= -1; 11 | b -= a; 12 | 13 | cout << "First number: " << a; 14 | cout << endl; 15 | cout << "Second number: " << b; 16 | 17 | //Second way : 18 | /* 19 | int x, y; 20 | cin >> x >> y; 21 | 22 | x = x + y; 23 | y = x - y; 24 | x = x - y; 25 | 26 | cout << "First number: " << x; 27 | cout << endl; 28 | cout << "Second number: " << y; 29 | */ 30 | } 31 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int radius = 0, x = 0, y = 0; 6 | cin >> radius >> x >> y; 7 | 8 | int xSquare = x * x; 9 | int ySquare = y * y; 10 | int radiusSquare = radius * radius; 11 | 12 | int sum = xSquare + ySquare; 13 | 14 | if (sum == radius) { 15 | cout << "On the circle" << endl; 16 | } else if (sum < radius) { 17 | cout << "Inside the circle" << endl; 18 | } else { 19 | cout << "Outside the circle" << endl; 20 | } 21 | } -------------------------------------------------------------------------------- /Sem. 04/example 01 - with functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPrime(unsigned n) 5 | { 6 | if(n <= 1) 7 | return false; 8 | 9 | double temp = sqrt(n); 10 | 11 | for (int i = 2; i <= temp; i++) 12 | { 13 | if (n % i == 0) 14 | return false; 15 | } 16 | 17 | return true; 18 | } 19 | 20 | int main() 21 | { 22 | int n; 23 | cin >> n; 24 | 25 | for (int i = 6; i < n; i += 6) 26 | { 27 | if (isPrime(i - 1) && isPrime(i + 1)) 28 | cout << i - 1 << " " << i + 1 << endl; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int lhs = 0, rhs = 0, num = 0; 6 | cin >> lhs >> rhs >> num; 7 | 8 | if (num <= lhs && num <= rhs) { 9 | cout << "The number is within the given interval."; 10 | 11 | if (num == lhs || num == rhs) { 12 | cout << "The interval is closed."; 13 | } else { 14 | cout << "The interval is open."; 15 | } 16 | } else { 17 | cout << "The number is NOT within the given interval."; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long sumInInterval(int m, int n) { 5 | long sum = 0; 6 | for (int i = m; i <= n; i++) { 7 | sum += i; 8 | } 9 | 10 | return sum; 11 | } 12 | 13 | bool validation(int m, int n) { 14 | if (m > n) { 15 | return false; 16 | } 17 | return true; 18 | } 19 | 20 | int main() { 21 | int m = 0, n = 0; 22 | cin >> m >> n; 23 | 24 | if (!validation(m, n)) { 25 | return 0; 26 | } 27 | 28 | cout << sumInInterval(m, n); 29 | } -------------------------------------------------------------------------------- /Sem. 05/maxAndMinDigit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void getMinAndMaxDigits(unsigned n, unsigned& maxDigit, unsigned& minDigit) 5 | { 6 | maxDigit = 0; 7 | minDigit = 9; 8 | 9 | while (n != 0) 10 | { 11 | int lastDigit = n % 10; 12 | if (lastDigit > maxDigit) 13 | maxDigit = lastDigit; 14 | if (lastDigit < minDigit) 15 | minDigit = lastDigit; 16 | 17 | n /= 10; 18 | } 19 | } 20 | 21 | 22 | 23 | int main() 24 | { 25 | unsigned max; 26 | unsigned min; 27 | getMinAndMaxDigits(1234, max, min); 28 | 29 | cout << max << " " << min; 30 | } 31 | -------------------------------------------------------------------------------- /C examples/Sem03/example4_prime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | unsigned n; 7 | scanf("%u", &n); 8 | 9 | bool isPrime = 1; 10 | unsigned sqrtOfN = sqrt(n); 11 | 12 | if (n <= 1) 13 | { 14 | isPrime = 0; 15 | } 16 | for (size_t divisorCandidate = 2; divisorCandidate <= sqrtOfN; divisorCandidate++) 17 | { 18 | if (n % divisorCandidate == 0) 19 | { 20 | isPrime = 0; 21 | break; 22 | } 23 | } 24 | printf("%u\n", isPrime); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Sem. 02/Solutions/01. DaysOfWeek.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num; 7 | cin >> num; 8 | 9 | switch (num) 10 | { 11 | case 1:cout << "Monday" << endl; break; 12 | case 2:cout << "Tuesday" << endl; break; 13 | case 3:cout << "Wednesday" << endl; break; 14 | case 4:cout << "Thursday" << endl; break; 15 | case 5:cout << "Friday" << endl; break; 16 | case 6:cout << "Saturday" << endl; break; 17 | case 7:cout << "Sunday" << endl; break; 18 | 19 | default: cout << "The number is not a valid day of the week" << endl; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task01.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int day; 5 | scanf_s("%d", &day); 6 | 7 | switch (day) { 8 | case 1: printf("Monday\n"); break; 9 | case 2: printf("Tuesday\n"); break; 10 | case 3: printf("Wednesday\n"); break; 11 | case 4: printf("Thursday\n"); break; 12 | case 5: printf("Friday\n"); break; 13 | case 6: printf("Saturday\n"); break; 14 | case 7: printf("Sunday\n"); break; 15 | default: printf("Invalid day!\n"); 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C examples/README.md: -------------------------------------------------------------------------------- 1 | ## Код от семинарите по "Увод в програмирането", спец. "Информатика" ## 2 | 3 | - **Тема 1** : Увод. Защо C? Променливи. Типове данни. Преобразуване между типове. Оператори. 4 | - **Тема 2** : Условни конструкции – if, switch и тернарен оператор. 5 | - **Тема 3** : Цикли – while, do-while и for. Оператори break и continue. Scope на променливи. 6 | - **Тема 4** : Функции 7 | - **Тема 5** : Побитови операции. 8 | - **Тема 6** : Побитови операции (част 2). 9 | - **Тема 7** : Масиви. 10 | - **Тема 8** : Многомерни масиви. 11 | - **Тема 9** : Динамична памет 12 | - **Тема 10** : Низове 13 | 14 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | if (n < 2) { 9 | cout << "Invalid number"; 10 | return 0; 11 | } 12 | 13 | int maxNum = INT_MIN; 14 | int secondMaxNum = INT_MIN; 15 | 16 | for (int i = 0; i < n; i++) { 17 | int x; 18 | cin >> x; 19 | 20 | if (x > maxNum) { 21 | secondMaxNum = maxNum; 22 | maxNum = x; 23 | } 24 | else if (x > secondMaxNum) { 25 | secondMaxNum = x; 26 | } 27 | 28 | } 29 | cout << "Max: " << maxNum << endl; 30 | cout << "Second to max: " << secondMaxNum << endl; 31 | } -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int digitOccurenices(int number, int digit) { 4 | int digitCount = 0; 5 | while (number > 0) { 6 | if (number % 10 == digit) { 7 | digitCount++; 8 | } 9 | number /= 10; 10 | } 11 | return digitCount; 12 | } 13 | bool hasUniqueDigits(int number) { 14 | 15 | for (size_t i = 0; i < 9; i++) 16 | { 17 | if (digitOccurenices(number, i) > 1) { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | 24 | 25 | int main() { 26 | int n = 12345; 27 | std::cout << hasUniqueDigits(n); 28 | } 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Sem. 06/binarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int binarySearch(const int arr[], int len, int el) 5 | { 6 | int left = 0; 7 | int right = len - 1; 8 | 9 | while (left <= right) 10 | { 11 | int mid = left + (right - left) / 2; 12 | //left + right) / 2; 13 | if (el > arr[mid]) 14 | left = mid + 1; 15 | else if (el < arr[mid]) 16 | right = mid - 1; 17 | else 18 | return mid; 19 | } 20 | return -1; 21 | } 22 | 23 | 24 | int main() 25 | { 26 | constexpr int size = 5; 27 | int sortedArr[size] = { 1,5,12,15,20 }; 28 | cout << binarySearch(sortedArr, size, 15); 29 | } 30 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isDigit(char symbol) 5 | { 6 | return (symbol >= '0' && symbol <= '9'); 7 | } 8 | int toNumber(char symbol) 9 | { 10 | if(isDigit(symbol)) 11 | { 12 | return symbol - '0'; 13 | } 14 | else 15 | { 16 | cout << "Not a number"; 17 | return - 1; 18 | } 19 | } 20 | char toCharacter(int number) 21 | { 22 | if(number >= 0 && number <= 9) 23 | { 24 | return '0' + number; 25 | } 26 | else 27 | { 28 | cout << "Not a digit"; 29 | return '?'; 30 | } 31 | } -------------------------------------------------------------------------------- /C examples/Sem05/bitwise.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | unsigned powerOfTwo(unsigned n) ///32 bita -> [0 .. 2^32 - 1] 5 | { 6 | if (n >= 32) 7 | return 0; 8 | return 1 << n; 9 | } 10 | int isEven(int n) 11 | { 12 | int mask = 1; 13 | return ~n & mask;// even -> 0 odd -> 1 14 | } 15 | 16 | int toggleBit(int n, int bitIndex) 17 | { 18 | int mask = 1 << bitIndex; 19 | return n ^ mask; 20 | } 21 | 22 | int checkBit(int n, int bitIndex) 23 | { 24 | int mask = 1 << bitIndex; 25 | return (n & mask) >> bitIndex; 26 | } 27 | 28 | int main() { 29 | 30 | int a = 7; 31 | printf("%d", toggleBit(10, 0)); 32 | } 33 | -------------------------------------------------------------------------------- /Sem. 15/boolVectorsGen.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void genBoolVectors(bool* arr, size_t size, unsigned beginIndex = 0) 5 | { 6 | if (beginIndex == size) 7 | { 8 | for (int i = 0; i < size; i++) 9 | cout << arr[i] << " "; 10 | cout << endl; 11 | return; 12 | } 13 | arr[beginIndex] = 0; 14 | genBoolVectors(arr, size, beginIndex + 1); 15 | arr[beginIndex] = 1; 16 | genBoolVectors(arr, size, beginIndex + 1); 17 | } 18 | 19 | int main() 20 | { 21 | const size_t SIZE = 4; 22 | bool arr[SIZE]; 23 | genBoolVectors(arr, SIZE); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int day, month; 5 | scanf_s("%d %d", &day, &month); 6 | 7 | int daysInMonth = 31; 8 | 9 | if (month == 2) { 10 | daysInMonth = 28; 11 | } else if (month == 4 || month == 6 || month == 9 || month == 11) { 12 | daysInMonth = 30; 13 | } 14 | 15 | day++; 16 | 17 | if (day > daysInMonth) { 18 | day = 1; 19 | month++; 20 | if (month > 12) { 21 | month = 1; 22 | } 23 | } 24 | 25 | printf("%d %d\n", day, month); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char ch = ' '; 6 | cin >> ch; 7 | 8 | bool isVowelLetter = (ch == 'a') || 9 | (ch == 'A') || 10 | 11 | (ch == 'e') || 12 | (ch == 'E') || 13 | 14 | (ch == 'i') || 15 | (ch == 'I') || 16 | 17 | (ch == 'o') || 18 | (ch == 'O') || 19 | 20 | (ch == 'u') || 21 | (ch == 'U'); 22 | 23 | cout << isVowelLetter; 24 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 4; 5 | 6 | // We use the same function for sum from task_01 7 | int sumOfElements(const int arr[], size_t size) { 8 | int sum = 0; 9 | 10 | for (size_t i = 0; i < size; i++) { 11 | sum += arr[i]; 12 | } 13 | 14 | return sum; 15 | } 16 | 17 | double getAverageValue(const int arr[], size_t size) { 18 | int sum = sumOfElements(arr, size); 19 | 20 | return (sum * 0.1) / size; 21 | } 22 | 23 | int main() { 24 | int arr[] = {5, 7, 4, 9}; 25 | cout << getAverageValue(arr, ARRAY_SIZE); 26 | } -------------------------------------------------------------------------------- /Sem. 14/binary_search.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool binarySearch(const int* arr, size_t size, int searched) 6 | { 7 | if(size == 0) 8 | return false; 9 | 10 | int mid = size / 2; 11 | if(arr[mid] == searched) 12 | return true; 13 | if(arr[mid] > searched) 14 | return binarySearch(arr, mid, searched); 15 | else 16 | return binarySearch(arr + mid + 1, size - mid - 1, searched); 17 | } 18 | 19 | int main() 20 | { 21 | int arr[] = {1,5,7,12,14,20}; 22 | 23 | cout << binarySearch(arr, 6, 5); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Sem. 15/high-order-functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | unsigned getCharsCountCond(const char* str, bool(*pred)(char)) 6 | { 7 | unsigned count = 0; 8 | 9 | while (*str) 10 | { 11 | if (pred(*str)) 12 | count++; 13 | str++; 14 | } 15 | return count; 16 | } 17 | 18 | bool isDigit(char ch) 19 | { 20 | return ch >= '0' && ch <= '9'; 21 | } 22 | int main() 23 | { 24 | cout << "Digits count: " << getCharsCountCond("abc341XY", isDigit) << endl; 25 | cout << "All chars: " << getCharsCountCond("abc341XY", [](char ch){return true;}) << endl; 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isLower(char symbol) 5 | { 6 | return (symbol >= 'a' && symbol <='z'); 7 | } 8 | bool isUpper(char symbol) 9 | { 10 | return (symbol >= 'A' && symbol <='Z'); 11 | } 12 | char toUpper(char symbol) 13 | { 14 | if(isLower(symbol)) 15 | { 16 | return symbol - ('a' - 'A'); 17 | } 18 | else 19 | { 20 | return symbol; 21 | } 22 | } 23 | char toLower(char symbol) 24 | { 25 | if(isUpper(symbol)) 26 | { 27 | return symbol + ('a' - 'A'); 28 | } 29 | else 30 | { 31 | return symbol; 32 | } 33 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_02.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | void swap(int& a, int& b) { 6 | 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | } 11 | int myGCD(int a, int b) { 12 | if (b < a) { 13 | swap(a, b); 14 | } 15 | while (b != 0) { 16 | int mod = a % b; 17 | a = b; 18 | b = mod; 19 | } 20 | return a; 21 | } 22 | void reduceFraction(int& num, int& denom) { 23 | int gcd = myGCD(num, denom); 24 | num /= gcd; 25 | denom /= gcd; 26 | 27 | } 28 | int main() { 29 | 30 | int a, b; 31 | cin >> a >> b; 32 | reduceFraction(a, b); 33 | cout << a << " " << b; 34 | 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Sem. 13/readme.md: -------------------------------------------------------------------------------- 1 | ## Анализ на алгоритми. Алгоритми сортиране. 2 | Сложност: Функция по големината на входа (N) 3 | 4 | **Пример**: 5 | 6 | - При търсене в масив - големината на входа е големината на масива 7 | - При проверка число дали е просто - големината на входа е числото. 8 | 9 | При анализа на алгоритми НЕ се интересуваме от точната функция! 10 | Интересуваме се само то асимптотиката на функцията. 11 | Колко бързо расте функцията при n->безкрайност. 12 | 13 | ![enter image description here](https://i.ibb.co/TP0Xrz4/ff.png) 14 | 15 | Тук релациите за по-малко и по-равно не са тези, които познаваме. 16 | Тук визираме скоростта на растежа на функцията. 17 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getDivision(int first, int second) { 5 | if (second == 0) { 6 | return 0; 7 | } 8 | 9 | int dividend = abs(first); 10 | int divisor = abs(second); 11 | 12 | int quotient = 0; 13 | while (dividend >= divisor) { 14 | dividend -= divisor; 15 | quotient++; 16 | } 17 | 18 | if (first < 0 || second < 0) { 19 | return -quotient; 20 | } 21 | 22 | return quotient; 23 | } 24 | 25 | int main() { 26 | int first = 0, second = 0; 27 | cin >> first >> second; 28 | 29 | cout << getDivision(first, second) << endl; 30 | } -------------------------------------------------------------------------------- /Sem. 06/sieveErathostenes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void markAsNotPrime(bool sieve[], int len, int current) 5 | { 6 | int toMark = current + current; 7 | 8 | while (toMark < len) 9 | { 10 | sieve[toMark] = true; 11 | toMark += current; 12 | } 13 | } 14 | void sieveErathostenes(bool sieve[], int len) 15 | { 16 | for (int i = 2; i <= len; i++) 17 | { 18 | if (!sieve[i]) 19 | { 20 | cout << i << endl; 21 | markAsNotPrime(sieve, len, i); 22 | } 23 | 24 | } 25 | } 26 | 27 | 28 | int main() 29 | { 30 | constexpr int MAX_SIZE = 1024; 31 | int n; 32 | cin >> n; 33 | bool sieve[MAX_SIZE]{0}; 34 | sieveErathostenes(sieve, n); 35 | } 36 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task09.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | double num1, num2; 5 | char op; 6 | scanf_s("%lf %c %lf", &num1, &op, 1, &num2); 7 | 8 | switch (op) { 9 | case '+': printf("%.2lf\n", num1 + num2); break; 10 | case '-': printf("%.2lf\n", num1 - num2); break; 11 | case '*': printf("%.2lf\n", num1 * num2); break; 12 | case '/': 13 | if (num2 != 0) { 14 | printf("%.2lf\n", num1 / num2); 15 | } else { 16 | printf("Cannot divide by zero\n"); 17 | } 18 | break; 19 | default: printf("Invalid operation\n"); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task07.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | for (size_t i = 0; i < n; i++) 9 | { 10 | int start = (i * n) + 1; 11 | int end = (start + n) - 1; 12 | 13 | if (i % 2 == 0) 14 | { 15 | for (int j = start; j <= end; j++) 16 | { 17 | printf("%d ", j); 18 | } 19 | printf("\n"); 20 | } 21 | else 22 | { 23 | for (int j = end; j >= start; j--) 24 | { 25 | printf("%d ", j); 26 | } 27 | printf("\n"); 28 | } 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | int mostCommonDigit = 0; 9 | int mostCommonDigitOccurrences = 0; 10 | 11 | for (int i = 9; i >= 0; i--) { 12 | int copyOfN = n; 13 | int currDigitOccurences = 0; 14 | 15 | while (copyOfN != 0) { 16 | int lastDigit = copyOfN % 10; 17 | 18 | if (lastDigit == i) { 19 | currDigitOccurences++; 20 | } 21 | copyOfN /= 10; 22 | } 23 | 24 | if (currDigitOccurences >= mostCommonDigitOccurrences) { 25 | mostCommonDigit = i; 26 | mostCommonDigitOccurrences = currDigitOccurences; 27 | } 28 | 29 | } 30 | cout << mostCommonDigit; 31 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | double a = 0, b = 0, c = 0; 6 | cin >> a >> b >> c; 7 | 8 | double aSquared = a * a, 9 | bSquared = b * b, 10 | cSquared = c * c; 11 | 12 | if (a == b && b == c) { 13 | cout << "Equilateral triangle"; 14 | } else if (a == b || b == c || a == c) { 15 | cout << "Isosceles triangle"; 16 | } else if ((aSquared + bSquared == cSquared) || 17 | (bSquared + cSquared == aSquared) || 18 | (aSquared + cSquared == bSquared)) { 19 | cout << "Right-angled triangle"; 20 | } else { 21 | cout << "Neither"; 22 | } 23 | } -------------------------------------------------------------------------------- /Sem. 15/sum_numbers_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isDigit(char ch) 5 | { 6 | return ch >= '0' && ch <= '9'; 7 | } 8 | unsigned getSumOfNumbersInStr(const char* str, unsigned currAccNumber = 0, unsigned globalSum = 0) 9 | { 10 | if (!*str) 11 | { 12 | globalSum += currAccNumber; 13 | return globalSum; 14 | } 15 | if (isDigit(*str)) 16 | (currAccNumber *= 10) += (*str - '0'); 17 | else 18 | { 19 | globalSum += currAccNumber; 20 | currAccNumber = 0; 21 | } 22 | return getSumOfNumbersInStr(str + 1, currAccNumber, globalSum); 23 | } 24 | 25 | int main() 26 | { 27 | cout << getSumOfNumbersInStr("asd123as33d"); 28 | } 29 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task04.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char figure; 5 | double a, b, result; 6 | double PI = 3.14; 7 | 8 | scanf_s(" %c", &figure, 1); 9 | 10 | if (figure == 's') { 11 | scanf_s("%lf", &a); 12 | result = a * a; 13 | } 14 | else if (figure == 'r') { 15 | scanf_s("%lf %lf", &a, &b); 16 | result = a * b; 17 | } 18 | else if (figure == 'c') { 19 | scanf_s("%lf", &a); 20 | result = PI * a * a; 21 | } 22 | else if (figure == 't') { 23 | scanf_s("%lf %lf", &a, &b); 24 | result = (a * b) / 2; 25 | } 26 | 27 | printf("%.3lf\n", result); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int a = 0, 6 | b = 0, 7 | c = 0, 8 | d = 0, 9 | e = 0; 10 | 11 | cin >> a >> b >> c >> d >> e; 12 | 13 | bool isTrion = false; 14 | 15 | if ((b >= a && b >= c) || (b <= a && b <= c)) { 16 | if ((c >= b && c >= d) || (c <= b && c <= d)) { 17 | if ((d >= c && d >= e) || (d <= c && d <= e)) { 18 | cout << "yes" << endl; 19 | } else { 20 | cout << "no" << endl; 21 | } 22 | } else { 23 | cout << "no" << endl; 24 | } 25 | } else { 26 | cout << "no" << endl; 27 | } 28 | } -------------------------------------------------------------------------------- /Sem. 15/fibb_memoization.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | unsigned fibb_rec(unsigned n, unsigned* cache) 7 | { 8 | 9 | if(n <= 1) 10 | return 1; 11 | 12 | if(cache[n] != 0) 13 | return cache[n]; 14 | else 15 | { 16 | int current = fibb_rec(n - 1, cache) + fibb_rec(n - 2, cache); 17 | cache[n] = current; 18 | return current; 19 | } 20 | } 21 | 22 | unsigned fibb(unsigned n) 23 | { 24 | unsigned* cache = new unsigned[n + 1]{0}; 25 | unsigned res = fibb_rec(n, cache); 26 | delete[] cache; 27 | return res; 28 | } 29 | 30 | 31 | int main() 32 | { 33 | cout << fibb(45); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 7; 5 | 6 | void printArray(const int arr[], size_t size) { 7 | for (int i = 0; i < size; i++) { 8 | cout << arr[i] << ", "; 9 | } 10 | cout << endl; 11 | } 12 | 13 | void reverseArray(int arr[], size_t size) { 14 | int lhs = 0; 15 | int rhs = size - 1; 16 | while (lhs < rhs) { 17 | int temp = arr[lhs]; 18 | arr[lhs] = arr[rhs]; 19 | arr[rhs] = arr[lhs]; 20 | 21 | lhs++; 22 | rhs--; 23 | } 24 | 25 | printArray(arr, size); 26 | } 27 | 28 | int main() { 29 | int arr[] = {1, 7, 4, -5, 12, -3, 6}; 30 | reverseArray(arr, ARRAY_SIZE); 31 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | if (n % 2 == 0 || n <= 0) { 9 | cout << "Enter natural number: "; 10 | } 11 | 12 | else { 13 | int half = n / 2; 14 | 15 | for (int i = 0; i <= half; i++) { 16 | 17 | for (int j = 0; j < i; j++) { 18 | cout << " "; 19 | } 20 | 21 | for (int k = i; k < n - i; k++) { 22 | cout << "*"; 23 | } 24 | 25 | cout << endl; 26 | } 27 | 28 | for (int i = half - 1; i >= 0; i--) { 29 | 30 | for (int j = 0; j < i; j++) { 31 | cout << " "; 32 | } 33 | 34 | for (int k = i; k < n - i; k++) { 35 | cout << "*"; 36 | } 37 | 38 | cout << endl; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getCountOfOccurs(const int arr[], size_t arrSize, int num) { 5 | int result = 0; 6 | for (int i = 0; i < arrSize; i++) { 7 | if (arr[i] == num) { 8 | result++; 9 | } 10 | } 11 | return result; 12 | } 13 | 14 | int findMissing(const int arr1[], size_t size1, const int arr2[], size_t size2) { 15 | for (int i = 0; i < size1; i++) { 16 | if (getCountOfOccurs(arr2, size2, arr1[i]) == 0) { 17 | return arr1[i]; 18 | } 19 | } 20 | return -1; 21 | } 22 | 23 | int main() { 24 | int arr1[] = {3, 1, 5, 7, 9}; 25 | int arr2[] = {9, 7, 5, 3}; 26 | cout << findMissing(arr1, 5, arr2, 4); 27 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_05_bonus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int first = 0, second = 0, third = 0; 6 | cin >> first >> second >> third; 7 | 8 | if (first > 9 || second > 9 || third > 9) { 9 | cout << "Incorrect input data"; 10 | } 11 | 12 | int temp = 0; 13 | if (first > second) { 14 | temp = first; 15 | first = second; 16 | second = temp; 17 | } 18 | 19 | if (second > third) { 20 | temp = second; 21 | second = third; 22 | third = temp; 23 | } 24 | 25 | if (first > second) { 26 | temp = first; 27 | first = second; 28 | second = temp; 29 | } 30 | 31 | cout << third << second << first; 32 | } -------------------------------------------------------------------------------- /Sem. 02/Solutions/04. Figures.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const double PI = 3.14; 5 | int main() 6 | { 7 | char figure; 8 | cin >> figure; 9 | 10 | if (figure == 's') 11 | { 12 | double side; 13 | cin >> side; 14 | cout << side * side << endl; 15 | } 16 | else if (figure == 'r') 17 | { 18 | double sideA, sideB; 19 | cin >> sideA >> sideB; 20 | cout << sideA * sideB << endl; 21 | } 22 | else if (figure == 'c') 23 | { 24 | double R; 25 | cin >> R; 26 | cout << PI * R * R << endl; 27 | } 28 | else if (figure == 't') 29 | { 30 | double width; 31 | double height; 32 | cin >> width >> height; 33 | cout << width * height / 2 << endl; 34 | } 35 | else 36 | { 37 | cout << "Invalid figure!" << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Sem. 13/insertionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void insertionSort(int* arr, size_t size) 5 | { 6 | for (int i = 1; i < size; i++) 7 | { 8 | int currentElement = arr[i]; 9 | int currentIndex = i - 1; 10 | while (currentIndex >= 0 && arr[currentIndex] > currentElement) 11 | { 12 | arr[currentIndex + 1] = arr[currentIndex]; 13 | currentIndex--; 14 | } 15 | arr[currentIndex + 1] = currentElement; 16 | } 17 | } 18 | 19 | void printArr(const int* arr, size_t size) 20 | { 21 | for (int i = 0; i < size; i++) 22 | cout << arr[i] << " "; 23 | cout << endl; 24 | } 25 | 26 | int main() 27 | { 28 | constexpr size_t SIZE = 5; 29 | int arr[SIZE] = { 5,4,3,2,1 }; 30 | 31 | insertionSort(arr, SIZE); 32 | printArr(arr, SIZE); 33 | } 34 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 02 - Conditions/Solutions/Task10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char A_start, A_end, B_start, B_end, letter; 5 | scanf_s(" %c %c %c %c %c", &A_start, 1, &A_end, 1, &B_start, 1, &B_end, 1, &letter, 1); 6 | 7 | int inA = (letter >= A_start && letter <= A_end) || 8 | (letter >= A_start + 32 && letter <= A_end + 32); 9 | 10 | int inB = (letter >= B_start && letter <= B_end) || 11 | (letter >= B_start - 32 && letter <= B_end - 32); 12 | 13 | printf("%s, ", inA || inB ? "true" : "false"); 14 | printf("%s, ", inA && inB ? "true" : "false"); 15 | printf("%s, ", inA && !inB ? "true" : "false"); 16 | printf("%s\n", (inA || inB) && !(inA && inB) ? "true" : "false"); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Sem. 03/ex4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | 11 | for (int i = n; i > 1; i--) 12 | { 13 | //check if i is prime 14 | bool isPrime = true; 15 | double temp = sqrt(i); 16 | for (int k = 2; k <= temp; k++) 17 | { 18 | if (i % k == 0) 19 | { 20 | isPrime = false; 21 | break; 22 | } 23 | } 24 | 25 | if (!isPrime) 26 | continue; //we don't need this number 27 | 28 | //check how many time i devides n 29 | int count = 0; 30 | while (n % i == 0) 31 | { 32 | count++; 33 | n /= i; 34 | } 35 | 36 | if (count >= 1) 37 | { 38 | cout << i; 39 | if(count >= 2) 40 | cout << "^" << count << " "; 41 | } 42 | } 43 | 44 | 45 | } 46 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task14.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b; 6 | scanf("%d %d", &a, &b); 7 | 8 | int match = 0; 9 | int cnt = 0; 10 | 11 | for (size_t i = a; i <= b; i++) 12 | { 13 | int sum = 0; 14 | int prod = 1; 15 | 16 | int temp = i; 17 | while (temp) 18 | { 19 | int reminder = temp % 10; 20 | 21 | if (reminder == 0) 22 | { 23 | prod = 1; 24 | } 25 | 26 | sum += reminder; 27 | prod *= reminder; 28 | 29 | temp /= 10; 30 | } 31 | 32 | if (sum == prod) 33 | { 34 | cnt++; 35 | } 36 | } 37 | 38 | printf("%d\n", cnt); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Sem. 03/ex6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin >> n; 10 | 11 | int mostCommonDigit = -1; 12 | int mostCommonDigitOccurences = 0; 13 | 14 | for (int currentDigit = 0; currentDigit <= 9; currentDigit++) 15 | { 16 | int copyOfN = n; 17 | int currentDigitOccurences = 0; 18 | while (copyOfN != 0) 19 | { 20 | int lastDigit = copyOfN % 10; 21 | if (lastDigit == currentDigit) 22 | currentDigitOccurences++; 23 | copyOfN /= 10; //removes the last digit; 24 | } 25 | 26 | if (currentDigitOccurences > mostCommonDigitOccurences) 27 | { 28 | mostCommonDigit = currentDigit; 29 | mostCommonDigitOccurences = currentDigitOccurences; 30 | } 31 | } 32 | 33 | cout << mostCommonDigit; 34 | } 35 | -------------------------------------------------------------------------------- /Sem. 14/max_element_in_array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int myMax(int a, int b) 6 | { 7 | return a > b ? a : b; 8 | } 9 | 10 | int getMaxInArr(const int* arr, size_t size) 11 | { 12 | if(size == 0) 13 | return INT_MIN; 14 | return myMax(arr[0], getMaxInArr(arr + 1, size - 1)); 15 | } 16 | 17 | int getMaxInArr2(const int* arr, size_t size) 18 | { 19 | if(size == 0) 20 | return INT_MIN; 21 | if(size == 1) 22 | return arr[0]; 23 | 24 | int mid = size/ 2; 25 | return myMax(getMaxInArr2(arr, mid), getMaxInArr2(arr + mid, size - mid)); 26 | } 27 | 28 | 29 | int main() 30 | { 31 | int arr[] = {1, 2, 6, 3, 1}; 32 | 33 | cout << getMaxInArr2(arr, 5); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Sem. 10/number_to_str.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | char getCharFromDigit(int digit) 5 | { 6 | if (digit < 0 || digit > 9) 7 | return '\0'; 8 | return digit + '0'; 9 | } 10 | 11 | unsigned getNumberLength(unsigned int n) 12 | { 13 | 14 | if (n == 0) 15 | return 1; 16 | unsigned int res = 0; 17 | 18 | while (n != 0) 19 | { 20 | res++; 21 | n /= 10; 22 | } 23 | return res; 24 | } 25 | 26 | 27 | void toString(unsigned int n, char* str) 28 | { 29 | unsigned int len = getNumberLength(n); 30 | 31 | for (int i = len - 1; i >= 0; i--) 32 | { 33 | str[i] = getCharFromDigit(n % 10); 34 | n /= 10; 35 | } 36 | str[len] = '\0'; 37 | } 38 | 39 | 40 | int main() 41 | { 42 | char str[50]; 43 | unsigned int a = 1234; 44 | 45 | toString(a, str); 46 | 47 | cout << str; 48 | } 49 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char symbol = ' '; 6 | cin >> symbol; 7 | 8 | if (symbol >= 'A' && symbol <= 'Z') { 9 | cout << "Uppercase letter"; 10 | symbol = symbol + ('a'- 'A'); 11 | } else if (symbol >= 'a' && symbol <= 'Z') { 12 | cout << "Lowercase letter"; 13 | symbol = symbol + ('A' - 'a'); 14 | } 15 | 16 | switch (symbol) { 17 | case 'A': 18 | case 'a': 19 | case 'E': 20 | case 'e': 21 | case 'I': 22 | case 'i': 23 | case 'O': 24 | case 'o': 25 | case 'U': 26 | case 'u': 27 | cout << "Vowel" << endl; break; 28 | default: 29 | cout << "Consonant" << endl; break; 30 | } 31 | } -------------------------------------------------------------------------------- /Sem. 14/searchInText.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isPrefixRec(const char* text, const char* pattern) 6 | { 7 | if(!*pattern) 8 | return true; 9 | return *pattern == *text && isPrefixRec(text + 1, pattern + 1); 10 | } 11 | bool findInTextRec(const char* text, size_t textLen, const char* pattern, size_t patternLen) 12 | { 13 | if(patternLen > textLen) 14 | return false; 15 | 16 | return isPrefixRec(text, pattern) || findInTextRec(text + 1, textLen - 1, pattern, patternLen); 17 | } 18 | 19 | bool findInText(const char* text, const char* pattern) 20 | { 21 | return findInTextRec(text, strlen(text), pattern, strlen(pattern)); 22 | } 23 | 24 | int main() 25 | { 26 | cout< 2 | #include 3 | 4 | int main() 5 | { 6 | int a, b; 7 | scanf("%d %d", &a, &b); 8 | 9 | if (a < 2) 10 | { 11 | a = 2; 12 | } 13 | 14 | int number = 0; 15 | for (size_t i = a; i <= b; i++) 16 | { 17 | bool prime = 1; 18 | double sqrtI = sqrt(i); 19 | 20 | for (int j = 2; j <= sqrtI; j++) // N.B.!! checks for divisors ONLY IN THE RANGE [2..sqrt(i)]. If there aren't any divisors in this interval, then there are NO DIVISORS AT ALL! 21 | { 22 | if (!(i % j)) 23 | { 24 | prime = 0; 25 | } 26 | } 27 | 28 | if (prime) 29 | { 30 | printf("%d ", i); 31 | } 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Sem. 10/str_to_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int convertCharToDigit(char ch) 5 | { 6 | if(ch >= '0' && ch <= '9') 7 | return ch - '0'; 8 | return -1; 9 | } 10 | unsigned convertStrToUnsigned(const char* str) 11 | { 12 | if (!str) 13 | return 0; 14 | 15 | unsigned result = 0; 16 | while (*str) 17 | { 18 | int digit = convertCharToDigit(*str); 19 | if (digit == -1) 20 | return 0; 21 | (result *= 10) += digit; 22 | str++; 23 | } 24 | return result; 25 | } 26 | int convertStrToSigned(const char* str) 27 | { 28 | if (!str) 29 | return 0; 30 | 31 | if (*str == '-') 32 | return -1 * convertStrToUnsigned(str + 1); 33 | else 34 | return convertStrToUnsigned(str); 35 | } 36 | 37 | int main() 38 | { 39 | unsigned int n = convertStrToUnsigned("123"); 40 | cout << n; 41 | } 42 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void reversePart(int arr[], size_t start, size_t end) { 5 | while (start < end) { 6 | int temp = arr[start]; 7 | arr[start] = arr[end]; 8 | arr[end] = temp; 9 | 10 | start++; 11 | end--; 12 | } 13 | } 14 | 15 | void rotateArray(int arr[], size_t arrSize, int n) { 16 | if (arrSize == 0 || n <= 0) { 17 | return; 18 | } 19 | 20 | n %= arrSize; 21 | if (n == 0) { 22 | return; 23 | } 24 | 25 | reversePart(arr, 0, arrSize - 1); // Обръщаме целия масив 26 | reversePart(arr, 0, n - 1); // Обръщаме първите n - елемента 27 | reversePart(arr, n, arrSize - 1); // Обръщаме останалите arrSize - n елемента 28 | 29 | } 30 | 31 | int main() { 32 | 33 | } -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr int ARR_SIZE = 6; 5 | 6 | void swap(int& a, int& b) { 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | } 11 | 12 | void sortArray(int arr[], size_t size) { 13 | for (size_t i = 0; i < size - 1; i++) { 14 | for (size_t j = 0; j < size - i - 1; j++) { 15 | if (arr[j] > arr[j + 1]) { 16 | swap(arr[j], arr[j + 1]); 17 | } 18 | } 19 | } 20 | } 21 | 22 | void printArr(const int arr[], size_t size) { 23 | for (size_t i = 0; i < size; i++) { 24 | cout << arr[i] << " "; 25 | } 26 | cout << endl; 27 | } 28 | 29 | int main() { 30 | int arr[] = {15, 4, 6, 0, 32, 12}; 31 | 32 | sortArray(arr, ARR_SIZE); 33 | printArr(arr, ARR_SIZE); 34 | } -------------------------------------------------------------------------------- /Sem. 14/lower_and_upper_count.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isCapital(char ch) 5 | { 6 | return ch >= 'A' && ch <= 'Z'; 7 | } 8 | 9 | bool isLower(char ch) 10 | { 11 | return ch >= 'a' && ch <= 'z'; 12 | } 13 | 14 | void getLowerAndUpperCount(const char* str, int& lowerCount, int& capitalCount) 15 | { 16 | if(*str == '\0') 17 | return; 18 | 19 | if(isCapital(str[0])) 20 | capitalCount++; 21 | else if(isLower(str[0])) 22 | lowerCount++; 23 | getLowerAndUpperCount(str + 1, lowerCount, capitalCount); 24 | } 25 | 26 | int main() 27 | { 28 | int lowers, capitals; 29 | lowers = capitals = 0; 30 | getLowerAndUpperCount("afsdfsdSDFFDS", lowers, capitals); 31 | 32 | cout << lowers << " " << capitals << endl; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 9; 5 | 6 | int findCountOfOccurs(const int arr[], size_t size, int number) { 7 | int count = 0; 8 | for (int i = 0; i < size; i++) { 9 | if (arr[i] == number) { 10 | count++; 11 | } 12 | } 13 | return count; 14 | } 15 | 16 | int findMostCommonNumber(const int arr[], size_t size) { 17 | int max = 0; 18 | for (int i = 0; i < size; i++) { 19 | int currentCount = findCountOfOccurs(arr, size, arr[i]); 20 | if (currentCount > max) { 21 | max = currentCount; 22 | } 23 | } 24 | return max; 25 | } 26 | 27 | int main() { 28 | int arr[] = {6, 3, 5, 7, 6, 5, 3, 6, 5}; 29 | cout << findMostCommonNumber(arr, ARRAY_SIZE) << endl; 30 | } -------------------------------------------------------------------------------- /Sem. 13/selectionSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swapNums(int& a, int& b) 5 | { 6 | int temp = a; 7 | a = b; 8 | b = temp; 9 | } 10 | 11 | void selectionSort(int* arr, size_t size) 12 | { 13 | for (int i = 0; i < size - 1; i++) 14 | { 15 | int minElementIndex = i; 16 | for (int j = i + 1; j < size; j++) 17 | { 18 | if (arr[j] < arr[minElementIndex]) 19 | minElementIndex = j; 20 | } 21 | if (minElementIndex != i) 22 | swapNums(arr[i], arr[minElementIndex]); 23 | } 24 | } 25 | 26 | void printArr(const int* arr, size_t size) 27 | { 28 | for (int i = 0; i < size; i++) 29 | cout << arr[i] << " "; 30 | cout << endl; 31 | } 32 | int main() 33 | { 34 | constexpr size_t SIZE = 5; 35 | int arr[SIZE] = { 5,4,3,2,1 }; 36 | 37 | selectionSort(arr, SIZE); 38 | printArr(arr, SIZE); 39 | } 40 | -------------------------------------------------------------------------------- /Sem. 06/printArr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int GRADES_COUNT = 5; 5 | 6 | void printArr(const int arr[], size_t size) 7 | { 8 | for (size_t i = 0; i < size; i++) 9 | { 10 | cout << arr[i] << " "; 11 | } 12 | cout << endl; 13 | } 14 | void printArrReverse(const int arr[], size_t size) 15 | { 16 | for (int i = size - 1; i >= 0; i--) //here should be signed 17 | { 18 | cout << arr[i] << " "; 19 | } 20 | cout << endl; 21 | } 22 | 23 | void setData(int arr[], size_t size, int data) 24 | { 25 | for (size_t i = 0; i < size; i++) 26 | { 27 | arr[i] = data; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | constexpr size_t ARR_SIZE = 5; 34 | int arr[ARR_SIZE]; 35 | setData(arr, 5, 3); 36 | printArr(arr, ARR_SIZE); 37 | 38 | 39 | int arr2[3] = { 1, 2, 3 }; 40 | printArrReverse(arr2, 3); 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, k; 6 | scanf("%d %d", &n, &k); 7 | 8 | int originalN = n; 9 | int originalK = k; 10 | 11 | int changed = 0; 12 | 13 | while (k > 0) 14 | { 15 | if (n == 0 && k > 0) 16 | { 17 | printf("k is NOT infix of n\n"); 18 | return 0; 19 | } 20 | 21 | if (n % 10 == k % 10) 22 | { 23 | n /= 10; 24 | k /= 10; 25 | changed = 1; 26 | continue; 27 | } 28 | 29 | if (changed && (n % 10 != k % 10)) 30 | { 31 | k = originalK; 32 | changed = 0; 33 | continue; 34 | } 35 | 36 | n /= 10; 37 | } 38 | 39 | printf("k is infix of n\n"); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 9; 5 | 6 | int findMin(const int arr[], size_t size) { 7 | int min = INT_MAX; // constant for the maximum value of int 8 | for (size_t i = 0; i < size; i++) { 9 | if (min < arr[i]) { 10 | min = arr[i]; 11 | } 12 | } 13 | return min; 14 | } 15 | 16 | int findMax(const int arr[], size_t size) { 17 | int max = INT_MIN; // constant for the minimum value of int 18 | for (size_t i = 0; i < size; i++) { 19 | if (max > arr[i]) { 20 | max = arr[i]; 21 | } 22 | } 23 | return max; 24 | } 25 | 26 | int main() { 27 | int arr[] = {3, 1, 2, 8, 13, 5, 1, 6, 25}; 28 | cout << findMin(arr, ARRAY_SIZE) << endl; 29 | cout << findMax(arr, ARRAY_SIZE) << endl; 30 | } -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int reverseNumber(int n) { 5 | int reversed = 0; 6 | while (n) { 7 | reversed *= 10; 8 | 9 | int lastDig = n % 10; 10 | reversed += lastDig; 11 | 12 | n /= 10; 13 | } 14 | return reversed; 15 | } 16 | 17 | bool isPalindrom(int n) { 18 | return (n == reverseNumber(n)); 19 | } 20 | 21 | int checkIfSpecial(int n, int k) { 22 | while (k) { 23 | int reversed = reverseNumber(n); 24 | int result = reversed + n; 25 | 26 | if (isPalindrom(result)) { 27 | return k; 28 | } 29 | 30 | k--; 31 | } 32 | 33 | return -1; 34 | } 35 | 36 | 37 | int main() { 38 | int n = 0, k = 0; 39 | cin >> n >> k; 40 | 41 | cout << checkIfSpecial(n, k) << endl; 42 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: g++.exe build active file", 6 | "command": "C:\\msys64\\ucrt64\\bin\\g++.exe", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void getLargestSum(int arr[], size_t size, size_t& maxInd, size_t& secondMaxInd, int& sum) { 6 | int maxNum = 0, secondMaxNum = 0; 7 | 8 | for (size_t i = 0; i < size; i++) 9 | { 10 | if (arr[i] > maxNum) { 11 | secondMaxNum = maxNum; 12 | secondMaxInd = maxInd; 13 | 14 | maxNum = arr[i]; 15 | maxInd = i; 16 | } 17 | else if (arr[i] > secondMaxNum) { 18 | secondMaxNum = arr[i]; 19 | secondMaxInd = i; 20 | } 21 | } 22 | sum = maxNum + secondMaxNum; 23 | 24 | } 25 | 26 | int main() { 27 | 28 | int arr[6] = { 1,2,56,4,5,9 }; 29 | size_t ind1, ind2; 30 | int sum; 31 | getLargestSum(arr, 6, ind1, ind2, sum); 32 | cout << ind1 << " " << ind2 << " " << sum << endl; 33 | 34 | 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /C examples/Sem01/Task05.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int money; 6 | scanf("%d", &money); 7 | 8 | unsigned int hundred = money / 100; 9 | printf("%u x 100 lv\n", hundred); 10 | money = money % 100; 11 | 12 | unsigned int fifty = money / 50; 13 | printf("%u x 50 lv\n", fifty); 14 | money = money % 50; 15 | 16 | unsigned int twenty = money / 20; 17 | printf("%u x 20 lv\n", twenty); 18 | money = money % 20; 19 | 20 | unsigned int ten = money / 10; 21 | printf("%u x 10 lv\n", ten); 22 | money = money % 10; 23 | 24 | unsigned int five = money / 5; 25 | printf("%u x 5 lv\n", five); 26 | money = money % 5; 27 | 28 | unsigned int two = money / 2; 29 | printf("%u x 2 lv\n", two); 30 | money = money % 2; 31 | 32 | printf("%d x 1 lv\n", money); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Sem. 01/Solutions/task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int money; 7 | cin >> money; 8 | 9 | int hundred = money / 100; 10 | cout << hundred << "x100 lv" << endl; 11 | money = money % 100; 12 | 13 | int fifty = money / 50; 14 | cout << fifty << "x50 lv" << endl; 15 | money = money % 50; 16 | 17 | 18 | int twenty = money / 20; 19 | cout << twenty << "x20 lv" << endl; 20 | money = money % 20; 21 | 22 | int ten = money / 10; 23 | cout << ten << "x10 lv" << endl; 24 | money = money % 10; 25 | 26 | int five = money / 5; 27 | cout << five << "x5 lv" << endl; 28 | money = money % 5; 29 | 30 | int two = money / 2; 31 | cout << two << "x2 lv" << endl; 32 | money = money % 2; 33 | 34 | cout << money << "x1 lv" << endl; 35 | } 36 | -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | constexpr size_t arr_size = 32; 4 | void generateFibSequence(int n, int arr[], size_t& size) { 5 | if (n <= 0) { 6 | size = 0; 7 | return; 8 | } 9 | 10 | arr[0] = 0; 11 | if (n == 1) { 12 | size = 1; 13 | return; 14 | } 15 | arr[1] = 1; 16 | for (size_t i = 2; i < n; i++) 17 | { 18 | arr[i] = arr[i - 1] + arr[i - 2]; 19 | } 20 | size = n; 21 | 22 | } 23 | void printArray(int arr[], size_t size) { 24 | for (size_t i = 0; i < size; i++) { 25 | cout << arr[i]; 26 | if (i + 1 < size) { 27 | cout << ", "; 28 | } 29 | } 30 | cout << endl; 31 | } 32 | int main() { 33 | 34 | int n; 35 | cin >> n; 36 | int arr[arr_size]; 37 | size_t size; 38 | generateFibSequence(n, arr, size); 39 | printArray(arr, size); 40 | 41 | 42 | } 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /Sem. 04/example 01 - no functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int main() 6 | { 7 | int n; 8 | cin >> n; 9 | 10 | for (int i = 6; i < n; i += 6) 11 | { 12 | int firstToCheck = i - 1; 13 | int secondToCheck = i + 1; 14 | 15 | //prime check 16 | bool isFirstPrime = true; 17 | double temp = sqrt(firstToCheck); 18 | for (int j = 2; j <= temp; j++) 19 | { 20 | if (firstToCheck % j == 0) 21 | isFirstPrime = false; 22 | } 23 | 24 | if (!isFirstPrime) 25 | continue; 26 | 27 | //prime check 28 | bool isSecondPrime = true; 29 | double temp2 = sqrt(secondToCheck); 30 | for (int j = 2; j <= temp2; j++) 31 | { 32 | if (secondToCheck % j == 0) 33 | isSecondPrime = false; 34 | } 35 | 36 | if (isSecondPrime) 37 | cout << firstToCheck << " " << secondToCheck << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Practicum/Week 01/Solutions/task_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int money; 7 | cin >> money; 8 | 9 | int hundred = money / 100; 10 | cout << hundred << "x100 lv" << endl; 11 | money = money % 100; 12 | 13 | int fifty = money / 50; 14 | cout << fifty << "x50 lv" << endl; 15 | money = money % 50; 16 | 17 | 18 | int twenty = money / 20; 19 | cout << twenty << "x20 lv" << endl; 20 | money = money % 20; 21 | 22 | int ten = money / 10; 23 | cout << ten << "x10 lv" << endl; 24 | money = money % 10; 25 | 26 | int five = money / 5; 27 | cout << five << "x5 lv" << endl; 28 | money = money % 5; 29 | 30 | int two = money / 2; 31 | cout << two << "x2 lv" << endl; 32 | money = money % 2; 33 | 34 | cout << money << "x1 lv" << endl; 35 | } 36 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 20; 5 | 6 | bool isValidInput(const int arr[], size_t size) { 7 | if (size != ARRAY_SIZE) { 8 | return false; 9 | } 10 | 11 | for (int i = 0; i < size; i++) { 12 | if (arr[i] < 0 || arr[i] > 20) { 13 | return false; 14 | } 15 | } 16 | 17 | return true; 18 | } 19 | 20 | int findCountOfOccurs(const int arr[], size_t size, int current) { 21 | int count = 0; 22 | for (int i = 0; i < size; i++) { 23 | if (arr[i] == current) { 24 | count++; 25 | } 26 | } 27 | return count; 28 | } 29 | 30 | void printCountOfAllOccurs(const int arr[], size_t size) { 31 | for (int i = 0; i < size; i++) { 32 | cout << findCountOfOccurs(arr, size, i) << ", "; 33 | } 34 | } 35 | 36 | int main() { 37 | 38 | } -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task08.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Enter rows of pyramid: "); 6 | int rows; 7 | scanf("%d", &rows); 8 | 9 | int cols = 2 * rows - 1; 10 | int middle = cols / 2; 11 | 12 | for (size_t i = 0; i < rows; i++) 13 | { 14 | int fill = 1; 15 | 16 | for (size_t j = 0; j < cols; j++) 17 | { 18 | if (j < middle - i || j > middle + i) 19 | { 20 | printf(" "); 21 | continue; 22 | } 23 | 24 | if (fill <= middle && j < middle) 25 | { 26 | printf("%d ", fill); 27 | ++fill; 28 | } 29 | else 30 | { 31 | printf("%d ", fill); 32 | --fill; 33 | } 34 | } 35 | printf("\n"); 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Sem. 03/ex7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 0; 6 | std::cin >> n; 7 | 8 | if (n % 2 == 0) 9 | ++n; 10 | 11 | for (int i = n; i > 0; i -= 2) 12 | { 13 | int whiteSpacesCount = (n - i) / 2; 14 | for (int j = 0; j < whiteSpacesCount; j++) 15 | { 16 | std::cout << ' '; 17 | } 18 | 19 | for (int j = 0; j < i; j++) 20 | { 21 | std::cout << '*'; 22 | } 23 | 24 | std::cout << std::endl; 25 | } 26 | 27 | for (size_t i = 3; i <= n; i += 2) 28 | { 29 | int whiteSpacesCount = (n - i) / 2; 30 | 31 | for (int j = 0; j < whiteSpacesCount; j++) 32 | { 33 | std::cout << ' '; 34 | } 35 | 36 | for (int j = 0; j < i; j++) 37 | { 38 | std::cout << '*'; 39 | } 40 | 41 | std::cout << std::endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task03.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a, b; 6 | scanf("%d %d", &a, &b); 7 | 8 | for (size_t i = a; i <= b; i++) 9 | { 10 | int keepI = i; 11 | int unique = 1; 12 | 13 | while (keepI != 0) 14 | { 15 | int quotient = keepI / 10; 16 | int remainder = keepI % 10; 17 | 18 | while (quotient != 0) 19 | { 20 | if (remainder == quotient % 10) 21 | { 22 | unique = 0; 23 | break; 24 | } 25 | quotient /= 10; 26 | } 27 | 28 | if (!unique) 29 | { 30 | break; 31 | } 32 | 33 | keepI /= 10; 34 | } 35 | 36 | if (unique) 37 | { 38 | printf("%d ", i); 39 | } 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int findSmallestMissing(int arr[], size_t n) { 5 | if (n == 0) { 6 | return -1; 7 | } 8 | 9 | size_t left = 0; 10 | size_t right = n - 1; 11 | int base = arr[0]; 12 | 13 | while (left <= right) { 14 | int mid = (left / 2) + (right / 2); // (left + right - left) / 2 15 | if (arr[mid] == base + mid) { 16 | // до mid всичко е наред, търсим вдясно 17 | left = mid + 1; 18 | } 19 | else { 20 | // намерихме нарушение -> търсим по-наляво дали има по-ранно нарушение 21 | right = mid - 1; 22 | } 23 | } 24 | // left е първият индекс с нарушение (или left == n ако няма нарушение) 25 | return base + left; 26 | } 27 | 28 | int main() { 29 | int arr[] = {5, 6, 7, 9, 10, 12, 13}; 30 | 31 | cout << findSmallestMissing(arr, 7); 32 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 7; 5 | constexpr int POSITION = 4; 6 | 7 | bool isValidPosition(size_t size, int position) { 8 | return (position >= 0 && position < size); 9 | } 10 | 11 | void printArray(const int arr[], size_t size) { 12 | for (int i = 0; i < size; i++) { 13 | cout << arr[i] << ", "; 14 | } 15 | cout << endl; 16 | } 17 | 18 | void removeFromArray(int arr[], size_t size, int position) { 19 | if (!isValidPosition(size, position)) { 20 | return; 21 | } 22 | 23 | for (size_t i = position; i < size - 1; i++) { 24 | int temp = arr[i]; 25 | arr[i] = arr[i + 1]; 26 | arr[i + 1] = arr[i]; 27 | } 28 | 29 | arr[size - 1] = -1; 30 | 31 | printArray(arr, size); 32 | } 33 | 34 | int main() { 35 | int arr[] = {0, 1, 2, 3, 4, 5, 6}; 36 | removeFromArray(arr, ARRAY_SIZE, POSITION); 37 | } -------------------------------------------------------------------------------- /Sem. 13/bubbleSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swapNums(int& a, int& b) 5 | { 6 | int temp = a; 7 | a = b; 8 | b = temp; 9 | } 10 | 11 | void bubbleSort(int* arr, size_t size) 12 | { 13 | unsigned lastSwapedIndex = size - 1; 14 | for (int i = 0; i < size - 1; i++) 15 | { 16 | int currentIterLastSwapped = 0; 17 | for (int j = 0; j < lastSwapedIndex; j++) 18 | { 19 | if (arr[j] > arr[j + 1]) 20 | { 21 | swapNums(arr[j], arr[j + 1]); 22 | currentIterLastSwapped = j; 23 | } 24 | } 25 | if (currentIterLastSwapped == 0) 26 | return; 27 | lastSwapedIndex = currentIterLastSwapped; 28 | } 29 | } 30 | 31 | void printArr(const int* arr, size_t size) 32 | { 33 | for (int i = 0; i < size; i++) 34 | cout << arr[i] << " "; 35 | cout << endl; 36 | } 37 | int main() 38 | { 39 | constexpr size_t SIZE = 5; 40 | int arr[SIZE] = { 5, 4, 3, 2, 1 }; 41 | 42 | bubbleSort(arr, SIZE); 43 | printArr(arr, SIZE); 44 | } 45 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY1_SIZE = 6; 5 | constexpr size_t ARRAY2_SIZE = 3; 6 | 7 | bool isFound(const int arr1[], const int arr2[], size_t size2, int i) { 8 | for (size_t j = 0; j < size2; j++) { 9 | if (arr1[i + j] != arr2[j]) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | 16 | bool isSubArray(const int arr1[], const int arr2[], size_t size1, size_t size2) { 17 | if (size2 > size1) { 18 | return false; 19 | } 20 | 21 | size_t size = size1 - size2; 22 | 23 | for (size_t i = 0; i <= size; i++) { 24 | if (isFound(arr1, arr2, size2, i)) { 25 | return true; 26 | } 27 | } 28 | return false; 29 | } 30 | 31 | int main() { 32 | int arr1[] = {1, 4, 4, 0, 4, 2}; 33 | int arr2[] = {4, 0, 4}; 34 | 35 | cout << isSubArray(arr1, arr2, ARRAY1_SIZE, ARRAY2_SIZE) << endl; 36 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | int fst = 0, snd = 0; 9 | 10 | fst = n % 10; 11 | n = n / 10; 12 | 13 | snd = n % 10; 14 | n = n / 10; 15 | 16 | snd = n % 10; 17 | n = n / 10; 18 | 19 | fst = n % 10; 20 | n = n / 10; 21 | 22 | // reverse fst and snd 23 | int fstReversed = fst % 10; 24 | fstReversed = fstReversed * 10; 25 | fst = fst / 10; 26 | fstReversed = fst % 10; 27 | 28 | int sndReversed = snd % 10; 29 | sndReversed = sndReversed * 10; 30 | snd = snd / 10; 31 | sndReversed = snd % 10; 32 | 33 | if (fstReversed == sndReversed) { 34 | cout << fstReversed << " = " << sndReversed; 35 | } else if (fstReversed < sndReversed) { 36 | cout << fstReversed << " < " << sndReversed; 37 | } else { 38 | cout << fstReversed << " > " << sndReversed; 39 | } 40 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getCountOfDigit(int number, int digit) { 5 | int count = 0; 6 | while (number) { 7 | int current = number % 10; 8 | if (digit == current) { 9 | count++; 10 | } 11 | number /= 10; 12 | } 13 | return count; 14 | } 15 | 16 | int addDigit(int number, int digit, int count) { 17 | while (count) { 18 | number *= 10; 19 | number += digit; 20 | count--; 21 | } 22 | return number; 23 | } 24 | 25 | int sortDigits(int number) { 26 | int result = 0; 27 | for (int i = 1; i <= 9; i++) { 28 | int currentCount = getCountOfDigit(number, i); 29 | result = addDigit(result, i, currentCount); 30 | } 31 | return result; 32 | } 33 | 34 | int main() { 35 | int input = 0; 36 | cin >> input; 37 | 38 | if (input < 0) { 39 | return 0; 40 | } 41 | 42 | cout << sortDigits(input); 43 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 5; 5 | 6 | int fromArrayToNumber(const int arr[]) { 7 | int number = 0; 8 | for (int i = 0; i < ARRAY_SIZE; i++) { 9 | number *= 10; 10 | number += arr[i]; 11 | } 12 | return number; 13 | } 14 | 15 | void fromNumberToArray(int arr[], int number) { 16 | size_t arrIter = ARRAY_SIZE - 1; 17 | while (number != 0) { 18 | arr[arrIter--] = number % 10; 19 | number /= 10; 20 | } 21 | } 22 | 23 | void printArr(const int arr[]) { 24 | for (int i = 0; i < ARRAY_SIZE; i++) { 25 | cout << arr[i] << ", "; 26 | } 27 | cout << endl; 28 | } 29 | 30 | void addOne(int arr[]) { 31 | int number = fromArrayToNumber(arr); 32 | number = number + 1; 33 | 34 | fromNumberToArray(arr, number); 35 | 36 | printArr(arr); 37 | } 38 | 39 | int main() { 40 | int arr[] = {0, 1, 0, 0, 9}; 41 | addOne(arr); 42 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_12.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 20; 5 | 6 | bool isValidInput(const int arr[], size_t size) { 7 | if (size != ARRAY_SIZE) { 8 | return false; 9 | } 10 | 11 | for (int i = 0; i < size; i++) { 12 | if (arr[i] < 0 || arr[i] > 20) { 13 | return false; 14 | } 15 | } 16 | 17 | return true; 18 | } 19 | 20 | int findCountOfOccurs(const int arr[], size_t size, int current) { 21 | int count = 0; 22 | for (int i = 0; i < size; i++) { 23 | if (arr[i] == current) { 24 | count++; 25 | } 26 | } 27 | return count; 28 | } 29 | 30 | int findMissing(const int arr[], size_t size) { 31 | for (int i = 0; i < size; i++) { 32 | int currentCount = findCountOfOccurs(arr, size, i); 33 | if (currentCount == 0) { 34 | return i; 35 | } 36 | } 37 | return -1; 38 | } 39 | 40 | int main() { 41 | 42 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int day = 0, month = 0; 6 | cin >> day >> month; 7 | 8 | if (day >= 1 && day <= 31) { 9 | if (month >= 1 && month <= 12) { 10 | day = day - 1; 11 | 12 | if (day <= 0) { 13 | month = month - 1; 14 | if (month == 1 || month == 3 || 15 | month == 5 || month == 7 || 16 | month == 8 || month == 10 || 17 | month == 12) { 18 | day = 31; 19 | } else { 20 | day = 30; 21 | } 22 | 23 | if (month <= 0) { 24 | month = 12; 25 | } 26 | } 27 | 28 | cout << day << " " << month; 29 | } else { 30 | cout << "Invalid month." << endl; 31 | } 32 | } else { 33 | cout << "Invalid day." << endl; 34 | } 35 | } -------------------------------------------------------------------------------- /C examples/Sem03/example5_prime-factors.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | unsigned n; 7 | scanf("%u", &n); 8 | 9 | for (size_t i = n; i > 1; i--) 10 | { 11 | // primality check 12 | bool isPrime = 1; 13 | double temp = sqrt(i); 14 | for (size_t divisorCandidate = 2; divisorCandidate <= temp; divisorCandidate++) 15 | { 16 | if (i % divisorCandidate == 0) 17 | { 18 | isPrime = 0; 19 | break; 20 | } 21 | } 22 | 23 | if (!isPrime) 24 | { 25 | continue; // we don't need this number 26 | } 27 | 28 | // check how many times i divides n 29 | unsigned count = 0; 30 | while (n % i == 0) 31 | { 32 | count++; 33 | n /= i; 34 | } 35 | if (count >= 1) 36 | { 37 | printf("%u^%u ", i, count); 38 | } 39 | } 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 9; 5 | 6 | bool hasDigit(int number, size_t digit) { 7 | while (number != 0) { 8 | if (number % 10 == digit) { 9 | return true; 10 | } 11 | number /= 10; 12 | } 13 | return false; 14 | } 15 | 16 | bool isValidSize(size_t size) { 17 | return size <= ARRAY_SIZE; 18 | } 19 | 20 | void printArr(const int arr[], size_t size) { 21 | for (int i = 0; i < size; i++) { 22 | cout << arr[i] << ", "; 23 | } 24 | cout << endl; 25 | } 26 | 27 | void refactorArray(int arr[], size_t size) { 28 | if (!isValidSize(size)) { 29 | return; 30 | } 31 | 32 | for (int i = 0; i < size; i++) { 33 | if (!hasDigit(arr[i], i)) { 34 | arr[i] = 0; 35 | } 36 | } 37 | 38 | printArr(arr, size); 39 | } 40 | 41 | int main() { 42 | int arr[] = {45, 62, 23, 47, 47, 65, 100}; 43 | refactorArray(arr, 7); 44 | } -------------------------------------------------------------------------------- /Sem. 10/search_in_text.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned myStrlen(const char* str) 5 | { 6 | if (!str) 7 | return 0; 8 | 9 | unsigned result = 0; 10 | while (*str) //is the same as (*str) != '\0' 11 | { 12 | result++; 13 | str++; 14 | } 15 | return result; 16 | } 17 | 18 | bool isPrefix(const char* pattern, const char* text) 19 | { 20 | while (*text != '\0' && *pattern != '\0') 21 | { 22 | if (*text != *pattern) 23 | return false; 24 | text++; 25 | pattern++; 26 | } 27 | return *pattern == '\0'; 28 | } 29 | 30 | bool searchInText(const char* text, const char* pattern) 31 | { 32 | size_t textLen = myStrlen(text); 33 | size_t patternLen = myStrlen(pattern); 34 | while(patternLen <= textLen) 35 | { 36 | if (isPrefix(pattern, text)) 37 | return true; 38 | text++; //подминаваме първия символ 39 | textLen--; 40 | } 41 | return false; 42 | } 43 | 44 | int main() 45 | { 46 | char str[] = "hello guys!"; 47 | char pattern[] = "guys"; 48 | 49 | cout << searchInText(str, pattern); 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Sem. 04/most_common_digit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | //задачата от предни семинар- решена с помощта на функции. 6 | int countDigitOccurences(int n, int digit) 7 | { 8 | if (digit > 9 || digit < 0) 9 | return -1; ///invalid input 10 | 11 | int count = 0; 12 | 13 | while (n != 0) 14 | { 15 | if (n % 10 == digit) 16 | count++; 17 | n /= 10; 18 | } 19 | return count; 20 | } 21 | 22 | int mostCommonDigit(int n) 23 | { 24 | int mostCommonDigit = -1; 25 | int mostCommonDigitOccurences = 0; 26 | 27 | for (int currentDigit = 0; currentDigit <= 9; currentDigit++) 28 | { 29 | int currentDigitOccurences = countDigitOccurences(n, currentDigit); 30 | 31 | if (currentDigitOccurences > mostCommonDigitOccurences) 32 | { 33 | mostCommonDigit = currentDigit; 34 | mostCommonDigitOccurences = currentDigitOccurences; 35 | } 36 | } 37 | 38 | return mostCommonDigit; 39 | } 40 | 41 | int main() 42 | { 43 | int n; 44 | cin >> n; 45 | cout << mostCommonDigit(n); 46 | } 47 | -------------------------------------------------------------------------------- /Sem. 05/sortNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned concatDigitAtBack(unsigned n, unsigned digit) 5 | { 6 | return n * 10 + digit; 7 | } 8 | 9 | unsigned countDigitOccurences(unsigned n, unsigned digit) 10 | { 11 | if (digit == 0 && n == 0) 12 | return 1; 13 | 14 | unsigned count = 0; 15 | while (n != 0) 16 | { 17 | int lastDigit = n % 10; 18 | if (lastDigit == digit) 19 | count++; 20 | n /= 10; 21 | } 22 | return count; 23 | } 24 | 25 | unsigned concatCountTimesDigit(unsigned a, unsigned digit, unsigned count) 26 | { 27 | unsigned result = a; 28 | for (int i = 0; i < count; i++) 29 | result = concatDigitAtBack(result, digit); 30 | return result; 31 | } 32 | 33 | unsigned sortNumber(unsigned n) 34 | { 35 | unsigned result = 0; 36 | for (int i = 9; i >= 0; i--) 37 | { 38 | unsigned count = countDigitOccurences(n, i); 39 | result = concatCountTimesDigit(result, i, count); 40 | } 41 | return result; 42 | } 43 | int main() 44 | { 45 | unsigned res = sortNumber(514332); 46 | 47 | cout << res; 48 | } 49 | -------------------------------------------------------------------------------- /Practicum/Week 07/MK.md: -------------------------------------------------------------------------------- 1 | # Малко контролно №2 2 | 3 | **Задача 1.** Какво ще се случи след изпълнението на следния код: 4 | 5 | ```c++ 6 | #include 7 | using namespace std; 8 | 9 | void calculate(int& a, int& b, int c) { 10 | a = a + b; 11 | c = c + b; 12 | b = a + c; 13 | a = b - a; 14 | } 15 | 16 | int main() { 17 | int x = 1, y = 2, z = 3; 18 | calculate(x, x, z); 19 | cout << x << " " << y << " " << z << endl; 20 | } 21 | ``` 22 | 23 | **Задача 2.** Какво ще се принтира след изпълнението на програмата: 24 | 25 | ```c++ 26 | #include 27 | using namespace std; 28 | 29 | void transformArr(const int arr[], int n) { 30 | for (int i = 0; i < n; i++) { 31 | arr[i] += arr[n - i - 1]; 32 | } 33 | } 34 | 35 | int main() { 36 | int arr[5] = { 1, 2, 3, 4, 5 }; 37 | transformArr(arr, 5); 38 | 39 | for (int i = 0; i < 5; i++) { 40 | cout << arr[i] << " "; 41 | } 42 | } 43 | ``` 44 | 45 | **Задача 3:** Да се напише програма, която извежда всички прости числа по-малки или равни на n с помощта на Решето на Ератостен. 46 | 47 | *n се въвежда от стандартния вход и е естествено число* -------------------------------------------------------------------------------- /Sem. 08/bitManipulations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | const int LIMIT = sizeof(unsigned) * 8; 6 | 7 | bool checkBit(unsigned int n, unsigned ind) 8 | { 9 | if (ind >= LIMIT) 10 | return false; 11 | 12 | unsigned int mask = 1; 13 | mask <<= ind; 14 | return (mask & n) == mask; 15 | } 16 | 17 | unsigned makeBitZero(unsigned int n, unsigned int ind) //clear bit 18 | { 19 | if (ind >= LIMIT) 20 | return false; 21 | 22 | unsigned int mask = 1; 23 | mask <<= ind; 24 | 25 | mask = ~mask; 26 | 27 | return n & mask; 28 | } 29 | 30 | unsigned makeBitOne(unsigned int n, unsigned int ind) //set bit 31 | { 32 | if (ind >= LIMIT) 33 | return false; 34 | 35 | unsigned int mask = 1; 36 | mask <<= ind; 37 | 38 | return n | mask; 39 | } 40 | 41 | 42 | unsigned toggleBitOne(unsigned int n, unsigned int ind) 43 | { 44 | if (ind >= LIMIT) 45 | return false; 46 | 47 | unsigned int mask = 1; 48 | mask <<= ind; 49 | 50 | return n ^ mask; 51 | } 52 | 53 | int main() 54 | { 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /Sem. 15/boolVectorsEqualOnesAndZeros.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool areZerosAndOnesEqual(const bool* arr, size_t size) 5 | { 6 | int zeros = 0, ones = 0; 7 | for (int i = 0; i < size; i++) 8 | { 9 | if (arr[i] == 0) 10 | zeros++; 11 | else 12 | ones++; 13 | } 14 | return zeros == ones; 15 | } 16 | void genBoolVectors(bool* arr, size_t size, bool(*filter)(const bool* arr, size_t size), unsigned beginIndex = 0) 17 | { 18 | if (beginIndex == size) 19 | { 20 | if (filter(arr, size)) 21 | { 22 | for (int i = 0; i < size; i++) 23 | cout << arr[i] << " "; 24 | cout << endl; 25 | 26 | } 27 | return; 28 | } 29 | arr[beginIndex] = 0; 30 | genBoolVectors(arr, size, filter, beginIndex + 1); 31 | arr[beginIndex] = 1; 32 | genBoolVectors(arr, size, filter, beginIndex + 1); 33 | } 34 | 35 | int main() 36 | { 37 | const size_t size = 4; 38 | bool arr[4]; 39 | 40 | genBoolVectors(arr, size, areZerosAndOnesEqual); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task13.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int leavesLevel; 6 | scanf("%d", &leavesLevel); 7 | 8 | int cols = 2 * leavesLevel - 1; 9 | int middle = cols / 2; 10 | int height = leavesLevel + (leavesLevel / 2); 11 | 12 | for (size_t i = 0; i < height; i++) 13 | { 14 | if (i < leavesLevel) 15 | { 16 | for (size_t j = 0; j < cols; j++) 17 | { 18 | if (j < middle - i || j > middle + i) 19 | { 20 | printf(" "); 21 | continue; 22 | } 23 | 24 | printf("* "); 25 | } 26 | } 27 | else 28 | { 29 | for (size_t j = 0; j < cols; j++) 30 | { 31 | if (j == middle) 32 | { 33 | printf("* "); 34 | } 35 | else 36 | { 37 | printf(" "); 38 | } 39 | } 40 | } 41 | printf("\n"); 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Sem. 14/readme.md: -------------------------------------------------------------------------------- 1 | # Рекурсия (част 1) 2 | 3 | **Задача 1:** Да се реализира рекурсивна функция, която пресмята n! 4 | 5 | **Задача 2:** Да се реализира рекурсивна функция, която пресмята n-тото число на фибоначи. 6 | 7 | **Задача 3:** Да се реализира рекурсивна функция, която вдига число на дадена степен. 8 | 9 | **Задача 4:** Да се реализира рекурсивна функция, която приема масив от цели числа и връща сумата на елементите в него. 10 | 11 | **Задача 5:** Да се реализира рекурсивна функция, която приема **произволен** масив и число и връща дали числото се съдържа в масива. (Линейно търсене). 12 | 13 | **Задача 6:** Да се реализира рекурсивна функция, която приема **сортиран** масив и число и връща дали числото се съдържа в масива. (Двоично търсене). 14 | 15 | **Задача 7:** Да се реализира рекурсивна функция, която приема стринг и връща дали стрингът е палиндром. 16 | 17 | **Задача 8:** Да се реализира рекурсивна функция, която приема стринг и връща броя на малките и големите символи в него. 18 | 19 | **Задача 9:** Да се реализира рекурсивна функция, която приема стринг и обръща малките бувки в главни и главните в малки. 20 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool validateIndexes(int i, int j, int len) { 5 | if (i < 0 || j < 0 || i > j 6 | || j > len) { 7 | return false; 8 | } 9 | return true; 10 | } 11 | 12 | int reverseNumber(int n) { 13 | int result = 0; 14 | while (n) { 15 | result *= 10; 16 | result += (n % 10); 17 | n /= 10; 18 | } 19 | return result; 20 | } 21 | 22 | int findNumLen(int n) { 23 | int len = 0; 24 | while (n) { 25 | len++; 26 | n /= 10; 27 | } 28 | return len; 29 | } 30 | 31 | int powOfTen(int pow) { 32 | int result = 1; 33 | for (int i = 0; i < pow; i++) { 34 | result *= 10; 35 | } 36 | return result; 37 | } 38 | 39 | void extract(int& n, int i, int j) { 40 | int len = findNumLen(n); 41 | if (!validateIndexes(i, j, len)) { 42 | return; 43 | } 44 | 45 | int rightCut = n / powOfTen(len - j); 46 | n = rightCut % powOfTen(j - i + 1); 47 | } 48 | 49 | int main() { 50 | int n = 1234567; 51 | extract(n, 2, 5); 52 | cout << n; 53 | } 54 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Drawings.txt: -------------------------------------------------------------------------------- 1 | Задача 7: 2 | Вход:3 3 | 1 2 3 4 | 6 5 4 5 | 7 8 9 6 | 7 | Вход:4 8 | 1 2 3 4 9 | 8 7 6 5 10 | 9 10 11 12 11 | 16 15 14 13 12 | 13 | Вход:2 14 | 1 2 15 | 4 3 16 | 17 | 18 | Задача 8: 19 | Вход:3 20 | 1 21 | 21 22 | 321 23 | 21 24 | 1 25 | 26 | Вход:4 27 | 1 28 | 21 29 | 321 30 | 4321 31 | 321 32 | 21 33 | 1 34 | 35 | 36 | Задача 10: 37 | Вход:4 38 | . . . . . . . . 39 | . . . . . . 40 | . . . . . . 41 | . . . . . . . . 42 | 43 | Вход:3 44 | . . . . . . 45 | . . . . . 46 | . . . . . . 47 | 48 | Вход:5 49 | . . . . . . . . . . 50 | . . . . 51 | . . . . . . . 52 | . . . . 53 | . . . . . . . . . . 54 | 55 | 56 | Задача 11: 57 | Вход:3 58 | 1 1 59 | 1 2 2 1 60 | 1 2 3 --- 3 2 1 61 | 1 2 2 1 62 | 1 1 63 | 64 | Вход:4 65 | 1 1 66 | 1 2 2 1 67 | 1 2 3 3 2 1 68 | 1 2 3 4 ---- 4 3 2 1 69 | 1 2 3 3 2 1 70 | 1 2 2 1 71 | 1 1 72 | 73 | Вход:2 74 | 1 1 75 | 1 2 –- 2 1 76 | 1 1 77 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_03.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | constexpr size_t SIZE = 20; 6 | 7 | void getPositiveAndNegativeCount(const int arr[], size_t size, int& positiveCount, int& negativeCount) { 8 | positiveCount = negativeCount = 0; 9 | for (size_t i = 0; i < size; i++) 10 | { 11 | if (arr[i] > 0) { 12 | positiveCount++; 13 | } 14 | else if (arr[i] < 0) { 15 | negativeCount++; 16 | } 17 | else 18 | continue; 19 | } 20 | } 21 | int main() { 22 | 23 | int arr[SIZE] = { 1 , -1, -4, -3 ,-5 ,6 ,3 ,-4 ,7 , 9}; 24 | int pos, neg; 25 | getPositiveAndNegativeCount(arr, SIZE, pos, neg); 26 | 27 | cout << "positive: " << pos << endl; 28 | cout << "negative: " << neg << endl; 29 | if (pos > neg) { 30 | cout << "The number of positive integers is greater than the number of negative integers"; 31 | } 32 | else if (pos < neg) { 33 | cout << "The number of positive integers is less than the number of negative integers"; 34 | } 35 | else { 36 | cout << "The number of negative integers is equal to the number of possitive integers"; 37 | } 38 | 39 | } 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int reverseNumber(int number) { 5 | int result = 0; 6 | while (number) { 7 | result *= 10; 8 | result += number % 10; 9 | number /= 10; 10 | } 11 | 12 | return result; 13 | } 14 | 15 | long concat(int first, int second) { 16 | long result = 0; 17 | 18 | int firstReversed = reverseNumber(first); 19 | int secondReversed = reverseNumber(second); 20 | 21 | while (firstReversed) { 22 | result *= 10; 23 | result += firstReversed % 10; 24 | firstReversed /= 10; 25 | } 26 | 27 | while (secondReversed) { 28 | result *= 10; 29 | result += secondReversed % 10; 30 | secondReversed /= 10; 31 | } 32 | 33 | return result; 34 | } 35 | 36 | bool validation(int first, int second) { 37 | if (first <= 0 || second <= 0) { 38 | return false; 39 | } 40 | 41 | return true; 42 | } 43 | 44 | int main() { 45 | int first = 0, second = 0; 46 | cin >> first >> second; 47 | 48 | if (!validation(first, second)) { 49 | return 0; 50 | } 51 | 52 | cout << concat(first, second); 53 | } -------------------------------------------------------------------------------- /Sem. 15/minOperations_memoization.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | unsigned min(unsigned a, unsigned b) 8 | { 9 | return a < b ? a : b; 10 | } 11 | unsigned min(unsigned a, unsigned b, unsigned c) 12 | { 13 | return min(min(a,b),c); 14 | } 15 | 16 | unsigned getMinOperations_rec(unsigned n, unsigned* cache) 17 | { 18 | if(n == 1) 19 | return 0; 20 | 21 | if(cache[n] != 0) 22 | { 23 | return cache[n]; 24 | } 25 | 26 | unsigned div2 = INT_MAX, div3 = INT_MAX, min1 = INT_MAX; 27 | 28 | if(n % 3 == 0) 29 | { 30 | div3 = getMinOperations_rec(n / 3, cache); 31 | } 32 | if(n % 2 == 0) 33 | { 34 | div2 = getMinOperations_rec(n / 2, cache); 35 | } 36 | min1 = getMinOperations_rec(n - 1, cache); 37 | 38 | unsigned res = min(div2, div3, min1) + 1; 39 | cache[n] = res; 40 | return res; 41 | 42 | } 43 | 44 | unsigned getMinOperations(unsigned n) 45 | { 46 | unsigned* cache = new unsigned[n + 1]{0}; 47 | unsigned res = getMinOperations_rec(n, cache); 48 | delete[] cache; 49 | return res; 50 | } 51 | int main() 52 | { 53 | cout << getMinOperations(200); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | for (int i = 1; i <= n; i++) 9 | { 10 | int p = 1; 11 | for (int j = 1; j <= i; j++) 12 | printf("%d ", p++); 13 | 14 | for (int j = i; j <= n; j++) 15 | printf(" "); 16 | 17 | for (int j = 1; j <= n; j++) 18 | { 19 | if (i == n) 20 | printf("- "); 21 | else 22 | printf(" "); 23 | } 24 | 25 | for (int j = i; j <= n; j++) 26 | printf(" "); 27 | 28 | for (int j = 1; j <= i; j++) 29 | printf("%d ", --p); 30 | 31 | printf("\n"); 32 | } 33 | 34 | for (int i = 1; i < n; i++) 35 | { 36 | int p = 1; 37 | for (int j = i; j < n; j++) 38 | printf("%d ", p++); 39 | 40 | for (int j = 1; j <= i; j++) 41 | printf(" "); 42 | 43 | for (int j = 1; j <= n + 2; j++) 44 | printf(" "); 45 | 46 | for (int j = 1; j <= i; j++) 47 | printf(" "); 48 | 49 | for (int j = i; j < n; j++) 50 | printf("%d ", --p); 51 | 52 | printf("\n"); 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int numLen(int n) { 5 | int len = 0; 6 | while (n) { 7 | len++; 8 | n /= 10; 9 | } 10 | return len; 11 | } 12 | 13 | bool isValidK(int k, int len) { 14 | if (k < 0 || k > len) { 15 | return false; 16 | } 17 | return true; 18 | } 19 | 20 | int powOfTen(int pow) { 21 | int result = 1; 22 | for (int i = 0; i < pow; i++) { 23 | result *= 10; 24 | } 25 | return result; 26 | } 27 | 28 | void reverseNumber(int& n) { 29 | int copyOfN = n; 30 | n = 0; 31 | while (copyOfN) { 32 | n *= 10; 33 | n += (copyOfN % 10); 34 | copyOfN /= 10; 35 | } 36 | } 37 | 38 | void transformNumber(int& a, int& b, int k) { 39 | int lenOfA = numLen(a); 40 | if (!isValidK(k, lenOfA)) { 41 | return; 42 | } 43 | 44 | int toExtract = powOfTen(k); 45 | int toAdd = a % toExtract; 46 | a /= powOfTen(k); 47 | 48 | reverseNumber(b); 49 | 50 | while(k) { 51 | b *= 10; 52 | b += toAdd % 10; 53 | toAdd /= 10; 54 | k--; 55 | } 56 | 57 | reverseNumber(b); 58 | } 59 | 60 | int main() { 61 | int a = 1233; 62 | int b = 1; 63 | 64 | transformNumber(a, b, 2); 65 | cout << a << " " << b; 66 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n = 0; 6 | cin >> n; 7 | 8 | int copyOfN = n; 9 | int nSize = 0; 10 | 11 | while (copyOfN > 0) { 12 | nSize++; 13 | copyOfN /= 10; 14 | } 15 | 16 | if (nSize % 2 == 0) { 17 | int result = 0; 18 | int resultSize = nSize - 2; 19 | int half = resultSize / 2; 20 | 21 | while (half != 0) { 22 | result *= 10; 23 | result += n % 10; 24 | n /= 10; 25 | half--; 26 | } 27 | 28 | n /= 100; 29 | half = resultSize / 2; 30 | 31 | while (half != 0) { 32 | result *= 10; 33 | result += n % 10; 34 | n /= 10; 35 | half--; 36 | } 37 | 38 | int reversed = 0; 39 | while (result != 0) { 40 | reversed *= 10; 41 | reversed += result % 10; 42 | result /= 10; 43 | } 44 | 45 | cout << reversed; 46 | } 47 | else { 48 | int result = 0; 49 | int resultSize = nSize - 1; 50 | int toRemove = resultSize / 2; 51 | 52 | for (int i = 0; i < nSize; i++) { 53 | if (i == toRemove) { 54 | n /= 10; 55 | continue; 56 | } 57 | result *= 10; 58 | result += n % 10; 59 | n /= 10; 60 | } 61 | 62 | int reversed = 0; 63 | while (result != 0) { 64 | reversed *= 10; 65 | reversed += result % 10; 66 | result /= 10; 67 | } 68 | 69 | cout << reversed; 70 | } 71 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_04.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | 6 | int getLength(int number) { 7 | int res = 0; 8 | if (number == 0){ 9 | return 1; 10 | } 11 | while (number != 0) { 12 | res++; 13 | number /= 10; 14 | } 15 | return res; 16 | } 17 | int reverseNumber(int number) { 18 | int result = 0; 19 | while (number != 0) { 20 | 21 | result *= 10; 22 | result += number % 10; 23 | number /= 10; 24 | } 25 | return result; 26 | } 27 | int getDigitOnPosition(int number, int k) { 28 | int num = reverseNumber(number); 29 | while (k > 1) { 30 | num /= 10; 31 | k--; 32 | } 33 | return num % 10; 34 | } 35 | int getPower(int number,int k) { 36 | int res = 1; 37 | int power = getLength(number) - k; 38 | while (power != 0) { 39 | res *= 10; 40 | power--; 41 | } 42 | return res; 43 | } 44 | void swapKthDigit(int& n, int& m, int k) { 45 | if (k > getLength(n) || k > getLength(m)) 46 | return; 47 | int kthDigitN = getDigitOnPosition(n, k); 48 | int kthDigitM = getDigitOnPosition(m, k); 49 | 50 | n = n - kthDigitN * getPower(n, k) + kthDigitM * getPower(n, k); 51 | m = m - kthDigitM * getPower(m, k) + kthDigitN * getPower(m, k); 52 | 53 | } 54 | int main() { 55 | 56 | int m, n, k; 57 | cin >> n >> m >> k; 58 | swapKthDigit(n, m, k); 59 | cout << n << " " << m << endl; 60 | } 61 | 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Код от семинарите по "Увод в програмирането" - спец. Информационни системи/Софтуерно инженерство/Компютърни науки 2 | 3 | - **Тема 1:** Увод. Примитивни типове данни. Преобразуване между типове. Оператори. 4 | - **Тема 2:** Връщане на резултат от оператор(lvalue/rvalue). Условни конструкции - if, switch и тернарен оператор. 5 | - **Тема 3:** Цикли – while, do-while и for. Оператори break и continue. Scope на променливи. 6 | - **Тема 4:** Функции. Оператор return. Абстракция. 7 | - **Тема 5:** Функции. Референции. Стекова рамка. Комуникация между функции. Пример с малък проект. 8 | - **Тема 6:** Масиви. 9 | - **Тема 7:** Бройни системи. Преобразуване между бройни системи. Представяния на отрицателни числа. Two's complement 10 | - **Тема 8:** Побитови операции. 11 | - **Тема 9:** Многомерни масиви. Пример с малък проект 2. 12 | - **Тема 10:** Въведение в указателите. Символни низове(стрингове). Основни операции с низове. 13 | - **Тема 11:** Предговор за указатели и референции. Заделяне на динамична памет. Оператори new и delete. 14 | - **Тема 12:** Работа с динамична памет (част 2). Работа с динамични масиви от динамични масиви. 15 | - **Тема 13:** Сложност и алгоритми. Анализ на Алгоритми. Алгоритми за сортиране. 16 | - **Тема 14:** Рекурсия (част 1). 17 | - **Тема 15:** Рекурсия (част 2). Опашкова рекурсия. Мемоизация. Търсене с връщане (backtracking). Функции от по-висок ред. 18 | 19 | 20 | -------------------------------------------------------------------------------- /Sem. 11/smallAndUpperStrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | bool isLower(char ch) 6 | { 7 | return ch >= 'a' && ch <= 'z'; 8 | } 9 | bool isUpper(char ch) 10 | { 11 | return ch >= 'A' && ch <= 'Z'; 12 | } 13 | void getLowerAndUpperCount(const char* str, unsigned& lowerCount, unsigned& upperCount) 14 | { 15 | upperCount = lowerCount = 0; 16 | if (!str) 17 | return; 18 | 19 | while (*str) 20 | { 21 | if (isLower(*str)) 22 | lowerCount++; 23 | else if (isUpper(*str)) 24 | upperCount++; 25 | str++; 26 | } 27 | } 28 | void getLowerAndUpperStrings(const char* str, char*& lower, char*& upper) 29 | { 30 | if (!str) 31 | return; 32 | unsigned lowerCount = 0, upperCount = 0; 33 | getLowerAndUpperCount(str, lowerCount, upperCount); 34 | lower = new char[lowerCount + 1]; 35 | upper = new char[upperCount + 1]; 36 | lower[lowerCount] = upper[upperCount] = '\0'; 37 | 38 | unsigned lowerIndex = 0, upperIndex = 0; 39 | while (*str) 40 | { 41 | if (isLower(*str)) 42 | lower[lowerIndex++] = *str; 43 | else if (isUpper(*str)) 44 | upper[upperIndex++] = *str; 45 | str++; 46 | } 47 | } 48 | 49 | 50 | int main() 51 | { 52 | char* lower = nullptr; 53 | char* upper = nullptr; 54 | getLowerAndUpperStrings("AAqew12423BBq", lower, upper); 55 | 56 | cout << lower << endl << upper << endl; 57 | delete[] lower; 58 | delete[] upper; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | // Upper part of the hourglass 9 | int numOfStars = 2 * n + 1; 10 | 11 | for (size_t i = 0; i < n; i++) 12 | { 13 | for (size_t j = 0; j < i; j++) 14 | { 15 | printf(" "); 16 | } 17 | for (size_t j = 0; j < numOfStars; j++) 18 | { 19 | printf("*"); 20 | } 21 | for (size_t j = 0; j < i; j++) 22 | { 23 | printf(" "); 24 | } 25 | 26 | numOfStars -= 2; 27 | printf("\n"); 28 | } // numOfStars = 1; 29 | 30 | for (int i = 0; i < n; i++) 31 | { 32 | printf(" "); 33 | } 34 | 35 | printf("*"); // middle star 36 | 37 | for (int i = 0; i < n; i++) 38 | { 39 | printf(" "); 40 | } 41 | printf("\n"); 42 | 43 | // Lower part of the hourglass 44 | for (size_t i = 0; i < n; i++) 45 | { 46 | numOfStars += 2; 47 | 48 | for (size_t j = 0; j < n - i - 1; j++) 49 | { 50 | printf(" "); 51 | } 52 | for (size_t j = 0; j < numOfStars; j++) 53 | { 54 | printf("*"); 55 | } 56 | for (size_t j = 0; j < n - i - 1; j++) 57 | { 58 | printf(" "); 59 | } 60 | 61 | printf("\n"); 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | constexpr int arr_size = 32; 4 | 5 | 6 | void printArray(int arr[], size_t size) { 7 | for (size_t i = 0; i < size; i++) 8 | { 9 | cout << arr[i] << ", "; 10 | if (i + 1 < size) { 11 | cout << ", "; 12 | } 13 | } 14 | } 15 | void swapArray(int arr[], size_t size) { 16 | for (int i = 0; i < size / 2; i++) { 17 | int temp = arr[i]; 18 | arr[i] = arr[size - 1 - i]; 19 | arr[size - 1 - i] = temp; 20 | } 21 | } 22 | void convertToBinary(int n, int binary[], size_t& size) { 23 | size = 0; 24 | if (n == 0) { 25 | binary[0] = 0; 26 | size = 1; 27 | return; 28 | } 29 | while (n > 0) { 30 | binary[size] = n % 2; 31 | n /= 2; 32 | size++; 33 | } 34 | swapArray(binary, size); 35 | } 36 | bool isPalindrome(int n, int arr [], size_t& size) { 37 | convertToBinary(n ,arr, size); 38 | for (size_t i = 0; i < size; i++) 39 | { 40 | if (arr[i] != arr[size - i - 1]) { 41 | return false; 42 | } 43 | } 44 | return true; 45 | } 46 | 47 | 48 | int main() { 49 | int n = 9; 50 | int binary[arr_size]; 51 | size_t size; 52 | 53 | cout << isPalindrome(n, binary, size) << endl; 54 | printArray(binary, size); 55 | 56 | } 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Sem. 10/basic_string_functions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | unsigned myStrlen(const char* str) 4 | { 5 | if (!str) 6 | return 0; 7 | 8 | unsigned result = 0; 9 | while (*str) //is the same as (*str) != '\0' 10 | { 11 | result++; 12 | str++; 13 | } 14 | return result; 15 | } 16 | 17 | //copy the source to destination. 18 | //We assume that in dest the are enough cells. 19 | void myStrcpy(const char* source, char* dest) 20 | { 21 | if (!source || !dest) 22 | return; 23 | while (*source) 24 | { 25 | *dest = *source; 26 | dest++; 27 | source++; 28 | } 29 | *dest = '\0'; 30 | } 31 | 32 | // - first < second 33 | // 0 first == second 34 | // + first > second 35 | int myStrcmp(const char* first, const char* second) 36 | { 37 | if (!first || !second) 38 | return 0; //some error value 39 | 40 | //we skip the common prefix, but not the terminating zero! 41 | while ((*first) && (*second) && ((*first) == (*second))) //acutally the (*second) check can be missed here. 42 | { 43 | first++; 44 | second++; 45 | } 46 | 47 | return (*first - *second); 48 | 49 | } 50 | 51 | 52 | void myStrcat(char* first, const char* second) 53 | { 54 | if (!first || !second) 55 | return; 56 | 57 | size_t firstLen = myStrlen(first); 58 | first += firstLen; 59 | myStrcpy(second, first); 60 | } 61 | int main() 62 | { 63 | char str[20] = "ABC"; 64 | char str2[] = "XY"; 65 | myStrCat(str, str2); 66 | 67 | cout << str; 68 | } 69 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned int getNumberLen(int number) { 5 | unsigned int length = 0; 6 | 7 | while (number) { 8 | length++; 9 | number /= 10; 10 | } 11 | 12 | return length; 13 | } 14 | 15 | int reverseNumber(int number) { 16 | int reversed = 0; 17 | while (number) { 18 | reversed *= 10; 19 | unsigned int currentDigit = number % 10; 20 | reversed += currentDigit; 21 | number /= 10; 22 | } 23 | 24 | return reversed; 25 | } 26 | 27 | unsigned int getDigitOnPosition(int number, unsigned int position) { 28 | unsigned int result = 0; 29 | unsigned int counter = 0; 30 | 31 | int reversed = reverseNumber(number); 32 | 33 | while (reversed) { 34 | if (counter == position) { 35 | return result; 36 | } 37 | counter++; 38 | result = reversed % 10; 39 | reversed /= 10; 40 | } 41 | 42 | return result; 43 | } 44 | 45 | bool validation(int number, unsigned int position) { 46 | unsigned int numberLength = getNumberLen(number); 47 | 48 | if (position > numberLength) { 49 | return false; 50 | } 51 | 52 | return true; 53 | } 54 | 55 | int main() { 56 | int input = 0; 57 | unsigned int position = 0; 58 | cin >> input >> position; 59 | 60 | if (!validation(input, position)) { 61 | return 0; 62 | } 63 | 64 | cout << getDigitOnPosition(input, position) << endl; 65 | } -------------------------------------------------------------------------------- /Sem. 11/sortLower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isLower(char ch) 5 | { 6 | return ch >= 'a' && ch <= 'z'; 7 | } 8 | 9 | unsigned getLowerCount(const char* str) 10 | { 11 | if(!str) 12 | return 0; 13 | 14 | unsigned count = 0; 15 | while(*str) 16 | { 17 | if(isLower(*str)) 18 | count++; 19 | 20 | str++; 21 | } 22 | return count; 23 | } 24 | 25 | void concatLettersAtBack(char* str, unsigned firstIndex, char ch, unsigned count) 26 | { 27 | for(int i = 0; i < count; i++) 28 | str[firstIndex + i] = ch; 29 | } 30 | 31 | char* sortLower(const char* str) 32 | { 33 | unsigned lower = getLowerCount(str); 34 | char* result = new char[lower + 1]; 35 | 36 | constexpr unsigned alphabet_size = 26; 37 | constexpr char first_letter = 'a'; 38 | 39 | result[lower] = '\0'; 40 | 41 | unsigned resultIndex = 0; 42 | int countLower[alphabet_size]{ 0 }; 43 | while (*str) 44 | { 45 | if (isLower(*str)) 46 | countLower[*str - 'a']++; 47 | str++; 48 | } 49 | for (int i = 0; i < alphabet_size; i++) 50 | { 51 | concatLettersAtBack(result, resultIndex, first_letter + i, countLower[i]); 52 | resultIndex += countLower[i]; //maybe this should be in the function 53 | } 54 | 55 | return result; 56 | } 57 | 58 | int main() 59 | { 60 | char* str = sortLower("zAzAbbzazQc"); 61 | 62 | cout << str; 63 | delete[] str; 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | for (int i = 1; i <= n; i++) 9 | { 10 | for (int j = 1; j <= n; j++) 11 | { 12 | if (i == 1 || i == n || j == 1 || j == n) 13 | { 14 | printf("."); 15 | } 16 | else 17 | { 18 | printf(" "); 19 | } 20 | } 21 | 22 | if (n % 2 == 0) 23 | { 24 | for (int j = 1; j < n - 1; j++) 25 | { 26 | if (i == n / 2 || i == n / 2 + 1) 27 | { 28 | printf("."); 29 | } 30 | else 31 | { 32 | printf(" "); 33 | } 34 | } 35 | } 36 | else 37 | { 38 | for (int j = 1; j < n - 1; j++) 39 | { 40 | if (i == n / 2 + 1) 41 | { 42 | printf("."); 43 | } 44 | else 45 | { 46 | printf(" "); 47 | } 48 | } 49 | } 50 | 51 | for (int j = 1; j <= n; j++) 52 | { 53 | if (i == 1 || i == n || j == 1 || j == n) 54 | { 55 | printf("."); 56 | } 57 | else 58 | { 59 | printf(" "); 60 | } 61 | } 62 | 63 | printf("\n"); 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Sem. 09/linearEquationsSolver.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void printMatrix(double arr[][3], size_t rows, size_t colls) 5 | { 6 | for (int i = 0; i < rows; i++) 7 | { 8 | for (int j = 0; j < colls; j++) 9 | cout << arr[i][j] << " "; 10 | cout << endl; 11 | } 12 | } 13 | 14 | void divideBy(double arr[], size_t size, double by) 15 | { 16 | for (int i = 0; i < size; i++) 17 | arr[i] /= by; 18 | } 19 | void substract(double first[], double second[], double coef, size_t size) 20 | { 21 | for (int i = 0; i < size; i++) 22 | first[i] -= (second[i] * coef); 23 | } 24 | void makeAllZeros(double matrix[][3], size_t rows, size_t colls, int currentRow) 25 | { 26 | for (int i = 0; i < rows; i++) 27 | { 28 | if (i != currentRow) 29 | substract(matrix[i], matrix[currentRow], matrix[i][currentRow], colls); 30 | } 31 | } 32 | 33 | void gauss(double matrix[][3], size_t n, double result[]) 34 | { 35 | size_t rows = n; 36 | size_t colls = n + 1; 37 | 38 | for (int i = 0; i < rows; i++) 39 | { 40 | divideBy(matrix[i], colls, matrix[i][i]); 41 | makeAllZeros(matrix, rows, colls, i); 42 | } 43 | for (int i = 0; i < rows; i++) 44 | result[i] = matrix[i][colls - 1]; 45 | 46 | } 47 | 48 | int main() 49 | { 50 | constexpr size_t rows = 2; 51 | constexpr size_t colls = 3; 52 | 53 | double m[rows][colls] = { {2, 6, 40}, // 2x + 6y = 40 54 | {4, 1, 14} };// 4x + y = 14 55 | 56 | double result[rows]; 57 | gauss(m, rows, result); 58 | 59 | for (int i = 0; i < rows; i++) 60 | cout << result[i] << " "; 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_11_bonus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int day = 0, month = 0, year = 0; 6 | cin >> day >> month; 7 | 8 | bool isValidDay = (day >= 1 && day <= 31); 9 | bool isValidMonth = (month >= 1 && month <= 12); 10 | bool isValidYear = (year >= 1 && year <= 2025); 11 | 12 | if (isValidDay && isValidMonth && isValidYear) { 13 | bool isLeapYear = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); 14 | 15 | day--; 16 | if (day <= 0) { // invalid day 17 | month--; 18 | if (month <= 0) { // invalid month 19 | month = 12; 20 | year--; 21 | if (year <= 0) { // invalid year 22 | cout << "Invalid date!" << endl; 23 | } 24 | } else { // check count of days in month 25 | if (month == 1 || month == 3 || 26 | month == 5 || month == 7 || 27 | month == 8 || month == 10 || 28 | month == 12) { 29 | day = 31; 30 | } else { 31 | if (month == 2 && isLeapYear == true) { 32 | day = 29; 33 | } else if (month == 2 && isLeapYear == false) { 34 | day = 28; 35 | } else { 36 | day = 30; 37 | } 38 | } 39 | } 40 | } 41 | 42 | } else { 43 | cout << "Invalid input!" << endl; 44 | } 45 | } -------------------------------------------------------------------------------- /Sem. 11/sortArrayConcatOfTwoSortedArrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void merge(const int* arr1, size_t size1, 6 | const int* arr2, size_t size2, 7 | int* result) 8 | { 9 | unsigned arr1Index = 0, arr2Index = 0, resultIndex = 0; 10 | 11 | while (arr1Index < size1 && arr2Index < size2) 12 | { 13 | if (arr1[arr1Index] <= arr2[arr2Index]) 14 | result[resultIndex++] = arr1[arr1Index++]; 15 | else 16 | result[resultIndex++] = arr2[arr2Index++]; 17 | } 18 | while(arr1Index < size1) 19 | result[resultIndex++] = arr1[arr1Index++]; 20 | 21 | while (arr2Index < size2) 22 | result[resultIndex++] = arr2[arr2Index++]; 23 | } 24 | 25 | int getBound(const int* arr, size_t size) 26 | { 27 | if (!arr) 28 | return -1; 29 | for (int i = 0; i < size - 1; i++) 30 | { 31 | if (arr[i] > arr[i + 1]) 32 | return i + 1; 33 | } 34 | return -1; 35 | } 36 | 37 | void sortArrayConcatOfTwoSortedArrays(int* arr, size_t size) 38 | { 39 | if (!arr) 40 | return; 41 | 42 | int secondArrayStartIndex = getBound(arr, size); 43 | if (secondArrayStartIndex == -1) //the array is sorted 44 | return; 45 | 46 | int* temp = new int[size]; 47 | merge(arr, secondArrayStartIndex, 48 | arr + secondArrayStartIndex, size - secondArrayStartIndex, 49 | temp); 50 | 51 | for (int i = 0; i < size; i++) 52 | arr[i] = temp[i]; 53 | delete[] temp; 54 | } 55 | 56 | int main() 57 | { 58 | constexpr size_t size = 6; 59 | int arr[size] = {1,5,9, 12, 4, 7}; 60 | 61 | sortArrayConcatOfTwoSortedArrays(arr, size); 62 | 63 | for(int i = 0; i < size; i++) 64 | cout << arr[i] << " "; 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Sem. 11/censoreNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned myStrlen(const char* str) 5 | { 6 | if (!str) 7 | return 0; 8 | 9 | unsigned result = 0; 10 | while (*str) 11 | { 12 | result++; 13 | str++; 14 | } 15 | return result; 16 | } 17 | 18 | bool isDigit(char ch) 19 | { 20 | return ch >= '0' && ch <= '9'; 21 | } 22 | unsigned getDigitsCount(const char* str) 23 | { 24 | if (!str) 25 | return 0; 26 | unsigned count = 0; 27 | 28 | while (*str) 29 | { 30 | if (isDigit(*str)) 31 | count++; 32 | str++; 33 | } 34 | return count; 35 | } 36 | 37 | unsigned getNumbersCount(const char* str) 38 | { 39 | if (!str) 40 | return 0; 41 | unsigned count = 0; 42 | while (*str) 43 | { 44 | if (isDigit(*str) && !isDigit(*(str + 1))) //we count the end of the numbers. Str + 1 is allowed, because we have a terminating zero (which is not a digit) at the end (we won't go out of bounds) 45 | count++; 46 | str++; 47 | } 48 | return count; 49 | } 50 | 51 | char* censoreNumbers(const char* str) 52 | { 53 | if (!str) 54 | return nullptr; 55 | 56 | size_t resultSize = myStrlen(str) 57 | - getDigitsCount(str) 58 | + getNumbersCount(str); 59 | 60 | char* result = new char[resultSize + 1]; //!!!!! '\0' 61 | unsigned resultIndex = 0; 62 | 63 | while (*str) 64 | { 65 | if (!isDigit(*str)) 66 | result[resultIndex++] = *str; 67 | else if(!isDigit(*(str + 1))) 68 | result[resultIndex++] = '*'; 69 | str++; 70 | } 71 | 72 | result[resultSize] = '\0'; 73 | return result; 74 | } 75 | int main () 76 | { 77 | char* str = censoreNumbers("abc1234dgg4fsdg5"); 78 | cout << str; 79 | 80 | delete[] str;//!!!!! 81 | } 82 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY_SIZE = 7; 5 | 6 | int findCountOfEven(const int arr[], size_t size) { 7 | int even = 0; 8 | for (size_t i = 0; i < size; i++) { 9 | if (arr[i] % 2 == 0) { 10 | even++; 11 | } 12 | } 13 | return even; 14 | } 15 | 16 | size_t findFirstOddOnTheLeft(const int arr[], size_t size, size_t left) { 17 | while (left < size && arr[left] % 2 == 0) { 18 | left++; 19 | } 20 | return left; 21 | } 22 | 23 | size_t findFirstEvenOnTheRight(const int arr[], size_t size, size_t right) { 24 | while (right > 0 && arr[right] % 2 != 0) { 25 | right--; 26 | } 27 | return right; 28 | } 29 | 30 | void printArray(const int arr[], size_t size) { 31 | for (size_t i = 0; i < size; i++) { 32 | cout << arr[i]; 33 | if (i != size - 1) { 34 | cout << ", "; 35 | } 36 | } 37 | cout << endl; 38 | } 39 | 40 | void refactorArray(int arr[], size_t size) { 41 | size_t left = 0; // evens index 42 | size_t right = size - 1; // odds index 43 | 44 | while (left < right) { 45 | 46 | left = findFirstOddOnTheLeft(arr, size, left); 47 | right = findFirstEvenOnTheRight(arr, size, right); 48 | 49 | if (left < right) { 50 | // swap 51 | int temp = arr[left]; 52 | arr[left] = arr[right]; 53 | arr[right] = arr[left]; 54 | 55 | left++; 56 | right--; 57 | } 58 | } 59 | printArray(arr, size); 60 | } 61 | 62 | int main() { 63 | int arr[] = {1, 4, 3, 5, 6, -8, 10}; 64 | 65 | refactorArray(arr, ARRAY_SIZE); 66 | } 67 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t ARRAY1_SIZE = 5; 5 | constexpr size_t ARRAY2_SIZE = 4; 6 | 7 | bool existsInArray(const int arr[], size_t size, int value) { 8 | for (size_t i = 0; i < size; i++) { 9 | 10 | if (arr[i] == value) { 11 | return true; 12 | } 13 | } 14 | 15 | return false; 16 | } 17 | 18 | void printUnion(const int arr1[], const int arr2[], size_t size1, size_t size2) { 19 | for (size_t i = 0; i < size1; i++) { 20 | cout << arr1[i]; 21 | 22 | if (i != size1 - 1) { 23 | cout << ", "; 24 | } 25 | } 26 | 27 | bool firstPrinted = (size1 > 0); 28 | 29 | for (size_t i = 0; i < size2; i++) { 30 | 31 | if (!existsInArray(arr1, size1, arr2[i])) { 32 | 33 | if (firstPrinted) { 34 | cout << ", "; 35 | } 36 | 37 | cout << arr2[i]; 38 | firstPrinted = true; 39 | } 40 | } 41 | cout << endl; 42 | } 43 | 44 | void printIntersection(const int arr1[], const int arr2[], size_t size1, size_t size2) { 45 | bool firstPrinted = false; 46 | 47 | for (size_t i = 0; i < size1; i++) { 48 | 49 | if (existsInArray(arr2, size2, arr1[i])) { 50 | 51 | if (firstPrinted) { 52 | cout << ", "; 53 | } 54 | 55 | cout << arr1[i]; 56 | firstPrinted = true; 57 | } 58 | } 59 | cout << endl; 60 | } 61 | 62 | int main() { 63 | int arr1[] = {2, 4, 1, 7, 8}; 64 | int arr2[] = {1, 2, 3, 5}; 65 | 66 | printIntersection(arr1, arr2, ARRAY1_SIZE, ARRAY2_SIZE); 67 | printUnion(arr1, arr2, ARRAY1_SIZE, ARRAY2_SIZE); 68 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/task_15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | char a = ' ', b = ' ', c = ' ', d = ' ', e = ' '; 6 | cin >> a >> b >> c >> d >> e; 7 | 8 | char e_small = e; 9 | char e_capital = e; 10 | 11 | if (e >= 'A' && e <= 'Z') { 12 | e_small = e + ('a' - 'A'); 13 | } else if (e >= 'a' && e <= 'z') { 14 | e_capital = e - ('a' - 'A'); 15 | } 16 | 17 | if ((e >= 'A' && e <= 'Z') || (e >= 'a' && e <= 'z')) { 18 | if (a >= 'A' && a <= 'Z' && b >= 'A' && b <= 'Z' && a < b) { 19 | if (c >= 'a' && c <= 'z' && d >= 'a' && d <= 'z' && c < d) { 20 | bool isInA = false, isInB = false; 21 | 22 | if (e_capital >= a && e_capital <= b) { 23 | isInA = true; 24 | } 25 | if (e_small >= c && e_small <= d) { 26 | isInB = true; 27 | } 28 | 29 | bool isInUnion = isInA || isInB; 30 | bool isInIntersection = isInA && isInB; 31 | bool isInDifference = isInA && !isInB; 32 | bool isInOnlyOne = isInUnion && !isInIntersection; 33 | 34 | cout << (isInUnion ? "true" : "false") << ", "; 35 | cout << (isInIntersection ? "true" : "false") << ", "; 36 | cout << (isInDifference ? "true" : "false") << ", "; 37 | cout << (isInOnlyOne ? "true" : "false") << ", "; 38 | 39 | } else { 40 | cout << "Invalid input!" << endl; 41 | } 42 | } else { 43 | cout << "Invalid input!" << endl; 44 | } 45 | } else { 46 | cout << "Invalid input!" << endl; 47 | } 48 | } -------------------------------------------------------------------------------- /C examples/Exam Preparation/Zad1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int** initialize(int rows, int cols) 5 | { 6 | int** boolVectors = (int**)malloc(rows * sizeof(int*)); 7 | 8 | for (int i = 0; i < rows; i++) 9 | { 10 | boolVectors[i] = (int*)calloc(cols, sizeof(int)); 11 | } 12 | 13 | return boolVectors; 14 | } 15 | 16 | void copyBoolVectorIntoCurrentRow(int* currentRow, int* boolVector, int size) 17 | { 18 | for (int i = 0; i < size; i++) 19 | { 20 | currentRow[i] = boolVector[i]; 21 | } 22 | } 23 | 24 | void generateCurrentBoolVector(int* currentRow, int* boolVector, int size) 25 | { 26 | for (int i = size - 1; i >= 0; i--) 27 | { 28 | if (boolVector[i] == 0) 29 | { 30 | boolVector[i] = 1; 31 | break; 32 | } 33 | 34 | boolVector[i] = 0; 35 | } 36 | 37 | copyBoolVectorIntoCurrentRow(currentRow, boolVector, size); 38 | } 39 | 40 | void generateBoolVectors(int** matrix, int rows, int cols) 41 | { 42 | int* boolVector = matrix[rows - 1]; 43 | 44 | for (int i = 1; i < rows; i++) 45 | { 46 | generateCurrentBoolVector(matrix[i], boolVector, cols); 47 | } 48 | } 49 | 50 | void print(int** matrix, int rows, int cols) 51 | { 52 | for (int i = 0; i < rows; i++) 53 | { 54 | for (int j = 0; j < cols; j++) 55 | { 56 | printf("%d ", matrix[i][j]); 57 | } 58 | 59 | printf("\n"); 60 | } 61 | } 62 | 63 | void deallocateMemory(int** matrix, int rows) 64 | { 65 | for (int i = 0; i < rows; i++) 66 | { 67 | free(matrix[i]); 68 | } 69 | 70 | free(matrix); 71 | } 72 | 73 | int main() 74 | { 75 | int n; // cols 76 | scanf_s("%d", &n); 77 | 78 | int rows = (1 << n); 79 | 80 | int** boolVectors = initialize(rows, n); 81 | 82 | generateBoolVectors(boolVectors, rows, n); 83 | 84 | print(boolVectors, rows, n); 85 | 86 | deallocateMemory(boolVectors, rows); 87 | } -------------------------------------------------------------------------------- /C examples/Sem04/functions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int isPrime(unsigned n) 6 | { 7 | double sqrtOfN = sqrtf(n); 8 | for (int i = 2; i <= sqrtOfN; i++) 9 | { 10 | if (n % i == 0) 11 | { 12 | return 0; 13 | } 14 | } 15 | return 1; 16 | } 17 | 18 | 19 | unsigned reverse(unsigned n) 20 | { 21 | int res = 0; 22 | 23 | while (n != 0) 24 | { 25 | int lastDigit = n % 10; 26 | res *= 10; 27 | res += lastDigit; 28 | n /= 10; 29 | } 30 | return res; 31 | } 32 | 33 | int isPalindrome(unsigned n) 34 | { 35 | return n == reverse(n); 36 | } 37 | 38 | int isSuffix(unsigned n, unsigned k) 39 | { 40 | if (k > n) 41 | return 0; 42 | 43 | if (k == 0 && n % 10 != 0) 44 | return 0; 45 | 46 | while (k != 0) 47 | { 48 | if (n % 10 != k % 10) 49 | return 0; 50 | n /= 10; 51 | k /= 10; 52 | } 53 | return 1; 54 | } 55 | 56 | int isPrefix(unsigned n, unsigned k) 57 | { 58 | if (k > n) 59 | return 0; 60 | return isSuffix(reverse(n), reverse(k)); 61 | } 62 | 63 | 64 | unsigned power(unsigned n, unsigned p) 65 | { 66 | unsigned result = 1; 67 | for (unsigned i = 0; i < p; i++) 68 | result *= n; 69 | return result; 70 | } 71 | 72 | unsigned logarithm(unsigned n, unsigned base) 73 | { 74 | unsigned current = 1; 75 | unsigned currentPower = 0; 76 | while (current <= n) 77 | { 78 | current *= base; 79 | currentPower += 1; 80 | } 81 | return currentPower - 1; 82 | } 83 | 84 | void testIsPrefix(unsigned n, unsigned k, int expected) { 85 | int result = isPrefix(n, k); 86 | if (result == expected) { 87 | printf("Test passed for isPrefix(%u, %u): expected %d, got %d\n", n, k, expected, result); 88 | } 89 | else { 90 | printf("Test failed for isPrefix(%u, %u): expected %d, got %d\n", n, k, expected, result); 91 | } 92 | } 93 | 94 | int main() { 95 | 96 | } 97 | -------------------------------------------------------------------------------- /Practicum/Week 04/MK.md: -------------------------------------------------------------------------------- 1 | # Малко контролно №1 2 | 3 | **Задача 1.** Какво ще се случи при компилация на следния код: 4 | ```c++ 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int number = 2; 10 | 11 | if (number < 2) { 12 | cout << "less than" << number << endl; 13 | } 14 | else if (number = 0) { 15 | cout << "equal to " << number << endl; 16 | } 17 | else if (number >= 2) { 18 | cout << "more than " << number << endl; 19 | } 20 | else { 21 | cout << "unknown " << number << endl; 22 | } 23 | } 24 | ``` 25 | **Задача 2.** Какво ще се случи при компилация на следния код: 26 | ```c++ 27 | #include 28 | using namespace std; 29 | 30 | int findSum(int n) { 31 | for (int i = 0; i < n; i++) { 32 | int sum = 0; 33 | sum += i; 34 | } 35 | 36 | return sum; 37 | } 38 | 39 | int main() { 40 | int n = 0; 41 | cin >> n; 42 | 43 | cout << findSum(n); 44 | } 45 | ``` 46 | 47 | **Задача 3.** Какво ще се случи при компилация на следния код: 48 | 49 | ```c++ 50 | char evaluate(int num) { 51 | switch(num % 5) { 52 | case 0: return 'A'; break; 53 | case 1: return 'B'; 54 | case 2: return 'C'; break; 55 | case 3: num += 7; return 'D'; 56 | case 4: return 'E'; 57 | default: return 'F'; 58 | } 59 | return ' '; 60 | } 61 | 62 | int main() { 63 | int x = 27; 64 | char letter = evaluate(x); 65 | 66 | cout << letter << ' ' << x; 67 | } 68 | ``` 69 | 70 | **Задача 4.** Напишете програма, която въвежда две **цели положителни** числа и извежда: 71 | * техния най-голям общ делител; 72 | * дали този делител е просто число. 73 | 74 | **Пример:** 75 | 76 | Вход: 77 | ```c++ 78 | 14 79 | 21 80 | ``` 81 | Изход: 82 | ```c++ 83 | 7 84 | true 85 | ``` -------------------------------------------------------------------------------- /Practicum - Informatics/Week 04 - Functions + Mini Exam/Week 04 - CoPrimes Task.md: -------------------------------------------------------------------------------- 1 | Прочетете едно цяло число N>20. За всяко едно число от 1 до N отпечатайте следното: 2 | 3 | • "{number} is Great", ако всяка цифра на number е взаимно просто число с N (приемаме че 0 е взамино просто с всяко число) 4 | 5 | • "{number} is Bad", ако горното не е изпълнено 6 | 7 | Направете валидация на входа. 8 | 9 | Използвайте функции за всяко действие, което ще направите, но не предобряйте с функциите. 10 | 11 | N = 21 -> Great Numbers: 1 2 4 5 8 10 11 12 14 15 18 20 12 | 13 | Решение 14 | 15 | ```C 16 | #include 17 | 18 | int Euclid(int digit, int N) 19 | { 20 | if (digit == 0) 21 | { 22 | return 1; 23 | } 24 | 25 | // no need to check for bigger number because every digits is in range [0..9] and N is bigger than 20 26 | // so N will always be bigger than digit 27 | 28 | int a = N; 29 | int b = digit; 30 | 31 | while (b != 0) 32 | { 33 | int remainder = a % b; 34 | a = b; 35 | b = remainder; 36 | } 37 | 38 | return a; 39 | } 40 | 41 | int checkForCoPrimesWithDigits(int number, int N) 42 | { 43 | while (number != 0) 44 | { 45 | int digit = number % 10; 46 | 47 | int gcd = Euclid(digit, N); 48 | 49 | if (gcd != 1) 50 | { 51 | return 0; 52 | } 53 | 54 | number /= 10; 55 | } 56 | 57 | return 1; 58 | } 59 | 60 | int main() 61 | { 62 | int N; 63 | printf("N= "); 64 | scanf_s("%d", &N); 65 | 66 | if (N <= 20) 67 | { 68 | printf("N needs to be bigger than 20!\n"); 69 | return 0; 70 | } 71 | 72 | for (int number = 1; number < N; number++) 73 | { 74 | int areCoPrimes = checkForCoPrimesWithDigits(number, N); 75 | 76 | if (areCoPrimes) 77 | { 78 | printf("%d is Great\n", number); 79 | } 80 | else 81 | { 82 | printf("%d is Bad\n", number); 83 | } 84 | } 85 | } 86 | ``` 87 | -------------------------------------------------------------------------------- /Sem. 15/backtracking_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | const size_t rows = 6; 6 | const size_t colls = 5; 7 | 8 | bool validCoordinates(int x, int y) 9 | { 10 | return x >= 0 && y >= 0 && x < rows&& y < colls; 11 | } 12 | bool findPathRec(const char field[rows][colls], bool visited[rows][colls], int currentX, int currentY, int destX, int destY) 13 | { 14 | if (!validCoordinates(currentX, currentY) || field[currentX][currentY] != ' ' || visited[currentX][currentY]) 15 | return false; 16 | 17 | visited[currentX][currentY] = true; 18 | 19 | if (currentX == destX && currentY == destY) 20 | return true; 21 | 22 | bool foundPath = findPathRec(field, visited, currentX + 1, currentY, destX, destY) || 23 | findPathRec(field, visited, currentX, currentY + 1, destX, destY) || 24 | findPathRec(field, visited, currentX - 1, currentY, destX, destY) || 25 | findPathRec(field, visited, currentX, currentY - 1, destX, destY); 26 | if (!foundPath) 27 | visited[currentX][currentY] = false; //you may skip that 28 | else 29 | std::cout << currentX << " " << currentY << std::endl; 30 | return foundPath; 31 | } 32 | 33 | bool findPath(const char field[rows][colls], int startX, int startY, int destX, int destY) 34 | { 35 | bool visited[rows][colls]{ false }; 36 | return findPathRec(field, visited, startX, startY, destX, destY); 37 | } 38 | 39 | int main() 40 | { 41 | char field[rows][colls] 42 | { 43 | {' ', ' ', ' ', ' ', ' '}, 44 | {' ', ' ', ' ', ' ', ' '}, 45 | {' ', 'X', ' ', ' ', ' '}, 46 | {' ', 'X', ' ', 'X', 'X'}, 47 | {'X', 'X', ' ', ' ', ' '}, 48 | {' ', ' ', ' ', 'X', ' '} 49 | 50 | }; 51 | 52 | findPath(field, 3, 0, 5, 4); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /C examples/Sem07/examples.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int linearSearch(const int arr[], size_t size, int el) 4 | { 5 | for (unsigned i = 0; i < size; i++) 6 | { 7 | if (arr[i] == el) 8 | return 1;//true 9 | } 10 | return 0; //false 11 | } 12 | 13 | int binarySearch(const int arr[], size_t size, int el) 14 | { 15 | int leftIndex = 0; 16 | int rightIndex = size - 1; 17 | 18 | while (leftIndex <= rightIndex) 19 | { 20 | int midIndex = leftIndex + (rightIndex - leftIndex) / 2; // (left + right) / 2; //left + right may overflow the int !! 21 | 22 | if (arr[midIndex] > el) 23 | { 24 | rightIndex = midIndex - 1; 25 | } 26 | else if (arr[midIndex] < el) 27 | { 28 | leftIndex = midIndex + 1; 29 | } 30 | else 31 | { 32 | return 1; //true!! 33 | } 34 | } 35 | return 0; //false 36 | } 37 | 38 | 39 | void reverseArray(int arr[], size_t size) 40 | { 41 | for (unsigned i = 0; i < size / 2; i++) 42 | { 43 | //swap i <-> size - i - 1 44 | 45 | int temp = arr[i]; 46 | arr[i] = arr[size - 1 - i]; 47 | arr[size - 1 - i] = temp; 48 | } 49 | } 50 | 51 | void printArr(const int arr[], size_t size) 52 | { 53 | for (unsigned i = 0; i < size; i++) 54 | printf("%d ", arr[i]); 55 | } 56 | 57 | #define MAX_ARR 1024 58 | 59 | void markAsNotPrime(int sieve[MAX_ARR], 60 | unsigned n, 61 | unsigned currPrime) 62 | { 63 | unsigned toMark = currPrime + currPrime; 64 | while (toMark <= n) 65 | { 66 | sieve[toMark] = 1; 67 | toMark += currPrime; 68 | } 69 | } 70 | 71 | void sieveEratosthenes(unsigned n) 72 | { 73 | int sieve[MAX_ARR] = { 0 }; 74 | for (unsigned i = 2; i <= n; i++) 75 | { 76 | if (!sieve[i]) 77 | { printf("%d ", i); 78 | markAsNotPrime(sieve, n, i); 79 | } 80 | } 81 | } 82 | 83 | int main() 84 | { 85 | 86 | sieveEratosthenes(1000); 87 | printf("%d", a); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isLeapYear(unsigned int year) { 5 | if (year % 400 == 0) { 6 | return true; 7 | } 8 | if (year % 100 == 0) { 9 | return false; 10 | } 11 | if (year % 4 == 0) { 12 | return true; 13 | } 14 | return false; 15 | } 16 | 17 | unsigned int getDaysInMonth(unsigned month, unsigned year) { 18 | switch (month) { 19 | case 1: case 3: case 5: case 7: case 8: case 10: case 12: 20 | return 31; 21 | case 4: case 6: case 9: case 11: 22 | return 30; 23 | case 2: 24 | return isLeapYear(year) ? 29 : 28; 25 | default: 26 | return 0; // невалиден месец 27 | } 28 | } 29 | 30 | bool isValidDate(unsigned int day, unsigned int month, unsigned int year) { 31 | if (month < 1 || month > 12) { 32 | return false; 33 | } 34 | if (day == 0) { 35 | return false; 36 | } 37 | 38 | unsigned int daysInMonth = getDaysInMonth(month, year); 39 | return day <= daysInMonth; 40 | } 41 | 42 | void nextDay(unsigned int& day, unsigned int& month, unsigned int& year) { 43 | if (!isValidDate(day, month, year)) { 44 | cout << "Invalid date" << endl; 45 | return; 46 | } 47 | 48 | 49 | unsigned int daysInMonth = getDaysInMonth(month, year); 50 | 51 | if (day < daysInMonth) { 52 | day++; 53 | } 54 | else { 55 | day = 1; 56 | if (month == 12) { 57 | month = 1; 58 | year++; 59 | } 60 | else { 61 | month++; 62 | } 63 | } 64 | } 65 | 66 | int main() { 67 | unsigned int d, m, y; 68 | 69 | cin >> d >> m >> y; 70 | nextDay(d, m, y); 71 | 72 | cout << d << "." << m << "." << y; 73 | return 0; 74 | } -------------------------------------------------------------------------------- /C examples/Exam Preparation/Zad3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int myStrLen(const char* text) 5 | { 6 | int index = 0; 7 | 8 | while (text[index++]) {} 9 | 10 | return index; 11 | } 12 | 13 | int isSymbolToRemove(char symbol, const char* symbolsToRemove) 14 | { 15 | for (int i = 0; symbolsToRemove[i]; i++) 16 | { 17 | if (symbol == symbolsToRemove[i]) 18 | { 19 | return 1; 20 | } 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | int getSymbolsToRemoveCount(const char* text, const char* symbolsToRemove) 27 | { 28 | int count = 0; 29 | 30 | for (int i = 0; text[i]; i++) 31 | { 32 | count += isSymbolToRemove(text[i], symbolsToRemove); 33 | } 34 | 35 | return count; 36 | } 37 | 38 | void removeSymbols(const char* text, const char* symbolsToRemove, char* sanitizedText) 39 | { 40 | int sanitizedTextIndex = 0; 41 | 42 | for (int i = 0; text[i]; i++) 43 | { 44 | if (!isSymbolToRemove(text[i], symbolsToRemove)) 45 | { 46 | sanitizedText[sanitizedTextIndex] = text[i]; 47 | sanitizedTextIndex++; 48 | } 49 | } 50 | 51 | sanitizedText[sanitizedTextIndex] = '\0'; 52 | } 53 | 54 | char* sanitizeText(const char* text, const char* symbolsToRemove) 55 | { 56 | int textLength = myStrLen(text); 57 | int symbolsToRemoveCount = getSymbolsToRemoveCount(text, symbolsToRemove); 58 | 59 | char* sanitizedText = malloc(textLength - symbolsToRemoveCount + 1); 60 | 61 | removeSymbols(text, symbolsToRemove, sanitizedText); 62 | 63 | return sanitizedText; 64 | } 65 | 66 | int main() 67 | { 68 | char text[1024]; 69 | scanf_s("%s", text, 1024); 70 | 71 | char symbolsToRemove[64]; 72 | scanf_s("%s", symbolsToRemove, 64); 73 | 74 | char* sanitizedText = sanitizeText(text, symbolsToRemove); 75 | 76 | printf("%s", sanitizedText); 77 | 78 | free(sanitizedText); 79 | } 80 | 81 | // Hello123World321INF321 82 | // 123 83 | 84 | -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | constexpr size_t arr_size = 32; 4 | 5 | void printArray(int arr[], size_t size) { 6 | for (size_t i = 0; i < size; i++) 7 | { 8 | cout << arr[i] << ", "; 9 | if (i + 1 < size) { 10 | cout << ", "; 11 | } 12 | } 13 | } 14 | void swapArray(int arr[], size_t size) { 15 | for (int i = 0; i < size / 2; i++) { 16 | int temp = arr[i]; 17 | arr[i] = arr[size - 1 - i]; 18 | arr[size - 1 - i] = temp; 19 | } 20 | } 21 | void convertToBinary(int n, int binary[], size_t& size) { 22 | size = 0; 23 | if (n == 0) { 24 | binary[0] = 0; 25 | size = 1; 26 | return; 27 | } 28 | while (n > 0) { 29 | binary[size] = n % 2; 30 | n /= 2; 31 | size++; 32 | } 33 | swapArray(binary, size); 34 | } 35 | int XOR(int a, int b) { 36 | if (a == b) 37 | return 0; 38 | else 39 | return 1; 40 | } 41 | void applyXOR(int arr1[],size_t size1, int arr2[],size_t size2, int result[], size_t& size) { 42 | if (size1 != size2) { 43 | size = 0; 44 | return; 45 | } 46 | 47 | for (size_t i = 0; i < size1; i++) 48 | { 49 | result[i] = XOR(arr1[i], arr2[i]); 50 | } 51 | size = size1; 52 | 53 | } 54 | int main() { 55 | 56 | 57 | int a = 5; 58 | int b = 9; 59 | 60 | int binaryA[arr_size], binaryB[arr_size], binaryResult[arr_size]; 61 | size_t sizeA, sizeB, sizeResult; 62 | 63 | convertToBinary(a, binaryA, sizeA); 64 | convertToBinary(b, binaryB, sizeB); 65 | 66 | applyXOR(binaryA, sizeA, binaryB, sizeB, binaryResult, sizeResult); 67 | 68 | cout << "XOR Result: "; printArray(binaryResult, sizeResult); 69 | 70 | return 0; 71 | 72 | } 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /Sem. 12/1. playersStatistics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //each rows have two "system" variables. 5 | //[0] is the count of the elements in the row 6 | //[1] is the sum of the elements of the row 7 | int** createMatrix(int rows) 8 | { 9 | int** mtx = new int* [rows]; 10 | for (int i = 0; i < rows; i++) 11 | { 12 | int colls; 13 | cin >> colls; 14 | 15 | mtx[i] = new int[colls + 2]{0}; 16 | mtx[i][0] = colls; 17 | } 18 | return mtx; 19 | } 20 | 21 | void freeMatrix(int** matrix, int rows) 22 | { 23 | for (int i = 0; i < rows; i++) 24 | delete[] matrix[i]; 25 | delete[] matrix; 26 | } 27 | 28 | void print(int** mtx, int rows) 29 | { 30 | for (int i = 0; i < rows; i++) 31 | { 32 | int sizeOfRow = mtx[i][0]; 33 | for (int j = 2; j < sizeOfRow + 2; j++) 34 | cout << mtx[i][j] << ' '; 35 | cout << endl; 36 | } 37 | } 38 | 39 | void swap(int*& first, int*& second) 40 | { 41 | int* temp = first; 42 | first = second; 43 | second = temp; 44 | } 45 | 46 | void moveTheRowToTheCorrectPosition(int** mtx, int lastModifedRow) 47 | { 48 | int current = lastModifedRow; 49 | 50 | while (current > 0 && mtx[current][1] > mtx[current - 1][1]) 51 | { 52 | swap(mtx[current], mtx[current - 1]); 53 | current--; 54 | } 55 | } 56 | 57 | void addPoints(int** mtx, int row, int coll, int point) 58 | { 59 | mtx[row][coll + 2] += point; 60 | mtx[row][1] += point; 61 | moveTheRowToTheCorrectPosition(mtx, row); 62 | } 63 | 64 | int main() 65 | { 66 | int rows; 67 | cin >> rows; 68 | 69 | int** mtx = createMatrix(rows); 70 | 71 | print(mtx, rows); 72 | 73 | int queriesCount; 74 | cin >> queriesCount; 75 | 76 | for (int i = 0; i < queriesCount; i++) 77 | { 78 | int row, coll, points; 79 | cin >> row >> coll >> points; 80 | addPoints(mtx, row, coll, points); 81 | print(mtx, rows); 82 | } 83 | 84 | print(mtx, rows); 85 | 86 | freeMatrix(mtx, rows); 87 | } 88 | -------------------------------------------------------------------------------- /C examples/Exam Preparation/Exam Preparation Tasks.md: -------------------------------------------------------------------------------- 1 | ## Подготовка за контролна работа 2 ## 2 | 3 | - **Задача 1** : Да се напише програма, която намира и извежда на екрана всички n-мерни вектори с компоненти 0 и 1 като редове в матрица. Например при n = 3, всички 3-мерни вектори с компоненти 0 и 1 като редове на матрица са:
(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)
Прочетете N от конзолата и генерирайте тази матрица 4 | 5 | - **Задача 2** : Да се дефинира функция която получава два параметъра - текст и поредица от символи, които са разделити. Функцията трябва да върне същия текст, но думите, разеделени между разделителите, трябва да бъдат обърнати. Пример
текст="Hello@World!--Up-INF##"
разделители="@!-#"
резултат="olleH@dlroW!--pU-FNI##" 6 | 7 | - **Задача 3** : Да се дефинира функция която получава два параметъра - текст и поредица от символи, които да бъдат изтрити. Функцията трябва да върне нов текст, с точна дължина, като бъдат изтрити тези символи 8 | 9 | - **Задача 4** : Прочетете едно число rightBound. След това прочетете едно число - setsCount - и създайте колекция с толкова масиви - всеки ще представя множество. За ввсяко множество прочетете колко елемента ще съдържа и след това елементите му. Ако някой елемент е по-малък от 0, го заменете с 0. Ако е по-голям от rightBound, го заменете с rightBound. След това за всяко множество прочетете по един символ - как се добавя към резултата - 1 (union) или 2 (intersection). Симулирайте операциите за обединение и сечение на множества по следния алгоритъм:
10 | -- Първата операция за първото множество се игнорира и то започва да играе роля на резултата.
11 | -- Взима се второто множество и се добавя по съответния начин към първото. След края на операцията двете обединени/сечени множества ще създадат нов резултат.
12 | -- Приложете третата операция по същия начин към резултата и тн със следващиге. 13 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPositiveNumber(int n) { 5 | return n > 0; 6 | } 7 | 8 | bool isEvenNumber(int n) { 9 | return n % 2 == 0; 10 | } 11 | 12 | int getCountOfPositiveNumbersInArray(const int arr[], size_t size) { 13 | int count = 0; 14 | for (int i = 0; i < size; i++) { 15 | if (isPositiveNumber(arr[i])) { 16 | count++; 17 | } 18 | } 19 | return count; 20 | } 21 | 22 | int findMinNegativeNumber(const int arr[], size_t size) { 23 | int min = INT_MAX; 24 | for (int i = 0; i < size; i++) { 25 | if (arr[i] < 0 && arr[i] < min) { 26 | min = arr[i]; 27 | } 28 | } 29 | return min; 30 | } 31 | 32 | int getAverageInArr(const int arr[], size_t size) { 33 | int sum = 0; 34 | for (int i = 0; i < size; i++) { 35 | sum += arr[i]; 36 | } 37 | return sum / size; 38 | } 39 | 40 | void transformNumbers(int arr[], size_t size) { 41 | int countOfPositiveNumbers = getCountOfPositiveNumbersInArray(arr, size); 42 | int minNegNum = findMinNegativeNumber(arr, size); 43 | int average = getAverageInArr(arr, size); 44 | 45 | for (int i = 0; i < size; i++) { 46 | if (arr[i] == 0) { 47 | arr[i] = average; 48 | arr[i] *= (-1); 49 | } else if (isPositiveNumber(arr[i])) { 50 | if (isEvenNumber(arr[i])) { 51 | arr[i] *= countOfPositiveNumbers; 52 | } else { 53 | if (minNegNum == INT_MAX) { 54 | continue; 55 | } 56 | arr[i] += minNegNum; 57 | } 58 | } else { 59 | arr[i] *= (-1); 60 | arr[i] /= 2; 61 | } 62 | } 63 | } 64 | 65 | int main() { 66 | int arr[] = {2, -3, 0, 5, -4, 6}; 67 | transformNumbers(arr, 6); 68 | 69 | for (int i = 0; i < 6; i++) { 70 | cout << arr[i] << " "; 71 | } 72 | } -------------------------------------------------------------------------------- /Practicum/Week 08/Solutions/task_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | constexpr size_t MIN_SIZE = 0; 5 | constexpr size_t MAX_SIZE = 1024; 6 | 7 | void swap(int& a, int& b) { 8 | int temp = a; 9 | a = b; 10 | b = temp; 11 | } 12 | 13 | void sortArr(int arr[], size_t size) { 14 | for (size_t i = 0; i < size - 1; i++) { 15 | for (size_t j = 0; j < size - i - 1; j++) { 16 | if (arr[j] > arr[j + 1]) { 17 | swap(arr[j], arr[j + 1]); 18 | } 19 | } 20 | } 21 | } 22 | 23 | int sumOfDigits(int n) { 24 | int sum = 0; 25 | while (n) { 26 | sum += (n % 10); 27 | n /= 10; 28 | } 29 | return sum; 30 | } 31 | 32 | int multOfDigits(int n) { 33 | int mult = 1; 34 | while (n) { 35 | mult *= (n % 10); 36 | n /= 10; 37 | } 38 | return mult; 39 | } 40 | 41 | bool isInArray(const int arr[], size_t size, int num) { 42 | for (size_t i = 0; i < size; i++) { 43 | if (arr[i] == num) { 44 | return true; 45 | } 46 | } 47 | return false; 48 | } 49 | 50 | void findMagicNumbers(const int arr[], size_t arrSize, int result[], size_t& resultSize) { 51 | for (size_t i = 0; i < arrSize; i++) { 52 | int sum = sumOfDigits(arr[i]); 53 | int mult = multOfDigits(arr[i]); 54 | 55 | if (sum % mult == 0) { 56 | if (!isInArray(result, resultSize, arr[i])) { 57 | result[resultSize++] = arr[i]; 58 | } 59 | } 60 | } 61 | 62 | sortArr(result, resultSize); 63 | } 64 | 65 | void printArr(const int arr[], size_t size) { 66 | for (size_t i = 0; i < size; i++) { 67 | cout << arr[i] << ", "; 68 | } 69 | cout << endl; 70 | } 71 | 72 | int main() { 73 | int arr[] = {12, 22, 13, 12, 111, 24, 36}; 74 | 75 | int result[MAX_SIZE] {0}; 76 | size_t resultSize = 0; 77 | 78 | findMagicNumbers(arr, 8, result, resultSize); 79 | 80 | printArr(result, resultSize); 81 | } -------------------------------------------------------------------------------- /Sem. 12/3. splitString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | unsigned countCharOccurences(const char* str, char ch) 6 | { 7 | unsigned count = 0; 8 | while(*str) 9 | { 10 | if(*str == ch) 11 | count++; 12 | str++; 13 | } 14 | return count; 15 | } 16 | 17 | unsigned getEndOfTokenIndex(const char* str, unsigned ch) 18 | { 19 | for(int i = 0;; i++) 20 | { 21 | if(str[i] == '\0' || str[i] == ch) 22 | return i; 23 | } 24 | return -1; 25 | } 26 | 27 | void copyNChars(const char* source, char* dest, unsigned N) 28 | { 29 | for(int i = 0; i < N; i++) 30 | dest[i] = source[i]; 31 | } 32 | 33 | char** split(const char* str, char separator) 34 | { 35 | unsigned resultSize = countCharOccurences(str, separator) + 2; 36 | char** result = new char*[resultSize]; 37 | result[resultSize - 1] = nullptr; //the sentinel 38 | unsigned resultIndex = 0; 39 | 40 | while(*str) 41 | { 42 | int sepIndex = getEndOfTokenIndex(str, separator); 43 | 44 | char* currentToken = new char[sepIndex + 1]; 45 | currentToken[sepIndex] = '\0'; //the string sentinel 46 | 47 | copyNChars(str, currentToken, sepIndex); 48 | str += sepIndex; //skip the token 49 | 50 | result[resultIndex++] = currentToken; 51 | 52 | if(*str == separator) 53 | str++; 54 | } 55 | return result; 56 | } 57 | 58 | void printWords(char** tokens) 59 | { 60 | while(*tokens) //if the first pointer is not nullptr 61 | { 62 | cout << *tokens << endl; 63 | tokens++; //go to the next pointer 64 | } 65 | } 66 | 67 | void free(const char** str) 68 | { 69 | for(int i = 0; str[i] != nullptr; i++) 70 | delete[] str[i]; 71 | delete[] str; 72 | } 73 | int main() 74 | { 75 | char** res = split("this!is!test!spliting", '!'); 76 | 77 | printWords(res); 78 | 79 | free(res); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | double getDist(int x1, int y1, 6 | int x2, int y2) { 7 | return sqrt((x2 - x1) * (x2 - x1) + 8 | (y2 - y1) * (y2 - y1)); 9 | } 10 | 11 | double getPerimeter(double sideA, double sideB, double sideC) { 12 | return sideA + sideB + sideC; 13 | } 14 | 15 | double getPerimeter(int x1, int y1, 16 | int x2, int y2, 17 | int x3, int y3) { 18 | double side1 = getDist(x1, y1, x2, y2); 19 | double side2 = getDist(x1, y1, x3, y3); 20 | double side3 = getDist(x2, y2, x3, y3); 21 | 22 | return getPerimeter(side1, side2, side3); 23 | } 24 | 25 | double getArea(int x1, int y1, 26 | int x2, int y2, 27 | int x3, int y3) { 28 | double side1 = getDist(x1, y1, x2, y2); 29 | double side2 = getDist(x1, y1, x3, y3); 30 | double side3 = getDist(x2, y2, x3, y3); 31 | 32 | double halfPer = getPerimeter(side1, side2, side3) / 2; 33 | 34 | return sqrt(halfPer * (halfPer - side1) * (halfPer - side2) * (halfPer - side3)); 35 | } 36 | 37 | double getDistFromCenter(int x, int y) { 38 | return sqrt(x * x + y * y); 39 | } 40 | 41 | bool isInCircleWithRadius(int x1, int y1, 42 | int x2, int y2, 43 | int x3, int y3, 44 | int radius) { 45 | double distA = getDistFromCenter(x1, y1); 46 | double distB = getDistFromCenter(x2, y2); 47 | double distC = getDistFromCenter(x3, y3); 48 | 49 | return (distA <= radius && 50 | distB <= radius && 51 | distC <= radius); 52 | } 53 | 54 | int main() { 55 | int x1 = 0, y1 = 0, 56 | x2 = 0, y2 = 0, 57 | x3 = 0, y3 = 0, 58 | radius = 0; 59 | cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> radius; 60 | 61 | cout << getPerimeter(x1, y1, x2, y2, x3, y3) << endl; 62 | cout << getArea(x1, y1, x2, y2, x3, y3) << endl; 63 | cout << isInCircleWithRadius(x1, y1, x2, y2, x3, y3, radius) << endl; 64 | } -------------------------------------------------------------------------------- /C examples/Exam Preparation/Zad2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int isDelimiter(char symbol, const char* delimiters) 5 | { 6 | for (int i = 0; delimiters[i]; i++) 7 | { 8 | if (symbol == delimiters[i]) 9 | { 10 | return 1; 11 | } 12 | } 13 | 14 | return 0; 15 | } 16 | 17 | int getWordStartIndex(const char* text, const char* delimiters, int startIndex) 18 | { 19 | int index = startIndex; 20 | 21 | while (1) 22 | { 23 | if (text[index] == '\0') 24 | { 25 | break; 26 | } 27 | 28 | if (!isDelimiter(text[index], delimiters)) 29 | { 30 | break; 31 | } 32 | 33 | index++; 34 | } 35 | 36 | return index; 37 | } 38 | 39 | int getWordEndIndex(const char* text, const char* delimiters, int startIndex) 40 | { 41 | int index = startIndex; 42 | 43 | while (1) 44 | { 45 | if (text[index] == '\0') 46 | { 47 | break; 48 | } 49 | 50 | if (isDelimiter(text[index], delimiters)) 51 | { 52 | break; 53 | } 54 | 55 | index++; 56 | } 57 | 58 | return index - 1; 59 | } 60 | 61 | void reverse(char* left, char* right) 62 | { 63 | while (left < right) 64 | { 65 | char leftSymbol = *left; 66 | char rightSymbol = *right; 67 | 68 | *left = rightSymbol; 69 | *right = leftSymbol; 70 | 71 | left++; 72 | right--; 73 | } 74 | } 75 | 76 | void reversOnlyWords(char* text, const char* delimiters) 77 | { 78 | int index = 0; 79 | 80 | while (1) 81 | { 82 | int wordStartIndex = getWordStartIndex(text, delimiters, index); 83 | 84 | if (text[wordStartIndex] == '\0') 85 | { 86 | return; 87 | } 88 | 89 | int wordEndIndex = getWordEndIndex(text, delimiters, wordStartIndex + 1); 90 | 91 | reverse(text + wordStartIndex, text + wordEndIndex); 92 | 93 | index = wordEndIndex + 1; 94 | } 95 | } 96 | 97 | int main() 98 | { 99 | char text[1024]; 100 | scanf_s("%s", text, 1024); 101 | 102 | char delimiters[64]; 103 | scanf_s("%s", delimiters, 64); 104 | 105 | reversOnlyWords(text, delimiters); 106 | 107 | printf("%s", text); 108 | } -------------------------------------------------------------------------------- /Sem. 12/2. stringToWords.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | bool wordSymbol(char ch) 6 | { 7 | return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); 8 | } 9 | unsigned getWordsCount(const char* str) 10 | { 11 | unsigned count = 0; 12 | while(*str) 13 | { 14 | if(wordSymbol(*str) && !wordSymbol(*(str + 1))) 15 | count++; 16 | str++; 17 | } 18 | return count; 19 | } 20 | 21 | void skipInvalid(const char*& str) 22 | { 23 | while(*str && !wordSymbol(*str)) 24 | str++; 25 | } 26 | 27 | char* getNextWord(const char*& str) 28 | { 29 | unsigned ind = 0; 30 | 31 | while(wordSymbol(str[ind])) 32 | { 33 | ind++; 34 | } 35 | char* word = new char[ind + 1]; 36 | word[ind] = '\0'; 37 | 38 | for(int i = 0; i < ind; i++) 39 | word[i] = str[i]; 40 | str += ind; 41 | return word; 42 | } 43 | 44 | char** tokenize(const char* str, size_t& size) 45 | { 46 | unsigned wordsCount = getWordsCount(str); 47 | char** words = new char*[wordsCount]; 48 | 49 | unsigned currentWordIndex = 0; 50 | while(*str) 51 | { 52 | skipInvalid(str); 53 | 54 | if (!(*str)) 55 | { 56 | break; 57 | } 58 | 59 | char* word = getNextWord(str); 60 | 61 | words[currentWordIndex++] = word; 62 | //we wont increment str since it's beeing changed in the skipInvalid/getNextWord functions. 63 | } 64 | size = wordsCount; //or we can skip the size and use a santinel -> nullptr 65 | return words; 66 | } 67 | 68 | void printWords(char** words, size_t size) 69 | { 70 | for(unsigned i = 0; i < size; i++) 71 | cout << words[i] << endl; 72 | } 73 | 74 | void free(char** words, size_t size) 75 | { 76 | for(unsigned i = 0; i < size; i++) 77 | delete[] words[i]; 78 | delete[] words; 79 | } 80 | int main() 81 | { 82 | size_t count; 83 | char** res = tokenize("hello i am a sentence", count); 84 | 85 | printWords(res, count); 86 | free(res, count); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 12 - Strings/Week 12.md: -------------------------------------------------------------------------------- 1 | ## Основни задачи: 2 | 1. Напишете имплементация на **strlen** - *връща колко символа има от началото до ‘\0’* 3 | 4 | 2. Напишете имплементация на **strcmp** - *сравнява 2 символни низа (> , = , <)* 5 | 6 | 3. Напишете имплементация на **strcpy** - *копира source в destination* 7 | 8 | 4. Напишете имплементация на **strcat** - *конкатенация* 9 | 10 | 5. Напишете имплементация на **аtoi** - *преобразуване на стринг в число* 11 | 12 | 6. Напишете импелментация на **getCharOccurrencesCount** - *брой срещания на кнокретен символ в текст*. 13 | 14 | 7. Напишете имплементация на **isPrefix** - *проверка дали низ е префикс на друг низ* 15 | 16 | 8. Напишете имплементация на **searchInText** - *търсене в текст*. 17 | 18 | ## Задачи 19 | 20 | **Задача 1.** Да се напишат функции
21 | - *toUpper*, която приема буква и я превръща в съответстващата й голяма;
22 | - *toLower*, която приема буква и я превръща в съответстващата й малка.
23 | 24 | **Задача 2:** Напишете функция, която приема 3 стринга - text, where и what и замества в text всяко срещане на where с what. 25 | 26 | *Да се реши без допълнителна памет (in-place).* 27 | 28 | *Вход: "I am the bestt of the bestt", "bestt" , "worst" , Изход: "I am the worst of the worst"* 29 | 30 | **Задача 3** Напишете функции toUpper и toLower, които приемат стринг и променят всички главни букви в малки/всички малки букви в главни. 31 | 32 | **Задача 4** Напишете функция, която приема стринг и връща броя на думите в него. (Думите са разделени с произволен брой интервали, табулации и препинателни знаци) 33 | 34 | *Вход: "Me? Why always me?, Изход: 4* 35 | 36 | 37 | **Задача 5** Напишете функция, която приема стринг и връща най-често срещаната дума.(Думите са разделени с произволен брой интервали, табулации и препинателни знаци). Игнорираме разликата между главни и малки букви за задачата. 38 | 39 | *Вход: "Me? Why always me?, Изход: me* 40 | 41 | **Задача 6** Напишете функция, която приема стринг и връща лексикографско най-малка дума. 42 | 43 | *Вход: "Me? Why always me?, Изход: always* 44 | 45 | **Задача 7.** Проверете дали масив е конкатенация на два палиндрома. 46 | -------------------------------------------------------------------------------- /C examples/Sem09/mem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | unsigned getEvenCount(const int* arr, size_t size) 5 | { 6 | int count = 0; 7 | for (int i = 0; i < size; i++) 8 | { 9 | if (arr[i] % 2 == 0) 10 | count++; 11 | } 12 | return count; 13 | } 14 | 15 | 16 | void printArr(const int* arr, size_t size) 17 | { 18 | for (int i = 0; i < size; i++) 19 | printf("%d ", arr[i]); 20 | printf("\n"); 21 | } 22 | //функция за добавяне на елемент в масив по индекс 23 | 24 | void putElementAtIndex(int** arr, size_t* size, int el, int ind) 25 | { 26 | printArr(*arr, *size); 27 | 28 | int* newArr = realloc(*arr, sizeof(int) * (*size) + 1); //увеличава размера на масива с 1 29 | if (newArr == NULL) 30 | { 31 | (*size) = 0; 32 | free(arr); 33 | return; 34 | } 35 | (*arr) = newArr; 36 | 37 | printArr(*arr, *size); 38 | (*size)++; 39 | for (int i = (*size) - 1; i > ind; i--) 40 | (*arr)[i] = (*arr)[i - 1]; 41 | (*arr)[ind] = el; 42 | } 43 | 44 | void extractEvenAndOdd(const int* arr, 45 | int arrSize, 46 | int** evenArr, //указател към указателя, който искате да промените!! 47 | int* evenCount, 48 | int** oddArr, //указател към указателя, който искате да промените!! 49 | int* oddCount) 50 | { 51 | (*evenCount) = getEvenCount(arr, arrSize); 52 | (*oddCount) = arrSize - (*evenCount); 53 | 54 | (*evenArr) = calloc((*evenCount), sizeof(int)); 55 | (*oddArr) = calloc((*oddCount), sizeof(int)); 56 | 57 | int evenIndex = 0, oddIndex = 0; 58 | 59 | for (int i = 0; i < arrSize; i++) 60 | { 61 | if (arr[i] % 2 == 0) 62 | (*evenArr)[evenIndex++] = arr[i]; 63 | else 64 | (*oddArr)[oddIndex++] = arr[i]; 65 | } 66 | } 67 | 68 | int main() 69 | { 70 | int* arr = calloc(4, sizeof(int)); 71 | arr[0] = 1; 72 | arr[1] = 1; 73 | arr[2] = 2; 74 | arr[3] = 2; 75 | 76 | size_t size = 4; 77 | putElementAtIndex(&arr, &size, 0, 1); 78 | printArr(arr, 5); 79 | 80 | /* 81 | int* evenArr = NULL; 82 | int evenCount = 0; 83 | 84 | int* oddArr = NULL; 85 | int oddCount = 0; 86 | 87 | extractEvenAndOdd(arr, 5, &evenArr, &evenCount, &oddArr, &oddCount); 88 | 89 | printArr(evenArr, evenCount); 90 | printArr(oddArr, oddCount); 91 | 92 | free(evenArr); 93 | free(oddArr); 94 | */ 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /Sem. 08/README.md: -------------------------------------------------------------------------------- 1 | 2 | #### Побитови операции 3 | прилагат върху един бит или набор от повече отделни битове на двоични числа 4 | 5 | - Побитово И (**&**) 6 | - Побитово ИЛИ (**|**) 7 | - Побитово ИЗКЛЮЧВАЩО ИЛИ (**^**) 8 | - Побитово отместване (**<<** ) ( **>>**) 9 | 10 | | | | 11 | |--------|-----------| 12 | | a | 101010100 | 13 | | b | 100101110 | 14 | | a&b | 100000100 | 15 | | a \| b | 101111110 | 16 | | a^b | 001111010 | 17 | | a << 2 | 101010000 | 18 | | a >> 2 | 001010101 | 19 | 20 | Примери: 21 | 22 | - Функция, която с побитови операции проверява дали число е четно 23 | - Функция, която с побитови операции повдига 2 на степен k. 24 | 25 | - Изчистване на бит (да стане 0) 26 | - Вдигане на бит (да стане 1) 27 | - Връщане на стойност на бит 28 | - Слагане на подадена стойност на бит. 29 | 30 |

Задачи

31 | 32 | 33 | **Задача 1:** Да се напише функция, която приема масив, в който всяко число се среща четен брой пъти с изключение на едно число, което се среща нечетен брой пъти. 34 | Напишете функция, която приема такъв масив и връща кое е това число. 35 | 36 | *Вход: [9 18 9 12 18 15 12], Изход: 15* 37 | 38 | 39 | **Задача 2:** Напишете функция, която приема цяло число и връща броя на 1-ците в двоичния му запис. 40 | 41 | *Вход: 189, Изход: 6* 42 | 43 | **Задача 3 :** Напишете функция, която приема цяло число и обръща най-десния бит в него, който е единица. 44 | 45 | *Вход: 12, Изход: 8* 46 | 47 | *Вход: 15, Изход: 14* 48 | 49 | **Задача 4:** Напишете функция, която по подадено цяло число n и цяло число k и връща числото, което е "закодирано" в последните k бита на n. 50 | 51 | *Вход: 15 2, Изход: 3* 52 | 53 | *Вход: 189 3, Изход: 5* 54 | 55 | *Вход: 189 4, Изход: 13* 56 | 57 | **Задача 5:** Напишете функция, която приема цяло число n и цяло число k и връща дали k в двоичен запис (разглеждайки го без водещите 0-ли) е част от двоичния запис на n. 58 | 59 | *Вход: 15 3, Изход: true* 60 | 61 | *Вход: 13 5 Изход: true* 62 | 63 | *Вход: 13 7, Изход: false* 64 | 65 | **Задача 6:** Напишете функция, която приема масив(разглеждаме го като множество) и отпечатва всички негови подмножества. 66 | 67 | *Вход: [1, 2, 3], Изход: [], [1], [2], [3], [1,2], [2,3], [1,3], [1,2,3]* 68 | 69 | *Вход: [5, 3] Изход: [], [5], [3], [5, 3]* 70 | 71 | -------------------------------------------------------------------------------- /Practicum - Informatics/Week 06 - Bitwise Operations/Week 06.md: -------------------------------------------------------------------------------- 1 | 2 | ## MUST KNOW 3 | - проверка дали число е степен на двойката 4 | - умножение на число по две 5 | - деление на число по две 6 | 7 | ## Задачa 0 8 | 9 | Напишете функция, която приема число и изпечатва двоичния му запис 10 | 11 | ## Задачa 1 12 | 13 | Напишете функция, която приема число и печата броя на 1 и броя на 0 в двоичния му запис
14 | 15 | 16 | ## Задачa 2 17 | 18 | Напишете функция, която приема число и стойност на бит и печата от кой до кой индекс е най-дългата последователност от еднакви битове, равни на подадения бит като аргумент 19 | 20 | ## Задачa 3 21 | Напишете функция, която приема число и разменя всеки два съседните бита - този на четна позиция се сменя с този на нечетна. 22 | 23 | ## Задачa 4 24 | Напишете функция, която приема число и reverse-ва битовете в двоичния запис 25 | 26 | ## Задачa 5 27 | Напишете функция, която приема число и проверява дали двоичният му запис е палиндром. 28 | 29 | ## Задачa 6 30 | Напишете функция, която приема число N отпечатва първите N числа, които имат равен брой 0 и 1 в двоичния запис 31 | 32 | ## Задачa 7 33 | Напишете функция, която приема две числа N и K и отпечатва първите N числа, които са съставени само от групи от едвакви битове с дължина K
34 | Пример: k=3 -> 111000111000111 (има само групи с дължина 3 от еднакви битове) 35 | 36 | ## Задачa 8 37 | Напишете функция, която приема две числа и изпечатва всички индекси, на които битове се различават. 38 | 39 | ## Задачa 9 40 | Напишете програма, която симулира множество от цели числа с краен брой елементи чрез битове(например 64). Напишете функции за добавяне и премахване на елемент, проверка дали даден елемент принадлежи на множестовото. Напишете функции за обединение и сечение на две множества. Напишете функция, котяо изпечатва елементите на множеството. 41 | 42 | ## Задачa 10 43 | Напишете функция, която приема две числа N и K. След това се въвеждат K на брой числа от клавиатурата - нека всяко въведено число бъде Ki. За всяко Ki вземете съответния ррой битове от N в друга променлива и я изпечетайте.
44 | Пример: N=10010111010110111
45 | K = 4 -> K1=5 K2=6 K3=2 K4=4 // (10010) (111010) (11) (0111)
46 | първите 5 бита (K1) - 10010
47 | следващите 6 бита (K2) - 111010
48 | следващите 2 бита (K3) - 11
49 | следващите 4 бита (K4) - 0111
50 | 51 | -------------------------------------------------------------------------------- /Sem. 07/readme.md: -------------------------------------------------------------------------------- 1 | 2 | # Бройни системи. Преобразуване между бройни системи. Представяния на отрицателни числа 3 | 4 | | Десетична (decimal) | Двоична (binary) | осмична (octal) | Шестнайсетична (hexadecimal) | Унарна (unary) | 5 | |---------------------|------------------|-----------------|------------------------------|-------| 6 | | 00 | 00000 | 00 | 00 | 00001 | 7 | | 01 | 00001 | 01 | 01 | 00011 | 8 | | 02 | 00010 | 02 | 02 | 00111 | 9 | | 03 | 00011 | 03 | 03 | 01111 | 10 | | 04 | 00100 | 04 | 04 | ... | 11 | | 05 | 00101 | 05 | 05 | | 12 | | 06 | 00110 | 06 | 06 | | 13 | | 07 | 00111 | 07 | 07 | | 14 | | 08 | 01000 | 10 | 08 | | 15 | | 09 | 01001 | 11 | 09 | | 16 | | 10 | 01010 | 12 | 0A | | 17 | | 11 | 01011 | 13 | 0B | | 18 | | 12 | 01100 | 14 | 0C | | 19 | | 13 | 01101 | 15 | 0D | | 20 | 21 | #### Преобразуване между бройни системи 22 | 23 | Примери: 24 | 25 | - Алгоритъм за преобразуване от произволна бройна система в дестична бройна система 26 | - Алгоритъм за преобразуване от десетчина бройна система в произволна бройна система 27 | 28 | #### Представяния на отрицателни числа 29 | 30 | - **One's complement:** 510 == 0..01012 -> (flip) 1..10102 == -510 31 | - **Two's complement:** 510 == 0..01012 -> (flip) 1..10102 -> (1..10102 + 0..00012) 1..10112 == -510 32 | -------------------------------------------------------------------------------- /Sem. 05/bullsAndCows.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int DIGIT_COUNT = 4; 5 | 6 | unsigned getDigitsCount(unsigned n) 7 | { 8 | if (n == 0) 9 | return 1; 10 | unsigned count = 0; 11 | 12 | while (n != 0) 13 | { 14 | count++; 15 | n /= 10; 16 | } 17 | return count; 18 | } 19 | 20 | 21 | unsigned countDigitOccurences(unsigned n, unsigned digit) 22 | { 23 | if (digit == 0 && n == 0) 24 | return 1; 25 | 26 | unsigned count = 0; 27 | while (n != 0) 28 | { 29 | int lastDigit = n % 10; 30 | if (lastDigit == digit) 31 | count++; 32 | n /= 10; 33 | } 34 | return count; 35 | } 36 | bool areAllDistinctDigits(unsigned n) 37 | { 38 | for (int i = 0; i <= 9; i++) 39 | { 40 | if (countDigitOccurences(n, i) > 1) 41 | return false; 42 | } 43 | return true; 44 | } 45 | 46 | bool isValidInput(unsigned n) 47 | { 48 | return areAllDistinctDigits(n) && getDigitsCount(n) == DIGIT_COUNT; 49 | } 50 | 51 | unsigned userInput() 52 | { 53 | unsigned n; 54 | do 55 | { 56 | cin >> n; 57 | } while (!isValidInput(n)); 58 | return n; 59 | } 60 | unsigned getBulls(unsigned toGuess, unsigned myGuess) 61 | { 62 | unsigned bulls = 0; 63 | while (toGuess != 0) 64 | { 65 | if (toGuess % 10 == myGuess % 10) 66 | bulls++; 67 | toGuess /= 10; 68 | myGuess /= 10; 69 | } 70 | return bulls; 71 | 72 | } 73 | 74 | unsigned getBullsAndCowsSum(unsigned toGuess, unsigned myGuess) 75 | { 76 | unsigned sum = 0; 77 | while (toGuess != 0) 78 | { 79 | int lastDigit = toGuess % 10; 80 | 81 | bool contains = countDigitOccurences(myGuess, lastDigit) == 1; 82 | 83 | if (contains) 84 | sum++; 85 | 86 | toGuess /= 10; 87 | } 88 | return sum; 89 | } 90 | 91 | void getBullsAndCows(unsigned toGuess, unsigned myGuess, 92 | unsigned& bulls, unsigned& cows) 93 | { 94 | bulls = getBulls(toGuess, myGuess); 95 | cows = getBullsAndCowsSum(toGuess, myGuess) - bulls; 96 | } 97 | int main() 98 | { 99 | unsigned toGuess = userInput(); 100 | cout << "Success! Try and guess it." << endl; 101 | unsigned bulls, cows; 102 | do 103 | { 104 | unsigned myGuess = userInput(); 105 | getBullsAndCows(toGuess, myGuess, bulls, cows); 106 | cout << "Bulls: " << bulls << " Cows: " << cows << endl; 107 | } while (bulls != DIGIT_COUNT); 108 | cout << "Congratulations! " << endl; 109 | } 110 | -------------------------------------------------------------------------------- /Practicum/Week 09/README.md: -------------------------------------------------------------------------------- 1 | # УП - Практикум 03.12.2025 Седмица 9 2 | 3 | **Задача 1:** Да се напише функция, която приема число k, между 0 и 31 и връща 2^k. 4 | 5 | **Пример:** 6 | 7 | Вход: 8 | ```c++ 9 | 3 10 | ``` 11 | Изход: 12 | ```c++ 13 | 8 14 | ``` 15 | 16 | **Задача 2:** Да се напише функция, която приема 2 цели числа *N* и *M* и връща бита на позиция *M* в числото *N* (отзад напред, започвайки от 0). 17 | 18 | **Пример:** 19 | 20 | Вход: 21 | ```c++ 22 | 9 2 23 | ``` 24 | Изход: 25 | ```c++ 26 | 0 // 9 = 1001 27 | ``` 28 | 29 | **Задача 3:** Да се напише функция, която приема 2 цели числа *N* и *M* и връща стойността на числото *N* с променена стойност на бита на позиция *M* (отзад напред, започвайки от 0). 30 | 31 | *Бонус: да се направи да се подава като параметър стойността на бита, която да се запази* 32 | 33 | **Пример:** 34 | 35 | Вход: 36 | ```c++ 37 | 5 0 // Обръщаме нулевия (последния) бит на 101 -> 100 38 | ``` 39 | Изход: 40 | ```c++ 41 | 4 // Вече числото е 100 42 | ``` 43 | 44 | 45 | **Задача 4:** Да се напише функция, която приема 3 цели числа *x*, *m* и *n* и връща числото, което се получава, ако от *x* се вземат *n* бита, започвайки от позиция *m*. 46 | 47 | **Пример:** 48 | 49 | Вход: 50 | ```c++ 51 | 16 4 3 52 | ``` 53 | Изход: 54 | ```c++ 55 | 4 //16 = 10000 -> |100|00 -> 100 = 4 56 | ``` 57 | 58 | **Задача 5:** Да се напише функция, която приема число и връща кодирано същото число по ключ, избран от вас. Да се напише функция, която по код на число и ключ за кодиране връща оригиналното число. 59 | 60 | **Пример (Encode):** 61 | 62 | Вход: 63 | ```c++ 64 | Enter a number: 10 65 | ``` 66 | Изход: 67 | ```c++ 68 | Code: 51932 //you chose key 51926 69 | ``` 70 | 71 | **Пример (Decode):** 72 | 73 | Вход: 74 | ```c++ 75 | Enter number code: 51932 76 | Enter key: 51926 77 | ``` 78 | Изход: 79 | ```c++ 80 | Number: 10 81 | ``` 82 | 83 | **Задача 6:** Да се напише функция, която приема 2 цели числа *a* и *b* и разменя стойностите им без допълнителна променлива с побитови операции. 84 | 85 | **Пример:** 86 | 87 | Вход: 88 | ```c++ 89 | 5 7 90 | ``` 91 | Изход: 92 | ```c++ 93 | 7 5 94 | ``` 95 | 96 | **Задача 7:** Да се напише функция, която приема 2 цели числа a и b и проверява дали битовете на a са пермутация на битовете на b. 97 | 98 | **Пример:** 99 | 100 | Вход: 101 | ```c++ 102 | 7 11 103 | ``` 104 | Изход: 105 | ```c++ 106 | true 107 | ``` 108 | -------------------------------------------------------------------------------- /Sem. 09/tic-tac-toe.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const size_t N = 3; 5 | const unsigned PLAYERS_COUNT = 2; 6 | const unsigned LINE_LENGTH = 3; 7 | const char EMPTY_FIELD = ' '; 8 | 9 | void init(char field[][N], char ch) 10 | { 11 | for (int i = 0; i < N; i++) 12 | { 13 | for (int j = 0; j < N; j++) 14 | field[i][j] = ch; 15 | } 16 | } 17 | 18 | void print(const char field[][N]) 19 | { 20 | for (int i = 0; i < N; i++) 21 | { 22 | for (int j = 0; j < N; j++) 23 | cout << "[" << field[i][j] << "]"; 24 | cout << endl; 25 | } 26 | } 27 | 28 | bool areValidIndices(int x, int y) 29 | { 30 | return x >= 0 && y >= 0 && x < N && y < N; 31 | } 32 | 33 | void input(char field[][N], int& x, int& y) 34 | { 35 | cin >> x >> y; 36 | 37 | while (!areValidIndices(x,y) || field[x][y] != EMPTY_FIELD) 38 | { 39 | cin >> x >> y; 40 | } 41 | } 42 | 43 | int getConsecutiveDirection(const char field[][N], int row, int coll, int rowMove, int collMove) 44 | { 45 | char ch = field[row][coll]; 46 | 47 | int count = 0; 48 | while (areValidIndices(row, coll) && field[row][coll] == ch) 49 | { 50 | count++; 51 | row += rowMove; 52 | coll += collMove; 53 | } 54 | return count; 55 | } 56 | 57 | unsigned getConsecutiveOnLine(const char field[][N], int row, int coll, int rowMove, int collMove) 58 | { 59 | return getConsecutiveDirection(field, row, coll, rowMove, collMove) + 60 | getConsecutiveDirection(field, row, coll, -rowMove, -collMove) - 1;//because we count the starting point twice. 61 | } 62 | 63 | bool isWinningMove(const char field[][N], int row, int coll) 64 | { 65 | return getConsecutiveOnLine(field, row, coll, 0, 1) >= LINE_LENGTH || //horizontal 66 | getConsecutiveOnLine(field, row, coll, 1, 0) >= LINE_LENGTH || //vertical 67 | getConsecutiveOnLine(field, row, coll, 1, 1) >= LINE_LENGTH || //main diagonal 68 | getConsecutiveOnLine(field, row, coll, 1, -1) >= LINE_LENGTH; //anti diagonal 69 | } 70 | 71 | int main() 72 | { 73 | char field[N][N]; 74 | init(field, EMPTY_FIELD); 75 | print(field); 76 | 77 | bool haveWinner = false; 78 | for(int i = 0, currentPlayer = 0; i < N * N && !haveWinner; i++, (++currentPlayer) %= PLAYERS_COUNT) 79 | { 80 | int row, coll; 81 | input(field, row, coll); 82 | field[row][coll] = 'A' + currentPlayer; 83 | print(field); 84 | haveWinner = isWinningMove(field, row, coll); 85 | } 86 | cout << (haveWinner ? "Winner!" : "Draw!") << endl; 87 | } 88 | -------------------------------------------------------------------------------- /Retake/solutions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //task 1 5 | void print(int n) 6 | { 7 | if(n % 2 == 0) 8 | return; 9 | for (int i = 0; i < n; i++) //rows 10 | { 11 | int procenctCount = (i < n / 2) ? ((i + 1) * 2 - 1) : ((n - i) * 2 - 1); //two cases: first n/2 rows and the second n/2 rows 12 | int spacesCount = 2*n - 2 * procenctCount; 13 | for (int i = 0; i < procenctCount; i++) 14 | cout << "%"; 15 | for (int i = 0; i < spacesCount; i++) 16 | cout << " "; 17 | for (int i = 0; i < procenctCount; i++) 18 | cout << "%"; 19 | cout << endl; 20 | } 21 | } 22 | 23 | //task 2 24 | int* concat(const int* arr1, size_t size1, const int* arr2, size_t size2) 25 | { 26 | int* result = new int[size1 + size2]; 27 | for (int i = 0; i < size1; i++) 28 | result[i] = arr1[i]; 29 | for (int i = 0; i < size2; i++) 30 | result[size1 + i] = arr2[i]; 31 | return result; 32 | } 33 | bool isPalindrome(const int* arr, size_t size) 34 | { 35 | for (int i = 0; i < size / 2; i++) 36 | if (arr[i] != arr[size - i - 1]) 37 | return false; 38 | return true; 39 | } 40 | bool isConcatPalindrome(const int* arr1, size_t size1, const int* arr2, size_t size2) 41 | { 42 | int* concated = concat(arr1, size1, arr2, size2); 43 | bool result = isPalindrome(concated, size1 + size2); 44 | delete[] concated; 45 | return result; 46 | } 47 | 48 | //task 3 49 | bool isLetter(char ch) 50 | { 51 | return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'; 52 | } 53 | size_t countLetters(const char* str) 54 | { 55 | size_t count = 0; 56 | while (*str) 57 | { 58 | if (isLetter(*str)) 59 | count++; 60 | str++; 61 | } 62 | return count; 63 | } 64 | char* extractLetters(const char* str) 65 | { 66 | char* result = new char[countLetters(str) + 1]; 67 | int resIter = 0; 68 | while (*str) 69 | { 70 | if (isLetter(*str)) 71 | result[resIter++] = *str; 72 | str++; 73 | } 74 | result[resIter] = '\0'; 75 | return result; 76 | } 77 | //tests 78 | int main() 79 | { 80 | print(5); 81 | 82 | int arr[] = { 1,2,3,2 }; 83 | int arr2[] = { 1 }; 84 | cout << isConcatPalindrome(arr, 4, arr2, 1) << endl;; 85 | 86 | char test[] = "asd23213AFSDG1!"; 87 | char* res = extractLetters(test); 88 | cout << res << endl; 89 | delete[] res; 90 | } 91 | --------------------------------------------------------------------------------