diff --git a/123.cpp b/123.cpp new file mode 100644 index 0000000..d8f13ef --- /dev/null +++ b/123.cpp @@ -0,0 +1,392 @@ +// +//#include +//#include +//#define N 5 +//const int m = 7; +//using namespace std; +// +///* +//type_or_void name_func(void_or_ type name1, type name2,..., type1 nameN=value){ +////body +//return value; +//} +// +//*/ +// +//double suma(int n = 7) { +// double x; +// double suma = 0; +// for (; n > 0; --n) { +// cout << "Input number: "; +// cin >> x; +// suma += x; +// } +// return suma; +//} +// +// +//double sumRec(int n, double arr[]) {//sum(n)=x+sum(n-1) +// if (n == 0) return 0; +// // double x; +// cout << "Input number: "; +// cin >> arr[n - 1]; +// return arr[n - 1] + sumRec(n - 1, arr); +//} +// +//double sumRecNew(int n, double arr[], double s = 0, int i = 0) { +// if (i == n) return s; +// // double x; +// cout << "Input number: "; +// cin >> arr[i]; +// return sumRecNew(n, arr, s + arr[i], i + 1); +//} +// +// +// +// +// +//double suma(double a[], int n) { +// double sum = 0; +// for (int i = n - 1; i >= 0; --i) +// sum += a[i]; +// return sum; +//} +// +// +//void updateElement(double a[], int n) { +// for (int i = n / 2; i < n; ++i) { +// a[i] += a[0]; +// } +//} +// +//void showArray(double a[], int n) { +// if (n < 2) return; +// for (int i = 0; i < n; ++i) { +// cout.width(4); +// cout << a[i]; +// } +// cout << endl; +// return; +//} +// +//void showArray(int a[][m], int n) { +// +// for (int i = 0; i < n; ++i) { +// for (int j = 0; j < m; ++j) { +// cout.width(4); +// cout << a[i][j]; +// } +// cout << endl; +// } +// +//} +// +//void updateArray(int a[][m], int index) { +// for (int i = 0; i < m; ++i) { +// a[index][i] *= -1; +// } +//} +// +// +// +//int main() +//{ +// srand(time(NULL)); +// const int n = 5; +// double a[5]{ 0 }; +// /* cout << "Input n: "; +// cin >> n;*/ +// double s = sumRec(n, a); +// cout << "\nArray1:\n"; +// showArray(a, n); +// cout << "S= " << s << endl; +// +// s = sumRecNew(n, a); +// cout << "\nArray1:\n"; +// showArray(a, n); +// cout << "S= " << s << endl; +// +// +// +// +// //const int n = 10; +// //double sum;// = suma(10); +// //cout << "sum(10) = \n" << sum +// // << "\nsum(7) = " << suma(); +// //cout << "\n n(n>0)= "; +// // int n; +// //cin >> n; +// //if (n <= 0) { +// // cerr << "Error: " << n << " < 0 !!!!\n n= "<<-n<> index; +// if (index < 0 || index >= N) { +// index = N / 2; +// } +// updateArray(arr, index); +// cout << "\n new Array:\n"; +// showArray(arr, N);*/ +// system("pause"); +//} +// +// +// +// +// +//#include +//using namespace std; +// +//void print(int n) { +// if (n == 0) { +// return; +// } +// cout << "*"; +// print(n - 1); +//} +// +//int main() { +// setlocale(0, ""); +// int n; +// cout << "Введiть кiлькiсть зiрок: "; +// cin >> n; +// +// print(n); +// cout << endl; +// +// system("pause"); +//} + + +//#include +//using namespace std; +// +//int sum(int a, int b) { +// if (a > b) { +// return 0; +// } +// return a + sum(a + 1, b); +//} +// +//int main() { +// setlocale(0, ""); +// int a; +// int b; +// cout << "Введiть a: "; +// cin >> a; +// cout << "Введiть b: "; +// cin >> b; +// +// cout << "Сума чисел вiд " << a << " до " << b << " = " << sum(a, b) << endl; +// +// return 0; +//} + +//#include +//#include +//#define N 5 +//#define B 5 +//using namespace std; +// +//int maxElem(int x, int y) { +// int max = (x > y) ? x : y; +// return max; +//} +// +//int maxElem(int x, int y, int z) { +// int max = (x > y) ? x : y; +// max = (max > z) ? max : z; +// return max; +//} +// +//int maxElem(int a[], int n) { +// int max = a[0]; +// for (int i = 1; i < n; i++) { +// if (a[i] > max) { +// max = a[i]; +// } +// } +// return max; +//} +// +//int maxElem(int a[][N], int n) { +// int max = a[0][0]; +// for (int i = 0; i < n; i++) { +// for (int j = 0; j < N; j++) { +// if (a[i][j] > max) { +// max = a[i][j]; +// } +// } +// } +// return max; +//} +// +//int maxElem(int a[][N][B], int n) { +// int max = a[0][0][0]; +// for (int i = 0; i < n; i++) { +// for (int j = 0; j < N; j++) { +// for (int k = 0; k < B; k++) { +// if (a[i][j][k] > max) { +// max = a[i][j][k]; +// } +// } +// } +// } +// return max; +//} +// +//int main() { +// srand(time(0)); +// setlocale(0, ""); +// +// int x = rand() % 100; +// int y = rand() % 100; +// int z = rand() % 100; +// cout << "Случайные числа: x = " << x << ", y = " << y << ", z = " << z << endl; +// cout << "max(2): " << maxElem(x, y) << endl; +// cout << "max(3): " << maxElem(x, y, z) << endl << endl; +// +// int arr1[5]; +// cout << "Одномерный массив: "; +// for (int i = 0; i < 5; i++) { +// arr1[i] = rand() % 100; +// cout << arr1[i] << " "; +// } +// cout << "\nmax(1D): " << maxElem(arr1, 5) << endl << endl; +// +// int arr2[2][N]; +// cout << "Двумерный массив:\n"; +// for (int i = 0; i < 2; i++) { +// for (int j = 0; j < N; j++) { +// arr2[i][j] = rand() % 100; +// cout << arr2[i][j] << "\t"; +// } +// cout << endl; +// } +// cout << "max(2D): " << maxElem(arr2, 2) << endl << endl; +// +// int arr3[2][N][B]; +// cout << "Трехмерный массив:\n"; +// for (int i = 0; i < 2; i++) { +// cout << "Плоскость " << i << ":\n"; +// for (int j = 0; j < N; j++) { +// for (int k = 0; k < B; k++) { +// arr3[i][j][k] = rand() % 100; +// cout << arr3[i][j][k] << "\t"; +// } +// cout << endl; +// } +// cout << endl; +// } +// cout << "max(3D): " << maxElem(arr3, 2) << endl; +// +// return 0; +//} + + +#include +#include +using namespace std; + +void bSort(int arr[], int size) { + for (int i = 0; i < size - 1; ++i) { + for (int j = 0; j < size - i - 1; ++j) { + if (arr[j] > arr[j + 1]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +int bSearch(int arr[], int size, int target) { + int low = 0; + int high = size - 1; + + while (low <= high) { + int m = low + (high - low) / 2; + + if (arr[m] == target) { + return m; + } + if (arr[m] < target) { + low = m + 1; + } + else { + high = m - 1; + } + } + + return -1; +} + +int main() { + srand(time(0)); + setlocale(0, ""); + + const int size = 10; + int arr[size]; + + cout << "Початковий масив: "; + for (int i = 0; i < size; ++i) { + arr[i] = rand() % 300 + 1; + cout << arr[i] << " "; + } + cout << endl; + + bSort(arr, size); + + cout << "Вiдсортований масив: "; + for (int i = 0; i < size; ++i) { + cout << arr[i] << " "; + } + cout << endl; + + int t; + cout << "Введiть елемент для пошуку: "; + cin >> t; + + int index = bSearch(arr, size, t); + + if (index != -1) { + cout << "Елемент знайдено на iндексi (у вiдсортованому масивi): " << index << endl; + } + else { + cout << "Елемент не знайдено." << endl; + } + + return 0; +}