├── ExamPrep ├── 01_CallByReference.cpp ├── 02_Exam1-4.cpp ├── 03_Exam5-7.cpp ├── 04_Exam17.cpp └── Usten izpit_UP_26_01_2019 (1).pdf ├── PreparationK1 ├── 01_ArrayWorkout.cpp ├── 02_Sequences.cpp └── Problems.pdf ├── PreparationK2 ├── 1.cpp ├── 2.cpp ├── 3.cpp ├── 4.cpp ├── 5.cpp ├── README.md └── zadachi.pdf ├── README.md ├── Sem01 └── sem01.cpp ├── Sem02 ├── TK.cpp ├── days_of_the_week.cpp ├── even_or_odd.cpp └── my_temperature.cpp ├── Sem03 ├── Bitwise_operators_exercises.cpp ├── Dividors_less_than_n.cpp ├── Factorial.cpp ├── GCD_Euclid.cpp ├── GCD_first.cpp ├── GCD_second.cpp ├── GCD_using_for.cpp ├── Intro_to_loops.cpp └── Sum_of_digits.cpp ├── Sem04 ├── 01_Scope_Easy.cpp ├── 02_Scope_Complex.cpp ├── 03_Ternary_Operator.cpp ├── 04_Power.cpp ├── 05_Binary_to_Decimal.cpp ├── 06_Decimal_to_Binary.cpp ├── 07_Nested_Loops_Construction.cpp └── 08_Pascals_Triangle.cpp ├── Sem05 ├── 01_Days_of_Month.cpp ├── 02_Days_of_Month.cpp ├── 03_Secondary_Diagonal.cpp ├── 04_Secondary_Diagonal.cpp ├── 05_Divisible_3digit_number.cpp ├── 06_Ascending_Digits.cpp ├── 07_Ascending_digits.cpp └── ТК.docx ├── Sem06 ├── 01_First_Occurance_of_an_Element.cpp ├── 02_All_Occurances_of_an_Element.cpp ├── 03_Double_Approximation.cpp ├── 04_Catalan_Formula.pdf └── 04_Catalan_Nmbers.cpp ├── Sem07 ├── 00_Functions_Examples.cpp ├── 01_Coefficients.cpp ├── 02_Smaller_Neighbours.cpp └── zadachi.pdf ├── Sem08 ├── 01_Pointers_and_Arrays.cpp ├── 02_Transpose_of_a_Matrix.cpp ├── 03_Row_vs_Column_Traversal.cpp └── 04_Spiral_Matrix.cpp ├── Sem09 ├── 01_Bubble_Sort.cpp ├── 02_Selection_Sort.cpp ├── 03_Insertion_Sort.cpp └── 04_Charcter_Arrays.cpp ├── Sem10 ├── 01_Capitalize.cpp ├── 02_Valid_Identificator.cpp ├── 03_isPalindrom.cpp ├── 04_NumbersInString.cpp ├── 05_ReplaceCharWithString.cpp ├── 06_Dynamic_Memory_Basics.cpp └── problems.pdf ├── Sem11 ├── 01_Dynamic_Memory.cpp ├── 02_Contains.cpp ├── 03_Character_array_recursion.cpp ├── README.md └── problems.pdf └── Sem12 ├── 01_K1.cpp ├── 02_Recursion.cpp ├── 03_Function_Overloading.cpp └── 04_Default_Parameters.cpp /ExamPrep/01_CallByReference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Pointers 5 | // Require additional memory 6 | // Dereferencing - * 7 | // Have a null value - nullptr 8 | 9 | // Reference 10 | // Cannot change the object it is referring to 11 | // Accessing value through identificator 12 | // Must be initialized upon declaration 13 | 14 | void f(int a) 15 | { 16 | a++; 17 | } 18 | 19 | void f2(int& a) 20 | { 21 | a++; 22 | } 23 | 24 | void f(int& a, int* b, int*& c, int d = 3) 25 | { 26 | int e = a + *b; // 10 27 | int f = *c - 1; // 15 - 1 = 14 28 | e /= d; // 3 29 | cout << "e: " << e << " &e: " << &e << endl; // 3 30 | cout << "f: " << f << " &f: " << &f << endl; // 14 31 | cout << "d: " << d << endl; // 3 32 | cout << "a: " << a << " &a: " << &a << endl; // 5 33 | cout << "b: " << *b << " &b: " << &b << endl; // 5 34 | cout << "c: " << *c << " &e: " << &c << endl; // 15 35 | } 36 | 37 | int main() 38 | { 39 | int a = 5; 40 | 41 | f(a); 42 | cout << a << endl; // 5 43 | f2(a); 44 | cout << a << endl; // 6 45 | 46 | a = 5; 47 | int b = 10, c = 15; 48 | int* ptr_a = &a; 49 | int* ptr_c = &c; 50 | 51 | f(a, ptr_a, ptr_c); 52 | cout << endl; 53 | cout << "a: " << a << " &a: " << ptr_a << endl; 54 | cout << "b: " << b << " &b: " << &b << endl; 55 | cout << "c: " << c << " &c: " << ptr_c << endl; 56 | cout << "c: " << c << " &ptr_c: " << &ptr_c << endl; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /ExamPrep/02_Exam1-4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int a = 5, b = 10, c = 15, d = 20, x = 25, x_coord = 0.5, y_coord = -2; 8 | 9 | // Задача 1 ------------------------------------------- 10 | double izr1 = 1 + x / (1 + x / (1 + (x / (1 + x)))); 11 | double izr2 = x * x + sqrt(1 + 2 * x + 3 * x * x); 12 | 13 | // Задача 2а ------------------------------------------ 14 | bool res = a > 0 || b > 0 || c > 0; 15 | 16 | // Задача 2б ------------------------------------------ 17 | bool isIn = false; 18 | if ((x_coord <= 2 && x_coord >= 0 && y_coord <= 0 && y_coord >= -2) || sqrt(x_coord * x_coord + y_coord * y_coord) <= 1) 19 | isIn = true; 20 | 21 | // Задача 3 ------------------------------------------- 22 | (a == b && a == c && b == c); 23 | (a == 0 || (a >= 5 && a <= 10)); 24 | (a > 1 && (a < 6 || a == 10)); 25 | 26 | // Задача 4 ------------------------------------------- 27 | x = !a || b || c; // a == f || b == t || c == t 28 | 29 | if (a) 30 | { 31 | if (b) 32 | x = true; 33 | else 34 | { 35 | if (c) 36 | x = true; 37 | else 38 | x = false; 39 | } 40 | } 41 | else 42 | x = true; 43 | 44 | x = (a) ? ((b) ? true : ((c) ? true : false)) true; 45 | } 46 | -------------------------------------------------------------------------------- /ExamPrep/03_Exam5-7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int min(int a, int b) 5 | { 6 | return (a > b) ? b : a; 7 | } 8 | 9 | int min(int a, int b, int c) 10 | { 11 | return min(a, min(b, c)); 12 | } 13 | 14 | int max(int a, int b) 15 | { 16 | return (a > b) ? a : b; 17 | } 18 | 19 | int max(int a, int b, int c, int d) 20 | { 21 | return max(max(a, b), max(c, d)); 22 | } 23 | 24 | int f6(int n) 25 | { 26 | int sum = 0; 27 | int counter = 2; 28 | 29 | for (int i = 1; i <= n; i++, counter++) 30 | { 31 | int curr_product = 1; 32 | for (int j = 0; j < counter; j++) 33 | curr_product *= (i + j); 34 | 35 | sum += curr_product; 36 | } 37 | return sum; 38 | } 39 | 40 | void f6_rec(int n, int beg, int end, int& sum) 41 | { 42 | if (end > n + n) 43 | return; 44 | 45 | int curr = 1; 46 | for (int i = beg; i <= end; i++) 47 | curr *= i; 48 | 49 | sum += curr; 50 | f6(n, beg + 1, end + 2, sum); 51 | } 52 | 53 | bool f7(double arr[], int size) 54 | { 55 | for (int i = 2; i < size; ++i) 56 | if (!((arr[i - 2] >= arr[i - 1] && arr[i - 1] <= arr[i]) || (arr[i - 2] <= arr[i - 1] && arr[i - 1] >= arr[i]))) 57 | return false; 58 | 59 | return true; 60 | } 61 | 62 | 63 | int main() 64 | { 65 | int a = 2, b = 3, c = 4, d = 5; 66 | 67 | // Задача 5 ------------------------------------------- 68 | int maxS = max(min(a, b, c), min(a, b, d), min(a, c, d), min(b, c, d)); 69 | 70 | // Задача 6 ------------------------------------------- 71 | cout << f6(5); 72 | 73 | int sum = 0; 74 | f6_rec(5, 1, 2, sum); 75 | cout << sum; 76 | 77 | // Задача 7 ------------------------------------------- 78 | const int SIZE = 16; 79 | double arr[SIZE] = {1, 4, 2, 19, 6, 7, 5, 22, 10, 30, 2, 34, 12, 15, 11, 16}; 80 | cout << f7(arr, SIZE); 81 | 82 | } 83 | -------------------------------------------------------------------------------- /ExamPrep/04_Exam17.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void func(int& x, int* y, int z) // x = b4 , y = &a, z = 4 5 | { 6 | int a = 3; 7 | int b = 5; 8 | x = z - b; // 4 - 5 = -1 9 | *y = 2 * z + 3 * x; // 2 * 4 - 3 = 5 10 | z = a + *y; // 3 + 5 = 8 11 | cout << "func: x = " << x << endl; // -1 12 | cout << "func: y = " << *y << endl; // 5 13 | cout << "func: z = " << z << endl; // 8 14 | cout << "func: a = " << a << endl; // 3 15 | cout << "func: b = " << b << endl; // 5 16 | return; 17 | } 18 | 19 | int main() 20 | { int a = 2; 21 | int b = 4; 22 | func(b, &a, b); 23 | cout << "main: a = " << a << endl; 24 | cout << "main: b = " << b << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /ExamPrep/Usten izpit_UP_26_01_2019 (1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/ExamPrep/Usten izpit_UP_26_01_2019 (1).pdf -------------------------------------------------------------------------------- /PreparationK1/01_ArrayWorkout.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void inputArray(int arr[], int size) 6 | { 7 | for (int i = 0; i < size; i++) 8 | cin >> arr[i]; 9 | } 10 | 11 | void removeElementAtIndex(int arr[], int size, int index) 12 | { 13 | if (index == size) 14 | return; 15 | 16 | for (int i = index + 1; i < size; i++) 17 | arr[i - 1] = arr[i]; 18 | } 19 | 20 | void insertElementAfterIndex(int arr[], int size, int element, int index) 21 | { 22 | for (int i = size; i > index; i--) 23 | arr[i] = arr[i - 1]; 24 | 25 | arr[index + 1] = element; 26 | } 27 | 28 | void printArray(int arr[], int size) 29 | { 30 | for (int i = 0; i < size; i++) 31 | cout << arr[i] << ' '; 32 | cout << endl; 33 | } 34 | 35 | bool isSaw(int arr[], int size) 36 | { 37 | bool isSaw = true; 38 | 39 | for (int i = size - 1; i > 0; i--) 40 | { 41 | if (!((arr[i + 1] >= arr[i] && arr[i] <= arr[i - 1]) || (arr[i + 1] <= arr[i] && arr[i] >= arr[i - 1]))) 42 | return false; 43 | } 44 | return true; 45 | } 46 | 47 | int main() 48 | { 49 | const int size = 6; 50 | int arr[size] = {1, 2, 0, 4, 3, 6}; 51 | 52 | // making a copy of an array 53 | int copy[size]; 54 | for (int i = 0; i < size; i++) 55 | copy[i] = arr[i]; 56 | 57 | cout << "This is a copy of our array: "; 58 | printArray(copy, size); 59 | 60 | cout << "Please enter the index of the element you would like to remove: "; 61 | int i; 62 | cin >> i; 63 | if (i < 0 || i > 5) 64 | { 65 | cout << "Invalid index. Romving last element..." << endl; 66 | i = 5; 67 | } 68 | 69 | cout << "The element to be removed is: " << arr[i] << endl; 70 | removeElementAtIndex(arr, size, i); 71 | 72 | cout << "The array after we have removed the element looks like this: "; 73 | printArray(arr, size - 1); 74 | 75 | cout << "Please enter a number you would like to insert into the array: "; 76 | int X; 77 | cin >> X; 78 | 79 | cout << "Please enter the index of the element after which you would like to insert your element: "; 80 | int j; 81 | cin >> j; 82 | if (j < 0 || j > 4) 83 | { 84 | cout << "Invalid index. Inserting element at the last position..." << endl; 85 | j = 4; 86 | } 87 | 88 | cout << "The array after we have inserted the element looks like this: "; 89 | insertElementAfterIndex(arr, size - 1, X, j); 90 | printArray(arr, size); 91 | 92 | if (isSaw(arr, size)) 93 | cout << "The array is \"saw\"" << endl; 94 | else 95 | cout << "The array is not \"saw\"" << endl; 96 | } -------------------------------------------------------------------------------- /PreparationK1/02_Sequences.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int SIZE = 100; 7 | int arr1[SIZE], arr2[SIZE]; 8 | 9 | int n, m; 10 | cin >> n; 11 | for (int i = 0; i < n; i++) 12 | cin >> arr1[i]; 13 | 14 | cin >> m; 15 | for (int i = 0; i < m; i++) 16 | cin >> arr2[i]; 17 | 18 | int index = -1; 19 | int traversedIndex = m - 1; 20 | for (int i = n - 1; i >= 0; i--) 21 | { 22 | bool hasOccured = false; 23 | while (traversedIndex >= 0) 24 | { 25 | if (arr2[traversedIndex] > arr1[i]) 26 | { 27 | traversedIndex--; 28 | continue; 29 | } 30 | 31 | if (arr2[traversedIndex] == arr1[i]) 32 | { 33 | hasOccured = true; 34 | traversedIndex--; 35 | break; 36 | } 37 | 38 | if (arr2[traversedIndex] < arr1[i]) 39 | { 40 | index = i; 41 | break; 42 | } 43 | } 44 | 45 | if (hasOccured) 46 | continue; 47 | index = i; 48 | } 49 | 50 | if (index == -1) 51 | cout << "There is no such element" << endl; 52 | 53 | else 54 | cout << index; 55 | } -------------------------------------------------------------------------------- /PreparationK1/Problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/PreparationK1/Problems.pdf -------------------------------------------------------------------------------- /PreparationK2/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int* arr, int i, int j) 5 | { 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | void insertionSort(int* arr, int len) 12 | { 13 | for (int i = 1; i < len; i++) 14 | { 15 | int element = arr[i]; 16 | int j = i - 1; 17 | while (j >= 0 && arr[j] > element) 18 | { 19 | arr[j + 1] = arr[j]; 20 | j--; 21 | } 22 | arr[j + 1] = element; 23 | } 24 | } 25 | 26 | bool permutation(int* arr1, int* arr2, int k) 27 | { 28 | for (int i = 0; i < k; i++) 29 | { 30 | if (arr1[i] != arr2[i]) 31 | return false; 32 | } 33 | 34 | return true; 35 | } 36 | 37 | bool permutationRecursive(int* arr1, int* arr2, int k) 38 | { 39 | if (k < 0) 40 | return true; 41 | 42 | return arr1[k] == arr2[k] && permutationRecursive(arr1, arr2, k - 1); 43 | } 44 | 45 | int main() 46 | { 47 | const int k = 6; 48 | int arr1[] = { 1,2,3,7,5,4 }; 49 | int arr2[] = { 1,0,2,7,5,4 }; 50 | 51 | /*int* arr1 = new int[k]; 52 | for (int i = 0; i < k; i++) 53 | cin >> arr1[i]; 54 | 55 | int* arr2 = new int[k]; 56 | for (int i = 0; i < k; i++) 57 | cin >> arr2[i];*/ 58 | 59 | insertionSort(arr1, k); 60 | insertionSort(arr2, k); 61 | 62 | cout << permutation(arr1, arr2, k); 63 | cout << permutationRecursive(arr1, arr2, k); 64 | } -------------------------------------------------------------------------------- /PreparationK2/2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool helper(char** arr, int i, int j, char* str, bool isRow) 6 | { 7 | int strIndex = 1; 8 | while (str[strIndex] != '\0' && str[strIndex] == arr[i][j]) 9 | { 10 | strIndex++; 11 | if (isRow) 12 | j++; 13 | else 14 | i++; 15 | } 16 | return strIndex == strlen(str); 17 | } 18 | 19 | bool contains(char** arr, int size, char* str) 20 | { 21 | int lastElement = size - strlen(str); 22 | 23 | for (int i = 0; i < size; i++) 24 | { 25 | for (int j = 0; j <= lastElement; j++) 26 | { 27 | if (arr[i][j] == str[0]) 28 | { 29 | if (helper(arr, i, j + 1, str, 1)) 30 | return true; 31 | } 32 | } 33 | 34 | for (int j = 0; j <= lastElement; j++) 35 | { 36 | if (arr[j][i] == str[0]) 37 | { 38 | if (helper(arr, j + 1, i, str, 0)) 39 | return true; 40 | } 41 | } 42 | } 43 | 44 | return false; 45 | } 46 | 47 | int main() 48 | { 49 | char** arr = new char*[5]; 50 | 51 | for (int i = 0; i < 5; i++) 52 | { 53 | arr[i] = new char[5]; 54 | for (int j = 0; j < 5; j++) 55 | cin >> arr[i][j]; 56 | } 57 | 58 | cin.ignore(); 59 | char* str = new char[6]; 60 | cin.getline(str, 5); 61 | 62 | cout << contains(arr, 5, str); 63 | delete[] str; 64 | 65 | for (int i = 0; i < 5; i++) 66 | delete[] arr[i]; 67 | 68 | delete[] arr; 69 | } 70 | -------------------------------------------------------------------------------- /PreparationK2/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int* arr, int i, int j) 5 | { 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | void bubbleSortRec(int* arr, int size) 12 | { 13 | if (size == 1) 14 | return; 15 | 16 | for (int i = 0; i < size - 1; i++) 17 | if (arr[i] > arr[i + 1]) 18 | swap(arr, i, i + 1); 19 | 20 | bubbleSortRec(arr, size - 1); 21 | } 22 | 23 | int main() 24 | { 25 | int arr[] = { 6, 4, 3 ,1, 7 }; 26 | bubbleSortRec(arr, 5); 27 | for (int i = 0; i < 5; i++) 28 | cout << arr[i] << ' '; 29 | } -------------------------------------------------------------------------------- /PreparationK2/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void insertionSortRec(int* arr, int size) 5 | { 6 | if (size == 1) 7 | return; 8 | 9 | insertionSortRec(arr, size - 1); 10 | 11 | int current = arr[size - 1]; 12 | int index = size - 2; 13 | 14 | while (index >= 0 && arr[index] > current) 15 | { 16 | arr[index + 1] = arr[index]; 17 | index--; 18 | } 19 | arr[index + 1] = current; 20 | } 21 | 22 | int main() 23 | { 24 | int arr[] = { 6, 4, 3 ,1, 7 }; 25 | insertionSortRec(arr, 5); 26 | for (int i = 0; i < 5; i++) 27 | cout << arr[i] << ' '; 28 | } -------------------------------------------------------------------------------- /PreparationK2/5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool primeCheck(int n, int square) 6 | { 7 | if (square == 1) 8 | return true; 9 | 10 | if (n % square == 0) 11 | return false; 12 | 13 | return primeCheck(n, --square); 14 | } 15 | 16 | int main() 17 | { 18 | int num = 0; 19 | 20 | do { 21 | cin >> num; 22 | } while (num <= 1); 23 | 24 | cout << primeCheck(num, (int)sqrt(num)); 25 | } -------------------------------------------------------------------------------- /PreparationK2/README.md: -------------------------------------------------------------------------------- 1 | **Задачи:** https://drive.google.com/drive/u/3/folders/1s_bMV7OQiJhE5BvHDp3tHMRsJx7Ua0Bp -------------------------------------------------------------------------------- /PreparationK2/zadachi.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/PreparationK2/zadachi.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Увод в програмирането 2020-2021 2 | 3 | **1.** Типове данни. Променливи и константи. Преобразуване - явно и неявно. Оператори. 4 | 5 | **2.** Условни оператори - if, else и switch. 6 | 7 | **3.** Побитови оператори. Итерации while, do-while, for. Задачи. 8 | 9 | **4.** Scope. Тернарен оператор. Още задачи върху цикли. Вложени цикли. 10 | 11 | **5.** break; continue; return; Още задачи върху цикли. 12 | 13 | **6.** Масиви. Задачи върху масиви. 14 | 15 | **7.:** Функции. По-трудни задачи върху масиви. 16 | 17 | **8.** Подготовка за контролно 1. 18 | 19 | **9.** Пойнтъри. Многомерни масиви - двумерни. Обхождания на двумерни масиви. 20 | 21 | **10.** Алгоритми за сортиране: Bubble sort, Selection sort, Insertion sort. Увод в низове. 22 | 23 | **11.** Задачи върху низове. Динамична памет. 24 | 25 | **12.** Динамична памет 2. Рекурсия. 26 | 27 | **13.** Подготовка за контролно 2. 28 | 29 | **14.** Function overloading. Дефолтни параметри. Още упражнение върху рекурсия. 30 | 31 | **15.** Референции. Подготовка за изпит. 32 | -------------------------------------------------------------------------------- /Sem01/sem01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num; // Декларация на променлива (казваме, че съществува) 7 | int a = 5; // Инициализация на променлива (даваме й стойност) 8 | int b = 2; 9 | 10 | // Аритметични оператори 11 | int difference = a - b; // difference = 5 - 2 12 | cout << difference << endl; // 3 13 | cout << a + b << endl; // 7 14 | cout << a * b << endl; // 10 15 | cout << a / b << endl; // 2 16 | int rem = a % b; // 17 | cout << rem << endl; // 1 18 | cout << endl; // Принтира нов ред 19 | 20 | // ++ и -- като префиксен и постфиксен оператор 21 | // Префиксен - инкрементира и връща *новата* стойност 22 | // Постфиксен - инкрементира и връща *старата* стойност 23 | a = 5; 24 | a++; // 6 25 | a--; // 5 26 | 27 | cout << a++ << endl; // 5 28 | cout << ++a << endl; // 7 29 | cout << --a << endl; // 6 30 | cout << a-- << endl; // 6 31 | cout << endl; 32 | 33 | // Type casting 34 | int a1 = 65; 35 | bool b1 = a1; 36 | char c1 = a1; 37 | double d1 = a1; 38 | 39 | cout << a1 << endl; // 65 40 | cout << b1 << endl; // 1 41 | cout << c1 << endl; // A 42 | cout << d1 << endl; // 65.0 43 | cout << endl; 44 | 45 | double d2 = 97.5; 46 | int a2 = d2; 47 | bool b2 = d2; 48 | char c2 = d2; 49 | 50 | cout << a2 << endl; // 97 51 | cout << b2 << endl; // 1 52 | cout << c2 << endl; // a 53 | cout << d2 << endl; // 97.5 54 | cout << endl; 55 | 56 | // Оператори за присвояване 57 | int c, d, e, f; 58 | cout << (a = b = c = d = e = f = 5) << endl; // Защо оператор = връща стойност 59 | cout << (a += 5) << endl; // a = a + 5; => a = 10; 60 | cout << a << endl; 61 | cout << (a -= 5) << endl; // a = a - 5; => a = 5 62 | cout << a << endl; 63 | cout << (a *= 5) << endl; // a = a * 5; => a = 25 64 | cout << a << endl; 65 | cout << (a /= 5) << endl; // a = a / 5; => a = 5 66 | cout << (a %= 5) << endl; // a = a % 5; => a = 0; 67 | cout << endl; 68 | 69 | // Оператори за сравнение (връщат булева стойност - true/false) 70 | a = 2; 71 | b = 3; 72 | c = 3; 73 | 74 | cout << (a == b) << endl; // 0 75 | cout << (a != b) << endl; // 1 76 | cout << (a <= b) << endl; // 1 77 | cout << (a >= c) << endl; // 0 78 | cout << (a < b) << endl; // 1 79 | cout << endl; 80 | 81 | // Побитови оператори - работят върху двоичното представяне на числото 82 | cout << a << " " << (a << 1) << endl; // 0010 => 0100 (При << всички символи се изместват наляво с дадения брой позиции) 83 | a <<= 1; // еквивалентно на: а = а << 1; 84 | cout << a << " " << (a << 1) << endl; // 0100 => 1000 85 | cout << a << " " << (a >> 2) << endl; // 0100 => 0001 (При >> всички символи се изместват надясно с дадения брой позиции) 86 | b = 5; 87 | cout << a << " " << (a | b) << endl; // 0100 => (0100 | 0101) = 0101 (на позициите, на които имаме поне една единица се запазва) 88 | cout << a << " " << (a & b) << endl; // 0100 => (0100 & 0101) = 0100 (на позициите, на които имаме 2 единици се запазва) 89 | } 90 | -------------------------------------------------------------------------------- /Sem02/TK.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a = 68; 7 | char c = 'C'; // Hint: 'A' = 65 8 | double d = 97.5; 9 | bool b = false; 10 | 11 | int output1 = d + 10.2; // 107 12 | /* 13 | 97.5 + 10.2 = 107.7, но тъй като тази стойност се присвоява на променлива от целочислен тип, 14 | се извършва неявно преобразуване и частта след десетичната запетайка се "изрязва". 15 | */ 16 | 17 | int output2 = (int)d + 10.7; // 107 18 | /* 19 | Първо правим явно преобразуване на d към целочислен тип и след това събираме с 10.7, 20 | тоест получаваме 97 + 10.7 = 107.7. Output2 е от тип, който очаква цяло число, така че 21 | всичко след десетичната запетая се игнорира. 22 | */ 23 | 24 | int output3 = c; // 67 25 | /* 26 | След като 'А' = 65 и в ASCII таблицата главните латински букви са записани поредно, 27 | то 'C' = 67. Оutput3 е от целочислен тип, значи се взима числовата стойност на символа. 28 | */ 29 | 30 | int output4 = 'F' + b; // 70 31 | /* 32 | След като 'А' = 65, то 'F' = 70. Променливата b е инициализирана със стойност false, 33 | която се изразява с 0, тоест 70 + 0 = 70. 34 | */ 35 | 36 | int output5 = (b + 1); // 1 37 | /* 38 | Променливата b има стойност 0, защото е false от инициализацията, следователно 0 + 1 = 1. 39 | */ 40 | 41 | int output6 = (a -= 1); // 67 42 | /* 43 | Изразът (а -= 1) е еквивалентен запис на (а = а - 1). Ние променяме стойността на а и 44 | тя става а = 68 - 1, тоест вече а = 67. Операторът за присвояване е дясноасоциативен, 45 | значи променя стойността и връща като нова стойност левия си операнд. 46 | */ 47 | 48 | char output7 = (a + b % 2); // C 49 | /* 50 | Променливата а има стойност 67, тъй като в output6 -> (а -= 1) задава нова стойност на нашата променлива. 51 | Булевата променлвиа b има стойност false, тоест 0. Следователно изразът ни има стойност (67 + 0 % 2). 52 | Операторът % е с по-висок приоритет от +, значи поредността на изпълнение на операциите е 53 | (67 + (0 % 2)) = (67 + 0) = 67. Присвоявайки тази стойност на променлива от символен тип, 54 | числото се замества със символа, на който отговоря, което в случая на 67 е 'C'. 55 | */ 56 | 57 | char output8 = (b += 5) + 'A'; // B 58 | /* 59 | Променливата b e от булев тип, а при него всичко != 0 се смята за 1. При b += 5 имаме b = b + 5, тоест 60 | b = 0 + 5. Но макар че 0 + 5 = 5, 5 != 0, значи имаме b = 1. 61 | Така целият израз е равен на (1 + 65) = 66. Тази стойност се присвоява на променлива от символен тип, значи 62 | трябва да я заместим със символ. Взимайки предвид, че 'А' = 65, то 66 трябва да е 'B'. 63 | */ 64 | 65 | double output9 = a / 100; // 0 66 | /* 67 | Променливата а е от целочислен тип. При деление на две цели числа резултатът е цяло число, независимо че се 68 | присвоява на променлива от тип double. (67 / 100) = 0. 69 | Ако имахме израза: 70 | double output9 = (double)a / 100; можете да тествате какво ще се получи. За да бъде резултатът дробно число, 71 | трябва поне едно от числата да е от такъв тип. 72 | */ 73 | 74 | double output10 = (d / 10); // 9.75 75 | /* 76 | Променливата d е от тип за реално число, тоест след делене пак ще получим реално число. Output10 също е от реален 77 | тип, така че данни не се губят. (97.5 / 10) = 9.75. 78 | */ 79 | 80 | cout << output1 << endl; 81 | cout << output2 << endl; 82 | cout << output3 << endl; 83 | cout << output4 << endl; 84 | cout << output5 << endl; 85 | cout << output6 << endl; 86 | cout << output7 << endl; 87 | cout << output8 << endl; 88 | cout << output9 << endl; 89 | cout << output10 << endl; 90 | 91 | return 0; 92 | } -------------------------------------------------------------------------------- /Sem02/days_of_the_week.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int day; // input 1-7 7 | cin >> day; 8 | 9 | // Грешно решение! Дори да намери деня (например при day = 2), пак ще мине през всички останали проверки 10 | if (day == 1) 11 | cout << "Monday" << endl; 12 | if (day == 2) 13 | cout << "Tuesday" << endl; 14 | if (day == 3) 15 | cout << "Wednesday" << endl; 16 | if (day == 4) 17 | cout << "Thursday" << endl; 18 | if (day == 5) 19 | cout << "Friday" << endl; 20 | if (day == 6) 21 | cout << "Saturday" << endl; 22 | if (day == 7) 23 | cout << "Sunday" << endl; 24 | 25 | // По-добро решение. 26 | // В случай, че имаме невалиден вход ще изведе подходящо съобщение. 27 | if (day == 1) 28 | cout << "Monday" << endl; 29 | else if (day == 2) 30 | cout << "Tuesday" << endl; 31 | else if (day == 3) 32 | cout << "Wednesday" << endl; 33 | else if (day == 4) 34 | cout << "Thursday" << endl; 35 | else if (day == 5) 36 | cout << "Friday" << endl; 37 | else if (day == 6) 38 | cout << "Saturday" << endl; 39 | else if (day == 7) 40 | cout << "Sunday" << endl; 41 | else 42 | cout << "Invalid day" << endl; 43 | 44 | // Вариант за решение със switch 45 | switch (day) 46 | { 47 | case 1: cout << "Monday" << endl; break; 48 | case 2: cout << "Tuesday" << endl; break; 49 | case 3: cout << "Wednesday" << endl; break; 50 | case 4: cout << "Thursday" << endl; break; 51 | case 5: cout << "Friday" << endl; break; 52 | case 6: cout << "Saturday" << endl; break; 53 | case 7: cout << "Sunday" << endl; break; 54 | default: cout << "Invalid day" << endl; 55 | } 56 | 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /Sem02/even_or_odd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // Задача: Извежда дали числото между 1 и 10 е четно или нечетно 6 | int number; 7 | cout << "Please enter a number between 1 and 10" << endl; 8 | cin >> number; 9 | 10 | // Наивен подход 11 | switch (number) 12 | { 13 | case 1: cout << "Hello, odd!" << endl; break; 14 | case 2: cout << "Hello, even!" << endl; break; 15 | case 3: cout << "Hello, odd!" << endl; break; 16 | case 4: cout << "Hello, even!" << endl; break; 17 | case 5: cout << "Hello, odd!" << endl; break; 18 | case 6: cout << "Hello, even!" << endl; break; 19 | case 7: cout << "Hello, odd!" << endl; break; 20 | case 8: cout << "Hello, even!" << endl; break; 21 | case 9: cout << "Hello, odd!" << endl; break; 22 | case 10: cout << "Hello, even!" << endl; break; 23 | default: cout << "Number out of bounds!" << endl; 24 | } 25 | 26 | // Няма нужда константите, с които сравняваме да са в нарастващ ред, но е хубаво кодът ни да е лесно четим 27 | // Ако няма ключова дума break ще продължи изпълнението на всичко надолу докато не стигне break или края на блока 28 | switch (number) 29 | { 30 | case 1: 31 | case 3: 32 | case 5: 33 | case 7: 34 | case 9: cout << "Hello, odd!" << endl; break; 35 | case 2: 36 | case 4: 37 | case 6: 38 | case 8: 39 | case 10: cout << "Hello, even!" << endl; break; 40 | default: cout << "Number out of bounds!" << endl; 41 | } 42 | 43 | // Грешно решение. В case-овете на switch очакваме да имаме константи. n не може да участва. 44 | switch (number) 45 | { 46 | case (n % 2 == 0): cout << "Hello, even!" << endl; break; 47 | case (n % 2 == 1): cout << "Hello, odd!" << endl; break; 48 | default: cout << "Index out of bounds!" << endl; 49 | } 50 | 51 | // Решение с вложен if 52 | if (number >= 1 && number <= 10) 53 | { 54 | if (number % 2 == 0) 55 | cout << "Hello, even!" << endl; 56 | else 57 | cout << "Hello, odd!" << endl; 58 | } 59 | else 60 | { 61 | cout << "Number out of bounds!" << endl; 62 | } 63 | 64 | // Решение с else-if 65 | if (!(number >= 1 && number <= 10)) 66 | cout << "Number out of bounds!" << endl; 67 | else if (number % 2 == 0) 68 | cout << "Hello, even!" << endl; 69 | else 70 | cout << "Hello, odd!" << endl; 71 | 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /Sem02/my_temperature.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | unsigned my_temperature; 7 | cin >> my_temperature; 8 | 9 | // Вариант с проверка с логическо И (и двата израза трябва да са истина, за да бъде целият израз за if истина) 10 | if (my_temperature > 35 && my_temperature < 37) 11 | { 12 | cout << "Please enter!" << endl; 13 | } 14 | else 15 | { 16 | cout << "Please do not enter!" << endl; 17 | } 18 | 19 | // Вариант с проверка с логическо ИЛИ => (поне едното трябва да е истина, за да бъде целият израз за if истина) 20 | if (my_temperature < 35 || my_temperature > 37) 21 | { 22 | cout << "Please do not enter!" << endl; 23 | } 24 | else 25 | { 26 | cout << "Please enter!" << endl; 27 | } 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Sem03/Bitwise_operators_exercises.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* 7 | & - побитово и 8 | | - побитово или 9 | ^ - побитово изключващо или 10 | ~ - побитово отрицание 11 | << - шифт наляво 12 | >> - шифт надясно 13 | */ 14 | 15 | // Разлика между & и && 16 | int temp1 = 2, temp2 = 5; 17 | cout << (temp1 & temp2) << endl; 18 | cout << (temp1 && temp2) << endl; 19 | 20 | // зад. 1: Вход - целочислено число; n-тия бит на числото 21 | // Изход - какъв е n-тия бит 22 | unsigned int n, nBit; 23 | cin >> n >> nBit; 24 | cout << ((n >> nBit) | 0) << endl; // 1-ви начин 25 | cout << ((n >> nBit) & 1) << endl; // 2-ри начин 26 | 27 | // зад. 2: Смяна на стойностите на 2 числа чрез побитови оператори 28 | int num1, num2; 29 | cin >> num1 >> num2; 30 | 31 | num1 ^= num2; 32 | num2 ^= num1; 33 | num1 ^= num2; 34 | 35 | cout << num1 << ' ' << num2 << endl; 36 | } -------------------------------------------------------------------------------- /Sem03/Dividors_less_than_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n; 7 | cin >> n; 8 | 9 | int limit = n / 2; 10 | for (int i = 1; i <= limit; i++) 11 | { 12 | if (n % i == 0) 13 | { 14 | cout << i << ' ' << endl; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Sem03/Factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num; 7 | cin >> num; 8 | 9 | // 1-ви начин 10 | int fact = 1; // 1 - неутрален елемент 11 | for (int i = 0; i < num; i++) 12 | { 13 | fact *= (num - i); 14 | } 15 | cout << fact << endl; 16 | 17 | // 2-ри начин 18 | fact = 1; 19 | for (int i = 1; i <= num; i++) 20 | { 21 | fact *= i; 22 | } 23 | cout << fact; 24 | } -------------------------------------------------------------------------------- /Sem03/GCD_Euclid.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num1, num2; 7 | cin >> num1 >> num2; 8 | 9 | if (num2 > num1) // always num1 > num2 10 | { 11 | int temp = num1; 12 | num1 = num2; 13 | num2 = temp; 14 | } 15 | 16 | // 106 % 16 = (10) 17 | // 16 % 10 = (6) 18 | // 10 % 6 = 4 19 | // 6 % 4 = 2 20 | // 4 % 2 = 0 21 | 22 | while (num2 != 0) 23 | { 24 | int div = (num1 % num2); 25 | num1 = num2; 26 | num2 = div; 27 | } 28 | 29 | cout << num1 << endl; 30 | } -------------------------------------------------------------------------------- /Sem03/GCD_first.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num1, num2; 7 | cin >> num1 >> num2; 8 | 9 | while (num1 != num2) 10 | { 11 | if (num1 > num2) 12 | num1 -= num2; 13 | else 14 | num2 -= num1; 15 | } 16 | 17 | cout << num1 << endl; 18 | } -------------------------------------------------------------------------------- /Sem03/GCD_second.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num1, num2; 7 | cin >> num1 >> num2; 8 | 9 | if (num2 > num1) // always num1 > num2 10 | { 11 | int temp = num1; 12 | num1 = num2; 13 | num2 = temp; 14 | } 15 | 16 | int divisor = num2; 17 | bool foundSolution = false; 18 | 19 | while (!foundSolution) 20 | { 21 | if (num1 % divisor == 0 && num2 % divisor == 0) 22 | foundSolution = true; 23 | else 24 | divisor--; 25 | } 26 | cout << divisor << endl; 27 | } -------------------------------------------------------------------------------- /Sem03/GCD_using_for.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int num1, num2; 7 | int gcd = 1; 8 | 9 | cin >> num1 >> num2; 10 | 11 | for (int i = 1; i <= num1 && i <= num2; i++) 12 | { 13 | if (num1 % i == 0 && num2 % i == 0) 14 | { 15 | gcd = i; 16 | } 17 | } 18 | cout << gcd << endl; 19 | } -------------------------------------------------------------------------------- /Sem03/Intro_to_loops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Задача: Сума на 5 числа 7 | 8 | // Без цикъл 9 | int n1, n2, n3, n4, n5; 10 | cout << "Input a number: " << endl; 11 | cin >> n1; 12 | cout << "Input a number: " << endl; 13 | cin >> n2; 14 | cout << "Input a number: " << endl; 15 | cin >> n3; 16 | cout << "Input a number: " << endl; 17 | cin >> n4; 18 | cout << "Input a number: " << endl; 19 | cin >> n5; 20 | cout << "Sum: " << (n1 + n2 + n3 + n4 + n5) << endl; 21 | 22 | // Итерация while, но без горна граница за брой числа 23 | int number1; 24 | cout << "Input a number: " << endl; // * 25 | cin >> number1; 26 | int sum1 = 0; 27 | while ( number1 != 0) 28 | { 29 | sum1 += number1; 30 | cout << "Input a number: " << endl; // * повторение на код преди и в цикъла 31 | cin >> number1; 32 | } 33 | cout << sum1 << endl; 34 | 35 | // Итерация do-while - без повторението от while и с горна граница 36 | int number2; 37 | int sum2 = 0; 38 | int maxNum = 5; 39 | do { 40 | cout << "Input a number: " << endl; 41 | cin >> number2; 42 | sum2 += number2; 43 | } while (--maxNum > 0); 44 | cout << sum2; 45 | 46 | // Итерация for 47 | int number3, sum3 = 0; 48 | for (int i = 0; i < 5; i++) 49 | { 50 | cout << "Input a number: " << endl; 51 | cin >> number3; 52 | sum3 += number3; 53 | } 54 | cout << sum3 << endl; 55 | } -------------------------------------------------------------------------------- /Sem03/Sum_of_digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int number, sumDigits = 0; 7 | cin >> number; 8 | 9 | while (number != 0) 10 | { 11 | int lastDigit = number % 10; 12 | sumDigits += lastDigit; 13 | number /= 10; 14 | } 15 | 16 | cout << "Sum of digits: " << sumDigits << endl; 17 | 18 | } -------------------------------------------------------------------------------- /Sem04/01_Scope_Easy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Example 1. Global variables without initialization 5 | int a; 6 | char c; 7 | bool b; 8 | 9 | int i = 7; 10 | 11 | int main() 12 | { 13 | cout << i << ' '; 14 | int i = 6; 15 | i = i + 1; 16 | 17 | { 18 | int i = -1; 19 | i = i + 1; 20 | cout << i << ' '; 21 | } 22 | 23 | cout << i << endl; 24 | cout << a << c << b << endl; // a = 0, c = ' ', b = 0 25 | } -------------------------------------------------------------------------------- /Sem04/02_Scope_Complex.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int i; 5 | 6 | int main() 7 | { 8 | int i; 9 | i = 5; 10 | { 11 | int j, i; 12 | j = 1; 13 | i = 0; 14 | { 15 | int k, i; 16 | i = -1; 17 | j = 6; 18 | k = 2; 19 | { 20 | int j; 21 | j = 2; 22 | i = 1; 23 | } 24 | } 25 | cout << j << ' '; // 6; Не е равно на 1, защото на ред 17 вече сме й променили стойността. 26 | } 27 | 28 | cout << i << endl; // 5 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Sem04/03_Ternary_Operator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a, b; 7 | cin >> a >> b; 8 | 9 | cout << "If: "; 10 | if (a > b) 11 | cout << a << endl; 12 | 13 | else 14 | cout << b << endl; 15 | 16 | 17 | cout << "Ternary operator: " << ((a > b || a == b) ? a : b); // (booleanExpr) ? T : F 18 | 19 | } -------------------------------------------------------------------------------- /Sem04/04_Power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int number, power; 7 | cin >> number >> power; 8 | int result = 1; 9 | 10 | for (int i = 0; i < power; i++) 11 | { 12 | result = result * number; // result *= number; 13 | } 14 | 15 | cout << result; 16 | } -------------------------------------------------------------------------------- /Sem04/05_Binary_to_Decimal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | //Algorithm: 101101 = 2^0 + 2^2 + 2^3 + 2^5 = 1 + 4 + 8 + 32 = 45 7 | 8 | int number; 9 | cin >> number; 10 | int result = 0; 11 | int coef = 1; 12 | 13 | while (number != 0) 14 | { 15 | int lastDigit = number % 10; 16 | result = result + lastDigit * coef; // result += (lastDigit * coef); 17 | coef = coef * 2; // coef *= 2; 18 | number = number / 10; 19 | } 20 | 21 | cout << result << '\n'; 22 | 23 | } -------------------------------------------------------------------------------- /Sem04/06_Decimal_to_Binary.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /* Algorithm: 7 | 2 / 2 = 1 (0) 8 | 1 / 2 = 0 (1) 9 | => 10 10 | 11 | 8 / 2 = 4 (0) 12 | 4 / 2 = 2 (0) 13 | 2 / 2 = 1 (0) 14 | 1 / 2 = 0 (1) 15 | => 1000 16 | 17 | 45 / 2 = 22 (1) 18 | 22 / 2 = 11 (0) 19 | 11 / 2 = 5 (1) 20 | 5 / 2 = 2 (1) 21 | 2 / 2 = 1 (0) 22 | 1 / 2 = 0 (1) 23 | => 101101 24 | */ 25 | 26 | int number; 27 | cin >> number; 28 | int binary = 0; 29 | int coef = 1; 30 | 31 | while (number > 0) 32 | { 33 | int lastDigitForFirst = number % 2; 34 | binary = binary + lastDigitForFirst * coef; 35 | coef = coef * 10; 36 | number = number / 2; 37 | } 38 | 39 | cout << binary; 40 | } -------------------------------------------------------------------------------- /Sem04/07_Nested_Loops_Construction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int count = 5; 7 | for (int i = 0; i < count; i++) 8 | { 9 | for (int j = 0; j < count; j++) // Различна променлива от тази във външния цикъл 10 | { 11 | cout << i * j << '\n'; 12 | } 13 | cout << '\n'; 14 | } 15 | } -------------------------------------------------------------------------------- /Sem04/08_Pascals_Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int row; 7 | cin >> row; 8 | int coef = 1; 9 | 10 | for (int currentRow = 0; currentRow < row; currentRow++) 11 | { 12 | int spacesLimit = row - currentRow; 13 | for (int space = 1; space < spacesLimit; space++) 14 | cout << ' '; 15 | 16 | for (int j = 0; j <= currentRow; j++) 17 | { 18 | if (j == 0 || currentRow == 0) 19 | coef = 1; 20 | else 21 | coef = coef * (currentRow - j + 1) / j; 22 | 23 | cout << coef << ' '; 24 | } 25 | 26 | cout << endl; 27 | } 28 | } -------------------------------------------------------------------------------- /Sem05/01_Days_of_Month.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int month; 7 | cin >> month; 8 | 9 | if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) 10 | cout << 31 << endl; 11 | 12 | else if (month == 4 || month == 6 || month == 9 || month == 11) 13 | cout << 30 << endl; 14 | 15 | else if (month == 2) 16 | cout << 28 << endl; 17 | 18 | else 19 | cout << "Invalid input" << endl; 20 | 21 | } -------------------------------------------------------------------------------- /Sem05/02_Days_of_Month.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int month; 7 | cout << "Enter a month: "; 8 | cin >> month; 9 | 10 | switch (month) 11 | { 12 | case 2: cout << 28 << endl; break; 13 | case 4: 14 | case 6: 15 | case 9: 16 | case 11: cout << 30 << endl; break; 17 | case 1: 18 | case 3: 19 | case 5: 20 | case 7: 21 | case 8: 22 | case 10: 23 | case 12: cout << 31 << endl; break; 24 | default: cout << "Invalid input" << endl; break; 25 | } 26 | } -------------------------------------------------------------------------------- /Sem05/03_Secondary_Diagonal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cout << "Enter the size of the matrix: "; 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) 10 | { 11 | int leadingZeros = N - i; 12 | for (int j = 0; j < leadingZeros; j++) 13 | cout << "- "; 14 | 15 | cout << i << ' '; 16 | 17 | int trailingDashes = i - 1; 18 | for (int j = trailingDashes; j > 0; j--) 19 | cout << "- "; 20 | 21 | cout << endl; 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /Sem05/04_Secondary_Diagonal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int N; 7 | cout << "Enter the size of the matrix: "; 8 | cin >> N; 9 | for (int i = 1; i <= N; i++) 10 | { 11 | for (int j = 0; j < N; j++) 12 | { 13 | if (j == N - i) 14 | cout << i << ' '; 15 | else 16 | cout << "- "; 17 | } 18 | cout << endl; 19 | } 20 | } -------------------------------------------------------------------------------- /Sem05/05_Divisible_3digit_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Print all 3-digit numbers that are divisible by all their digits and do not contain 0 7 | 8 | // isDivisible has initial value false 9 | for (int i = 100; i < 1000; i++) 10 | { 11 | bool isDivisible = false; 12 | int num = i; 13 | 14 | while (num != 0) 15 | { 16 | isDivisible = false; 17 | int lastDigit = num % 10; 18 | if (lastDigit == 0) 19 | break; 20 | 21 | if (i % lastDigit == 0) 22 | isDivisible = true; 23 | 24 | if (!isDivisible) 25 | break; 26 | 27 | num /= 10; 28 | } 29 | if (isDivisible) 30 | cout << i << ' '; 31 | } 32 | 33 | cout << endl; 34 | 35 | // isDivisible has initial value true 36 | for (int i = 100; i < 1000; i++) 37 | { 38 | bool isDivisible = true; 39 | int num = i; 40 | 41 | while (num != 0) 42 | { 43 | int lastDigit = num % 10; 44 | if (lastDigit == 0 || i % lastDigit != 0) 45 | { 46 | isDivisible = false; 47 | break; 48 | } 49 | num /= 10; 50 | } 51 | 52 | if (isDivisible) 53 | cout << i << ' '; 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /Sem05/06_Ascending_Digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Determine whether a positive number's digits are ascending from the beginning until the end 7 | // 12345 -> Yes 8 | // 12334 -> No 9 | // 12354 -> No 10 | // 1 -> Yes 11 | 12 | int n; 13 | do { 14 | cin >> n; 15 | } while (n < 0); 16 | 17 | bool isAscending = true; 18 | while ( n != 0 ) 19 | { 20 | int lastDigit = n % 10; 21 | n /= 10; 22 | int penultimateDigit = n % 10; 23 | 24 | if (lastDigit > penultimateDigit) 25 | continue; 26 | 27 | else 28 | { 29 | isAscending = false; 30 | break; 31 | } 32 | } 33 | 34 | if (isAscending) 35 | cout << "Yes" << endl; 36 | 37 | else 38 | cout << "No" << endl; 39 | } -------------------------------------------------------------------------------- /Sem05/07_Ascending_digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Determine whether a positive number's digits are ascending from the beginning until the end 7 | // 12345 -> Yes 8 | // 12334 -> No 9 | // 12354 -> No 10 | // 1 -> Yes 11 | 12 | int num; 13 | do { 14 | cin >> num; 15 | } while (num < 0); 16 | 17 | bool ascending = true; 18 | int toCompare = 10; 19 | 20 | while ( num != 0) 21 | { 22 | int lastDigit = num % 10; 23 | if (lastDigit >= toCompare) 24 | { 25 | ascending = false; 26 | break; 27 | } 28 | 29 | toCompare = lastDigit; 30 | num /= 10; 31 | } 32 | 33 | if (ascending) 34 | cout << "Yes" << endl; 35 | 36 | else 37 | cout << "No" << endl; 38 | } -------------------------------------------------------------------------------- /Sem05/ТК.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/Sem05/ТК.docx -------------------------------------------------------------------------------- /Sem06/01_First_Occurance_of_an_Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int SIZE = 5; 7 | int arr[SIZE]; 8 | 9 | for (int i = 0; i < SIZE; i++) 10 | cin >> arr[i]; 11 | 12 | int element; 13 | cin >> element; 14 | bool isFound = false; 15 | int index = -1; 16 | 17 | for (int i = 0; i < SIZE; i++) 18 | { 19 | if (arr[i] == element) 20 | { 21 | isFound = true; 22 | index = i; 23 | break; 24 | } 25 | } 26 | 27 | cout << isFound << endl; 28 | if (isFound) 29 | cout << index << endl; 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Sem06/02_All_Occurances_of_an_Element.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // 1st 7 | const int size = 8; 8 | int numbers[size] = { 2, 3, 4, 2, 8, 19, 36, 2 }; // 2 -> 0, 3, 7 9 | int indices[size]; 10 | int counter = 0; 11 | 12 | int element; 13 | cin >> element; 14 | 15 | for (int i = 0; i < size; i++) 16 | { 17 | if (numbers[i] == element) 18 | indices[counter++] = i; 19 | } 20 | 21 | for (int i = 0; i < counter; i++) 22 | cout << indices[i] << ' '; 23 | 24 | // 2nd - demonstrating what a histogram is 25 | const int SIZE = 8; 26 | int nums[SIZE] = { 2, 3, 4, 2, 8, 19, 36, 2 }; 27 | bool histogram[SIZE] = {}; 28 | 29 | int element; 30 | cin >> element; 31 | 32 | for (int i = 0; i < SIZE; i++) 33 | { 34 | if (nums[i] == element) 35 | histogram[i] = 1; 36 | } 37 | 38 | for (int i = 0; i < SIZE; i++) 39 | { 40 | if (histogram[i]) 41 | cout << i << ' '; 42 | } 43 | 44 | return 0; 45 | } -------------------------------------------------------------------------------- /Sem06/03_Double_Approximation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | double d = 0.1; 7 | double r = 0; 8 | 9 | for (int i = 0; i < 10; i++) // 0.1 + 0.1 + ... + 0.1 (10x) = 1 10 | r += d; 11 | 12 | cout << (r == 1) << endl; 13 | double eps = 0.0001; 14 | if (r - 1 < eps) 15 | cout << "Yes" << endl; 16 | } -------------------------------------------------------------------------------- /Sem06/04_Catalan_Formula.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/Sem06/04_Catalan_Formula.pdf -------------------------------------------------------------------------------- /Sem06/04_Catalan_Nmbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int SIZE = 11; 7 | int arr[SIZE] = {}; 8 | 9 | // Грешката беше, че нямахме база! Винаги трябва да имаме база при рекурентно зададените формули 10 | arr[0] = 1; // Даваме база -> първото число на Каталан 11 | // Всички останали елементи трябва да са инициализирани с 0, защото работим със сбор, а не можем да събираме 12 | // неинициализирани променливи. 0 е неутралният елемент по отношение на събирането, затова е дефолтна стойност на инициализацията 13 | 14 | for (int i = 1; i < 11; i++) // След като имаме база започваме от 1вия индекс 15 | for (int j = i - 1; j >= 0; j--) // Искаме да умножим всички двойки предишни елементи и сумата им ще бъде нашия (това е самата формула) 16 | arr[i] += arr[j] * arr[i - j - 1]; // Разписваме формулата 17 | 18 | for (int i = 0; i < 11; i++) 19 | cout << arr[i] << ' '; 20 | cout << endl; 21 | 22 | } -------------------------------------------------------------------------------- /Sem07/00_Functions_Examples.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Кратка теория: 6 | 7 | Скелет на функция: 8 | 9 | <тип на връщане> <име>([<параметри>]) 10 | { 11 | <тяло на функцията> 12 | ... 13 | ... 14 | ... 15 | 16 | // return statement, ако типът го изисква (т.е. не е void) 17 | } 18 | 19 | Типове на връщане: 20 | -всички изучавани до момента 21 | -void (празен тип; не изисква функцията да връща нищо) 22 | 23 | Параметри: 24 | -задават се чрез *типа* им и името, с което ще ги ползваме 25 | 26 | Аргументи: 27 | -подават се на функцията при извикването й 28 | -трябва да бъдат инициализирани 29 | 30 | Декларация vs дефиниция: 31 | -чрез декларацията казваме, че "такава функция съществува" 32 | -чрез дефиницията казваме "какво ще прави тази функция" 33 | -в С++ всички функции трябва да бъдат декларирани *преди* main функцията! 34 | -дефиниция и декларация могат да се разделят 35 | -дефиницията може да е след main 36 | 37 | Извикване: 38 | -става чрез името на функцията и подаване на аргументи, ако такива са нужни 39 | -можем да викаме една функция много пъти 40 | -ако функцията връща стойност можем да я присвояваме на променлива от подходящ тип 41 | -функции могат да се влагат една в друга 42 | */ 43 | 44 | // Декларация: 45 | void thisFunctionExists(); 46 | 47 | // Дефиниция: 48 | void printHelloWorld() 49 | { 50 | cout << "Hello world!" << endl; 51 | } 52 | 53 | // Влагане на функции: 54 | void printNumber(int num) 55 | { 56 | cout << num << ' '; 57 | } 58 | void findEvenNumbersUntil10() 59 | { 60 | for (int i = 0; i < 10; i++) 61 | if (i % 2 == 0) 62 | printNumber(i); 63 | cout << endl; 64 | } 65 | 66 | // Връщане на стойност 67 | int factorial(int num) 68 | { 69 | int fact = 1; 70 | for (int i = 2; i <= num; i++) 71 | fact *= i; 72 | 73 | return fact; 74 | } 75 | 76 | int main() 77 | { 78 | printHelloWorld(); 79 | findEvenNumbersUntil10(); 80 | 81 | int a = 3, b = 4, c = 5, d = 6; 82 | 83 | // Намиране на факториел без функции: 84 | int fact_A1 = 1, fact_B1 = 1, fact_C1 = 1, fact_D1 = 1; 85 | for (int i = 2; i <= a; i++) 86 | fact_A1 *= i; 87 | for (int i = 2; i <= b; i++) 88 | fact_B1 *= i; 89 | for (int i = 2; i <= c; i++) 90 | fact_C1 *= i; 91 | for (int i = 2; i <= d; i++) 92 | fact_D1 *= i; 93 | 94 | // Намиране на факториел с функции: 95 | int fact_A2 = factorial(a); 96 | int fact_B2 = factorial(b); 97 | int fact_C2 = factorial(c); 98 | int fact_D2 = factorial(d); 99 | 100 | cout << fact_A1 << ' ' << fact_B1 << ' ' << fact_C1 << ' ' << fact_D1 << endl; 101 | cout << fact_A2 << ' ' << fact_B2 << ' ' << fact_C2 << ' ' << fact_D2 << endl; 102 | } -------------------------------------------------------------------------------- /Sem07/01_Coefficients.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void inputArray(int arr[], int size) 5 | { 6 | for (int i = size; i >= 0; --i) 7 | cin >> arr[i]; 8 | } 9 | 10 | void multiplyPolynoms(int res[], int size, int arr1[], int size1, int arr2[], int size2) 11 | { 12 | for (int i = 0; i <= size1; i++) 13 | for (int j = 0; j <= size2; j++) 14 | res[i + j] += arr1[i] * arr2[j]; 15 | } 16 | 17 | void printArray(int res[], int size) 18 | { 19 | for (int i = 0; i <= size; i++) 20 | cout << res[size - i] << ' '; 21 | } 22 | 23 | int main() 24 | { 25 | const int SIZE = 1024; 26 | int n, m; 27 | int P[SIZE], Q[SIZE], RESULT[SIZE*2] = {}; 28 | 29 | cin >> n; 30 | inputArray(P, n); 31 | 32 | cin >> m; 33 | inputArray(Q, m); 34 | 35 | multiplyPolynoms(RESULT, n + m, P, n, Q, m); 36 | printArray(RESULT, n + m); 37 | } -------------------------------------------------------------------------------- /Sem07/02_Smaller_Neighbours.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int loseNeighbours(int arr[], int size) 5 | { 6 | bool removed = true; 7 | while (removed) 8 | { 9 | removed = false; 10 | for (int i = 1; i < size - 1; i++) 11 | { 12 | if (arr[i - 1] > arr[i] && arr[i] < arr[i + 1]) 13 | { 14 | for (int j = i; j < size - 1; j++) 15 | { 16 | arr[j] = arr[j + 1]; 17 | } 18 | size--; 19 | removed = true; 20 | } 21 | } 22 | } 23 | 24 | return size; 25 | } 26 | 27 | int main() 28 | { 29 | const int SIZE = 1024; 30 | int arr[SIZE]; 31 | 32 | int elementsCount = 0; 33 | cin >> elementsCount; 34 | 35 | // Елементите на масива се въвеждат от потребителя 36 | for (int i = 0; i < elementsCount; i++) 37 | cin >> arr[i]; 38 | 39 | int newSize = loseNeighbours(arr, elementsCount); 40 | 41 | // Принтира елементите на получения масив 42 | for (int i = 0; i < newSize; i++) 43 | cout << arr[i] << ' '; 44 | 45 | } -------------------------------------------------------------------------------- /Sem07/zadachi.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/Sem07/zadachi.pdf -------------------------------------------------------------------------------- /Sem08/01_Pointers_and_Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f(int* a) 5 | { 6 | *a += 10; 7 | } 8 | 9 | int main() 10 | { 11 | int variableToChange = 6; 12 | f(&variableToChange); 13 | cout << variableToChange << endl; 14 | 15 | // Pointers are variables that hold an address. 16 | int a = 5, b = 6; 17 | int *a_ptr = &a, *b_ptr = &b; 18 | 19 | cout << a_ptr << ' ' << b_ptr << endl; 20 | cout << *a_ptr << endl; 21 | 22 | *a_ptr = *b_ptr; 23 | cout << a << endl; 24 | 25 | int c = 7; 26 | int *c_ptr; 27 | c_ptr = &c; 28 | 29 | a_ptr = c_ptr; 30 | cout << *a_ptr << endl; 31 | 32 | // Arrays are pointers to their first element. 33 | int matrix[5] = { 1, 2, 3, 4, 5 }; 34 | int* ptr = matrix; // Now both pointers are pointing to the same memory. 35 | cout << ptr[2] << endl; 36 | ptr[2] = 22; 37 | cout << matrix[2] << endl; 38 | 39 | // If we miss the * we just copy the value in another vairable of the same type. 40 | int var = 5; 41 | int ptr_var = var; // This is not a pointer. 42 | 43 | // N-dimensional arrays 44 | int one_d[2]; 45 | int two_d[3][2] = { {1,2}, {2, 3}, {5,6} }; 46 | 47 | for (int i = 0; i < 3; i++) 48 | for (int j = 0; j < 2; j++) 49 | cout << two_d[i][j] << ' '; 50 | 51 | cout << endl; 52 | int three_d[4][3][2] = {{ {1,2}, {2, 3}, {5,6} }, { {1,2}, {2, 3}, {5,6} }, { {1,2}, {2, 3}, {5,6} }, { {1,2}, {2, 3}, {5,6} } }; 53 | 54 | for (int i = 0; i < 4; i++) 55 | for (int j = 0; j < 3; j++) 56 | for (int k = 0; k < 2; k++) 57 | cout << three_d[i][j][k] << ' '; 58 | } -------------------------------------------------------------------------------- /Sem08/02_Transpose_of_a_Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const int ROWS = 5; 7 | const int COLS = 6; 8 | int matrix[ROWS][COLS] = {}; 9 | 10 | for (int i = 0; i < ROWS; i++) 11 | { 12 | for (int j = 0; j < COLS; j++) 13 | { 14 | if (i == j) 15 | continue; 16 | 17 | matrix[i][j] = i * j + j; 18 | } 19 | } 20 | 21 | for (int i = 0; i < ROWS; i++) 22 | { 23 | for (int j = 0; j < COLS; j++) 24 | cout << matrix[i][j] << ' '; 25 | 26 | cout << endl; 27 | } 28 | cout << endl; 29 | 30 | for (int i = 0; i < ROWS; i++) 31 | { 32 | for (int j = 0; j < COLS; j++) 33 | cout << (matrix[i][j] += 100) << ' '; 34 | 35 | cout << endl; 36 | } 37 | cout << endl; 38 | 39 | for (int i = 0; i < COLS; i++) 40 | { 41 | for (int j = 0; j < ROWS; j++) 42 | cout << matrix[j][i] << ' '; 43 | 44 | cout << endl; 45 | } 46 | } -------------------------------------------------------------------------------- /Sem08/03_Row_vs_Column_Traversal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | const int SIZE = 10000; 6 | int A[SIZE][SIZE] = {}; 7 | 8 | int main() 9 | { 10 | clock_t Col = clock(); 11 | 12 | for (int i = 0; i < SIZE; i++) 13 | for (int j = 0; j < SIZE; j++) 14 | A[j][i]++; 15 | 16 | double col = ((double)clock() - Col) / CLOCKS_PER_SEC; 17 | 18 | clock_t Row = clock(); 19 | 20 | for (int i = 0; i < SIZE; i++) 21 | for (int j = 0; j < SIZE; j++) 22 | A[i][j]++; 23 | 24 | double row = ((double)clock() - Row) / CLOCKS_PER_SEC; 25 | 26 | cout << "Row: " << row << endl; 27 | cout << "Col: " << col << endl; 28 | 29 | } -------------------------------------------------------------------------------- /Sem08/04_Spiral_Matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int ROWS = 4; 5 | const int COLS = 6; 6 | 7 | void printSpiralTraversal(int matrix[ROWS][COLS]) 8 | { 9 | int currentRow = 0, currentCol = 0; 10 | int maxRow = ROWS, maxCol = COLS; 11 | 12 | while (currentRow < maxRow && currentCol < maxCol) 13 | { 14 | for (int i = currentCol; i < maxCol; i++) 15 | cout << matrix[currentRow][i] << ' '; 16 | currentRow++; 17 | 18 | for (int i = currentRow; i < maxRow; i++) 19 | cout << matrix[i][maxCol - 1] << ' '; 20 | maxCol--; 21 | 22 | if (currentRow < maxRow) 23 | { 24 | for (int i = maxCol - 1; i >= currentCol; i--) 25 | cout << matrix[maxRow - 1][i] << ' '; 26 | maxRow--; 27 | } 28 | 29 | if (currentCol < maxCol) 30 | { 31 | for (int i = maxRow - 1; i >= currentRow; i--) 32 | cout << matrix[i][currentCol] << ' '; 33 | currentCol++; 34 | } 35 | } 36 | } 37 | 38 | int main() 39 | { 40 | int matrix[ROWS][COLS] = { {1, 2, 3, 4, 5, 6}, 41 | {16, 17, 18, 19, 20, 7}, 42 | {15, 24, 23, 22, 21, 8}, 43 | {14, 13, 12, 11, 10, 9} }; 44 | printSpiralTraversal(matrix); 45 | } -------------------------------------------------------------------------------- /Sem09/01_Bubble_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int* arr, int i, int j) 5 | { 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | // B.C. : O(n-1) = O(n) 12 | // A.C. : O(n^2) 13 | // W.C. : (n - 1) + (n - 2) + ... + (1) = ((n-1)*n)/2 = (n^2 - n)/2 = O(n^2) 14 | // Memory : O(1) 15 | // Stable : Yes 16 | // When : Studying 17 | 18 | void BubbleSort(int* arr, int size) 19 | { 20 | for (int i = 0; i < size; i++) 21 | { 22 | bool isSwapped = false; 23 | 24 | for (int j = 0; j < size - i - 1; j++) 25 | { 26 | if (arr[j] > arr[j + 1]) 27 | { 28 | swap(arr, j, j + 1); 29 | isSwapped = true; 30 | } 31 | } 32 | 33 | if (!isSwapped) 34 | return; 35 | 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | const int size = 8; 42 | int best[size] = { 1,2,3,4,5,6,7,8 }; 43 | int average[size] = { 1,2,3,4,9,2,4,5 }; 44 | int worst[size] = { 9,8,7,6,5,4,3,2 }; 45 | 46 | BubbleSort(best, size); 47 | for (int i = 0; i < size; i++) 48 | { 49 | cout << best[i] << ' '; 50 | } 51 | cout << endl; 52 | 53 | BubbleSort(average, size); 54 | for (int i = 0; i < size; i++) 55 | { 56 | cout << average[i] << ' '; 57 | } 58 | cout << endl; 59 | 60 | BubbleSort(worst, size); 61 | for (int i = 0; i < size; i++) 62 | { 63 | cout << worst[i] << ' '; 64 | } 65 | } -------------------------------------------------------------------------------- /Sem09/02_Selection_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int* arr, int i, int j) 5 | { 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | // B.C. : O(n^2) 12 | // A.C. : O(n^2) 13 | // W.C. : O(n^2) 14 | // Stable: No 15 | // Memory : O(1) 16 | // Best : Min swaps 17 | 18 | void SelectionSort(int* arr, int size) 19 | { 20 | int upperBound = size - 1; 21 | for (int i = 0; i < upperBound; i++) 22 | { 23 | int minIndex = i; 24 | for (int j = i + 1; j < size; j++) 25 | { 26 | if (arr[j] < arr[minIndex]) 27 | minIndex = j; 28 | } 29 | if (minIndex != i) 30 | swap(arr, i, minIndex); 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | const int size = 8; 37 | int best[size] = { 1,2,3,4,5,6,7,8 }; 38 | int average[size] = { 1,2,3,4,9,2,4,5 }; 39 | int worst[size] = { 9,8,7,6,5,4,3,2 }; 40 | 41 | SelectionSort(best, size); 42 | for (int i = 0; i < size; i++) 43 | { 44 | cout << best[i] << ' '; 45 | } 46 | cout << endl; 47 | 48 | SelectionSort(average, size); 49 | for (int i = 0; i < size; i++) 50 | { 51 | cout << average[i] << ' '; 52 | } 53 | cout << endl; 54 | 55 | BubbleSort(worst, size); 56 | for (int i = 0; i < size; i++) 57 | { 58 | cout << worst[i] << ' '; 59 | } 60 | } -------------------------------------------------------------------------------- /Sem09/03_Insertion_Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int* arr, int i, int j) 5 | { 6 | int temp = arr[i]; 7 | arr[i] = arr[j]; 8 | arr[j] = temp; 9 | } 10 | 11 | // B.C. : O(n) 12 | // A.C. : O(n^2) 13 | // W.C. : O(n^2) 14 | // Stable : Yes 15 | // Memory : O(1) 16 | // When to use : quite ordered arrays 17 | 18 | void InsertionSort(int* arr, int size) 19 | { 20 | for (int i = 1; i < size; i++) 21 | { 22 | int element = arr[i]; 23 | int index = i - 1; 24 | 25 | while (index >= 0 && arr[index] > element) 26 | { 27 | arr[index + 1] = arr[index]; 28 | index--; 29 | } 30 | 31 | arr[index + 1] = element; 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | const int size = 8; 38 | int best[size] = { 1,2,3,4,5,6,7,8 }; 39 | int average[size] = { 1,2,3,4,9,2,4,5 }; 40 | int worst[size] = { 9,8,7,6,5,4,3,2 }; 41 | 42 | SelectionSort(best, size); 43 | for (int i = 0; i < size; i++) 44 | { 45 | cout << best[i] << ' '; 46 | } 47 | cout << endl; 48 | 49 | SelectionSort(average, size); 50 | for (int i = 0; i < size; i++) 51 | { 52 | cout << average[i] << ' '; 53 | } 54 | cout << endl; 55 | 56 | BubbleSort(worst, size); 57 | for (int i = 0; i < size; i++) 58 | { 59 | cout << worst[i] << ' '; 60 | } 61 | } -------------------------------------------------------------------------------- /Sem09/04_Charcter_Arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char arr[21]; // '\0' 8 | char name0[10] = { 'I', 'v', 'a', 'n', 'a', '\0'}; 9 | char name1[] = "Ivana"; 10 | char st[30]; 11 | cin >> st; 12 | cout << st << endl; 13 | 14 | // We don't need the size, because once we reach '\0' we know it stands for the end of the character array. 15 | //for (int i = 0; arr[i] != '\0'; i++) 16 | //{ 17 | // 18 | //} 19 | 20 | // Problem 1: Replace every occurance if character x with character y. 21 | char input[] = "asdadad"; 22 | char toReplace = 'a'; 23 | char replaceWith = 'x'; 24 | 25 | for (int i = 0; input[i] != '\0'; i++) 26 | { 27 | if (input[i] == toReplace) 28 | input[i] = replaceWith; 29 | } 30 | 31 | cout << input << endl; 32 | 33 | // Problem 2: Count the digits in a character array. 34 | char str[] = "123hsds12"; 35 | int count = 0; 36 | 37 | for (int i = 0; str[i] != '\0'; i++) 38 | { 39 | if (str[i] >= '0' && str[i] <= '9') 40 | count++; 41 | } 42 | 43 | cout << count; 44 | 45 | 46 | } -------------------------------------------------------------------------------- /Sem10/01_Capitalize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool validate(char* str) 5 | { 6 | for (int i = 0; str[i] != '\0'; i++) 7 | if (str[i] < 'a' || str[i] > 'z') 8 | return false; 9 | 10 | return true; 11 | } 12 | 13 | void capitalize(char* str) 14 | { 15 | int diff = 'A' - 'a'; 16 | for (int i = 0; str[i] != '\0'; i++) 17 | str[i] += diff; // str[i] = str[i] + diff; 18 | } 19 | 20 | int main() 21 | { 22 | char str[1024]; 23 | cin >> str; 24 | 25 | if (validate(str)) 26 | { 27 | capitalize(str); 28 | cout << str << endl; 29 | } 30 | else 31 | cout << "Invalid input" << endl; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Sem10/02_Valid_Identificator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isKeyword(char* str); 6 | bool startsWithADigit(char str[]); 7 | bool containsInvalidSymbol(char* str); 8 | 9 | int main() 10 | { 11 | char str[1024]; 12 | cin >> str; 13 | 14 | cout << !(containsInvalidSymbol(str) || isKeyword(str) || startsWithADigit(str)) << endl; 15 | } 16 | 17 | bool containsInvalidSymbol(char* str) 18 | { 19 | for (int i = 0; str[i] != '\0'; i++) 20 | { 21 | if ((str[i] < 'a' || str[i] > 'z') && 22 | (str[i] < 'A' || str[i] > 'Z') && 23 | (str[i] < '0' || str[i] > '9') && 24 | (str[i] != '_')) 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | bool startsWithADigit(char str[]) 31 | { 32 | return str[0] >= '0' && str[0] <= '9'; 33 | } 34 | 35 | bool isKeyword(char* str) 36 | { 37 | if (!strcmp(str, "auto") || !strcmp(str, "double") 38 | || !strcmp(str, "int") || !strcmp(str, "struct") 39 | || !strcmp(str, "break") || !strcmp(str, "else") 40 | || !strcmp(str, "long") || !strcmp(str, "switch") 41 | || !strcmp(str, "case") || !strcmp(str, "enum") 42 | || !strcmp(str, "register") || !strcmp(str, "char") 43 | || !strcmp(str, "extern") || !strcmp(str, "typedef") 44 | || !strcmp(str, "return") || !strcmp(str, "union") 45 | || !strcmp(str, "const") || !strcmp(str, "float") 46 | || !strcmp(str, "short") || !strcmp(str, "unsigned") 47 | || !strcmp(str, "continue") || !strcmp(str, "for") 48 | || !strcmp(str, "signed") || !strcmp(str, "void") 49 | || !strcmp(str, "default") || !strcmp(str, "goto") 50 | || !strcmp(str, "sizeof") || !strcmp(str, "volatile") 51 | || !strcmp(str, "do") || !strcmp(str, "if") 52 | || !strcmp(str, "asm") || !strcmp(str, "while") 53 | || !strcmp(str, "dynamic_cast") || !strcmp(str, "static") 54 | || !strcmp(str, "namespace") || !strcmp(str, "reinterpret_cast") 55 | || !strcmp(str, "bool") || !strcmp(str, "catch") 56 | || !strcmp(str, "explicit") || !strcmp(str, "false") 57 | || !strcmp(str, "new") || !strcmp(str, "operator") 58 | || !strcmp(str, "static_cast") || !strcmp(str, "template") 59 | || !strcmp(str, "class") || !strcmp(str, "const_cast") 60 | || !strcmp(str, "friend") || !strcmp(str, "inline") 61 | || !strcmp(str, "private") || !strcmp(str, "public") 62 | || !strcmp(str, "this") || !strcmp(str, "throw") 63 | || !strcmp(str, "delete") || !strcmp(str, "protected") 64 | || !strcmp(str, "mutable") || !strcmp(str, "true") 65 | || !strcmp(str, "try") || !strcmp(str, "typeid") 66 | || !strcmp(str, "typename") || !strcmp(str, "using") 67 | || !strcmp(str, "wchar_t")) 68 | return true; 69 | return false; 70 | } -------------------------------------------------------------------------------- /Sem10/03_isPalindrom.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | bool isPalindrom(char* str) 6 | { 7 | int size = strlen(str); 8 | int limit = size / 2; 9 | for (int i = 0; i < limit; i++) 10 | { 11 | if (str[i] != str[size - i - 1]) 12 | return false; 13 | } 14 | return true; 15 | } 16 | 17 | bool isPalindromRev(char* str) 18 | { 19 | char reverse[101]; 20 | int size = strlen(str); 21 | for (int i = 0; i < size; i++) 22 | reverse[i] = str[size - i - 1]; 23 | 24 | reverse[size] = '\0'; 25 | return !strcmp(reverse, str); 26 | } 27 | 28 | int main() 29 | { 30 | char str[101]; 31 | cin >> str; 32 | 33 | cout << isPalindrom(str) << endl; 34 | cout << isPalindromRev(str); 35 | } -------------------------------------------------------------------------------- /Sem10/04_NumbersInString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int numbersCount(char* str) 5 | { 6 | bool prevDigit = false; 7 | int numbersCount = 0; 8 | for (int i = 0; str[i] != '\0'; i++) 9 | { 10 | if (str[i] >= '0' && str[i] <= '9') 11 | { 12 | if (!prevDigit) 13 | { 14 | numbersCount++; 15 | prevDigit = true; 16 | } 17 | } 18 | else 19 | prevDigit = false; 20 | } 21 | 22 | return numbersCount; 23 | } 24 | 25 | int main() 26 | { 27 | char str[101]; 28 | cin >> str; 29 | 30 | cout << numbersCount(str); 31 | } -------------------------------------------------------------------------------- /Sem10/05_ReplaceCharWithString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | char str[101], word[11], x; 8 | cin >> str >> word >> x; 9 | 10 | char result[1001]; // 100 * 10 + 1 11 | int len1 = strlen(str); 12 | int len2 = strlen(word); 13 | 14 | int cursor = 0; 15 | for (int i = 0; i < len1; i++) 16 | { 17 | if (str[i] == x) 18 | { 19 | for (int j = 0; j < len2; j++, cursor++) 20 | result[cursor] = word[j]; 21 | } 22 | else 23 | result[cursor++] = str[i]; 24 | } 25 | result[cursor] = '\0'; 26 | 27 | cout << result; 28 | } -------------------------------------------------------------------------------- /Sem10/06_Dynamic_Memory_Basics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Memory leak 5 | // new == delete 6 | void f() 7 | { 8 | int* arr = new int[5]; 9 | // delete[] arr; 10 | } 11 | 12 | int* f1() 13 | { 14 | int arr[4] = { 1, 2, 3, 4 }; 15 | int* ptr = arr; 16 | return ptr; 17 | } // arr gets automatically deleted from the stack once the function ends => ptr points to deallocated memory 18 | 19 | int* f2() 20 | { 21 | int* ptr = new int[4]; 22 | ptr[0] = 1; 23 | ptr[1] = 2; 24 | ptr[2] = 3; 25 | ptr[3] = 4; 26 | return ptr; 27 | } // ptr is allocated dynamically => unless we delete the memory from the heap it will not be automatically deleted 28 | 29 | int main() 30 | { 31 | int* arr1 = f1(); 32 | int* arr2 = f2(); 33 | cout << arr1[0]; 34 | cout << arr2[0]; 35 | 36 | delete[] arr2; 37 | 38 | // Check for a memory leak: 39 | while (true) 40 | { 41 | f(); 42 | } 43 | 44 | int a; // 4 bytes 45 | char c; // 1 byte 46 | bool b; // 1 byte 47 | char arr1[7]; // 8 bytes 48 | // Total: 14 bytes 49 | 50 | // Stack vs Heap 51 | int arr2[7]; // static 52 | int* ptr = new int[7]; // dynamic 53 | 54 | // Difference between: 55 | // delete[] ptr; 56 | // delete ptr; 57 | } -------------------------------------------------------------------------------- /Sem10/problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/Sem10/problems.pdf -------------------------------------------------------------------------------- /Sem11/01_Dynamic_Memory.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // n! = n * (n - 1) * (n - 2) * ... * 1 5 | int factorial(int n) 6 | { 7 | if (n == 1) 8 | return 1; 9 | return n * factorial(n - 1); 10 | } 11 | 12 | int arraySum(int* arr, int size) 13 | { 14 | int sum = 0; 15 | for (int i = 0; i < size; i++) 16 | sum += arr[i]; 17 | 18 | return sum; 19 | } 20 | 21 | void swap(int** matrix, int i, int j) 22 | { 23 | int* temp = matrix[i]; 24 | matrix[i] = matrix[j]; 25 | matrix[j] = temp; 26 | } 27 | 28 | void SelectionSort(int** matrix, int size) 29 | { 30 | int upperBound = size - 1; 31 | for (int i = 0; i < upperBound; i++) 32 | { 33 | int minIndex = i; 34 | for (int j = i + 1; j < size; j++) 35 | { 36 | // if (arraySum(matrix[j], matrix[j][0]) < arraySum(matrix[minIndex], matrix[minIndex][0])) -> тук по много пъти смятаме една и съща сума 37 | if (matrix[j][matrix[j][0] - 1] < matrix[minIndex][matrix[minIndex][0] - 1])) 38 | minIndex = j; 39 | } 40 | if (minIndex != i) 41 | swap(matrix, i, minIndex); 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | int n; 48 | cin >> n; 49 | 50 | int** matrix = new int* [n]; 51 | 52 | for (int i = 0; i < n; i++) 53 | { 54 | int size; 55 | cin >> size; 56 | size += 2; // за размер и сума 57 | matrix[i] = new int[size]; 58 | matrix[i][0] = size; 59 | for (int j = 1; j < size - 1; j++) 60 | cin >> matrix[i][j]; 61 | 62 | matrix[i][size - 1] = arraySum(++matrix, size - 1); 63 | } 64 | 65 | SelectionSort(matrix, n); 66 | 67 | for (int i = 0; i < n; i++) 68 | { 69 | for (int j = 1; j < matrix[i][0]; j++) 70 | cout << matrix[i][j] << ' '; 71 | cout << endl; 72 | } 73 | 74 | for (int i = 0; i < n; i++) 75 | delete[] matrix[i]; 76 | 77 | delete[] matrix; 78 | } -------------------------------------------------------------------------------- /Sem11/02_Contains.cpp: -------------------------------------------------------------------------------- 1 | // Contains -> element in an array with recurion 2 | bool contains(int* arr, int len, int el, int index) 3 | { 4 | if (index >= len) 5 | return false; 6 | 7 | if (el == arr[index]) 8 | return true; 9 | 10 | return contains(arr, len, el, index + 1); 11 | } 12 | 13 | bool contains(int* arr, int len, int el) 14 | { 15 | if (len == 0) 16 | return false; 17 | return arr[len - 1] == el || contains(arr, len - 1, el); 18 | } -------------------------------------------------------------------------------- /Sem11/03_Character_array_recursion.cpp: -------------------------------------------------------------------------------- 1 | // Дали една дума е префикс на друга 2 | bool isPrefix(char* prefix, char* text) 3 | { 4 | if (prefix[0] == '\0') 5 | return true; 6 | 7 | return prefix[0] == text[0] && isPrefix(++prefix, ++text); 8 | } 9 | 10 | // Дали дума се съдържа в друга 11 | bool isSubstring(char* str, char* text, int textSize, int strSize) 12 | { 13 | if (textSize < strSize) 14 | return false; 15 | return isPrefix(str, text) || isSubstring(str, ++text, textSize - 1, strSize); 16 | } -------------------------------------------------------------------------------- /Sem11/README.md: -------------------------------------------------------------------------------- 1 | **Задачи:** https://drive.google.com/drive/u/3/folders/1ikIq3yEHkyI8k_uKlWKFAB2o6Xsj2su7 -------------------------------------------------------------------------------- /Sem11/problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ivanahristova/introduction-to-programming-fmi/0c1873dc51f3826c70ac85a9a154aaef47ffe52d/Sem11/problems.pdf -------------------------------------------------------------------------------- /Sem12/01_K1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // нередактирано решение! 4 | 5 | /* Примери: 6 | a b c d e f g h i j k l m n o 7 | 9 8 | a . f g / 9 | Maximum consecutive words in text: 2 10 | Statistic: 11 | a 1 12 | . 0 13 | f 1 14 | g 1 15 | / 0 16 | Percent: 60% 17 | 18 | i study cs at the faculty of mathematics and informatics at sofia university st. kliment ohridski 19 | 20 20 | cs at the at sofia . 21 | Maximum consecutive words in text: 5 22 | Statistic: 23 | cs 1 24 | at 2 25 | the 1 26 | at 2 27 | sofia 1 28 | . 0 29 | Percent: 83.3333% 30 | 31 | a b c d e f g h i j k l m n o p q r s t u v w x y z this is the english alphapet 32 | 29 33 | a . f g / this is t l n . y z 34 | Maximum consecutive words in text: 5 35 | Statistic: 36 | a 1 37 | . 0 38 | f 1 39 | g 1 40 | / 0 41 | this 1 42 | is 1 43 | t 1 44 | l 1 45 | n 1 46 | . 0 47 | y 1 48 | Percent: 76.9231% 49 | */ 50 | 51 | bool existsInText(char text[], int textSize, char* word) 52 | { 53 | int wordIndex = 0; 54 | for (int i = 0; i < textSize; i++) 55 | { 56 | if (text[i] != word[wordIndex]) 57 | continue; 58 | 59 | if (i != 0 && text[i - 1] != ' ') // it is not separated by a space 60 | continue; 61 | 62 | while (wordIndex < strlen(word) && text[i++] == word[wordIndex++]) 63 | ; 64 | 65 | if (wordIndex == strlen(word) && (i == textSize || text[i] == ' ')) 66 | return true; 67 | else 68 | wordIndex = 0; 69 | } 70 | 71 | return false; 72 | } 73 | 74 | int getTextOccurances(char text[], int textSize, char* word) 75 | { 76 | int count = 0; 77 | int wordIndex = 0; 78 | for (int i = 0; i < textSize; i++) 79 | { 80 | if (text[i] != word[wordIndex]) 81 | continue; 82 | 83 | if (i != 0 && text[i - 1] != ' ') // it is not separated by a space 84 | continue; 85 | 86 | while (wordIndex < strlen(word) && text[i++] == word[wordIndex++]) 87 | ; 88 | 89 | if (wordIndex == strlen(word) && (i == textSize || text[i] == ' ')) 90 | { 91 | wordIndex = 0; 92 | count++; 93 | } 94 | else 95 | wordIndex = 0; 96 | } 97 | 98 | return count; 99 | } 100 | 101 | int findConsecutiveWordsCount(char text[], int textSize, char* sentence) 102 | { 103 | int sentenceLength = strlen(sentence); 104 | char* word = new char[sentenceLength]; 105 | int index = 0; 106 | int currentCount = 0; 107 | int maxCount = 0; 108 | for (int i = 0; i < sentenceLength; i++) 109 | { 110 | if ((sentence[i] == ' ' && index > 0) || (index == sentenceLength - 1)) 111 | { 112 | word[index] = '\0'; 113 | index = 0; 114 | if (existsInText(text, textSize, word)) 115 | { 116 | currentCount++; 117 | if (currentCount > maxCount) 118 | maxCount = currentCount; 119 | } 120 | else 121 | currentCount = 0; 122 | 123 | continue; 124 | } 125 | word[index++] = sentence[i]; 126 | } 127 | 128 | delete[] word; 129 | return maxCount; 130 | } 131 | 132 | void getWordsStatistic(char text[], int textSize, char* sentence) 133 | { 134 | int sentenceLength = strlen(sentence); 135 | char* word = new char[sentenceLength + 1]; 136 | int index = 0; 137 | int currentCount = 0; 138 | int maxCount = 0; 139 | for (int i = 0; i < sentenceLength; i++) 140 | { 141 | if (sentence[i] == ' ' && index > 0) 142 | { 143 | word[index] = '\0'; 144 | index = 0; 145 | std::cout << word << ' ' << getTextOccurances(text, textSize, word) << std::endl; 146 | continue; 147 | } 148 | word[index++] = sentence[i]; 149 | } 150 | word[index] = '\0'; 151 | std::cout << word << ' ' << getTextOccurances(text, textSize, word) << std::endl; 152 | delete[] word; 153 | } 154 | 155 | double findSameWordsPercent(char text[], int textSize, char* sentence) 156 | { 157 | int sentenceLength = strlen(sentence); 158 | char* word = new char[sentenceLength]; 159 | int index = 0; 160 | int occured = 0; 161 | int words = 0; 162 | for (int i = 0; i < sentenceLength; i++) 163 | { 164 | if (sentence[i] == ' ' && index > 0) 165 | { 166 | word[index] = '\0'; 167 | index = 0; 168 | if (existsInText(text, textSize, word)) 169 | occured++; 170 | 171 | words++; 172 | continue; 173 | } 174 | word[index++] = sentence[i]; 175 | } 176 | 177 | // for the last word 178 | if (existsInText(text, textSize, word)) 179 | occured++; 180 | words++; 181 | 182 | delete[] word; 183 | return ((double)occured / words) * 100; 184 | } 185 | 186 | int main() 187 | { 188 | const int textMaxSize = 1025; 189 | char text[textMaxSize]; 190 | for (int i = 0; i < textMaxSize; i++) 191 | { 192 | char ch; 193 | std::cin.get(ch); 194 | if (ch == '\n') 195 | { 196 | text[i] = '\0'; 197 | break; 198 | } 199 | text[i] = ch; 200 | } 201 | 202 | int sentenceSize; 203 | std::cin >> sentenceSize; 204 | sentenceSize++; 205 | std::cin.ignore(); 206 | char* sentence = new char[sentenceSize + 1]; 207 | std::cin.getline(sentence, sentenceSize); 208 | sentence[sentenceSize] = '\0'; 209 | 210 | std::cout << "Maximum consecutive words in text: " << findConsecutiveWordsCount(text, strlen(text), sentence) << std::endl; 211 | std::cout << "Statistic: " << std::endl; 212 | getWordsStatistic(text, strlen(text), sentence); 213 | std::cout << "Percent: " << findSameWordsPercent(text, strlen(text), sentence) << std::endl; 214 | 215 | delete[] sentence; 216 | } -------------------------------------------------------------------------------- /Sem12/02_Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | /* 5 | Зад. 3 6 | Ще получите число n, отговарящо на броя на елементите от 7 | множество от естествени числа, 8 | които ще бъдат последователно въведени от потребителя. 9 | 1.) Да се изведат всички числа от множеството, в чийто двоичен запис 10 | броят на единиците е по-голям от броя на нулите. 11 | 2.) Да се провери дали в множеството присъстват всички цифри. 12 | 3.) Ако точка 2. е изпълнена, да се сортират елемнтите на множеството 13 | според сумата от цифрите им. 14 | (Числата с еднаква сума трябва да запазват относителната си наредба) 15 | 16 | За решението на 1. и 2. да се използва рекурсия. 17 | 18 | Примери: 19 | Вход: 10 20 | 2 8 11 12 24 37 102 340 756 1024 21 | Изход: 1.) 11 102 756 22 | 2.) 0 23 | Вход: 20 24 | 2 3 4 5 6 7 8 23 24 25 26 30 31 78 456 4567 10235 12555 17889 20000 25 | Изход: 1.) 3 5 6 7 23 25 26 30 31 78 4567 10235 26 | 2.) 1 27 | 3.) 2 20000 3 30 4 31 5 23 6 24 7 25 8 26 10235 78 456 12555 4567 17889 28 | 29 | */ 30 | 31 | void digits(int number, bool* hist) 32 | { 33 | if (number == 0) 34 | return; 35 | 36 | int last = number % 10; 37 | number /= 10; 38 | hist[last] = true; 39 | return digits(number, hist); 40 | } 41 | 42 | bool allDigit(bool* hist) 43 | { 44 | for (int j = 0; j < 9; j++) 45 | if (hist[j] == 0) 46 | return false; 47 | 48 | return true; 49 | } 50 | 51 | int sumDigits(int number) 52 | { 53 | if (number == 0) 54 | return 0; 55 | 56 | return (number % 10 + sumDigits(number / 10)); 57 | } 58 | 59 | void InsertionSort(int* arr, int size, int* sumedNumbers) 60 | { 61 | for (int i = 1; i < size; i++) 62 | { 63 | int elementSum = sumedNumbers[i]; 64 | int arrElement = arr[i]; 65 | int ind = i - 1; 66 | 67 | while (ind >= 0 && sumedNumbers[ind] > elementSum) 68 | { 69 | sumedNumbers[ind + 1] = sumedNumbers[ind]; 70 | arr[ind + 1] = arr[ind]; 71 | ind--; 72 | } 73 | sumedNumbers[ind + 1] = elementSum; 74 | arr[ind + 1] = arrElement; 75 | } 76 | } 77 | 78 | int main() 79 | { 80 | int n; 81 | cin >> n; 82 | int* arr = new int[n]; 83 | for (int i = 0; i < n; i++) 84 | cin >> arr[i]; 85 | 86 | // 1. е правена на практикум 87 | 88 | // 2. 89 | const int SIZE = 10; 90 | bool histogram[SIZE] = {}; // Всички елементи се нулират 91 | 92 | bool hasAllDigits = false; 93 | for (int i = 0; i < n; i++) 94 | { 95 | if (arr[i] == 0) 96 | histogram[0] = true; 97 | digits(arr[i], histogram); 98 | 99 | if (allDigit(histogram)) 100 | { 101 | hasAllDigits = true; 102 | break; 103 | } 104 | } 105 | 106 | // 3. се изпълнява само при hasAllDigits = true 107 | if (!hasAllDigits) 108 | return 0; 109 | 110 | int* sumNumberArray = new int[n]; 111 | for (int i = 0; i < n; i++) 112 | sumNumberArray[i] = sumDigits(arr[i]); 113 | 114 | // сортираме спрямо намерените суми 115 | // така извършваме сумирането само по един път върху всеки елемент 116 | InsertionSort(arr, n, sumNumberArray); 117 | for (int i = 0; i < n; i++) 118 | cout << arr[i] << ' '; 119 | 120 | delete[] arr; 121 | delete[] sumNumberArray; 122 | } -------------------------------------------------------------------------------- /Sem12/03_Function_Overloading.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void print(int a) 5 | { 6 | cout << "Integer: " << a << endl; 7 | } 8 | 9 | void print(double d) 10 | { 11 | cout << "Double: " << d << endl; 12 | } 13 | 14 | void print(bool b) 15 | { 16 | cout << "Bool: " << b << endl; 17 | } 18 | 19 | void print(char c) 20 | { 21 | cout << "Character: " << c << endl; 22 | } 23 | 24 | int max(int a, int b) 25 | { 26 | return (a > b) ? a : b; 27 | } 28 | 29 | int max(int a, int b, int c) 30 | { 31 | return max(max(a, b), c); 32 | } 33 | 34 | int max(int a, int b, int c, int d) 35 | { 36 | return max(max(a,b,c), d); 37 | // return max(max(a, b), max(c, d)); 38 | } 39 | 40 | int max(int a, int b, int c, int d, int e) 41 | { 42 | return max(max(a, b, c, d), e); 43 | } 44 | 45 | int main() 46 | { 47 | // Function overloading 48 | int a = 5; 49 | bool b = true; 50 | double d = 5.5; 51 | char c = 'A'; 52 | 53 | print(a); 54 | print(b); 55 | print(d); 56 | print(c); // 'A' = 65 57 | 58 | cout << max(1, 2, 3, 4, 5) << endl; 59 | cout << max(10, 0, 23) << endl; 60 | } -------------------------------------------------------------------------------- /Sem12/04_Default_Parameters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double calculate(int a, double d, bool b = true, bool b2 = false) 5 | { 6 | if (b || b2) 7 | return a + d; 8 | else 9 | return b; 10 | } 11 | 12 | int main() 13 | { 14 | int a = 5; 15 | bool b = true; 16 | double d = 5.5; 17 | 18 | cout << calculate(a, d, b) << endl; 19 | } --------------------------------------------------------------------------------