initial
This commit is contained in:
commit
2802401eee
79 changed files with 2650 additions and 0 deletions
66
1sem/programming basics/z9/25.cpp
Normal file
66
1sem/programming basics/z9/25.cpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// pb_z9_25.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sort(double arr[], int len, bool asc);
|
||||
void print_array(const double *arr, size_t len);
|
||||
double *get_array(size_t len);
|
||||
int comp_asc(const void* a, const void* b);
|
||||
int comp_desc(const void* a, const void* b);
|
||||
|
||||
// 10 1 2 3 5 4 3 2 2 9 1 0
|
||||
int main() {
|
||||
size_t len;
|
||||
cout << "Array length >>>";
|
||||
cin >> len;
|
||||
cout << "Enter array elements >>>";
|
||||
double *a = get_array(len);
|
||||
bool asc;
|
||||
cout << "Ascending sort? (1/0) >>>";
|
||||
cin >> asc;
|
||||
cout << "Array before: ";
|
||||
print_array(a, len);
|
||||
sort(a, len, asc);
|
||||
cout << "Array after: ";
|
||||
print_array(a, len);
|
||||
}
|
||||
|
||||
void sort(double arr[], int len, bool asc) {
|
||||
qsort(arr, len, sizeof(double), asc? comp_asc : comp_desc);
|
||||
}
|
||||
|
||||
void print_array(const double *arr, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
double *get_array(size_t len) {
|
||||
auto *a = new double[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cin >> *(a + i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
int comp_asc(const void* a, const void* b) {
|
||||
double arg1 = *(const double*)(a);
|
||||
double arg2 = *(const double*)(b);
|
||||
|
||||
if(arg1 < arg2) return -1;
|
||||
if(arg1 > arg2) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int comp_desc(const void* a, const void* b) {
|
||||
double arg1 = *(const double*)(a);
|
||||
double arg2 = *(const double*)(b);
|
||||
|
||||
if(arg1 < arg2) return 1;
|
||||
if(arg1 > arg2) return -1;
|
||||
return 0;
|
||||
}
|
||||
70
1sem/programming basics/z9/26.cpp
Normal file
70
1sem/programming basics/z9/26.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// pb_z9_26.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sort(int arr[], int len, bool even_greater);
|
||||
void print_array(const int *arr, size_t len);
|
||||
int *get_array(size_t len);
|
||||
|
||||
// 8 1 2 3 4 3 2 6 5 1
|
||||
int main() {
|
||||
size_t len;
|
||||
cout << "Array length >>>";
|
||||
cin >> len;
|
||||
cout << "Enter array elements >>>";
|
||||
int *a = get_array(len);
|
||||
bool even_greater;
|
||||
cout << "Is even greater? (1/0) >>>";
|
||||
cin >> even_greater;
|
||||
cout << "Array before: ";
|
||||
print_array(a, len);
|
||||
sort(a, len, even_greater);
|
||||
cout << "Array after: ";
|
||||
print_array(a, len);
|
||||
}
|
||||
|
||||
void sort(int arr[], int len, bool even_greater) {
|
||||
bool swapped;
|
||||
do {
|
||||
swapped = false;
|
||||
for (size_t i = 0; i < len - 1; i++) {
|
||||
int cv = *(arr + i),
|
||||
nv = *(arr + i + 1);
|
||||
if (cv % 2 == nv % 2) {
|
||||
if (cv > nv) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (even_greater) {
|
||||
if (cv % 2 == 0 && nv % 2 != 0) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (cv % 2 != 0 && nv % 2 == 0) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (swapped);
|
||||
}
|
||||
|
||||
void print_array(const int *arr, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int *get_array(size_t len) {
|
||||
int *a = new int[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cin >> *(a + i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
65
1sem/programming basics/z9/27.cpp
Normal file
65
1sem/programming basics/z9/27.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// pb_z9_27.cpp
|
||||
// Горбацевич Андрей
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void sort(double arr[], int len, double x);
|
||||
void print_array(const double *arr, size_t len);
|
||||
double *get_array(size_t len);
|
||||
|
||||
// 8 1 2 3.5 4 3 2 2.9 1 2.5
|
||||
int main() {
|
||||
size_t len;
|
||||
cout << "Array length >>>";
|
||||
cin >> len;
|
||||
cout << "Enter array elements >>>";
|
||||
double *a = get_array(len);
|
||||
double x;
|
||||
cout << "x >>>";
|
||||
cin >> x;
|
||||
cout << "Array before: ";
|
||||
print_array(a, len);
|
||||
sort(a, len, x);
|
||||
cout << "Array after: ";
|
||||
print_array(a, len);
|
||||
auto *dist = new double[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
*(dist + i) = abs(*(a + i) - x);
|
||||
}
|
||||
cout << "Array dist: ";
|
||||
print_array(dist, len);
|
||||
}
|
||||
|
||||
void sort(double arr[], int len, double x) {
|
||||
bool swapped;
|
||||
do {
|
||||
swapped = false;
|
||||
for (size_t i = 0; i < len-1; i++) {
|
||||
double cv = *(arr + i),
|
||||
nv = *(arr + i + 1),
|
||||
cvd = abs(cv - x),
|
||||
nvd = abs(nv - x);
|
||||
if (cvd > nvd) {
|
||||
swap(*(arr + i), *(arr + i + 1));
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
} while (swapped);
|
||||
}
|
||||
|
||||
void print_array(const double *arr, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cout << *(arr + i) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
double *get_array(size_t len) {
|
||||
auto *a = new double[len];
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
cin >> *(a + i);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue