├── C examples ├── Exam Preparation │ ├── Exam Preparation Tasks.md │ ├── Zad1.c │ ├── Zad2.c │ ├── Zad3.c │ └── Zad4.c ├── README.md ├── Sem01 │ ├── README.md │ ├── Task01.c │ ├── Task02.c │ ├── Task03.c │ ├── Task04.c │ ├── Task05.c │ ├── Task06.c │ ├── Task07.c │ └── images │ │ ├── DataTypes.png │ │ ├── OperatorCharacteristics.png │ │ └── Variable.png ├── Sem02 │ └── README.md ├── Sem03 │ ├── README.md │ ├── Solutions │ │ ├── Drawings.txt │ │ ├── Task01.c │ │ ├── Task02.c │ │ ├── Task03.c │ │ ├── Task04.c │ │ ├── Task05.c │ │ ├── Task06.c │ │ ├── Task07.c │ │ ├── Task08.c │ │ ├── Task09.c │ │ ├── Task10.c │ │ ├── Task11.c │ │ ├── Task12.c │ │ ├── Task13.c │ │ ├── Task14.c │ │ ├── Task15.c │ │ └── Task16.c │ ├── example2_gcd_naive.c │ ├── example3_gcd_euclid.c │ ├── example4_prime.c │ ├── example5_prime-factors.c │ ├── example5_prime-factors_better.c │ └── images │ │ ├── ExampleDrawings_Task07.png │ │ ├── ExampleDrawings_Task08.png │ │ ├── ExampleDrawings_Task10.png │ │ ├── ExampleDrawings_Task11.png │ │ ├── ExampleDrawings_Task12.png │ │ ├── ExampleDrawings_Task13.png │ │ └── Loop.png ├── Sem04 │ └── functions.c ├── Sem05 │ └── bitwise.c ├── Sem07 │ └── examples.c ├── Sem09 │ └── mem.c └── Sem10 │ └── strings.c ├── Practicum - Informatics ├── Week 01 - Data Types │ ├── Week 01.md │ └── Week01-Solutions.c ├── Week 02 - Conditions │ ├── Solutions │ │ ├── Task01.c │ │ ├── Task02.c │ │ ├── Task03.c │ │ ├── Task04.c │ │ ├── Task05.c │ │ ├── Task06.c │ │ ├── Task07.c │ │ ├── Task08.c │ │ ├── Task09.c │ │ └── Task10.c │ └── Week 02.md ├── Week 03 - Loops │ └── Week 03.md ├── Week 03 │ └── Week 03.md ├── Week 04 - Functions + Mini Exam │ ├── Week 04 - CoPrimes Task.md │ └── Week 04.md ├── Week 05 - Hard Problems Over Weeks 01 to 05 │ └── Week 05.md ├── Week 06 - Bitwise Operations │ └── Week 06.md ├── Week 07 - Bitwise Operations And Counting Systems │ └── Week 07.md ├── Week 08 - Arrays │ └── Week 08.md ├── Week 10 - Multdimensional Arrays │ └── Week 10.md ├── Week 11 - Dynamic memory │ ├── Week 11.md │ ├── advanced_tasks.md │ └── more_tasks.md ├── Week 12 - Strings │ └── Week 12.md └── Week 13 - Recursion │ └── Week 13.md ├── Practicum ├── README.md ├── Week 01 │ ├── Pract Solutions │ │ ├── Task01.cpp │ │ ├── Task02.cpp │ │ ├── Task03.cpp │ │ ├── Task04.cpp │ │ ├── Task05.cpp │ │ ├── Task06.cpp │ │ ├── Task07.cpp │ │ ├── Task08.cpp │ │ ├── Task09.cpp │ │ ├── Task10.cpp │ │ ├── Task11.cpp │ │ ├── Task12.cpp │ │ ├── Task13.cpp │ │ ├── Task14.cpp │ │ ├── Task15.cpp │ │ ├── Task16.cpp │ │ └── Task17.cpp │ └── README.md ├── Week 02 │ ├── MiniTest.md │ ├── Readme.md │ └── Solutions │ │ ├── Task1.cpp │ │ ├── Task10.cpp │ │ ├── Task11.cpp │ │ ├── Task12.cpp │ │ ├── Task13.cpp │ │ ├── Task14.cpp │ │ ├── Task15.cpp │ │ ├── Task16.cpp │ │ ├── Task2.cpp │ │ ├── Task3.cpp │ │ ├── Task4.cpp │ │ ├── Task5.5.cpp │ │ ├── Task5.cpp │ │ ├── Task6.cpp │ │ ├── Task7.cpp │ │ ├── Task8.cpp │ │ └── Task9.cpp ├── Week 03 │ ├── README.md │ ├── Solutions │ │ ├── task01_factorial.cpp │ │ ├── task02_ASCII_codes.cpp │ │ ├── task03_solutions_to_equation.cpp │ │ ├── task04_sum_of_digits.cpp │ │ ├── task04_sum_of_digits_bonus.cpp │ │ ├── task05_reversed_square_of_n.cpp │ │ ├── task06_print_numbers.cpp │ │ ├── task07_sum_of_three_digit_nums.cpp │ │ ├── task08_first_n_of_fibb.cpp │ │ ├── task09_print_primes.cpp │ │ ├── task10_missing_number.cpp │ │ ├── task11_divide_by_ten.cpp │ │ ├── task12_result_of_sum.cpp │ │ ├── task13_print_hours.cpp │ │ ├── task14_mutually_prime_numbers.cpp │ │ ├── task15_sides_of_triangle.cpp │ │ ├── task16_perfect_numbers.cpp │ │ ├── task17_sum_or_max_diff.cpp │ │ └── task18_special_number.cpp │ └── do-while.md ├── Week 04 │ ├── README.md │ └── Solutions │ │ ├── task01_absoluteValue.cpp │ │ ├── task02_bonus.cpp │ │ ├── task02_isDigit.cpp │ │ ├── task03_toUpper_toLower.cpp │ │ ├── task04_toNum_toChar.cpp │ │ ├── task05_power.cpp │ │ ├── task06_gcd.cpp │ │ ├── task07_isPrime.cpp │ │ ├── task08_getLength.cpp │ │ ├── task09_getDigitOnPosition.cpp │ │ ├── task10_lcm.cpp │ │ ├── task11_concat.cpp │ │ ├── task12_sumInInterval.cpp │ │ ├── task13_isSquareRoot.cpp │ │ ├── task14_Per_Area_isInCircle.cpp │ │ ├── task15_sortDigits.cpp │ │ ├── task16_isDivisibleByValue.cpp │ │ ├── task17_getRemainder.cpp │ │ ├── task18_getDivision.cpp │ │ └── task19_squareRootToLower.cpp ├── Week 05 │ ├── Solutions │ │ ├── task01_swapFunctions.cpp │ │ ├── task02_simplifyNominatorAndDenominator.cpp │ │ ├── task03_sortNumbers.cpp │ │ ├── task04_removeKth.cpp │ │ ├── task05_evenAndOddPositions.cpp │ │ ├── task06_subNumber.cpp │ │ ├── task07_swapDigitsInNum.cpp │ │ ├── task09_modifyNumber.cpp │ │ └── task10_countOfSwaps.cpp │ └── readme.md ├── Week 06 │ ├── Solutions │ │ ├── task01_getAverage.cpp │ │ ├── task02_isSorted.cpp │ │ ├── task03_getMinAndMax.cpp │ │ ├── task04_removeNumber.cpp │ │ ├── task05_isSymmetrical.cpp │ │ ├── task06_getLongestSeq.cpp │ │ ├── task07_reverseArray.cpp │ │ ├── task08_filterNegatives.cpp │ │ ├── task09_increaseByOne.cpp │ │ ├── task10_makeZero.cpp │ │ ├── task11_mostCommonNumber.cpp │ │ ├── task12_05_findMissing.cpp │ │ ├── task12_getCountOfOccurs.cpp │ │ ├── task13_Union_Intersection.cpp │ │ ├── task14_unifyAndSort.cpp │ │ └── task15_isSubarray.cpp │ ├── longestIncreasingSubsequence_Solution.cpp │ └── readme.md ├── Week 07 │ └── readme.md ├── Week 08 │ └── readme.md ├── Week 09 │ ├── Solutions │ │ ├── task_01_minElementInMatrix.cpp │ │ ├── task_02_printDiagonals.cpp │ │ ├── task_03_printZigZag.cpp │ │ ├── task_04_checkIfTriangleMatrix.cpp │ │ ├── task_05_transposeMatrix.cpp │ │ ├── task_06_isMagicSquare.cpp │ │ ├── task_07_sumAndMultByNMatrixes.cpp │ │ ├── task_08_multiplyMatrixes.cpp │ │ ├── task_09_spiralPrint.cpp │ │ └── task_10_turnRight.cpp │ ├── kontrolno.md │ └── readme.md ├── Week 10 │ ├── Additional.md │ ├── Readme.md │ ├── Solutions │ │ ├── task_01_isStringInMatrix.cpp │ │ ├── task_02_isPalindrome.cpp │ │ ├── task_03_modifyString.cpp │ │ ├── task_04_areAllLettersUnique.cpp │ │ ├── task_05_insertInText.cpp │ │ ├── task_06_countOfDigits.cpp │ │ ├── task_07_findDiff.cpp │ │ ├── task_08_makeLettersCapital.cpp │ │ ├── task_09_removeOccursOfChar.cpp │ │ ├── task_10_sortString.cpp │ │ └── task_11_fromStringToMatrix.cpp │ └── Solutions_AdditionalTasks │ │ ├── task_01_getCountOfOccurs.cpp │ │ ├── task_02_saveOnlyLowerCase.cpp │ │ ├── task_03_getSubstr.cpp │ │ ├── task_04_replaceInString.cpp │ │ ├── task_05_getLongestCommonPrefix.cpp │ │ ├── task_06_getIndexOfFirstOccur.cpp │ │ ├── task_07_countOfOccursOfString.cpp │ │ └── task_08_replaceWithStars.cpp ├── Week 11 │ └── readme.md ├── Week 12 │ └── readme.md └── Week 13 │ └── readme.md ├── README.md ├── Sem. 01 ├── Solutions │ ├── task1.cpp │ ├── task2.cpp │ ├── task3.cpp │ ├── task4.cpp │ ├── task5.cpp │ ├── task6.cpp │ ├── task7.cpp │ └── task8.cpp └── readme.md ├── Sem. 02 ├── Solutions │ ├── 01. DaysOfWeek.cpp │ ├── 02. Between100and200.cpp │ ├── 03. SpeedCheck.cpp │ ├── 04. Figures.cpp │ ├── 05. Time+15.cpp │ ├── 06. Chainsaw.cpp │ └── 07. PointInCircle.cpp └── readme.md ├── Sem. 03 ├── drawing_examples.cpp ├── ex1.cpp ├── ex2.cpp ├── ex3.cpp ├── ex4.cpp ├── ex5.cpp ├── ex6.cpp ├── ex7.cpp └── readme.md ├── Sem. 04 ├── Solutions │ ├── d │ ├── task 04.cpp │ └── task 09.cpp ├── example 01 - no functions.cpp ├── example 01 - with functions.cpp ├── examples 02-09.cpp ├── most_common_digit.cpp └── readme.md ├── Sem. 05 ├── README.md ├── bullsAndCows.cpp ├── maxAndMinDigit.cpp └── sortNumber.cpp ├── Sem. 06 ├── Readme.md ├── binarySearch.cpp ├── linearSearch.cpp ├── printArr.cpp ├── reverseArr.cpp └── sieveErathostenes.cpp ├── Sem. 07 ├── fromBinaryToDecimalWithIntegers.cpp ├── numericSystemsConverter.cpp ├── readme.md └── toBinaryFromDecimalWithIntegers.cpp ├── Sem. 08 ├── README.md ├── allEvenOneOdd.cpp ├── bitManipulations.cpp └── powerOfTwo.cpp ├── Sem. 09 ├── linearEquationsSolver.cpp ├── readme.md └── tic-tac-toe.cpp ├── Sem. 10 ├── basic_string_functions.cpp ├── number_to_str.cpp ├── readme.md ├── search_in_text.cpp └── str_to_number.cpp ├── Sem. 11 ├── censoreNumbers.cpp ├── readme.md ├── smallAndUpperStrings.cpp ├── sortArrayConcatOfTwoSortedArrays.cpp └── sortLower.cpp ├── Sem. 12 ├── 1. playersStatistics.cpp ├── 2. stringToWords.cpp ├── 3. splitString.cpp └── README.md ├── Sem. 13 ├── bubbleSort.cpp ├── insertionSort.cpp ├── readme.md ├── selectionSort.cpp └── Сортиращи_алгоритми.pdf ├── Sem. 14 ├── binary_search.cpp ├── fact.cpp ├── fast_power.cpp ├── fibb.cpp ├── invertCase.cpp ├── isPalindrome.cpp ├── linearSearch.cpp ├── lower_and_upper_count.cpp ├── max_element_in_array.cpp ├── power.cpp ├── readme.md ├── searchInText.cpp └── sum_array.cpp └── Sem. 15 ├── backtracking_example.cpp ├── boolVectorsEqualOnesAndZeros.cpp ├── boolVectorsGen.cpp ├── fibb_memoization.cpp ├── high-order-functions.cpp ├── minOperations_memoization.cpp ├── sumArrTail.cpp └── sum_numbers_string.cpp /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /C examples/Sem01/images/DataTypes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem01/images/DataTypes.png -------------------------------------------------------------------------------- /C examples/Sem01/images/OperatorCharacteristics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem01/images/OperatorCharacteristics.png -------------------------------------------------------------------------------- /C examples/Sem01/images/Variable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem01/images/Variable.png -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task01.c: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /C examples/Sem03/Solutions/Task02.c: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/ExampleDrawings_Task07.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/ExampleDrawings_Task08.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/ExampleDrawings_Task10.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/ExampleDrawings_Task11.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/ExampleDrawings_Task12.png -------------------------------------------------------------------------------- /C examples/Sem03/images/ExampleDrawings_Task13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/ExampleDrawings_Task13.png -------------------------------------------------------------------------------- /C examples/Sem03/images/Loop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/C examples/Sem03/images/Loop.png -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 - 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 | -------------------------------------------------------------------------------- /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 - 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 - 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 - 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 | -------------------------------------------------------------------------------- /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 - 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 - 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 - 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Practicum/README.md: -------------------------------------------------------------------------------- 1 | TBD -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | bool isEven = (n % 2 == 0); 9 | cout << isEven; 10 | } 11 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, m; 7 | cin >> n >> m; 8 | 9 | cout << "Divison quotient: " << (n / m); 10 | cout << endl; //new line 11 | cout << "Divison remainder: " << (n % m); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n, result = 0; 7 | cin >> n; 8 | 9 | result += n % 10; //get last digit of n 10 | result *= 10; 11 | n /= 10; //remove last digit of n 12 | 13 | result += n % 10; 14 | result *= 10; 15 | n /= 10; 16 | 17 | result += n; 18 | result++; 19 | 20 | cout << result; 21 | } 22 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | char ch; 7 | cin >> ch; 8 | 9 | bool isVowel = ((ch == 'a') || (ch == 'A') 10 | || (ch == 'e') || (ch == 'E') 11 | || (ch == 'i') || (ch == 'I') 12 | || (ch == 'o') || (ch == 'O') 13 | || (ch == 'u') || (ch == 'U')); 14 | cout << isVowel; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int grade; 7 | cin >> grade; 8 | 9 | bool isValidGrade = ((grade >= 2) && (grade <= 6)); 10 | 11 | cout << isValidGrade; 12 | } 13 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int number; 7 | cin >> number; 8 | 9 | int result = number / 10; 10 | cout << result; 11 | } 12 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | double centimeters; 7 | cin >> centimeters; 8 | 9 | double mult = 0.393701; 10 | double result = centimeters * mult; 11 | 12 | cout << result; 13 | } 14 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int num1, num2; 7 | cin >> num1 >> num2; 8 | 9 | int expr1 = num1 + num2; 10 | int expr2 = num1 - num2; 11 | 12 | int res = (expr1 * expr1 * expr1 * expr1) - (expr2 * expr2); 13 | 14 | cout << res; 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task09.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 01/Pract Solutions/Task10.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 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task11.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 01/Pract Solutions/Task12.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 01/Pract Solutions/Task13.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 01/Pract Solutions/Task14.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 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task15.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 01/Pract Solutions/Task16.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 | -------------------------------------------------------------------------------- /Practicum/Week 01/Pract Solutions/Task17.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 02/MiniTest.md: -------------------------------------------------------------------------------- 1 | ## Мини тест 11.10.2024г. 2 | 3 | **Въпрос 1:** Какво ще се изведе на конзолата? 4 | 5 | ```c++ 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int number = 2; 12 | 13 | if (number < 2) 14 | cout << "first"; 15 | else if (number = 0) 16 | cout << "second"; 17 | else if (number >= 2) 18 | cout << "third"; 19 | else 20 | cout << "unknown"; 21 | } 22 | ``` 23 | 24 | **Въпрос 2:** Какво ще се изведе след изпълнение на кода? 25 | 26 | ```c++ 27 | #include 28 | using namespace std; 29 | 30 | int main() 31 | { 32 | int num = 25; 33 | 34 | if (25 == (++(--(++num)))) { 35 | cout << "first"; 36 | } 37 | else if (27 == +num) { 38 | cout << "second"; 39 | } 40 | else if (26 == num--) { 41 | cout << "third"; 42 | } 43 | else if (26 == ++num) { 44 | cout << "fourth"; 45 | } 46 | else { 47 | cout << "fifth"; 48 | } 49 | } 50 | ``` 51 | 52 | **Въпрос 3:** Ще има ли разлика след изпълнение на двете програми? 53 | 54 | ***Bonus:** А коя е по-оптимална (бърза)?* 55 | 56 | ```c++ 57 | #include 58 | using namespace std; 59 | 60 | int main() 61 | { 62 | int grade; 63 | cin >> grade; 64 | if (grade == 2) 65 | cout << "fail"; 66 | else if (grade == 5) 67 | cout << "nice"; 68 | else if (grade == 6) 69 | cout << "excellent"; 70 | else 71 | cout << "error"; 72 | } 73 | ``` 74 | 75 | ```c++ 76 | #include 77 | using namespace std; 78 | 79 | int main() 80 | { 81 | int grade; 82 | cin >> grade; 83 | switch(grade) 84 | { 85 | case 2: cout << "fail"; break; 86 | case 3: 87 | case 4: 88 | case 5: cout << "nice"; break; 89 | case 6: cout << "excellent"; break; 90 | default: cout << "error"; 91 | } 92 | } 93 | ``` -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int number = 0; 7 | cin >> number; 8 | 9 | if (number > 0) 10 | { 11 | cout << "Positive"; 12 | } 13 | else if (number < 0) 14 | { 15 | cout << "Negative"; 16 | } 17 | else 18 | { 19 | cout << "Zero"; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const double EPSILON = 1e-6; 7 | double begin = 0, end = 0, value = 0; 8 | cin >> begin >> end >> value; 9 | 10 | if (value < begin || value > end) 11 | { 12 | cout << "Value out of bounds"; 13 | } 14 | else if (abs(value - begin) < EPSILON || abs(end - value) < EPSILON) 15 | { 16 | cout << "The number is within the given interval. The interval is closed."; 17 | } 18 | else 19 | { 20 | cout << "The number is within the given interval. The interval is open."; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int num = 0; 7 | cin >> num; 8 | 9 | unsigned int firstNumber = num / 1000 % 10 * 10 + num % 10; 10 | unsigned int secondSumber = num / 100 % 10 * 10 + num / 10 % 10; 11 | 12 | cout << firstNumber; 13 | 14 | if (firstNumber < secondSumber) 15 | { 16 | cout << " < "; 17 | } 18 | else if (firstNumber > secondSumber) 19 | { 20 | cout << " > "; 21 | } 22 | else 23 | { 24 | cout << " = "; 25 | } 26 | 27 | cout << secondSumber; 28 | 29 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // The data here is not validated. For Homework, try adding validation. 7 | // Also, think about adding the year as a third input parameter. 8 | unsigned int day = 0, month = 0; 9 | cout << "Date: "; cin >> day >> month; 10 | 11 | day += 1; 12 | 13 | switch (month) 14 | { 15 | case 1: 16 | case 3: 17 | case 5: 18 | case 7: 19 | case 8: 20 | case 10: 21 | case 12: 22 | if (day == 32) 23 | { 24 | day = 1; 25 | month++; 26 | } 27 | break; 28 | 29 | case 4: 30 | case 6: 31 | case 9: 32 | case 11: 33 | if (day == 31) 34 | { 35 | day = 1; 36 | month++; 37 | } 38 | break; 39 | 40 | case 2: 41 | if (day == 29) 42 | { 43 | day = 1; 44 | month++; 45 | } 46 | break; 47 | } 48 | 49 | month %= 12; 50 | 51 | cout << "Next date: " << day << " " << month; 52 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | double firstOperand = 0, secondOperand = 0; 7 | char operation = '\0'; 8 | 9 | cin >> firstOperand >> operation >> secondOperand; 10 | 11 | switch (operation) 12 | { 13 | case '+': 14 | cout << firstOperand + secondOperand; 15 | break; 16 | 17 | case '-': 18 | cout << firstOperand - secondOperand; 19 | break; 20 | 21 | case '*': 22 | cout << firstOperand * secondOperand; 23 | break; 24 | 25 | case '/': 26 | if (secondOperand == 0) 27 | { 28 | cout << "Cannot divide by 0"; 29 | break; 30 | } 31 | 32 | cout << firstOperand / secondOperand; 33 | break; 34 | } 35 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task14.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int a = 0, b = 0, c = 0, d = 0, e = 0; 7 | cin >> a >> b >> c >> d >> e; 8 | 9 | if (((a <= b && b >= c) || (a >= b && b <= c)) && 10 | ((b <= c && c >= d) || (b >= c && c <= d)) && 11 | ((c <= d && d >= e) || (c >= d && d <= e)) 12 | ) 13 | { 14 | cout << "yes"; 15 | } 16 | else 17 | { 18 | cout << "no"; 19 | } 20 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task15.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int radius = 0, x = 0, y = 0; 7 | cin >> radius >> x >> y; 8 | 9 | int distanceFromCentreSquared = x * x + y * y; 10 | int radiusSquared = radius * radius; 11 | 12 | if (distanceFromCentreSquared < radiusSquared) 13 | { 14 | cout << "In the circle"; 15 | } 16 | else if (distanceFromCentreSquared > radiusSquared) 17 | { 18 | cout << "Out of the circle"; 19 | } 20 | else 21 | { 22 | cout << "On the circle"; 23 | } 24 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task16.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | const char CONVERT_FROM_UPPER_TO_LOWER = 'a' - 'A'; 7 | 8 | // Think about adding validation to the input parameters 9 | char capitalStart, capitalEnd, lowerStart, lowerEnd, currentSymbol; 10 | cin >> capitalStart >> capitalEnd >> lowerStart >> lowerEnd >> currentSymbol; 11 | 12 | if (currentSymbol >= 'A' && currentSymbol <= 'Z') 13 | { 14 | currentSymbol += CONVERT_FROM_UPPER_TO_LOWER; 15 | } 16 | 17 | capitalStart += CONVERT_FROM_UPPER_TO_LOWER; 18 | capitalEnd += CONVERT_FROM_UPPER_TO_LOWER; 19 | 20 | // We will not be able to use these two variables for sub-task #4 21 | bool isInFirstSet = (currentSymbol >= capitalStart && currentSymbol <= capitalEnd); 22 | bool isInSecondSet = (currentSymbol >= lowerStart && currentSymbol <= lowerEnd); 23 | 24 | // Sub-task #1 25 | bool isInUnion = isInFirstSet || isInSecondSet; 26 | // Sub-task #2 27 | bool isInIntersection = isInFirstSet && isInSecondSet; 28 | // Sub-task #3 29 | bool isInDifference = isInFirstSet && !isInSecondSet; 30 | // Sub-task #4 31 | bool isInOnlyOneSet = isInUnion && !isInIntersection; 32 | 33 | cout << (isInUnion ? "true, " : "false, "); 34 | cout << (isInIntersection ? "true, " : "false, "); 35 | cout << (isInDifference ? "true, " : "false, "); 36 | cout << (isInOnlyOneSet ? "true, " : "false, "); 37 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int first = 0, second = 0; 7 | cin >> first >> second; 8 | 9 | cout << ((first > second) ? first : second); 10 | } 11 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int dayCode = 0; 7 | cin >> dayCode; 8 | 9 | switch (dayCode) 10 | { 11 | case 1: 12 | cout << "Monday"; 13 | break; 14 | case 2: 15 | cout << "Tuesday"; 16 | break; 17 | case 3: 18 | cout << "Wednesday"; 19 | break; 20 | case 4: 21 | cout << "Thursday"; 22 | break; 23 | case 5: 24 | cout << "Friday"; 25 | break; 26 | case 6: 27 | cout << "Saturday"; 28 | break; 29 | case 7: 30 | cout << "Sunday"; 31 | break; 32 | default: 33 | cout << "ERROR"; 34 | break; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int year = 0; 7 | cin >> year; 8 | 9 | if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) 10 | { 11 | cout << "Leap year"; 12 | } 13 | else 14 | { 15 | cout << "Not a leap year"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task5.5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int first = 0, second = 0, third = 0; 7 | cin >> first >> second >> third; 8 | 9 | if (first > 9 || second > 9 || third > 9) 10 | { 11 | cout << "Incorrect input data"; 12 | } 13 | 14 | int temp = 0; 15 | if (first > second) 16 | { 17 | temp = first; 18 | first = second; 19 | second = temp; 20 | } 21 | 22 | if (second > third) 23 | { 24 | temp = second; 25 | second = third; 26 | third = temp; 27 | } 28 | 29 | if (first > second) 30 | { 31 | temp = first; 32 | first = second; 33 | second = temp; 34 | } 35 | 36 | cout << third << second << first; 37 | } 38 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int first = 0, second = 0, third = 0; 7 | cin >> first >> second >> third; 8 | 9 | int temp = 0; 10 | if (first > second) 11 | { 12 | temp = first; 13 | first = second; 14 | second = temp; 15 | } 16 | 17 | if (second > third) 18 | { 19 | temp = second; 20 | second = third; 21 | third = temp; 22 | } 23 | 24 | if (first > second) 25 | { 26 | temp = first; 27 | first = second; 28 | second = temp; 29 | } 30 | 31 | 32 | cout << first << ' ' << second << ' ' << third; 33 | } 34 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int number = 2; 7 | 8 | if (number < 2) 9 | cout << "first"; 10 | else if (number = 0) 11 | cout << "second"; 12 | else if (number >= 2) 13 | cout << "third"; 14 | else 15 | cout << "unknown"; 16 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | char symbol = '\0'; 7 | cin >> symbol; 8 | 9 | if (symbol >= 'A' && symbol <= 'Z') 10 | { 11 | cout << "Uppercase "; 12 | symbol += 'a' - 'A'; 13 | } 14 | else if (symbol >= 'a' && symbol <= 'z') 15 | { 16 | cout << "Lowercase "; 17 | symbol += 'A' - 'a'; 18 | } 19 | 20 | switch (symbol) 21 | { 22 | case 'A': 23 | case 'a': 24 | case 'E': 25 | case 'e': 26 | case 'I': 27 | case 'i': 28 | case 'O': 29 | case 'o': 30 | case 'U': 31 | case 'u': 32 | cout << "Vowel" << endl; 33 | break; 34 | default: 35 | cout << "Consonant" << endl; 36 | break; 37 | } 38 | 39 | cout << symbol; 40 | } 41 | -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task8.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int number = 0; 7 | cin >> number; 8 | 9 | switch (number) 10 | { 11 | case 1: cout << 1 << ' '; 12 | case 3: cout << 3 << ' '; 13 | case 5: cout << 5 << ' '; 14 | case 7: cout << 7 << ' '; 15 | case 9: cout << 9 << ' '; 16 | case 11: 17 | cout << 11 << ' '; 18 | break; 19 | 20 | case 0: cout << 0 << ' '; 21 | case 2: cout << 2 << ' '; 22 | case 4: cout << 4 << ' '; 23 | case 6: cout << 6 << ' '; 24 | case 8: cout << 8 << ' '; 25 | case 10: cout << 10 << ' '; 26 | case 12: 27 | cout << 12 << ' '; 28 | break; 29 | 30 | default: 31 | cout << "Invalid number"; 32 | break; 33 | } 34 | } -------------------------------------------------------------------------------- /Practicum/Week 02/Solutions/Task9.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | double first = 0, second = 0, third = 0; 7 | cin >> first >> second >> third; 8 | 9 | if (first <= 0 || second <= 0 || third <= 0) 10 | { 11 | cout << "Incorrect input data"; 12 | } 13 | else if ((first + second) > third && 14 | (second + third) > first && 15 | (first + third) > second) 16 | { 17 | cout << "This triangle exists."; 18 | } 19 | else 20 | { 21 | cout << "This triangle doesn't exist."; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task01_factorial.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | int factoriel = 1; 11 | 12 | while (n > 0) { 13 | factoriel *= n; 14 | n--; 15 | } 16 | 17 | cout << factoriel; 18 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task02_ASCII_codes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, m; 8 | cin >> n >> m; 9 | if ((n < 0 || n > 255) || (m < 0 || m > 255) || (n > m)) { 10 | cout << "Inavlid codes!"; 11 | } 12 | else { 13 | for (int i = n; i <= m; i++) { 14 | char current = i; 15 | cout << current << endl; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task03_solutions_to_equation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n; 8 | cin >> n; 9 | 10 | for (int i = 1; i < n; i++) { 11 | for (int j = 1; j < n; j++) { 12 | if (i + j == n) { 13 | cout << "x = " << i << ", y = " << j << endl; 14 | } 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task04_sum_of_digits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, res = 0; 6 | cin >> n; 7 | 8 | if (n < 0) 9 | n = n * -1; 10 | 11 | 12 | while (n != 0) { 13 | res = res + (n % 10); 14 | n = n / 10; 15 | } 16 | 17 | cout << res; 18 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task04_sum_of_digits_bonus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int n, res = 0; 7 | cin >> n; 8 | 9 | if (n < 0) 10 | n = n * -1; 11 | 12 | do { 13 | if (res != 0) { 14 | n = res; 15 | res = 0; 16 | } 17 | 18 | 19 | while (n != 0) { 20 | res += (n % 10); 21 | n /= 10; 22 | } 23 | 24 | } while (res > 9); 25 | 26 | 27 | cout << res; 28 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task05_reversed_square_of_n.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n; 6 | cin >> n; 7 | int toReverse = n * n; 8 | int reversed = 0; 9 | while (toReverse != 0) { 10 | reversed *= 10; 11 | int lastDigit = toReverse % 10; 12 | reversed += lastDigit; 13 | toReverse /= 10; 14 | } 15 | cout << reversed; 16 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task06_print_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, m; 6 | cin >> n >> m; 7 | int nMultByM = n * m; 8 | for (int i = 1; i <= nMultByM; i++) { 9 | cout << i << " "; 10 | 11 | if (i % n == 0) { 12 | cout << endl; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task07_sum_of_three_digit_nums.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int result = 0; 6 | for (int i = 100; i < 1000; i++) { 7 | 8 | int copyOfI = i; 9 | int sumOfDigits = 0; 10 | 11 | while (copyOfI > 0) { 12 | sumOfDigits += (copyOfI % 10); 13 | copyOfI /= 10; 14 | } 15 | 16 | if (sumOfDigits < 10 || sumOfDigits > 99) { 17 | continue; 18 | } 19 | 20 | bool isPrime = true; 21 | int sqrtOfSum = sqrt(sumOfDigits); 22 | 23 | for (int j = 2; j <= sqrtOfSum; j++) { 24 | if (sumOfDigits % j == 0) { 25 | isPrime = false; 26 | break; 27 | } 28 | } 29 | 30 | if (isPrime == true) { 31 | result += i; 32 | } 33 | } 34 | cout << result; 35 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task08_first_n_of_fibb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | unsigned n, first = 0, second = 1; 6 | cin >> n; 7 | 8 | if (n > 0) 9 | cout << first << " "; 10 | 11 | if (n > 1) 12 | cout << second << " "; 13 | 14 | for (int i = 3; i <= n; i++) { 15 | unsigned next = first + second; 16 | first = second; 17 | second = next; 18 | cout << next << " "; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task09_print_primes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n, m; 8 | cin >> n >> m; 9 | 10 | int sum = 0; 11 | 12 | for (int i = n; i <= m; i++) { 13 | 14 | int sqrtOfI = sqrt(i); 15 | bool isPrime = true; 16 | for (int j = 2; j <= sqrtOfI; j++) { 17 | if (i % j == 0) { 18 | isPrime = false; 19 | break; 20 | } 21 | } 22 | if (isPrime == true) { 23 | sum += i; 24 | } 25 | } 26 | cout << sum; 27 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task10_missing_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | 9 | int sumOfN = n * (n + 1) / 2; 10 | 11 | int currentSum = 0; 12 | for (int i = 0; i < n - 1; i++) { 13 | int current; 14 | cin >> current; 15 | currentSum += current; 16 | } 17 | 18 | cout << sumOfN - currentSum; 19 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task11_divide_by_ten.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | while (true) { 7 | 8 | int n; 9 | cin >> n; 10 | 11 | int cpyOfN = n; 12 | int sumOfDigits = 0; 13 | 14 | while (cpyOfN > 0) { 15 | sumOfDigits += cpyOfN % 10; 16 | cpyOfN /= 10; 17 | } 18 | 19 | int sum = n + sumOfDigits; 20 | if (sum % 10 == 0) { 21 | cout << "The sum is: " << sum; 22 | break; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task12_result_of_sum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | unsigned n, res = 0; 6 | cin >> n; 7 | 8 | for (unsigned i = 1; i <= n; i++) 9 | res += (i * (i + 1) / 2); 10 | 11 | cout << res; 12 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task13_print_hours.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/Practicum/Week 03/Solutions/task13_print_hours.cpp -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task14_mutually_prime_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | unsigned int number = 0; 7 | cin >> number; 8 | 9 | unsigned int sum = 0; 10 | int current = 0; 11 | cin >> current; 12 | 13 | while (current != -1) 14 | { 15 | int first = number; 16 | int second = current; 17 | 18 | if (first < second) //swap 19 | { 20 | int temp = first; 21 | first = second; 22 | second = temp; 23 | } 24 | 25 | while (second != 0) 26 | { 27 | int mod = first % second; 28 | first = second; 29 | second = mod; 30 | } 31 | 32 | if (first == 1) 33 | { 34 | sum += current; 35 | } 36 | 37 | cin >> current; 38 | } 39 | 40 | cout << sum; 41 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task15_sides_of_triangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int N; 6 | cin >> N; 7 | 8 | for (int a = 1; a <= N; a++) { 9 | for (int b = a; b <= N; b++) { 10 | for (int c = b; c <= N; c++) { 11 | if (a * a + b * b == c * c) { 12 | cout << a << ", " << b << ", " << c << endl; 13 | } 14 | } 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task16_perfect_numbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int start = 0, end = 0; 6 | cin >> start >> end; 7 | 8 | for (int i = start; i <= end; i++) { 9 | 10 | int sum = 0; 11 | for (int j = 1; j < i; j++) { 12 | if (i % j == 0) { 13 | sum += j; 14 | if (sum > i) { 15 | break; 16 | } 17 | } 18 | } 19 | 20 | if (sum == i) { 21 | cout << i << endl; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task17_sum_or_max_diff.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | unsigned int groupsCount = 0; 7 | cin >> groupsCount; 8 | 9 | int first = 0, second = 0; 10 | cin >> first >> second; 11 | int lastSum = first + second; 12 | unsigned int maxDiff = 0; 13 | 14 | for (size_t i = 1; i < groupsCount; i++) { 15 | cin >> first >> second; 16 | int currSum = first + second; 17 | 18 | unsigned int diff = abs(currSum - lastSum); 19 | if (diff >= maxDiff) { 20 | maxDiff = diff; 21 | } 22 | 23 | lastSum = currSum; 24 | } 25 | 26 | if (maxDiff == 0) { 27 | cout << "Equal sums: " << lastSum; 28 | } 29 | else { 30 | cout << "Different sums. Max Difference: " << maxDiff; 31 | } 32 | } -------------------------------------------------------------------------------- /Practicum/Week 03/Solutions/task18_special_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | unsigned int baseNumber = 0; 6 | cin >> baseNumber; 7 | int start = 0, end = 0; 8 | cin >> start >> end; 9 | 10 | for (int i = start; i <= end; i++) { 11 | int curr = i; 12 | bool isSpecial = true; 13 | do { 14 | int digit = curr % 10; 15 | if (digit == 0 || baseNumber % digit != 0) { 16 | isSpecial = false; 17 | break; 18 | } 19 | 20 | curr /= 10; 21 | } while (curr != 0); 22 | 23 | if (isSpecial) { 24 | cout << i << ' '; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Practicum/Week 03/do-while.md: -------------------------------------------------------------------------------- 1 | # Цикъл do-while 2 | 3 | ```c++ 4 | do { 5 | <действия> 6 | } while(<условие>); 7 | ``` 8 | 9 | Семантика: 10 | 11 | * do (направи/прави), while (докато) – запазени думи. 12 | * <действия> - инициализира се кодът, който трябва да се изпълни определен брой пъти. 13 | * <условие> - булев израз 14 | 15 | Изпълнява се тялото на цикъла поне веднъж, след което се пресмята стойността на <условие>. 16 | Ако то е false, изпълнението на оператора do/while завършва. 17 | В противен случай се повтарят действията: 18 | 19 | * Изпълнение на тялото на цикъла. 20 | * Пресмятане на стойността на <условие>, докато <условие> = true. 21 | 22 | **Пример 1:** 23 | ```c++ 24 | int i = 2; 25 | do { 26 | std::cout << "Hello World\n"; 27 | i++; 28 | } while (i < 1); 29 | ``` 30 | 31 | **Пример 2:** 32 | ```c++ 33 | int a = 0; 34 | do { 35 | std::cout << "value of a: " << a << std::endl; 36 | a += 1; 37 | } while (a < 20) 38 | ``` 39 | -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task01_absoluteValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned absoluteValue(int n) { 5 | if (n < 0) 6 | return -n; 7 | return n; 8 | } 9 | 10 | int main() { 11 | int n; 12 | cin >> n; 13 | cout << absoluteValue(n); 14 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task02_bonus.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isLower(char a) { 5 | return ((a >= 'a') && (a <= 'z')); 6 | } 7 | 8 | bool isUpper(char a) { 9 | return ((a >= 'A') && (a <= 'Z')); 10 | } 11 | 12 | int main() { 13 | char a; 14 | cin >> a; 15 | cout << isLower(a) << " " << isUpper(a); 16 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task02_isDigit.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isDigit(char a) { 5 | return ((a >= '0') && (a <= '9')); 6 | } 7 | 8 | int main() { 9 | char ch; 10 | cin >> ch; 11 | cout << isDigit(ch); 12 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task03_toUpper_toLower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //functions from task 2 5 | bool isLower(char a) { 6 | return ((a >= 'a') && (a <= 'z')); 7 | } 8 | bool isUpper(char a) { 9 | return ((a >= 'A') && (a <= 'Z')); 10 | } 11 | 12 | 13 | char toUpper(char a) { 14 | if (!isLower(a)) { 15 | return a; 16 | } 17 | return a - 'a' + 'A'; 18 | } 19 | 20 | char toLower(char a) { 21 | if (!isUpper(a)) { 22 | return a; 23 | } 24 | return a + 'a' - 'A'; 25 | } 26 | 27 | int main() { 28 | 29 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task04_toNum_toChar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //function from task 2 5 | bool isDigit(char a) { 6 | return ((a >= '0') && (a <= '9')); 7 | } 8 | 9 | unsigned toNumber(char a) { 10 | if (!isDigit(a)) { 11 | return a; 12 | } 13 | return a - 48; 14 | } 15 | 16 | char toCharacter(int num) { 17 | if (num > 9 || num < 0) { 18 | return num; 19 | } 20 | return num + '0'; 21 | } 22 | 23 | 24 | int main() { 25 | 26 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task05_power.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int base, unsigned int exponent) { 5 | int result = 1; 6 | 7 | if (!base) 8 | return base; 9 | 10 | if (!exponent) 11 | return exponent; 12 | 13 | for (unsigned i = 0; i < exponent; i++) 14 | result = result * base; 15 | 16 | return result; 17 | } 18 | 19 | int main() { 20 | int num, k; 21 | cin >> num >> k; 22 | cout << power(num, k); 23 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task06_gcd.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned gcd(int n, int k) { 5 | if (n < k) { 6 | int temp = n; 7 | n = k; 8 | k = temp; 9 | } 10 | while (k != 0) { 11 | int mod = n % k; 12 | n = k; 13 | k = mod; 14 | } 15 | return n; 16 | } 17 | 18 | int main() { 19 | int n, k; 20 | cin >> n >> k; 21 | int result = gcd(n, k); 22 | cout << result; 23 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task07_isPrime.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPrime(int num) { 5 | if (num <= 1) { 6 | return false; 7 | } 8 | bool isPrime = true; 9 | double rootOfNum = sqrt(num); 10 | for (unsigned i = 0; i <= rootOfNum; i++) { 11 | if (num % i == 0) { 12 | isPrime = false; 13 | break; 14 | } 15 | } 16 | return isPrime; 17 | } 18 | 19 | int main() { 20 | int n = 0; 21 | cin >> n; 22 | bool result = isPrime(n); 23 | cout << result; 24 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task08_getLength.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned getLength(int number) { 5 | unsigned result = 0; 6 | if (!number) { 7 | return ++result; 8 | } 9 | while (number != 0) { 10 | ++result; 11 | number = number / 10; 12 | } 13 | return result; 14 | } 15 | 16 | int main() { 17 | int num; 18 | cin >> num; 19 | unsigned len = getLength(num); 20 | cout << len; 21 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task09_getDigitOnPosition.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //function from task 8 5 | unsigned getLength(int number) { 6 | unsigned result = 0; 7 | if (!number) { 8 | return ++result; 9 | } 10 | while (number != 0) { 11 | ++result; 12 | number = number / 10; 13 | } 14 | return result; 15 | } 16 | 17 | //function from task 1 18 | unsigned absoluteValue(int n) { 19 | if (n < 0) 20 | return -n; 21 | return n; 22 | } 23 | 24 | int getDigitOnPosition(int number, unsigned position) { 25 | unsigned len = getLength(number); 26 | if (position > len) { 27 | return -1; 28 | } 29 | //We convert the number to possitive because if we have (-123, 1) the program will return -1 30 | number = absoluteValue(number); 31 | 32 | unsigned whereTo = len - position; 33 | for (unsigned i = 0; i < whereTo; i++) { 34 | number = number / 10; 35 | } 36 | return number % 10; 37 | } 38 | 39 | int main() { 40 | 41 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task10_lcm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //func from task 6 5 | unsigned gcd(int n, int k) { 6 | if (n < k) { 7 | int temp = n; 8 | n = k; 9 | k = temp; 10 | } 11 | while (k != 0) { 12 | int mod = n % k; 13 | n = k; 14 | k = mod; 15 | } 16 | return n; 17 | } 18 | 19 | //func from task 1 20 | unsigned absoluteValue(int n) { 21 | if (n < 0) 22 | return -n; 23 | return n; 24 | } 25 | 26 | unsigned lcm(int n, int k) { 27 | if (n < 0) 28 | n = absoluteValue(n); 29 | if (k < 0) 30 | k = absoluteValue(k); 31 | 32 | return ((n * k) / gcd(n, k)); 33 | } 34 | 35 | int main() { 36 | 37 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task11_concat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int power(int base, unsigned int exponent) { 5 | int result = 1; 6 | 7 | if (!base) 8 | return base; 9 | 10 | if (!exponent) 11 | return exponent; 12 | 13 | for (unsigned i = 0; i < exponent; i++) 14 | result = result * base; 15 | 16 | return result; 17 | } 18 | 19 | unsigned getLength(int number) { 20 | unsigned result = 0; 21 | if (!number) { 22 | return ++result; 23 | } 24 | while (number != 0) { 25 | ++result; 26 | number = number / 10; 27 | } 28 | return result; 29 | } 30 | 31 | unsigned concat(unsigned first, unsigned second) { 32 | //Here we take advantage of the fact that the function takes 2 unsigned numbers and we don't need to look after corner cases 33 | first = first * power(10, getLength(second)); 34 | return first + second; 35 | } 36 | 37 | int main() { 38 | 39 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task12_sumInInterval.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumInIntervalSlow(int start, int finish) { 5 | int result = 0; 6 | for (int i = start; i <= finish; ++i) 7 | result += i; 8 | return result; 9 | } 10 | 11 | int sumInIntervalFast(int start, int finish) { 12 | return ((finish - start + 1) * start + ((finish - start + 1) * (finish - start) / 2)); 13 | } 14 | 15 | 16 | int main() { 17 | 18 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task13_isSquareRoot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSquareRootVar1(int number) { 5 | double root = sqrt(number), epsilon = 1e-6; 6 | 7 | int rootInt = root; 8 | return (root - rootInt < epsilon); 9 | } 10 | 11 | bool isSquareRootVar2(int number) { 12 | double root = sqrt(number); 13 | int rootInt = root; 14 | return ((rootInt * rootInt) == number); 15 | } 16 | 17 | int main() { 18 | int n; 19 | cin >> n; 20 | bool res1 = isSquareRootVar1(n); 21 | bool res2 = isSquareRootVar2(n); 22 | cout << res1 << " " << res2; //res1 and res2 are supposed to be equal 23 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task14_Per_Area_isInCircle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isTriangleValid(unsigned a, unsigned b, unsigned c) { 5 | return ((a + b > c) && (a + c > b) && (b + c > a)); 6 | } 7 | 8 | unsigned getDist(int x1, int y1, int x2, int y2) { 9 | return sqrt((x1 * x2) + (y1 * y2)); 10 | } 11 | 12 | unsigned perimeter(int x1, int y1, int x2, int y2, int x3, int y3) { 13 | unsigned side1 = getDist(x1, y1, x2, y2); 14 | unsigned side2 = getDist(x1, y1, x3, y3); 15 | unsigned side3 = getDist(x2, y2, x3, y3); 16 | 17 | if (isTriangleValid(side1, side2, side3)) 18 | return (getDist(x1, y1, x2, y2) + getDist(x1, y1, x3, y3) + getDist(x2, y2, x3, y3)); 19 | 20 | return 0; 21 | } 22 | 23 | unsigned getArea(int x1, int y1, int x2, int y2, int x3, int y3) { 24 | unsigned side1 = getDist(x1, y1, x2, y2); 25 | unsigned side2 = getDist(x1, y1, x3, y3); 26 | unsigned side3 = getDist(x2, y2, x3, y3); 27 | 28 | if (isTriangleValid(side1, side2, side3)) 29 | { 30 | double p = (side1 + side2 + side3) / 2; 31 | return sqrt(p * (p - side1) * (p - side2) * (p - side3)); 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | bool isInCircleWithRadius(int x1, int y1, int x2, int y2, int x3, int y3, unsigned r) { 38 | unsigned side1 = getDist(x1, y1, x2, y2); 39 | unsigned side2 = getDist(x1, y1, x3, y3); 40 | unsigned side3 = getDist(x2, y2, x3, y3); 41 | 42 | if (!isTriangleValid(side1, side2, side3)) 43 | return false; 44 | 45 | return ((getDist(x1, y1, 0, 0) <= r) && (getDist(x2, y2, 0, 0) <= r) && (getDist(x3, y3, 0, 0) <= r)); 46 | } 47 | 48 | int main() { 49 | 50 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task15_sortDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned concatDigitAtBack(unsigned n, unsigned digit) { 5 | return n * 10 + digit; 6 | } 7 | 8 | unsigned countDigitOccurences(unsigned n, unsigned digit) { 9 | if (digit == 0 && n == 0) 10 | return 1; 11 | 12 | unsigned count = 0; 13 | while (n != 0) 14 | { 15 | int lastDigit = n % 10; 16 | if (lastDigit == digit) 17 | count++; 18 | n /= 10; 19 | } 20 | return count; 21 | } 22 | 23 | unsigned concatCountTimesDigit(unsigned a, unsigned digit, unsigned count) { 24 | unsigned result = a; 25 | for (int i = 0; i < count; i++) 26 | result = concatDigitAtBack(result, digit); 27 | return result; 28 | } 29 | 30 | unsigned sortDigits(unsigned n) { 31 | unsigned result = 0; 32 | for (int i = 9; i >= 0; i--) 33 | { 34 | unsigned count = countDigitOccurences(n, i); 35 | result = concatCountTimesDigit(result, i, count); 36 | } 37 | return result; 38 | } 39 | 40 | int main() { 41 | unsigned input; 42 | cin >> input; 43 | unsigned res = sortDigits(input); 44 | cout << res; 45 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task16_isDivisibleByValue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isDivisableByValue(int number, int divisor) { 5 | return (((number / divisor) * divisor) == number); 6 | } 7 | 8 | int main() { 9 | int num, div; 10 | cin >> num >> div; 11 | bool result = isDivisableByValue(num, div); 12 | cout << result; 13 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task17_getRemainder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned getRemainder(int first, int second) { 5 | return first - (first / second); 6 | } 7 | 8 | int main() { 9 | int a, b; 10 | cin >> a >> b; 11 | cout << getRemainder(a, b); 12 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task18_getDivision.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned absoluteValue(int n) { 5 | if (n < 0) 6 | return -n; 7 | return n; 8 | } 9 | 10 | int getDivision(int first, int second) { 11 | bool negativeRes = ((first < 0 && second >= 0) || (first >= 0 && second < 0)); 12 | first = absoluteValue(first); 13 | second = absoluteValue(second); 14 | 15 | first = first - (first % second); //10 - (10 % 2) = 10 - 0 16 | int result = 0; 17 | while (first != 0) 18 | { 19 | ++result; //1, 2, 3, 4, 5 20 | first = first - second; //10 - 2 = 8, 8 - 2 = 6, 6 - 2 = 4, 4 - 2 = 2, 2 - 2 = 0 -> end 21 | } 22 | 23 | if (negativeRes) 24 | return -result; 25 | 26 | return result; 27 | } 28 | 29 | int main() { 30 | 31 | } -------------------------------------------------------------------------------- /Practicum/Week 04/Solutions/task19_squareRootToLower.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned squareRootToLower(int number) { 5 | if (number < 1) 6 | return 0; 7 | 8 | unsigned result = 0; 9 | 10 | while ((result * result) <= number) 11 | result++; 12 | 13 | return --result; 14 | } 15 | 16 | int main() { 17 | int num; 18 | cin >> num; 19 | unsigned res = squareRootToLower(num); 20 | cout << res; 21 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task01_swapFunctions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int& first, int& second) { 5 | int temp = first; 6 | first = second; 7 | second = temp; 8 | } 9 | 10 | void swap(double& first, double& second) { 11 | double temp = first; 12 | first = second; 13 | second = temp; 14 | } 15 | 16 | int main() { 17 | int a, b; 18 | cin >> a >> b; 19 | swap(a,b); 20 | cout << a << " " << b; 21 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task03_sortNumbers.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int& a, int& b) { 5 | int temp = a; 6 | a = b; 7 | b = temp; 8 | } 9 | 10 | void sortNumbers(int& min, int& mid, int& max) { 11 | if(min > mid) { 12 | swap(min, mid); 13 | } 14 | if(mid > max) { 15 | swap(mid, max); 16 | } 17 | } 18 | 19 | int main() { 20 | int a, b, c; 21 | cin >> a >> b >> c; //3 2 4 22 | sortNumbers(a, b, c); //2 3 4 23 | cout << a << ", " << b << ", " << c << endl; 24 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task04_removeKth.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned getLength(int number) { 5 | unsigned result = 0; 6 | if (!number) { 7 | return ++result; 8 | } 9 | while (number != 0) { 10 | ++result; 11 | number = number / 10; 12 | } 13 | return result; 14 | } 15 | 16 | int power(int base, unsigned int exponent) { 17 | int result = 1; 18 | if (!base) { 19 | return base; 20 | } 21 | if (!exponent) { 22 | return exponent; 23 | } 24 | for (unsigned i = 0; i < exponent; i++) { 25 | result = result * base; 26 | } 27 | return result; 28 | } 29 | 30 | unsigned int reverse(unsigned int n) { 31 | unsigned reversedNumber = 0; 32 | while (n != 0) { 33 | unsigned int lastDigit = n % 10; 34 | (reversedNumber *= 10) += lastDigit; 35 | n /= 10; //removes the last digit 36 | } 37 | return reversedNumber; 38 | } 39 | 40 | 41 | void removeAtIndex(int& n, int k) { 42 | int len = getLength(n); 43 | if (len < k) { 44 | return; 45 | } 46 | int whereTo = len - k; 47 | int remainder = 0; 48 | for (unsigned i = 0; i < whereTo; i++) { 49 | remainder = ((remainder * 10) + (n % 10)); 50 | n = n / 10; 51 | } 52 | 53 | remainder = reverse(remainder); 54 | n = n / 10; 55 | n = (n * power(10, whereTo)) + remainder; 56 | } 57 | 58 | int main() { 59 | 60 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task05_evenAndOddPositions.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned int reverse(unsigned int n) { 5 | unsigned reversedNumber = 0; 6 | while (n != 0) { 7 | unsigned int lastDigit = n % 10; 8 | (reversedNumber *= 10) += lastDigit; 9 | n /= 10; //removes the last digit 10 | } 11 | return reversedNumber; 12 | } 13 | 14 | void splitInOddsAndEvens(int number, int& resultEven, int& resultOdd) { 15 | resultEven = 0; 16 | resultOdd = 0; 17 | 18 | unsigned reversedNum = reverse(number); 19 | bool isCurrentPosOdd = true; 20 | 21 | while (reversedNum != 0) { 22 | 23 | unsigned digitToAdd = reversedNum % 10; 24 | reversedNum = reversedNum / 10; 25 | 26 | if (isCurrentPosOdd) { 27 | resultOdd = resultOdd * 10 + digitToAdd; 28 | isCurrentPosOdd = false; 29 | 30 | continue; 31 | } 32 | 33 | resultEven = resultEven * 10 + digitToAdd; 34 | isCurrentPosOdd = true; 35 | } 36 | } 37 | 38 | int main() { 39 | int n; 40 | cin >> n; 41 | 42 | int even, odd; 43 | splitInOddsAndEvens(n, even, odd); 44 | cout << even << ", " << odd; 45 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task06_subNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | unsigned getLength(int number) { 6 | unsigned result = 0; 7 | if (!number) 8 | return ++result; 9 | 10 | while (number != 0) { 11 | ++result; 12 | number = number / 10; 13 | } 14 | return result; 15 | } 16 | 17 | int power(int base, unsigned power) { 18 | int result = 1; 19 | for (unsigned i = 0; i < power; ++i) { 20 | result *= base; 21 | } 22 | return result; 23 | } 24 | 25 | void subNumber(int& n, unsigned i, unsigned j) { 26 | unsigned len = getLength(n); 27 | 28 | if (j > len) 29 | return; 30 | 31 | unsigned iterations = len - i + 1, whenToStartSaving = len - j; 32 | unsigned sub = 0; 33 | for (int i = 1; i <= iterations; ++i) { 34 | if (i > whenToStartSaving) { 35 | int removedNum = n % 10; 36 | n /= 10; 37 | 38 | //Could be optimised easily by not calling the Power function, but to save the power of 10 39 | removedNum *= power(10, i - whenToStartSaving - 1); 40 | sub += removedNum; 41 | } 42 | else 43 | n /= 10; 44 | } 45 | n = sub; 46 | } 47 | 48 | void subNumberOptimized(int& n, unsigned i, unsigned j) { 49 | unsigned len = getLength(n); 50 | 51 | if (j > len) 52 | return; 53 | 54 | unsigned iterations = len - i + 1, whenToStartSaving = len - j, power = 1; 55 | unsigned sub = 0; 56 | for (int i = 1; i <= iterations; ++i) { 57 | if (i > whenToStartSaving) { 58 | int removedNum = n % 10; 59 | n /= 10; 60 | 61 | removedNum *= power; 62 | power *= 10; 63 | sub += removedNum; 64 | } 65 | else 66 | n /= 10; 67 | } 68 | n = sub; 69 | } 70 | 71 | 72 | int main() { 73 | int n = 1234567; 74 | subNumberOptimized(n, 2, 5); 75 | cout << n; 76 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task07_swapDigitsInNum.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned getLength(int number) { 5 | unsigned result = 0; 6 | if (!number) { 7 | return ++result; 8 | } 9 | while (number != 0) { 10 | ++result; 11 | number = number / 10; 12 | } 13 | return result; 14 | } 15 | 16 | unsigned absN(int n) { 17 | if (n < 0) { 18 | return -n; 19 | } 20 | return n; 21 | } 22 | 23 | int power(int base, unsigned int exponent) { 24 | int result = 1; 25 | 26 | if (!base) { 27 | return base; 28 | } 29 | if (!exponent) { 30 | return exponent; 31 | } 32 | for (unsigned i = 0; i < exponent; i++) { 33 | result = result * base; 34 | } 35 | return result; 36 | } 37 | 38 | int getDigitOnPosition(int number, unsigned position) { 39 | unsigned len = getLength(number); 40 | if (position > len) { 41 | return -1; 42 | } 43 | //We convert the number to possitive because if we have (-123, 1) the program will return -1 44 | number = absN(number); 45 | 46 | unsigned whereTo = len - position; 47 | for (unsigned i = 0; i < whereTo; i++) 48 | number = number / 10; 49 | 50 | return number % 10; 51 | } 52 | 53 | //This solution could be optimised by not using getDigitOnPosition 54 | void swapDigitsOnPos(int& n, int& m, int index) { 55 | unsigned lenN = getLength(n), lenM = getLength(m); 56 | if (index >= lenN || index >= lenM) 57 | return; 58 | 59 | unsigned digitN = getDigitOnPosition(n, index), digitM = getDigitOnPosition(m, index); 60 | unsigned cutterN = power(10, lenN - index), cutterM = power(10, lenM - index); 61 | unsigned remainderN = n % cutterN, remainderM = m % cutterM; 62 | 63 | n = n / (cutterN * 10); 64 | m = m / (cutterM * 10); 65 | 66 | n = n * 10 + digitM; 67 | m = m * 10 + digitN; 68 | 69 | n = n * cutterN + remainderN; 70 | m = m * cutterM + remainderM; 71 | } 72 | 73 | int main() { 74 | 75 | } -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task09_modifyNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int extractLastKDigits(int a, int k) { 5 | int power = 1; 6 | for (int i = 0; i < k; i++) { 7 | power *= 10; 8 | } 9 | return a % power; 10 | } 11 | 12 | int removeLastKDigits(int a, int k) { 13 | int power = 1; 14 | for (int i = 0; i < k; i++) { 15 | power *= 10; 16 | } 17 | int result = a / power; 18 | return result; 19 | } 20 | 21 | int countDigits(int n) { 22 | int count = 0; 23 | while (n > 0) { 24 | n /= 10; 25 | count++; 26 | } 27 | return count; 28 | } 29 | 30 | int mergeNumbers(int lastKDigits, int b) { 31 | int bDigitsCount = countDigits(b); 32 | int power = 1; 33 | 34 | for (int i = 0; i < bDigitsCount; i++) { 35 | power *= 10; 36 | } 37 | int result = lastKDigits * power + b; 38 | return result; 39 | } 40 | 41 | int main() { 42 | int a, b, k; 43 | cin >> a >> b >> k; 44 | 45 | int lastKDigits = extractLastKDigits(a, k); 46 | 47 | int newA = removeLastKDigits(a, k); 48 | 49 | int result = mergeNumbers(lastKDigits, b); 50 | 51 | cout << newA << endl; 52 | cout << result << endl; 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Practicum/Week 05/Solutions/task10_countOfSwaps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool validationOfANumber(int number, int n, int& countOfZeros) { 5 | while (number != 0) { 6 | int digit = number % 10; 7 | if (digit == 1) { 8 | n--; 9 | number = number / 10; 10 | continue; 11 | } 12 | else if (digit == 0) { 13 | n--; 14 | countOfZeros++; 15 | number = number / 10; 16 | continue; 17 | } 18 | 19 | return false; 20 | } 21 | 22 | countOfZeros += n; 23 | return (n >= 0); 24 | } 25 | 26 | bool validationOfTwoNumbers(int first, int second, int n) { 27 | int zerosInFirst = 0, zerosInSecond = 0; 28 | bool okFirst = validationOfANumber(first, n, zerosInFirst); 29 | bool okSecond = validationOfANumber(second, n, zerosInSecond); 30 | 31 | return okFirst && okSecond && (zerosInFirst == zerosInSecond); 32 | } 33 | 34 | int counterOfMissmatchedZeros(int first, int second, int n) { 35 | int result = 0; 36 | while (n != 0) { 37 | int lastDigitOfFirst = first % 10, lastDigitOfSecond = second % 10; 38 | 39 | if (!lastDigitOfFirst) { 40 | if (lastDigitOfFirst != lastDigitOfSecond) 41 | result++; 42 | } 43 | 44 | first = first / 10; 45 | second = second / 10; 46 | n--; 47 | } 48 | 49 | return result; 50 | } 51 | 52 | int countOfSwaps() { 53 | int n; 54 | cin >> n; 55 | int first, second; 56 | cin >> first >> second; 57 | 58 | bool okay = validationOfTwoNumbers(first, second, n); 59 | 60 | if (okay) { 61 | return counterOfMissmatchedZeros(first, second, n); 62 | } 63 | return -1; 64 | } 65 | 66 | int main() { 67 | 68 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task01_getAverage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned ARR_SIZE = 4; 5 | 6 | double average(const int array[], unsigned size) { 7 | double sum = 0; 8 | for (unsigned i = 0; i < size; i++) { 9 | sum += array[i]; 10 | } 11 | return (sum / size); 12 | } 13 | 14 | int main() { 15 | int arr[ARR_SIZE] = {5, 7, 4, 9}; 16 | cout << average(arr, ARR_SIZE); 17 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task02_isSorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isSorted(const int array[], unsigned size) { 5 | if (size <= 1) { 6 | return true; 7 | } 8 | int howSorted; // -1 - Downgrading, 1 - Upgrading, 0 - Undefined 9 | 10 | if (array[0] == array[1]) { 11 | howSorted = 0; 12 | } 13 | else if (array[0] < array[1]) { 14 | howSorted = -1; 15 | } 16 | else { 17 | howSorted = 1; 18 | } 19 | size = size - 1; 20 | 21 | for (unsigned i = 1; i < size; ++i){ 22 | 23 | if (howSorted == -1 && array[i] >= array[i + 1]) { 24 | continue; 25 | } 26 | else if (howSorted == 1 && array[i] <= array[i + 1]) { 27 | continue; 28 | } 29 | else if (howSorted == 0 && array[i] == array[i + 1]) { //The first 3 cases can and should be united in one, but we do it like that so the code can be read easily 30 | continue; 31 | } 32 | else if (howSorted == 0 && array[i] > array[i + 1]) { 33 | howSorted = -1; 34 | continue; 35 | } 36 | else if (howSorted == 0 && array[i] < array[i + 1]) { 37 | howSorted = 1; 38 | continue; 39 | } 40 | else { 41 | return false; 42 | } 43 | } 44 | return true; 45 | } 46 | 47 | int main() { 48 | 49 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task03_getMinAndMax.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned ARR_SIZE = 5; 5 | 6 | void minAndMax(const int array[], unsigned size, int& min, int& max) { 7 | min = INT_MIN; // The lowest value of Integer 8 | max = INT_MAX; // The highest value of Integer 9 | 10 | for (unsigned i = 0; i < size; i++) { 11 | if (array[i] < min) { 12 | min = array[i]; 13 | } 14 | else if (array[i] > max) { 15 | max = array[i]; 16 | } 17 | } 18 | } 19 | 20 | int main() { 21 | int arr[ARR_SIZE]; 22 | for(int i = 0; i < ARR_SIZE; i++) { 23 | cin >> arr[i]; 24 | } 25 | int min, max; 26 | minAndMax(arr, ARR_SIZE, min, max); 27 | cout << min << ", " << max; 28 | } 29 | -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task04_removeNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Notice that this time the array is not CONST because we will change it 5 | void removeElAtIndex(int array[], unsigned size, unsigned index) { 6 | if (index >= size) { 7 | return; 8 | } 9 | size = size - 1; 10 | for (unsigned i = index; i < size; i++) { 11 | array[i] = array[i + 1]; 12 | } 13 | array[size] = INT16_MIN; // We do this so we flag this this cell as invalid 14 | } 15 | 16 | int main() { 17 | 18 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task05_isSymmetrical.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned ARR_SIZE = 10; 5 | 6 | bool isPalindrome(const int array[], unsigned size) { 7 | if (size < 2) { 8 | return true; 9 | } 10 | for (unsigned i = 0, j = 0; i <= j; i++, j--) { 11 | if (array[i] != array[j]) { 12 | return false; 13 | } 14 | } 15 | return true; 16 | } 17 | 18 | int main() { 19 | int arr[ARR_SIZE]; 20 | for(int i = 0; i < ARR_SIZE; i++) { 21 | cin >> arr[i]; 22 | } 23 | cout << isPalindrome(arr, ARR_SIZE); 24 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task06_getLongestSeq.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | unsigned longestContinuity(const int array[], unsigned size) { 5 | unsigned maxLen = 0, currentMaxLen=1; 6 | if (size == 1) { 7 | return 1; 8 | } 9 | 10 | for (unsigned i = 1; i < size; i++) { 11 | if (array[i] == array[i - 1]) { 12 | currentMaxLen++; 13 | } 14 | else { 15 | if (currentMaxLen > maxLen) { 16 | maxLen = currentMaxLen; 17 | } 18 | currentMaxLen = 0; 19 | } 20 | } 21 | if (currentMaxLen > maxLen) { 22 | maxLen = currentMaxLen; 23 | } 24 | return maxLen; 25 | } 26 | 27 | int main() { 28 | 29 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task07_reverseArray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void swap(int& first, int& second) { 5 | int temp = first; 6 | first = second; 7 | second = temp; 8 | } 9 | 10 | void reverse(int array[], unsigned size) { 11 | unsigned half = size / 2; 12 | for (unsigned i = 0; i <= half; i++) { 13 | swap(array[i], array[size - i - 1]); 14 | } 15 | } 16 | 17 | int main() { 18 | 19 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task08_filterNegatives.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned ARR_SIZE = 7; 5 | 6 | void removeNegative(int array[], unsigned size) { 7 | for (unsigned i = 0; i < size; i++) { 8 | if (array[i] < 0) { 9 | for (unsigned j = i + 1; j < size; j++) { 10 | if ((j == size - 1) && array[j] < 0) { 11 | return; 12 | } 13 | else if (array[j] > 0) { 14 | swap(array[i], array[j]); 15 | break; 16 | } 17 | } 18 | } 19 | } 20 | } 21 | 22 | void printArr(const int arr[], unsigned size) { 23 | for(unsigned i = 0; i < size; i++) { 24 | cout << arr[i] << " "; 25 | } 26 | } 27 | 28 | int main() { 29 | int arr[ARR_SIZE]; 30 | for(int i = 0; i < ARR_SIZE; i++) { 31 | cin >> arr[i]; 32 | } 33 | removeNegative(arr, ARR_SIZE); 34 | printArr(arr, ARR_SIZE); 35 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task09_increaseByOne.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void increaseByOne(int array[]) { 5 | 6 | if (array[4] == 9) { 7 | array[4] = 0; 8 | for (int i = 3; i >= 0; i++) { 9 | if (array[i] == 9) { 10 | array[i] = 0; 11 | } 12 | else { 13 | ++array[i]; 14 | break; 15 | } 16 | } 17 | } 18 | else 19 | array[9]++; 20 | } 21 | 22 | int main() { 23 | 24 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task10_makeZero.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned SIZE = 10; 5 | 6 | bool isDigitInNummber(int number, int digit) { 7 | while (number != 0) { 8 | if (number % 10 == digit) { 9 | return true; 10 | } 11 | number = number / 10; 12 | } 13 | } 14 | 15 | void filterArray(int array[], unsigned size) { 16 | for (unsigned i = 0; i < 10 && i < size; i++) { 17 | if (isDigitInNummber(array[i], i)) { 18 | array[i] = 0; 19 | } 20 | } 21 | } 22 | 23 | void inputArr(int array[], unsigned size) { 24 | for(unsigned i = 0; i < size; i++) { 25 | cin >> array[i]; 26 | } 27 | } 28 | 29 | void printArr(const int array[], unsigned size) { 30 | for(unsigned i = 0; i < size; i++) { 31 | cout << array[i] << ", "; 32 | } 33 | } 34 | 35 | int main() { 36 | int arr[SIZE]; 37 | inputArr(arr, SIZE); 38 | filterArray(arr, SIZE); 39 | printArr(arr, SIZE); 40 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task11_mostCommonNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned SIZE = 10; 5 | 6 | void inputArr(int arr[], unsigned size) { 7 | for(int i = 0; i < size; i++) { 8 | cin >> arr[i]; 9 | } 10 | } 11 | 12 | int mostCommonNumber(const int array[], unsigned size) { 13 | int currentMostCommonNumber, currentCount = 0; 14 | 15 | for (int i = 0; i < size; i++) { 16 | int currentOcurances = 1; 17 | for (int j = 0; j < size; j++) { 18 | if (array[j] == array[i]) { 19 | currentOcurances++; 20 | } 21 | } 22 | 23 | if (currentOcurances > currentCount) { 24 | currentMostCommonNumber = array[i]; 25 | currentCount = currentOcurances; 26 | } 27 | } 28 | 29 | return currentMostCommonNumber; 30 | } 31 | 32 | int main() { 33 | int arr[SIZE]; 34 | inputArr(arr, SIZE); 35 | int mostCommonNum = mostCommonNumber(arr, SIZE); 36 | cout << mostCommonNum; 37 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task12_05_findMissing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned SIZE = 10; 5 | 6 | int missingNumberSlow(const int array[]) { 7 | bool buff[SIZE]; 8 | for (int i = 0; i < SIZE; i++) { 9 | buff[i] = false; 10 | } 11 | for (int i = 0; i < SIZE; i++) { 12 | buff[array[i]] = true; 13 | } 14 | for (int i = 0; i < SIZE; i++) { 15 | if (!array[i]) { 16 | return array[i]; 17 | } 18 | } 19 | } 20 | 21 | int missingNumberSlow(const int array[]) { 22 | int sum = (SIZE * SIZE / 2); 23 | int sumArr = 0; 24 | 25 | for (int i = 0; i < SIZE; i++) { 26 | sumArr += array[i]; 27 | } 28 | return sum - sumArr; 29 | } 30 | 31 | int main() { 32 | 33 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task12_getCountOfOccurs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned SIZE = 20; 5 | 6 | void occurances(const int array[]) { 7 | int occurances[SIZE]; 8 | 9 | for (int i = 0; i < SIZE; i++) { 10 | occurances[i] = 0; 11 | } 12 | for (int i = 0; i < SIZE; i++) { 13 | occurances[array[i]]++; 14 | } 15 | for (int i = 0; i < SIZE; i++) { 16 | cout << i << " - " << occurances[i]; 17 | } 18 | } 19 | 20 | int main() { 21 | int array[SIZE] = {1,0,3,2,4,5,2,19,5,2,7,0,2,4,5,6,4,2,6,7}; 22 | occurances(array); 23 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task14_unifyAndSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const unsigned SIZE = 5; 5 | 6 | void inputArray(int arr[], unsigned size) { 7 | for (unsigned i = 0; i < size; i++) { 8 | cin >> arr[i]; 9 | } 10 | } 11 | 12 | void printArr(const int arr[], unsigned size) { 13 | for (unsigned i = 0; i < size; i++) { 14 | cout << arr[i] << " "; 15 | } 16 | } 17 | 18 | void mergeArrays(const int arr1[], const int arr2[], unsigned size1, unsigned size2, int result[], unsigned& resultSize) { 19 | unsigned iter1 = 0; 20 | unsigned iter2 = 0; 21 | unsigned resIter = 0; 22 | resultSize = size1 + size2; 23 | 24 | while (iter1 < size1 && iter2 < size2) { 25 | 26 | if (arr1[iter1] <= arr2[iter2]) { 27 | 28 | result[resIter] = arr1[iter1]; 29 | 30 | iter1++; 31 | resIter++; 32 | 33 | continue; 34 | } 35 | result[resIter] = arr2[iter2]; 36 | 37 | iter2++; 38 | resIter++; 39 | } 40 | 41 | //if there are more elements in arr1 42 | while (iter1 < size1) { 43 | result[resIter] = arr1[iter1]; 44 | resIter++; 45 | iter1++; 46 | } 47 | 48 | //if there are more elements in arr2 49 | while (iter2 < size2) { 50 | result[resIter] = arr2[iter2]; 51 | resIter++; 52 | iter2++; 53 | } 54 | } 55 | 56 | int main() { 57 | int arr1[SIZE]; 58 | inputArray(arr1, SIZE); 59 | 60 | int arr2[SIZE]; 61 | inputArray(arr2, SIZE); 62 | 63 | int result[SIZE + SIZE]{ 0 }; 64 | unsigned resultSize = 0; 65 | 66 | mergeArrays(arr1, arr2, SIZE, SIZE, result, resultSize); 67 | printArr(result, resultSize); 68 | } -------------------------------------------------------------------------------- /Practicum/Week 06/Solutions/task15_isSubarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isArrSubarray(const int array[], unsigned size1, const int subArray[], unsigned size2) { 5 | unsigned indexToLook = 0; 6 | for (unsigned i = 0; i < size1; ++i) { 7 | if (indexToLook == size2) { 8 | return true; 9 | } 10 | if (array[i] == subArray[indexToLook]) { 11 | indexToLook++; 12 | } 13 | else { 14 | indexToLook = 0; 15 | } 16 | } 17 | 18 | return indexToLook == size2; 19 | } 20 | 21 | int main() { 22 | 23 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_01_minElementInMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_SIZE = 3; 5 | 6 | int findMinElementInMatrix(const int matrix[][MATRIX_SIZE]) { 7 | int minEl = INT_MAX; 8 | for(int i = 0; i < MATRIX_SIZE; i++) { 9 | for(int j = 0; j < MATRIX_SIZE; j++) { 10 | if(matrix[i][j] < minEl) { 11 | minEl = matrix[i][j]; 12 | } 13 | } 14 | } 15 | return minEl; 16 | } 17 | 18 | int main() { 19 | int matrix[MATRIX_SIZE][MATRIX_SIZE] = { 20 | {3, 5, 1}, 21 | {6, 2, 4}, 22 | {9, 0, 8} 23 | }; 24 | cout << findMinElementInMatrix(matrix) << endl; 25 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_02_printDiagonals.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_SIZE = 3; 5 | 6 | void printLeftDiagonal(const int matrix[][MATRIX_SIZE]) { 7 | for (int i = 0; i < MATRIX_SIZE; i++) { 8 | cout << matrix[i][i] << " "; 9 | } 10 | cout << endl; 11 | } 12 | 13 | void printRightDiagonal(const int matrix[][MATRIX_SIZE]) { 14 | for (int i = 0; i < MATRIX_SIZE; i++) { 15 | cout << matrix[i][MATRIX_SIZE - 1 - i] << " "; 16 | } 17 | cout << endl; 18 | } 19 | 20 | int main() { 21 | int matrix[MATRIX_SIZE][MATRIX_SIZE] = { 22 | {1, 2, 3}, 23 | {4, 5, 6}, 24 | {7, 8, 9} 25 | }; 26 | printLeftDiagonal(matrix); 27 | printRightDiagonal(matrix); 28 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_03_printZigZag.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_ROWS_SIZE = 3; 5 | const int MATRIX_COLS_SIZE = 3; 6 | 7 | void printZigZad(const int matrix[][MATRIX_COLS_SIZE]) { 8 | for(int i = 0; i < MATRIX_ROWS_SIZE; i++) { 9 | if (i % 2 == 0) { 10 | for(int j = 0; j < MATRIX_COLS_SIZE; j++) { 11 | cout << matrix[i][j] << " "; 12 | } 13 | } 14 | else { 15 | for(int j = MATRIX_COLS_SIZE - 1; j >= 0; j--) { 16 | cout << matrix[i][j] << " "; 17 | } 18 | } 19 | cout << endl; 20 | } 21 | } 22 | 23 | int main() { 24 | int matrix[MATRIX_ROWS_SIZE][MATRIX_COLS_SIZE] = { 25 | {1, 2, 3}, 26 | {4, 5, 6}, 27 | {7, 8, 9} 28 | }; 29 | printZigZad(matrix); 30 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_04_checkIfTriangleMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_SIZE = 3; 5 | 6 | bool isMatrixTriangle(const matrix[][MATRIX_SIZE]) { 7 | 8 | } 9 | 10 | int main() { 11 | int matrix[MATRIX_SIZE][MATRIX_SIZE] = { 12 | {5, 1, 5}, 13 | {0, 5, 9}, 14 | {0, 6, 7} 15 | }; 16 | cout << isMatrixTriangle(matrix) << endl; 17 | int matrix2[MATRIX_SIZE][MATRIX_SIZE] = { 18 | {5, 1, 5}, 19 | {0, 5, 9}, 20 | {0, 0, 7} 21 | }; 22 | cout << isMatrixTriangle(matrix) << endl; 23 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_05_transposeMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_ROWS = 3; 5 | const int MATRIX_COLS = 3; 6 | 7 | const int MAX_ROWS = 100; 8 | const int MAX_COLS = 100; 9 | 10 | void transposeMatrix(const int original[][MAX_COLS], int transposed[][MAX_COLS], int rows, int cols) { 11 | for (int i = 0; i < rows; i++) { 12 | for (int j = 0; j < cols; j++) { 13 | transposed[j][i] = original[i][j]; 14 | } 15 | } 16 | } 17 | 18 | void printMatrix(const int matrix[][MAX_COLS], int rows, int cols) { 19 | for (int i = 0; i < rows; i++) { 20 | for (int j = 0; j < cols; j++) { 21 | cout << matrix[i][j] << " "; 22 | } 23 | cout << endl; 24 | } 25 | cout << endl; 26 | } 27 | 28 | int main() { 29 | int matr[MAX_COLS][MAX_COLS] = { 30 | {1, 2, 3}, 31 | {4, 5, 6}, 32 | {7, 8, 9} 33 | }; 34 | int result[MAX_COLS][MAX_COLS] = { 0 }; 35 | transposeMatrix(matr, result, MATRIX_ROWS, MATRIX_COLS); 36 | printMatrix(result, MATRIX_COLS, MATRIX_ROWS); 37 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_06_isMagicSquare.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_ROWS_SIZE = 4; 5 | const int MATRIX_COLS_SIZE = 4; 6 | 7 | int sumOfLeftDiagonal(const int matrix[][MATRIX_COLS_SIZE]) { 8 | int sum = 0; 9 | for (int i = 0; i < MATRIX_ROWS_SIZE; i++) { 10 | sum += matrix[i][i]; 11 | } 12 | return sum; 13 | } 14 | 15 | int sumOfRightDiagonal(const int matrix[][MATRIX_COLS_SIZE]) { 16 | int sum = 0; 17 | for (int i = 0; i < MATRIX_ROWS_SIZE; i++) { 18 | sum += matrix[i][MATRIX_ROWS_SIZE - 1 - i]; 19 | } 20 | return sum; 21 | } 22 | 23 | int sumOfRow(const int row[]) { 24 | int sum = 0; 25 | for (int i = 0; i < MATRIX_COLS_SIZE; i++) { 26 | sum += row[i]; 27 | } 28 | return sum; 29 | } 30 | 31 | int sumOfCol(const int matrix[][MATRIX_COLS_SIZE], int col) { 32 | int sum = 0; 33 | for (int i = 0; i < MATRIX_COLS_SIZE; i++) { 34 | sum += matrix[i][col]; 35 | } 36 | return sum; 37 | } 38 | 39 | bool isMagicSquare(const int matrix[][MATRIX_COLS_SIZE]) { 40 | int leftDiag = sumOfLeftDiagonal(matrix); 41 | int rightDiag = sumOfRightDiagonal(matrix); 42 | 43 | if (leftDiag != rightDiag) { 44 | return false; 45 | } 46 | 47 | for (int i = 0; i < MATRIX_ROWS_SIZE; i++) { 48 | if (sumOfRow(matrix[i]) != leftDiag) { 49 | return false; 50 | } 51 | } 52 | 53 | for (int i = 0; i < MATRIX_ROWS_SIZE; i++) { 54 | if (sumOfCol(matrix, i) != leftDiag) { 55 | return false; 56 | } 57 | } 58 | 59 | return true; 60 | } 61 | 62 | int main() { 63 | int matrix[MATRIX_ROWS_SIZE][MATRIX_COLS_SIZE] = { 64 | {16, 3, 2, 13}, 65 | {5, 10, 11, 8}, 66 | {9, 6, 7, 12}, 67 | {4, 15, 14, 1} 68 | }; 69 | 70 | cout << isMagicSquare(matrix); 71 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_07_sumAndMultByNMatrixes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MATRIX_ROWS = 2; 5 | const int MATRIX_COLS = 3; 6 | 7 | void sumOfTwoMatrixes(const int first[][MATRIX_COLS], int rowsFirst, int colsFirst, const int second[][MATRIX_COLS], int rowsSecond, int colsSecond, int result[][MATRIX_COLS]) { 8 | if (rowsFirst != rowsSecond || colsFirst != colsSecond) { 9 | return; 10 | } 11 | 12 | for (int i = 0; i < rowsFirst; i++) { 13 | for (int j = 0; j < colsFirst; j++) { 14 | result[i][j] = first[i][j] + second[i][j]; 15 | } 16 | } 17 | } 18 | 19 | void multByN(int matrix[][MATRIX_COLS], int rows, int n) { 20 | for(int i = 0; i < rows; i++) { 21 | for(int j = 0; j < MATRIX_COLS; j++) { 22 | matrix[i][j] *= n; 23 | } 24 | } 25 | } 26 | 27 | void printMatrix(const int matr[][MATRIX_COLS], int rows) { 28 | for (int i = 0; i < rows; i++) { 29 | for (int j = 0; j < MATRIX_COLS; j++) { 30 | cout << matr[i][j] << " "; 31 | } 32 | cout << endl; 33 | } 34 | cout << endl; 35 | } 36 | 37 | int main() { 38 | int matr1[MATRIX_ROWS][MATRIX_COLS] = { 39 | {1, 2, 3}, 40 | {4, 5, 6} 41 | }; 42 | int matr2[MATRIX_ROWS][MATRIX_COLS] = { 43 | {5, 6, 7}, 44 | {8, 9, 10} 45 | }; 46 | 47 | int res[MATRIX_ROWS][MATRIX_COLS]; 48 | 49 | sumOfTwoMatrixes(matr1, MATRIX_ROWS, MATRIX_COLS, matr2, MATRIX_ROWS, MATRIX_COLS, res); 50 | printMatrix(res, MATRIX_ROWS); 51 | 52 | int mult; 53 | cin >> mult; 54 | multByN(matr1, MATRIX_ROWS, mult); 55 | printMatrix(matr1, MATRIX_ROWS); 56 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_08_multiplyMatrixes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int FIRST_MATRIX_ROWS = 5; 5 | const int FIRST_MATRIX_COLS = 2; 6 | 7 | const int SECOND_MATRIX_ROWS = 2; 8 | const int SECOND_MATRIX_COLS = 3; 9 | 10 | const int MATRIX_ROWS = FIRST_MATRIX_ROWS; 11 | const int MATRIX_COLS = SECOND_MATRIX_COLS; 12 | 13 | bool canMultiply(int firstMatrixCols, int secondMatrixRows) { 14 | return firstMatrixCols == secondMatrixRows; 15 | } 16 | 17 | void multiplyMatrixes(const int matr1[][FIRST_MATRIX_COLS], int matr1Rows, int matr1Cols, 18 | const int matr2[][SECOND_MATRIX_COLS], int matr2Rows, int matr2Cols, 19 | int result[][MATRIX_COLS]) { 20 | if (!canMultiply(matr1Cols, matr2Rows)) { 21 | return; 22 | } 23 | 24 | for (int i = 0; i < FIRST_MATRIX_ROWS; i++) { 25 | for (int j = 0; j < SECOND_MATRIX_COLS; j++) { 26 | for (int k = 0; k < FIRST_MATRIX_COLS; k++) { 27 | result[i][j] += matr1[i][k] * matr2[k][j]; 28 | } 29 | } 30 | } 31 | } 32 | 33 | void printMatrix(const int matrix[][MATRIX_COLS], int rows) { 34 | for (int i = 0; i < rows; i++) { 35 | for (int j = 0; j < MATRIX_COLS; j++) { 36 | cout << matrix[i][j] << " "; 37 | } 38 | cout << endl; 39 | } 40 | cout << endl; 41 | } 42 | 43 | int main() { 44 | int matr1[FIRST_MATRIX_ROWS][FIRST_MATRIX_COLS] = { 45 | {1, 2}, 46 | {8, 9}, 47 | {10, 0}, 48 | {1, 0}, 49 | {2, 2} 50 | }; 51 | 52 | int matr2[SECOND_MATRIX_ROWS][SECOND_MATRIX_COLS] = { 53 | {1, 2, 3}, 54 | {4, 5, 6} 55 | }; 56 | 57 | int result[MATRIX_ROWS][MATRIX_COLS] = { 0 }; 58 | 59 | multiplyMatrixes(matr1, FIRST_MATRIX_ROWS, FIRST_MATRIX_COLS, matr2, SECOND_MATRIX_ROWS, SECOND_MATRIX_COLS, result); 60 | printMatrix(result, MATRIX_ROWS); 61 | } 62 | -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_09_spiralPrint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_ROWS = 100; 5 | const int MAX_COLS = 100; 6 | 7 | const int MATRIX_ROWS = 3; 8 | const int MATRIX_COLS = 3; 9 | 10 | void spiralPrint(const int matrix[][MAX_COLS], int rows, int cols) { 11 | int top = 0, bottom = rows - 1; 12 | int left = 0, right = cols - 1; 13 | 14 | while (top <= bottom && left <= right) { 15 | for (int i = left; i <= right; i++) { 16 | cout << matrix[top][i] << " "; 17 | } 18 | top++; 19 | 20 | for (int i = top; i <= bottom; i++) { 21 | cout << matrix[i][right] << " "; 22 | } 23 | right--; 24 | 25 | if (top <= bottom) { 26 | for (int i = right; i >= left; i--) { 27 | cout << matrix[bottom][i] << " "; 28 | } 29 | bottom--; 30 | } 31 | 32 | if (left <= right) { 33 | for (int i = bottom; i >= top; i--) { 34 | cout << matrix[i][left] << " "; 35 | } 36 | left++; 37 | } 38 | } 39 | cout << endl; 40 | } 41 | 42 | int main() { 43 | int matrix[MAX_ROWS][MAX_COLS] = { 44 | {1, 2, 3}, 45 | {4, 5, 6}, 46 | {7, 8, 9} 47 | }; 48 | spiralPrint(matrix, MATRIX_ROWS, MATRIX_COLS); 49 | } -------------------------------------------------------------------------------- /Practicum/Week 09/Solutions/task_10_turnRight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int MAX_SIZE = 100; 5 | const int MATRIX_SIZE = 3; 6 | 7 | void rotateMatrixRight(int matrix[][MAX_SIZE], int n) { 8 | for (int i = 0; i < n; i++) { 9 | for (int j = i + 1; j < n; j++) { 10 | swap(matrix[i][j], matrix[j][i]); 11 | } 12 | } 13 | for (int i = 0; i < n; i++) { 14 | for (int j = 0; j < n / 2; j++) { 15 | swap(matrix[i][j], matrix[i][n - j - 1]); 16 | } 17 | } 18 | } 19 | 20 | void printMatrix(int matrix[][MAX_SIZE], int n) { 21 | for (int i = 0; i < n; i++) { 22 | for (int j = 0; j < n; j++) { 23 | cout << matrix[i][j] << " "; 24 | } 25 | cout << endl; 26 | } 27 | } 28 | 29 | int main() { 30 | int matrix[][MAX_SIZE] = { 31 | {1, 2, 3}, 32 | {4, 5, 6}, 33 | {7, 8, 9} 34 | }; 35 | 36 | rotateMatrixRight(matrix, MATRIX_SIZE); 37 | printMatrix(matrix, MATRIX_SIZE); 38 | } -------------------------------------------------------------------------------- /Practicum/Week 09/kontrolno.md: -------------------------------------------------------------------------------- 1 | # Задачи за контролна работа 29.11.2024г # 2 | 3 | **Задача 1:** 4 | 5 | Да се напише функция, която приема масив от цели числа и обръща елементите му, без заделяне на допълнителна памет. 6 | 7 | ***Пример: Вход: [1, 2, 3, 6, 5, 8] Изход: [8, 5, 6, 3, 2, 1]*** 8 | 9 | **Задача 2:** 10 | 11 | Да се напише функция, която приема масив от цели числа и премества в началото на масива четните числа, а в края - нечетните. Трябва да се прави с помощен масив. 12 | 13 | ***Пример:*** 14 | 15 | ***Вход: [0 1 3 4 5 6 7 8 10]*** 16 | 17 | ***Изход: [0 10 8 4 6 5 7 3 1]*** 18 | 19 | *// Приема се и всяка друга пермутация, отговаряща на условието* 20 | 21 | **Задача 3:** 22 | 23 | Да се напише функция, която приема два масива с цели числа. Масив 1 е с размер N и съдържа само уникални положителни числа. Масив 2 е с размер N + 1 и е копие на масив 1, но на някоя позиция е вмъкнато числото 0. Функцията трябва за логаритмичен брой стъпки да намира къде е вмъкнато числото 0. 24 | 25 | ***Пример:*** 26 | 27 | ***Вход: [1 7 2 6 5 4], [1 0 7 2 6 5 4]*** 28 | 29 | ***Изход: 1*** 30 | -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_01_isStringInMatrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/Practicum/Week 10/Solutions/task_01_isStringInMatrix.cpp -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_02_isPalindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int strLen(const char* str) { 5 | if (!str) { 6 | return 0; 7 | } 8 | 9 | unsigned result = 0; 10 | while (*str) { 11 | result++; 12 | str++; 13 | } 14 | return result; 15 | } 16 | 17 | bool isPalindrome(const char* arr) { 18 | if (!arr) { 19 | return 0; 20 | } 21 | 22 | unsigned len = strLen(arr); 23 | const char* end = arr + len - 1; 24 | 25 | while (arr < end) { 26 | if (*arr != *end) { 27 | return false; 28 | } 29 | arr++; 30 | end--; 31 | } 32 | return true; 33 | } 34 | 35 | int main() { 36 | 37 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_03_modifyString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void smallToCapital(char& ch) { 5 | ch = ch - 'a' + 'A'; 6 | } 7 | 8 | void capitalToSmall(char& ch) { 9 | ch = ch + 'a' - 'A'; 10 | } 11 | 12 | bool isLetterSmall(char ch) { 13 | return ('a' <= ch && ch <= 'z'); 14 | } 15 | 16 | bool isLetterCapital(char ch) { 17 | return ('A' <= ch && ch <= 'Z'); 18 | } 19 | 20 | void convertStringLetters(char* str) { 21 | if (!str) { 22 | return; 23 | } 24 | 25 | while (*str) { 26 | if (isLetterSmall(*str)) { 27 | smallToCapital(*str); 28 | } 29 | else if (isLetterCapital(*str)) { 30 | capitalToSmall(*str); 31 | } 32 | str++; 33 | } 34 | } 35 | 36 | int main() { 37 | 38 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_04_areAllLettersUnique.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fillBoolWithZeros(bool* arr, int size) { 5 | for (int i = 0; i < size; i++) { 6 | arr[i] = false; 7 | } 8 | } 9 | 10 | bool areLettersUnique(const char* str) { 11 | if (!str) { 12 | return false; 13 | } 14 | 15 | bool letters[LETTERS]; 16 | fillBoolWithZeros(letters, LETTERS); 17 | 18 | while (*str) { 19 | if (letters[*str - 'a'] == true) { 20 | return false; 21 | } 22 | else { 23 | letters[*str - 'a'] = true; 24 | } 25 | 26 | str++; 27 | } 28 | 29 | return true; 30 | } 31 | 32 | int main() { 33 | 34 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_05_insertInText.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void insertLetter(const char* text, char* result, char symbol, int index) { 5 | if (!text || !result) { 6 | return; 7 | } 8 | 9 | for (int i = 0; i < index; i++) { 10 | result[i] = text[i]; 11 | } 12 | 13 | result[index] = symbol; 14 | text += index; 15 | result = result + index + 1; 16 | 17 | while (*text) { 18 | *result = *text; 19 | result++; 20 | text++; 21 | } 22 | 23 | *result = '\0'; 24 | } 25 | 26 | int main() { 27 | 28 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_06_countOfDigits.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int DIGITS = 10; 5 | 6 | void fillIntegerWithZeros(int* array, int size) { 7 | for (int i = 0; i < size; i++) 8 | array[i] = 0; 9 | } 10 | 11 | int convertCharToInt(char ch) { 12 | return ch - '0'; 13 | } 14 | 15 | char convertIntToChar(int num) { 16 | return num + '0'; 17 | } 18 | 19 | int reverseNumber(int number) { 20 | int res = 0; 21 | while (number != 0) { 22 | res = 10 * res + (number % 10); 23 | number = number / 10; 24 | } 25 | return res; 26 | } 27 | 28 | void digitOccurrences(const char* str, char* result) { 29 | if (!str || !result) { 30 | return; 31 | } 32 | 33 | int digitsCount[DIGITS]; 34 | fillIntegerWithZeros(digitsCount, DIGITS); 35 | 36 | while (*str) { 37 | digitsCount[convertCharToInt(*str)]++; 38 | str++; 39 | } 40 | 41 | int indexToFill = 0; 42 | for (int i = 0; i < DIGITS; i++) { 43 | int reversedNum = reverseNumber(digitsCount[i]); 44 | 45 | do { 46 | result[indexToFill] = convertIntToChar(reversedNum % 10); 47 | reversedNum = reversedNum / 10; 48 | indexToFill++; 49 | } while (reversedNum != 0); 50 | 51 | result[indexToFill++] = 'x'; 52 | result[indexToFill++] = convertIntToChar(i); 53 | result[indexToFill++] = ','; 54 | } 55 | 56 | result[indexToFill] = '\0'; 57 | } 58 | 59 | int main() { 60 | 61 | } 62 | -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_07_findDiff.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fillBoolWithZeros(bool* arr, int size) { 5 | for (int i = 0; i < size; i++) { 6 | arr[i] = false; 7 | } 8 | } 9 | 10 | int convertCharToInt(char ch) { 11 | return ch - '0'; 12 | } 13 | 14 | void difference(const char* str1, const char* str2, char* res) { 15 | if (!str1 || !str2 || !res) 16 | return; 17 | 18 | bool letters1[LETTERS]; 19 | bool letters2[LETTERS]; 20 | 21 | fillBoolWithZeros(letters1, LETTERS); 22 | fillBoolWithZeros(letters2, LETTERS); 23 | 24 | while (*str1) { 25 | letters1[convertCharToInt(*str1)] = true; 26 | str1++; 27 | } 28 | 29 | while (*str2) { 30 | letters2[convertCharToInt(*str2)] = true; 31 | str2++; 32 | } 33 | 34 | for (int i = 0; i < LETTERS; i++) { 35 | if (letters1[i] != letters2[i]) { 36 | *res = i + 'a'; 37 | res++; 38 | } 39 | } 40 | *res = '\0'; 41 | } 42 | 43 | int main() { 44 | 45 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_08_makeLettersCapital.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isLetterSmall(char ch) { 5 | return ('a' <= ch && ch <= 'z'); 6 | } 7 | 8 | void smallToCapital(char& ch) { 9 | ch = ch - 'a' + 'A'; 10 | } 11 | 12 | void capitalWords(char* str) { 13 | if (!str) { 14 | return; 15 | } 16 | 17 | bool toChange = true; 18 | while (*str) { 19 | if (toChange && isLetterSmall(*str)) { 20 | smallToCapital(*str); 21 | toChange = false; 22 | } 23 | else if (*str == ' ') { 24 | toChange = true; 25 | } 26 | str++; 27 | } 28 | } 29 | 30 | int main() { 31 | 32 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_09_removeOccursOfChar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | size_t strLen(const char* str) { 5 | size_t len = 0; 6 | while(*str) { 7 | len++; 8 | str++; 9 | } 10 | return len; 11 | } 12 | 13 | void strCopy(char* dest, const char* source) { 14 | if (!dest || !source) { 15 | return; 16 | } 17 | while (*source) { 18 | *dest = *source; 19 | dest++; 20 | source++; 21 | } 22 | *dest = '\0'; 23 | } 24 | 25 | void removeChar(char* arr, char ch) { 26 | if (!arr) { 27 | return; 28 | } 29 | char buff[MAX_SIZE]; 30 | size_t len = strLen(arr); 31 | char buffCnt = 0; 32 | 33 | for (size_t i = 0; i < len; ++i) { 34 | if (arr[i] != ch) { 35 | buff[buffCnt] = arr[i]; 36 | ++buffCnt; 37 | } 38 | } 39 | buff[buffCnt] = '\0'; 40 | strCopy(arr, buff); 41 | } 42 | 43 | int main() { 44 | 45 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_10_sortString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fromCapital(char* str) { 5 | if (!str) { 6 | return; 7 | } 8 | while (*str) { 9 | if (isLetterCapital(*str)) { 10 | capitalToSmall(*str); 11 | } 12 | } 13 | } 14 | 15 | void validation(char* arr) { 16 | fromCapital(arr); 17 | size_t len = strLen(arr); 18 | for (size_t i = 0; i < len; ++i) { 19 | if(!isLetterSmall(arr[i])) { 20 | removeChar(arr, arr[i]); 21 | --i; 22 | len = strLen(arr); 23 | } 24 | } 25 | } 26 | 27 | void sort(char* str) { 28 | if (!str) { 29 | return; 30 | } 31 | 32 | validation(str); 33 | int counter[LETTERS]; 34 | 35 | fillIntegerWithZeros(counter, LETTERS); 36 | size_t len = strLen(str); 37 | for (size_t i = 0; i < len; ++i) { 38 | counter[str[i] - 'a'] += 1; 39 | } 40 | 41 | char buff[MAX_SIZE]; 42 | *buff = '\0'; 43 | 44 | unsigned indexToPut = 0; 45 | for (unsigned i = 0; i < LETTERS; ++i) { 46 | char letterToPut = i + 'a'; 47 | for (unsigned j = 0; j < counter[i]; ++j) { 48 | buff[indexToPut] = letterToPut; 49 | ++indexToPut; 50 | } 51 | } 52 | strcpy(str, buff); 53 | } 54 | 55 | int main() { 56 | 57 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions/task_11_fromStringToMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int SIZE = 100; 5 | const int SIZE2 = 10; 6 | 7 | void strCopy(char* dest, const char* source) { 8 | if (!dest || !source) { 9 | return; 10 | } 11 | while (*source) { 12 | *dest = *source; 13 | dest++; 14 | source++; 15 | } 16 | *dest = '\0'; 17 | } 18 | 19 | void splitWordsWithBuffer(const char* array, char matrix[][SIZE2], size_t size) { 20 | if (!array) { 21 | return; 22 | } 23 | char buffer[SIZE]; 24 | int line = 0, index = 0; 25 | 26 | while (*array) { 27 | if (*array == ' ') { 28 | buffer[index] = '\0'; 29 | strCopy(matrix[line], buffer); 30 | line++; 31 | buffer[0] = '\0'; 32 | index = 0; 33 | } 34 | else { 35 | buffer[index] = *array; 36 | index++; 37 | } 38 | array++; 39 | } 40 | buffer[index] = '\0'; 41 | strCopy(matrix[line], buffer); 42 | } 43 | 44 | void splitWordsWithoutBuffer(const char* array, char matrix[][SIZE2], size_t size) { 45 | if (!array) { 46 | return; 47 | } 48 | int line = 0, indexToPut = 0; 49 | 50 | while (*array) { 51 | if (*array == ' ') { 52 | matrix[line][indexToPut] = '\0'; 53 | line++; 54 | indexToPut = 0; 55 | } 56 | else { 57 | matrix[line][indexToPut] = *array; 58 | indexToPut++; 59 | } 60 | array++; 61 | } 62 | matrix[line][indexToPut] = '\0'; 63 | } 64 | 65 | void printMatrix(const char matrix[SIZE][SIZE2]) { 66 | for (int i = 0; i < SIZE; i++) { 67 | cout << matrix[i]<<'\n'; 68 | } 69 | } 70 | 71 | void clear(char matrix[SIZE][SIZE2]) { 72 | for (int i = 0; i < SIZE; i++) { 73 | for (int j = 0; j < SIZE2; j++) { 74 | matrix[i][j] = '\0'; 75 | } 76 | } 77 | } 78 | 79 | int main() 80 | { 81 | char str[] = "hi my name is hristo"; 82 | char matrix[SIZE][SIZE2]; 83 | clear(matrix); 84 | splitWordsWithoutBuffer(str, matrix, SIZE2); 85 | printMatrix(matrix); 86 | 87 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_01_getCountOfOccurs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getCountOfOccurs(const char* str, char ch) { 5 | if (!str) { 6 | return -1; 7 | } 8 | int count = 0; 9 | 10 | while (*str) { 11 | if (*str == ch) { 12 | count++; 13 | } 14 | str++; 15 | } 16 | return count; 17 | } 18 | 19 | int main() { 20 | 21 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_02_saveOnlyLowerCase.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const size_t SIZE = 1024; 5 | 6 | bool isLowerCase(char ch) { 7 | return ch >= 'a' && ch <= 'z'; 8 | } 9 | 10 | void saveOnlyLowerCase(const char* str, char* result) { 11 | if (!str) { 12 | return; 13 | } 14 | 15 | while(*str) { 16 | if (isLowerCase(*str)) { 17 | *result = *str; 18 | result++; 19 | } 20 | str++; 21 | } 22 | *result = '\0'; 23 | } 24 | 25 | int main() { 26 | char str[SIZE] = "aBc!?Def"; 27 | char result[SIZE] = ""; 28 | saveOnlyLowerCase(str, result); 29 | cout << result; 30 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_03_getSubstr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const size_t SIZE = 1024; 5 | 6 | size_t strLen(const char* str) { 7 | if (!str) { 8 | return 0; 9 | } 10 | size_t len = 0; 11 | while (*str) { 12 | len++; 13 | str++; 14 | } 15 | return len; 16 | } 17 | 18 | void getSubStr(const char* str, char* result, size_t begin, size_t end) { 19 | size_t len = strLen(str); 20 | if (!str || begin >= len || begin > end) { 21 | return; 22 | } 23 | end = (end > len) ? len : end; 24 | int resIter = 0; 25 | for (int i = begin; i <= end; i++) { 26 | result[resIter++] = str[i]; 27 | } 28 | resIter++; 29 | result[resIter] = '\0'; 30 | } 31 | 32 | int main() { 33 | char str[SIZE] = "Hello"; 34 | char res[SIZE] = ""; 35 | getSubStr(str, res, 1, 3); 36 | cout << res; 37 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_04_replaceInString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const size_t SIZE = 1024; 5 | 6 | void replaceInString(char* str, char x, char a, char b) { 7 | size_t counter = 0; 8 | while (*str) { 9 | if (*str == x) { 10 | if (!(counter % 2)) { 11 | *str = a; 12 | } 13 | else { 14 | *str = b; 15 | } 16 | counter++; 17 | } 18 | str++; 19 | } 20 | } 21 | 22 | int main() { 23 | char str[SIZE] = "H_ll_ my fri_nd"; 24 | replaceInString(str, '_', 'e', 'o'); 25 | cout << str; 26 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_05_getLongestCommonPrefix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const size_t SIZE = 1024; 5 | 6 | void getLongestCommonPreffix(const char* str1, const char* str2, const char* str3, char* res) { 7 | if (!str1 || !str2 || !str3) { 8 | return; 9 | } 10 | 11 | while (*str1 && *str2 && *str3) { 12 | if (*str1 == *str2 && *str1 == *str3) { 13 | *res = *str1; 14 | res++; 15 | } 16 | str1++; 17 | str2++; 18 | str3++; 19 | } 20 | *res = '\0'; 21 | } 22 | 23 | int main() { 24 | char str1[SIZE] = "flower"; 25 | char str2[SIZE] = "flow"; 26 | char str3[SIZE] = "flight"; 27 | char res[SIZE] = ""; 28 | getLongestCommonPreffix(str1, str2, str3, res); 29 | cout << res; 30 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_06_getIndexOfFirstOccur.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | size_t strLen(const char* str) { 5 | if (!str) { 6 | return 0; 7 | } 8 | size_t len = 0; 9 | while (*str) { 10 | len++; 11 | str++; 12 | } 13 | return len; 14 | } 15 | 16 | void setFound(const char* text, const char* word, bool& found, int i, size_t wordLength) { 17 | for (int j = 0; j < wordLength; ++j) { 18 | if (text[i + j] != word[j]) { 19 | found = false; 20 | break; 21 | } 22 | } 23 | } 24 | 25 | int findFirstOccurrence(const char* text, const char* word) { 26 | size_t textLength = strLen(text); 27 | size_t wordLength = strLen(word); 28 | 29 | if (wordLength == 0 || textLength == 0 || wordLength > textLength) { 30 | return -1; 31 | } 32 | 33 | for (int i = 0; i <= textLength - wordLength; ++i) { 34 | bool found = true; 35 | setFound(text, word, found, i, wordLength); 36 | if (found) { 37 | return i; 38 | } 39 | } 40 | 41 | return -1; 42 | } 43 | 44 | int main() { 45 | const char text[] = "Hello my friend!"; 46 | const char word[] = "my"; 47 | 48 | cout << findFirstOccurrence(text, word); 49 | } 50 | -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_07_countOfOccursOfString.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int SIZE = 1024; 5 | 6 | size_t strLen(const char* str) { 7 | if (!str) { 8 | return 0; 9 | } 10 | 11 | size_t len = 0; 12 | while (*str) { 13 | len++; 14 | str++; 15 | } 16 | return len; 17 | } 18 | 19 | int countOccurrences(const char* text, const char* word) { 20 | size_t textLength = strLen(text); 21 | size_t wordLength = strLen(word); 22 | size_t count = 0; 23 | 24 | if (wordLength == 0 || textLength == 0 || wordLength > textLength) { 25 | return 0; 26 | } 27 | 28 | for (int i = 0; i <= textLength - wordLength; ++i) { 29 | bool found = true; 30 | 31 | for (int j = 0; j < wordLength; ++j) { 32 | if (text[i + j] != word[j]) { 33 | found = false; 34 | break; 35 | } 36 | } 37 | 38 | if (found) { 39 | ++count; 40 | i += wordLength - 1; 41 | } 42 | } 43 | 44 | return count; 45 | } 46 | 47 | int main() { 48 | const char text[SIZE] = "abc abc abc"; 49 | const char word[SIZE] = "abc"; 50 | 51 | cout << countOccurrences(text, word); 52 | } -------------------------------------------------------------------------------- /Practicum/Week 10/Solutions_AdditionalTasks/task_08_replaceWithStars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | size_t strLen(const char* str) { 5 | if (!str) { 6 | return 0; 7 | } 8 | size_t len = 0; 9 | while (*str) { 10 | len++; 11 | str++; 12 | } 13 | reutrn len; 14 | } 15 | 16 | void replaceSubstringWithStars(char* text, const char* substring) { 17 | int textLength = strLen(text); 18 | int subLength = strLen(substring); 19 | 20 | if (subLength == 0 || subLength > textLength) { 21 | return; 22 | } 23 | 24 | for (int i = 0; i <= textLength - subLength; ++i) { 25 | bool match = true; 26 | 27 | for (int j = 0; j < subLength; ++j) { 28 | if (tolower(text[i + j]) != tolower(substring[j])) { 29 | match = false; 30 | break; 31 | } 32 | } 33 | 34 | if (match) { 35 | for (int j = 0; j < subLength; ++j) { 36 | text[i + j] = '*'; 37 | } 38 | i += subLength - 1; 39 | } 40 | } 41 | } 42 | 43 | int main() { 44 | char text[] = "Howdy! How are you? How was your day?"; 45 | const char substring[] = "how"; 46 | 47 | replaceSubstringWithStars(text, substring); 48 | 49 | cout << text; 50 | } -------------------------------------------------------------------------------- /Practicum/Week 11/readme.md: -------------------------------------------------------------------------------- 1 | # УП - Практикум 10.01.2025 Седмица 11 2 | 3 | **Задача 1:** 4 | От стандартния вход се чете естествено число n. След това се четат n цели числа и се записват в едномерен масив. Да се изведе сбора на числата на четни позиции. 5 | 6 | **Задача 2:** 7 | От стандартния вход се чете число n и се създава масив от n цели числа. След това от стандартния вход се въвеждат n цели числа като числата на четни позиции се записват в първата половина на масива а тези на нечетни във втората. 8 | 9 | **Задача 3:** 10 | На стандартния вход се въвежда естествено число n. Чете се масив от n цели числа. След това се чете втори масив от n нули или единици. Да се преобразува масивът така, че да останат само числата от първия масив, на чиято позиция във втория масив има единица. За резултатния масив е заделено точно количество памет. 11 | 12 | **Пример:** 13 | 14 | 7 15 | 16 | 1 2 3 4 5 6 7 17 | 18 | 0 1 1 0 0 1 0 19 | 20 | Изход: 21 | [2, 3, 6] 22 | 23 | **Задача 4:** 24 | 25 | От стандартния вход се въвежда естествено число n. След това на нови редове се въвеждат два низа с дължина n. Да се напише функция, която връща низ, представляващ най - дългия общ префикс на двата входни масива. 26 | 27 | ``` 28 | Вход: 29 | 5 30 | aaabb 31 | aabab 32 | 33 | Изход: 34 | Низ с дължина 3 и стойност {'a', 'a', '\0'}. 35 | ``` 36 | 37 | **Задача 5:** 38 | На стандартния вход се въвежда естествено число n и масив с n елемента. Да се напише функция, която отстранява всички елементи от масива, за които е вярно, че са по - малки едновременно от двата си съседа. След края на програмата в масива не трябва да има такива елементи. 39 | 40 | Бонус: Изходният масив да заема точно толкова памет, колкото му е нужна да запише числата останали след прилагането на функцията. 41 | 42 | **Задача 6:** 43 | 44 | От стандартния вход се чете низ. Да се създаде масив, съдържащ всички суфикси на входния низ. 45 | 46 | Пример: 47 | 48 | Вход: 49 | 50 | Input 51 | 52 | Изход: 53 | 54 | [[""], ["t"], ["ut"], ["put"], ["nput"], ["Input"]] 55 | -------------------------------------------------------------------------------- /Practicum/Week 12/readme.md: -------------------------------------------------------------------------------- 1 | # УП - Практикум 17.01.2025 Седмица 12 2 | 3 | **Задача 1.** 4 | Напишете рекурсивна функция, която приема цяло число n и връща масив от първите n числа на Фибоначи. 5 | 6 | ### Пример: 7 | Вход: 5 8 | Изход: 1 1 2 3 5 9 | 10 | **Задача 2.** 11 | Напишете рекурсивна функция, която пресмята дали едно число е просто. 12 | 13 | **Задача 3.** 14 | Напишете функция, която проверява дали при подаден масив от цели положителни числа, стартов индекс има път от стартовия до клетка със стойност 0 предвижвайки се според стойностите в клетките (Можете да се придвижвате както наляво, така и надясно). 15 | 16 | ### Пример: 17 | Вход : [0, 3, 1, 3, 8, 2, 1] 6 18 | Процес: [0, 3, 1, 3, 8, 2, {1}] 6 19 | [0, 3, 1, 3, 8, {2}, 1] 5 20 | [0, 3, 1, {3}, 8, 2, 1] 4 21 | [{0}, 3, 1, 3, 8, 2, 1] 0 22 | Изход: True! 23 | 24 | **Задача 4.** 25 | Даден е лабиринт под формата на матрица с размер n x m, където свободно квадратче се отбелязва с 1, а стена – с 0. Намерете има ли път от позиция (0,0) до позиция (n-1, m-1), движейки се в четирите основни посоки и само през свободни квадратчета. 26 | 27 | **Задача 5.** 28 | Напишете рекурсивна функция, която пресмята броя срещания на символ c в низ str. 29 | 30 | ### Пример: 31 | Вход: alabala a 32 | Изход: 4 33 | 34 | **Задача 6.** 35 | Напишете рекурсивна фунцкия за двоично търсене. 36 | -------------------------------------------------------------------------------- /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. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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. 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 | -------------------------------------------------------------------------------- /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/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. 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. 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. 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 | -------------------------------------------------------------------------------- /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. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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. 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. 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 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/d: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Sem. 04/Solutions/task 04.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/Sem. 04/Solutions/task 04.cpp -------------------------------------------------------------------------------- /Sem. 04/Solutions/task 09.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/Sem. 04/Solutions/task 09.cpp -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/Sem. 05/README.md -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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. 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. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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. 13/Сортиращи_алгоритми.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Angeld55/Introduction_to_programming_FMI/618177f861730543e54abb3aa9912827009e73d3/Sem. 13/Сортиращи_алгоритми.pdf -------------------------------------------------------------------------------- /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. 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | #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. 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 | getLowerAndUpperCount("afsdfsdSDFFDS", lowers, capitals); 30 | 31 | cout << lowers << " " << capitals << endl; 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /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. 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/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 | -------------------------------------------------------------------------------- /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 | 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------