diff options
12 files changed, 165 insertions, 5 deletions
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task4 - abs.cpp | 4 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task4 - fabs.cpp | 4 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task7 - a).cpp | 3 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task7 - b).cpp | 3 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task8.cpp | 9 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions/task9.cpp | 17 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task10.cpp | 9 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task11.cpp | 25 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task11_recursive.cpp | 12 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task12.cpp | 35 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task13.cpp | 43 | ||||
| -rw-r--r-- | pract_SI/Pract. 04/Solutions_10-14/task14.cpp | 6 |
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/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 index c95c36c..405bcf7 100644 --- a/pract_SI/Pract. 04/Solutions_10-14/task14.cpp +++ b/pract_SI/Pract. 04/Solutions_10-14/task14.cpp @@ -8,7 +8,6 @@ bool isPrime(int number) { int size = sqrt(number); - for (int i = 2; i <= size; i++) { if (number % i == 0) @@ -88,9 +87,6 @@ int main() std::cout << primeOfNumber << " + " << number - primeOfNumber << std::endl; else std::cout << "Error!" << std::endl; - - - - + return 0; } |
