Compare commits
32 Commits
C++DZ-09.0
...
C++ClassWo
| Author | SHA1 | Date | |
|---|---|---|---|
| 6314f437db | |||
| a541adab0b | |||
| 9043777a10 | |||
| d747caaf36 | |||
| 53581f2888 | |||
| ad94106e74 | |||
| a860f1d299 | |||
| 290b59fee3 | |||
| 89d2b6c8e5 | |||
| c5e2420500 | |||
| 00296eede8 | |||
| dec9d44643 | |||
| bee1877856 | |||
| 99ec7830f7 | |||
| 0c5d8e04f5 | |||
| 8c99163ff3 | |||
| 29b940b6b2 | |||
| 64a71ee7dc | |||
| 42dcc1011a | |||
| f434c2e894 | |||
| 39096faf4b | |||
| 46c2825d9c | |||
| 5af0942e4f | |||
| 496c7956e3 | |||
| 655afa03d6 | |||
| 1d2b458c2c | |||
| a00daf56f3 | |||
| 298e172db1 | |||
| 9e104306e6 | |||
| 1008cecd8f | |||
| d6a88bde24 | |||
| 6d87e57c90 |
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;
|
||||||
|
}
|
||||||
127
task1.cpp
127
task1.cpp
@@ -1,77 +1,50 @@
|
|||||||
//#include <iostream>
|
#include <iostream>
|
||||||
//
|
using namespace std;
|
||||||
//using namespace std;
|
|
||||||
//
|
void sortArr(int* p, int n) {
|
||||||
//int main() {
|
for (int i = 0; i < n - 1; i++) {
|
||||||
// int s;
|
for (int j = 0; j < n - i - 1; j++) {
|
||||||
// cout << "Size: ";
|
if (p[j] > p[j + 1]) {
|
||||||
// cin >> s;
|
int t = p[j];
|
||||||
// int t;
|
p[j] = p[j + 1];
|
||||||
// cout << "Shape (1-left,2-right,3-up,4-down,5-square): ";
|
p[j + 1] = t;
|
||||||
// cin >> t;
|
}
|
||||||
// if (s < 1) return 0;
|
}
|
||||||
// switch (t) {
|
}
|
||||||
// case 1: {
|
}
|
||||||
// int i;
|
|
||||||
// int j;
|
void printArr(int* p, int n) {
|
||||||
// for (i = 1; i <= s; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
// for (j = 1; j <= i; j++) cout << "*";
|
cout << p[i] << " ";
|
||||||
// cout << "\n";
|
}
|
||||||
// }
|
cout << "\n";
|
||||||
// break;
|
}
|
||||||
// }
|
|
||||||
// case 2: {
|
int main() {
|
||||||
// int i;
|
srand(time(0));
|
||||||
// int j;
|
int n;
|
||||||
// for (i = 1; i <= s; i++) {
|
|
||||||
// for (j = 1; j <= s - i; j++) cout << " ";
|
cout << "Enter N: ";
|
||||||
// for (j = 1; j <= i; j++) cout << "*";
|
cin >> n;
|
||||||
// cout << "\n";
|
|
||||||
// }
|
int* a = new int[n];
|
||||||
// break;
|
|
||||||
// }
|
for (int i = 0; i < n; i++) {
|
||||||
// case 3: {
|
a[i] = rand() % 101 - 50;
|
||||||
// int i;
|
}
|
||||||
// int j;
|
|
||||||
// int k;
|
cout << "\nOriginal array:\n";
|
||||||
// for (i = 1; i <= s; i++) {
|
printArr(a, n);
|
||||||
// for (j = 1; j <= s - i; j++) cout << " ";
|
|
||||||
// for (k = 1; k <= 2 * i - 1; k++) cout << "*";
|
sortArr(a, n);
|
||||||
// cout << "\n";
|
|
||||||
// }
|
cout << "\nSorted array:\n";
|
||||||
// break;
|
printArr(a, n);
|
||||||
// }
|
|
||||||
// case 4: {
|
cout << "\nArray without first and last elements:\n";
|
||||||
// int i;
|
printArr(a + 1, n - 2);
|
||||||
// int j;
|
|
||||||
// int k;
|
delete[] a;
|
||||||
// for (i = s; i >= 1; i--) {
|
return 0;
|
||||||
// for (j = 1; j <= s - i; j++) cout << " ";
|
|
||||||
// for (k = 1; k <= 2 * i - 1; k++) cout << "*";
|
}
|
||||||
// cout << "\n";
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// case 5: {
|
|
||||||
// int i;
|
|
||||||
// int j;
|
|
||||||
// int k;
|
|
||||||
// for (i = s; i >= 1; i--) {
|
|
||||||
// for (j = 1; j <= s - i; j++) cout << " ";
|
|
||||||
// for (k = 1; k <= 2 * i - 1; k++) cout << "*";
|
|
||||||
// cout << "\n";
|
|
||||||
// }
|
|
||||||
// for (i = 1; i <= s; i++) {
|
|
||||||
// for (j = 1; j <= s - i; j++) cout << " ";
|
|
||||||
// for (k = 1; k <= 2 * i - 1; k++) cout << "*";
|
|
||||||
// cout << "\n";
|
|
||||||
// }
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// default: {
|
|
||||||
// cout << "Wrong choice\n";
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return 0;
|
|
||||||
//}
|
|
||||||
|
|||||||
40
task2.cpp
40
task2.cpp
@@ -1,40 +0,0 @@
|
|||||||
//#include <iostream>
|
|
||||||
//
|
|
||||||
//using namespace std;
|
|
||||||
//
|
|
||||||
//int main() {
|
|
||||||
// long long x;
|
|
||||||
// cout << "Number: ";
|
|
||||||
// cin >> x;
|
|
||||||
// long long y;
|
|
||||||
// y = llabs(x);
|
|
||||||
// if (y == 0) {
|
|
||||||
// cout << "Digits: 1\n";
|
|
||||||
// cout << "Sum: 0\n";
|
|
||||||
// cout << "Avg: 0\n";
|
|
||||||
// cout << "Zeros: 1\n";
|
|
||||||
// return 0;
|
|
||||||
// }
|
|
||||||
// int c;
|
|
||||||
// c = 0;
|
|
||||||
// int s;
|
|
||||||
// s = 0;
|
|
||||||
// int z;
|
|
||||||
// z = 0;
|
|
||||||
// while (y > 0) {
|
|
||||||
// int d;
|
|
||||||
// d = y % 10;
|
|
||||||
// s += d;
|
|
||||||
// if (d == 0) z++;
|
|
||||||
// y /= 10;
|
|
||||||
// c++;
|
|
||||||
// }
|
|
||||||
// double a;
|
|
||||||
// a = (c == 0) ? 0.0 : (double)s / c;
|
|
||||||
// cout << "Digits: " << c << "\n";
|
|
||||||
// cout << "Sum: " << s << "\n";
|
|
||||||
// cout << "Avg: " << a << "\n";
|
|
||||||
// cout << "Zeros: " << z << "\n";
|
|
||||||
// return 0;
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
85
task3-2.0.cpp
Normal file
85
task3-2.0.cpp
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int* Pos(int* a, int n, int& res_n) {
|
||||||
|
int* tmp = new int[3];
|
||||||
|
res_n = 0;
|
||||||
|
for (int i = 0; i < 3 && i < n; ++i)
|
||||||
|
if (a[i] > 0)
|
||||||
|
tmp[res_n++] = a[i];
|
||||||
|
int* res = new int[res_n];
|
||||||
|
for (int i = 0; i < res_n; ++i) res[i] = tmp[i];
|
||||||
|
delete[] tmp;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* last3(int* a, int n, int& res_n) {
|
||||||
|
res_n = (n >= 3) ? 3 : n;
|
||||||
|
int* res = new int[res_n];
|
||||||
|
for (int i = 0; i < res_n; ++i)
|
||||||
|
res[i] = a[n - res_n + i];
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int* min3(int* a, int n, int& res_n) {
|
||||||
|
int* tmp = new int[n];
|
||||||
|
for (int i = 0; i < n; ++i) tmp[i] = a[i];
|
||||||
|
for (int i = 0; i < n - 1; ++i)
|
||||||
|
for (int j = i + 1; j < n; ++j)
|
||||||
|
if (tmp[i] > tmp[j]) { int t = tmp[i]; tmp[i] = tmp[j]; tmp[j] = t; }
|
||||||
|
res_n = (n >= 3) ? 3 : n;
|
||||||
|
int* res = new int[res_n];
|
||||||
|
for (int i = 0; i < res_n; ++i) res[i] = tmp[i];
|
||||||
|
delete[] tmp;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef int* (*searchFunc)(int*, int, int&);
|
||||||
|
|
||||||
|
void service(int* a, int n, searchFunc f) {
|
||||||
|
int res_n;
|
||||||
|
int* res = f(a, n, res_n);
|
||||||
|
cout << "Result: ";
|
||||||
|
for (int i = 0; i < res_n; ++i) cout << res[i] << " ";
|
||||||
|
cout << endl;
|
||||||
|
delete[] res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(NULL));
|
||||||
|
int n;
|
||||||
|
cout << "n(n>=3)= ";
|
||||||
|
cin >> n;
|
||||||
|
if (n < 3) {
|
||||||
|
cerr << "Error: size Array!!";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int* arr = new int[n];
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
arr[i] = rand() % 41 - 20;
|
||||||
|
|
||||||
|
cout << "Array: ";
|
||||||
|
for (int i = 0; i < n; ++i) cout << arr[i] << " ";
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
searchFunc arrFunc[3] = { Pos, last3, min3 };
|
||||||
|
|
||||||
|
int choice;
|
||||||
|
do {
|
||||||
|
cout << "\nMenu:\n"
|
||||||
|
<< "1) Array of the first 3 elements > 0\n"
|
||||||
|
<< "2) Array of the last 3 elements\n"
|
||||||
|
<< "3) Array of the 3 smallest elements\n"
|
||||||
|
<< "4) Exit\n"
|
||||||
|
<< "Select action (1-4): ";
|
||||||
|
cin >> choice;
|
||||||
|
|
||||||
|
if (choice >= 1 && choice <= 3)
|
||||||
|
service(arr, n, arrFunc[choice - 1]);
|
||||||
|
else if (choice != 4)
|
||||||
|
cout << "Incorrect choice!\n";
|
||||||
|
} while (choice != 4);
|
||||||
|
|
||||||
|
delete[] arr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
89
task3.cpp
89
task3.cpp
@@ -1,19 +1,70 @@
|
|||||||
//#include <iostream>
|
#include <iostream>
|
||||||
//
|
using namespace std;
|
||||||
//using namespace std;
|
|
||||||
//
|
void Pos(int* a, int n) {
|
||||||
//int main() {
|
int count = 0;
|
||||||
// int k;
|
for (int i = 0; i < 3 && i < n; ++i)
|
||||||
// k = 0;
|
if (a[i] > 0) ++count;
|
||||||
// for (int i = 100; i <= 999; i++) {
|
cout << "The number of the first 3 elements > 0: " << count << endl;
|
||||||
// int a;
|
}
|
||||||
// a = i / 100;
|
|
||||||
// int b;
|
void Show1(int* a, int n) {
|
||||||
// b = (i / 10) % 10;
|
cout << "The last 3 elements: ";
|
||||||
// int c;
|
for (int i = (n >= 3 ? n - 3 : 0); i < n; ++i)
|
||||||
// c = i % 10;
|
cout << a[i] << " ";
|
||||||
// if (a != b && a != c && b != c) k++;
|
cout << endl;
|
||||||
// }
|
}
|
||||||
// cout << k << "\n";
|
|
||||||
// return 0;
|
void Show2(int* a, int n) {
|
||||||
//}
|
int* b = new int[n];
|
||||||
|
for (int i = 0; i < n; ++i) b[i] = a[i];
|
||||||
|
|
||||||
|
for (int i = 0; i < n-1; ++i)
|
||||||
|
for (int j = i+1; j < n; ++j)
|
||||||
|
if (b[i] > b[j]) { int t = b[i]; b[i] = b[j]; b[j] = t; }
|
||||||
|
cout << "The three smallest elements: ";
|
||||||
|
for (int i = 0; i < 3 && i < n; ++i) cout << b[i] << " ";
|
||||||
|
cout << endl;
|
||||||
|
delete[] b;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void (*searchFunc)(int*, int);
|
||||||
|
|
||||||
|
void service(int* a, int n, searchFunc f) {
|
||||||
|
f(a, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(NULL));
|
||||||
|
int n;
|
||||||
|
cout << "n(n>=3)= ";
|
||||||
|
cin >> n;
|
||||||
|
if (n < 3) {
|
||||||
|
cerr << "Error: size Array!!";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
int* arr = new int[n];
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
arr[i] = rand() % 41 - 20;
|
||||||
|
|
||||||
|
cout << "Array: ";
|
||||||
|
for (int i = 0; i < n; ++i) cout << arr[i] << " ";
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
searchFunc arrFunc[3] = { Pos, Show1, Show2 };
|
||||||
|
|
||||||
|
cout << "Menu:\n"
|
||||||
|
<< "1) The number of the first 3 elements > 0\n"
|
||||||
|
<< "2) Show the last 3 items\n"
|
||||||
|
<< "3) Display the 3 smallest elements\n"
|
||||||
|
<< "Select an action (1-3): ";
|
||||||
|
int choice;
|
||||||
|
cin >> choice;
|
||||||
|
if (choice >= 1 && choice <= 3)
|
||||||
|
service(arr, n, arrFunc[choice-1]);
|
||||||
|
else
|
||||||
|
cout << "Incorrect choice!\n";
|
||||||
|
|
||||||
|
delete[] arr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
18
task4.cpp
18
task4.cpp
@@ -1,18 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
long long n;
|
|
||||||
cout << "Number: ";
|
|
||||||
cin >> n;
|
|
||||||
long long m;
|
|
||||||
m = llabs(n);
|
|
||||||
if (m == 0) {
|
|
||||||
cout << "All integers divide 0\n";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
for (long long i = 1; i <= m; i++) if (m % i == 0) cout << i << " ";
|
|
||||||
cout << "\n";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user