diff options
8 files changed, 159 insertions, 0 deletions
| -rw-r--r-- | pract_SI/Pract. 05/Solutions/task1.cpp | 13 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/Solutions/task1_changedCondition.cpp | 10 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/Solutions/task3.cpp | 50 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/Solutions/task4.cpp | 23 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/Solutions/task5.cpp | 23 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/Solutions/task6.cpp | 26 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/input.cpp | 7 | ||||
| -rw-r--r-- | pract_SI/Pract. 05/output.cpp | 7 |
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/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 |
