Добавить task1-2.0.cpp
This commit is contained in:
67
task1-2.0.cpp
Normal file
67
task1-2.0.cpp
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void sortArr(int* a, int n) {
|
||||||
|
for (int i = 1; i < n; i++) {
|
||||||
|
int key = a[i];
|
||||||
|
int j = i - 1;
|
||||||
|
while (j >= 0 && a[j] > key) {
|
||||||
|
a[j + 1] = a[j];
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
a[j + 1] = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void RemovableLmR(int* a, int& n) {
|
||||||
|
if (n <= 2) {
|
||||||
|
n = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Зсуваємо масив вліво, видаляємо перший
|
||||||
|
for (int i = 0; i < n - 1; i++) {
|
||||||
|
a[i] = a[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Просто зменшуємо розмір ще на один, прибираємо останній
|
||||||
|
n = n - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printArr(int* a, int n) {
|
||||||
|
if (n == 0) {
|
||||||
|
cout << "(empty)\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
cout << a[i] << " ";
|
||||||
|
}
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(0));
|
||||||
|
int n;
|
||||||
|
cout << "Enter N: ";
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
int* a = new int[n];
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
a[i] = rand() % 101 - 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "\nOriginal array:\n";
|
||||||
|
printArr(a, n);
|
||||||
|
|
||||||
|
sortArr(a, n);
|
||||||
|
cout << "\nSorted array:\n";
|
||||||
|
printArr(a, n);
|
||||||
|
|
||||||
|
RemovableLmR(a, n);
|
||||||
|
cout << "\nArray after removing first and last:\n";
|
||||||
|
printArr(a, n);
|
||||||
|
|
||||||
|
delete[] a;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user