diff options
25 files changed, 450 insertions, 0 deletions
diff --git a/pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp b/pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp new file mode 100644 index 0000000..6ca43c5 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task3 - isNegative().cpp @@ -0,0 +1,4 @@ +bool isNegative(int num) +{ + return num < 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task3.cpp b/pract_SI/Pract. 04/Solutions/task3 - isPositive().cpp index 937b833..937b833 100644 --- a/pract_SI/Pract. 04/Solutions/task3.cpp +++ b/pract_SI/Pract. 04/Solutions/task3 - isPositive().cpp diff --git a/pract_SI/Pract. 04/Solutions/task4 - abs.cpp b/pract_SI/Pract. 04/Solutions/task4 - abs.cpp new file mode 100644 index 0000000..771550b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task4 - abs.cpp @@ -0,0 +1,4 @@ +int abs(int num1, int num2) +{ + return ((num1 - num2) >= 0 ? (num1 - num2) : (num2 - num1)); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task4 - fabs.cpp b/pract_SI/Pract. 04/Solutions/task4 - fabs.cpp new file mode 100644 index 0000000..bb2f15b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task4 - fabs.cpp @@ -0,0 +1,4 @@ +double fabs(double &num) +{ + return num > 0.0 ? num : -num; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task6.cpp b/pract_SI/Pract. 04/Solutions/task6.cpp new file mode 100644 index 0000000..cbca3de --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task6.cpp @@ -0,0 +1,4 @@ +bool isCapitalLetter(char S) +{ + return (S >= 'A' && S <= 'Z'); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task7 - a).cpp b/pract_SI/Pract. 04/Solutions/task7 - a).cpp new file mode 100644 index 0000000..8a590b4 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task7 - a).cpp @@ -0,0 +1,3 @@ +bool isInInterval(int num) { + return num >= 100 && num <= 10000; +} diff --git a/pract_SI/Pract. 04/Solutions/task7 - b).cpp b/pract_SI/Pract. 04/Solutions/task7 - b).cpp new file mode 100644 index 0000000..4dde747 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task7 - b).cpp @@ -0,0 +1,3 @@ +bool isNumberInInterval(long longnum, long long interval_lower_bound, long long interval_upper_bound,) { + return num >= interval_lower_bound && num <= interval_upper_bound; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task8.cpp b/pract_SI/Pract. 04/Solutions/task8.cpp new file mode 100644 index 0000000..9d91c6a --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task8.cpp @@ -0,0 +1,9 @@ +long long pow(int num, unsigned N) +{ + long long result = 1; + + for (size_t i = 0; i < N; i++) + result *= num; + + return result; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions/task9.cpp b/pract_SI/Pract. 04/Solutions/task9.cpp new file mode 100644 index 0000000..0356de8 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions/task9.cpp @@ -0,0 +1,17 @@ +int calculate(int a, int b, char action = '+') +{ + switch (action) + { + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + case '%': return a % b; + + default: return -1; + } +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task10.cpp b/pract_SI/Pract. 04/Solutions_10-14/task10.cpp new file mode 100644 index 0000000..e9e83b9 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task10.cpp @@ -0,0 +1,9 @@ +int askUser(int from, int to) +{ + int number; + while (std::cin >> number) + { + if (number >= from && number <= to) + return number; + } +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task11.cpp b/pract_SI/Pract. 04/Solutions_10-14/task11.cpp new file mode 100644 index 0000000..77d76e5 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task11.cpp @@ -0,0 +1,25 @@ +void convertIfNegative(int &number) +{ + if (number < 0) + number *= -1; +} + +unsigned long long toBinary(int number) +{ + convertIfNegative(number); + + unsigned long long toBinary = 0; + short digits = 0; + while (number > 0) + { + unsigned long long temp = 1; + for (size_t i = 0; i < digits; i++) + temp *= 10; + + toBinary += (number % 2) * temp; + digits++; + number /= 2; + } + + return toBinary; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp b/pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp new file mode 100644 index 0000000..1b9a3e3 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp @@ -0,0 +1,12 @@ +/* +NOTE! +Current Solution uses recursion, which we still have NOT taken/discussed. +*/ + +void toBinary(unsigned int num) +{ + if (num > 1) { + toBinary(num / 2); + } + std::cout << num % 2; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task12.cpp b/pract_SI/Pract. 04/Solutions_10-14/task12.cpp new file mode 100644 index 0000000..464b17b --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task12.cpp @@ -0,0 +1,35 @@ +#include <iostream> +using namespace std; + +int gcd(int x, int y) +{ + while (x != y) + if (x > y) + x -= y; + else + y -= x; + return x; +} + +int main() +{ + int a, b, c, d; + + cout << "a, b, c, d= "; + cin >> a >> b >> c >> d; + if (!cin || a < 1 || b < 1 || c < 1 || d < 1) + { + cout << "Error! \n"; + return 1; + } + + int r = gcd(a, b); + cout << "gcd{" << a << ", " << b << "}= " << r << "\n"; + + int s = gcd(c, d); + cout << "gcd{" << c << ", " << d << "}= " << s << "\n"; + + cout << "gcd{" << a << ", " << b << ", " << c << ", " << d << "}= " << gcd(r, s) << "\n"; + + return 0; +} diff --git a/pract_SI/Pract. 04/Solutions_10-14/task13.cpp b/pract_SI/Pract. 04/Solutions_10-14/task13.cpp new file mode 100644 index 0000000..8acb1c9 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task13.cpp @@ -0,0 +1,43 @@ +#include<iostream> +using namespace std; + +int input() +{ + int number; + do + { + cout << "Enter a valid number: "; + cin >> number; + } while (!cin || number < 100 || number > 100000); + return number; +} + +int dist(int number1, int number2) +{ + int diff = number1 - number2; + return diff >= 0 ? diff : -diff; +} + +int closestLargerPowerOf2(int number) +{ + int power = 1; + while (power < number) + { + power <<= 1; + } + return power; +} + +int main() +{ + int number = input(); + int larger = closestLargerPowerOf2(number); + int smaller = larger >> 1; + + int distToSmaller = dist(number, smaller); + int distToLarger = dist(number, larger); + + cout << "Closest power of two to " << number << ": " << ((distToSmaller < distToLarger) ? smaller : larger) << endl; + + return 0; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 04/Solutions_10-14/task14.cpp b/pract_SI/Pract. 04/Solutions_10-14/task14.cpp new file mode 100644 index 0000000..405bcf7 --- /dev/null +++ b/pract_SI/Pract. 04/Solutions_10-14/task14.cpp @@ -0,0 +1,92 @@ +#include <iostream> +#include <cmath> + +using namespace std; + +//Function to check whether a number is prime +bool isPrime(int number) +{ + int size = sqrt(number); + + for (int i = 2; i <= size; i++) + { + if (number % i == 0) + return false; + } + + return true; +} + +//Print all prime numbers that are smaller than N +void printAllPrimeToNumber(int number) //Interval [1 ; N] +{ + std::cout << "1" << ", 2"; + + for (int i = 3; i <= number; i += 2) + { + if (isPrime(i)) + std::cout << ", " << i; + } + + std::cout << "." << std::endl; +} + +//PrimeFactorization +void PrimeFactor(int number) +{ + for (int i = 2; i <= number; i++) + { + if (isPrime(i) && number % i == 0) + { + if (i == number) //last step, no space + std::cout << i; + else + std::cout << i << " "; + + number /= i; + i--; + } + } + std::cout << std::endl; +} + +//Function whether the number N can be expressed as a sum of two prime numbers +bool checkSumOfTwoPrimes(int number,int &primeOfNumber) +{ + int size = number / 2; //36 = 7 + 29, 16 = 3 + 13 , 36 = 5 + 31 + for (int i = 2;i <= size; i++) + { + //16 = 3 + x , x = 16 - 3 , x = 13 PRIME + if (isPrime(i) && isPrime(number - i)) + { + primeOfNumber = i; + return true; + } + } + return false; +} + +int main() +{ + int number; + std::cin >> number; + + if (isPrime(number)) + std::cout << number << " is prime!" << std::endl; + else + std::cout << number << " is composite number!" << std::endl; + + std::cout << std::endl << "Prime numbers up to " << number << " are: "; + printAllPrimeToNumber(number); + + std::cout << "Factoring of: " << number << " is: "; + PrimeFactor(number); + + int primeOfNumber; + if (checkSumOfTwoPrimes(number, primeOfNumber)) + std::cout << primeOfNumber << " + " << number - primeOfNumber << std::endl; + else + std::cout << "Error!" << std::endl; + + return 0; +} diff --git a/pract_SI/Pract. 04/Tasks4.txt b/pract_SI/Pract. 04/Tasks4.txt index 4daf06c..5470b9a 100644 --- a/pract_SI/Pract. 04/Tasks4.txt +++ b/pract_SI/Pract. 04/Tasks4.txt @@ -29,4 +29,17 @@ Входа да се валидира като при грешен ход да се дава възможност за нов опит. Изкарайте отделните функционалности във функции. +14. a)Напишете функция, която проверява дали дадено число е просто. +14. b)Напишете функция, която извежда на екрана всички прости числа до дадено число. +14. c)Напишете програма, която приема число и отпечатва неговата *факторизация. +*Декомпозиция на числото на произведението на други прости числа(множители), които след това умножение дават оригинала* + +Вход: 225 +Изход: 3.3.5.5 + +Вход: 31668 +Изход: 2.2.3.7.13.29 + +14. d)Напишете функция, която проверява дали дадено число може да бъде представено като сбор на 2 прости числа. + diff --git a/pract_SI/Pract. 05/Solutions/task1.cpp b/pract_SI/Pract. 05/Solutions/task1.cpp new file mode 100644 index 0000000..7832c63 --- /dev/null +++ b/pract_SI/Pract. 05/Solutions/task1.cpp @@ -0,0 +1,13 @@ +int firstDuplicateInArray(int *arr, int size) +{ + for (int i = 0; i < size - 1; i++) + { + for (int j = i + 1; j < size; j++) + { + if (arr[i] == arr[j]) + return arr[i]; + } + } + + return -1; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 05/Solutions/task1_changedCondition.cpp b/pract_SI/Pract. 05/Solutions/task1_changedCondition.cpp new file mode 100644 index 0000000..35b0d8f --- /dev/null +++ b/pract_SI/Pract. 05/Solutions/task1_changedCondition.cpp @@ -0,0 +1,10 @@ +bool isNumberInArray(int *arr, int size,int number) +{ + for (int i = 0; i < size; i++) + { + if (arr[i] == number) + return true; + } + + return false; +}
\ No newline at end of file diff --git a/pract_SI/Pract. 05/Solutions/task3.cpp b/pract_SI/Pract. 05/Solutions/task3.cpp new file mode 100644 index 0000000..b8693d9 --- /dev/null +++ b/pract_SI/Pract. 05/Solutions/task3.cpp @@ -0,0 +1,50 @@ +long long toBinary(int number) +{ + if (number < 0) + number *= -1; + + int buffer[MAX_POWTWO_SIZE]; + int size = 0; + + while (number > 0) + { + buffer[size] = number % 2; + number /= 2; + size++; + } + + long long convertedToBinary = 0; + long long multiply = 1; + for (int i = 0; i < size; i++) + { + convertedToBinary += buffer[i] * multiply; + multiply *= 10; + } + + return convertedToBinary; +} + +void convertToBinary(long long &number) +{ + if (number < 0) + number *= -1; + + int buffer[MAX_POWTWO_SIZE]; + int size = 0; + + while (number > 0) + { + buffer[size] = number % 2; + number /= 2; + size++; + } + + long long multiply = 1; + for (int i = 0; i < size; i++) + { + if(buffer[i] == 1) + number += multiply; + + multiply *= 10; + } +}
\ No newline at end of file diff --git a/pract_SI/Pract. 05/Solutions/task4.cpp b/pract_SI/Pract. 05/Solutions/task4.cpp new file mode 100644 index 0000000..19c1f38 --- /dev/null +++ b/pract_SI/Pract. 05/Solutions/task4.cpp @@ -0,0 +1,23 @@ +bool isPalindrom(int *arr, int size) +{ + for (int i = 0; i < size / 2; i++) + { + if (arr[i] != arr[size - i - 1]) + return false; + } + return true; +} + +bool isPalindrom(int number) +{ + int buffer[MAX_SIZE]; + int size = 0; + while (number > 0) //1505 + { + buffer[size] = number % 10; //5 0 5 1 + number /= 10; + size++; + } + + return isPalindrom(buffer, size); +}
\ No newline at end of file diff --git a/pract_SI/Pract. 05/Solutions/task5.cpp b/pract_SI/Pract. 05/Solutions/task5.cpp new file mode 100644 index 0000000..0676987 --- /dev/null +++ b/pract_SI/Pract. 05/Solutions/task5.cpp @@ -0,0 +1,23 @@ +void swap(int &first,int &second) +{ + int temp = first; + first = second; + second = temp; +} + +void specificOrderArray(int *arr,int size) +{ + bool isBigger = true; + + for (int i=0; i < size - 1; i++) + { + if (isBigger) + if (arr[i] < arr[i+1]) + swap(arr[i], arr[i+1]); + else + if (arr[i] > arr[i+1]) + swap(arr[i], arr[i+1]); + + isBigger = !isBigger; + } +}
\ No newline at end of file diff --git a/pract_SI/Pract. 05/Solutions/task6.cpp b/pract_SI/Pract. 05/Solutions/task6.cpp new file mode 100644 index 0000000..a3804fc --- /dev/null +++ b/pract_SI/Pract. 05/Solutions/task6.cpp @@ -0,0 +1,26 @@ +/* +A iterative binary search function. +It returns location of x in given array arr[l..r] if present, +otherwise -1. +*/ + +//int l - left side index +//int r - right side index +//int m - middle index +//int x - number 'x' we are searching for + +int binarySearch1(int *arr, int l, int r, int x) +{ + while (l <= r) + { + int m = l + (r - l) / 2; + if (arr[m] == x)// Check if x is present at mid + return m; + if (arr[m] < x)// If x greater, ignore left half + l = m + 1; + else + r = m - 1; // If x is smaller, ignore right half + } + + return -1; // if we reach here, then the element was not present +}
\ No newline at end of file diff --git a/pract_SI/Pract. 05/Tasks5.txt b/pract_SI/Pract. 05/Tasks5.txt new file mode 100644 index 0000000..5ef0385 --- /dev/null +++ b/pract_SI/Pract. 05/Tasks5.txt @@ -0,0 +1,14 @@ +#Никола Светославов - task5 + +## Задачи + +1.Напишете програма, която намира първия повтарящ се елемент в редица от N числа. +2.Напишете програма, която подрежда елементите на редица от N числа, + като първите елементи са четните, вторите - нечетните. +3.Напишете програма, която превръща дадено естествено число в двоична бройна система. +4.Напишете програма, която извежда дали едно число е палиндром. +5.Напишете програма, която подрежда елементите на редица от N числа по следния начин: +а1 > а2 < а3 > а4 < (...) +6.Напишете функция, която сортира масив във възходящ ред. + Напишете функция binarySearch, приемаща като аргументи сортиран във възходящ ред масив от цели числа, + размер на масива и търсен елемент.Функцията трябва да връща индекса на търсения елемент или -1 ,ако той не присъства в масива. diff --git a/pract_SI/Pract. 05/input.cpp b/pract_SI/Pract. 05/input.cpp new file mode 100644 index 0000000..9b8c5da --- /dev/null +++ b/pract_SI/Pract. 05/input.cpp @@ -0,0 +1,7 @@ +#include <iostream> + +void input(int *arr,const int size) +{ + for (int i = 0; i < size; i++) + std::cin >> arr[i]; +} diff --git a/pract_SI/Pract. 05/output.cpp b/pract_SI/Pract. 05/output.cpp new file mode 100644 index 0000000..79829e0 --- /dev/null +++ b/pract_SI/Pract. 05/output.cpp @@ -0,0 +1,7 @@ +#include <iostream> + +void output(int *arr,const int size) +{ + for (int i = 0; i < size; i++) + std::cout << arr[i] << std::endl; +}
\ No newline at end of file |
