Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <random>
- #include <algorithm>
- #include <vector>
- using namespace std;
- using box_t = pair<int*, const size_t>;
- class Array {
- public:
- static void fill(box_t& box, const int a, const int b) {
- auto[sequence, length] = box;
- for (auto i = 0U; i < length; ++i) sequence[i] = generate(a, b);
- }
- static void show(const box_t& box, const streamsize width = 4U) {
- auto[sequence, length] = box;
- for (auto i = 0U; i < length; ++i) cout << setw(width) << sequence[i];
- cout.put('\n');
- }
- static pair<int, int> first_even(const box_t& box) {
- auto[sequence, length] = box;
- for (auto i = 0; i < int(length); ++i) if (0 == sequence[i] % 2) return { i, sequence[i] };
- return { -1, 0 };
- }
- static pair<int, int> last_pos(const box_t& box) {
- auto[sequence, length] = box;
- int pos = length - 1;
- do if (sequence[pos] > 0) break; while (--pos >= 0);
- if (pos < 0) return { -1, 0 };
- return { pos, sequence[pos] };
- }
- static void sort_increase(box_t& box, size_t start, size_t last) {
- auto[sequence, length] = box;
- if (start > last) swap(start, last);
- if (start == last || last >= length) return;
- sort(sequence + start, sequence + last);
- }
- static void show_prime(box_t& box, const streamsize width = 3U) {
- auto[sequence, length] = box;
- vector<size_t> indexes;
- for (auto i = 0; i < int(length); ++i) if (is_prime(abs(sequence[i]))) indexes.push_back(i);
- cout << indexes.size() << ';';
- for (const auto index : indexes) cout << ' ' << index;
- cout.put('\n');
- }
- private:
- static int generate(int a, int b) {
- if (a > b) swap(a, b);
- const uniform_int_distribution<> uid(a, b);
- random_device rd;
- mt19937 gen(rd());
- return uid(gen);
- }
- static bool is_prime(const int num) {
- bool prime;
- if (num == 2 || num == 3 || num == 5) prime = true;
- else if (~num & 1 || num < 2 || 0 == num % 3 || 0 == num % 5) prime = false;
- else {
- int n;
- for (n = 3; n * n <= num && num % n; n += 2) { ; }
- prime = n * n > num;
- }
- return prime;
- }
- };
- int main() {
- const auto n = 27U;
- const auto l = -48, r = 59;
- int a[n];
- box_t box_a{ a, n };
- Array::fill(box_a, l, r);
- cout << "Array a:";
- Array::show(box_a);
- int b[n];
- box_t box_b{ b, n };
- Array::fill(box_b, l, r);
- cout << "Array b:";
- Array::show(box_b);
- auto[index_a_even, value_a_even] = Array::first_even(box_a);
- if (index_a_even != -1) cout
- << "\nThe value of the first even element for a: " << value_a_even
- << "\n The index of the first even element for a: " << index_a_even;
- auto[index_a_pos, value_a_pos] = Array::last_pos(box_a);
- if (index_a_pos != -1) cout
- << "\nThe value of the last positive element for a: " << value_a_pos
- << "\nThe index of the last positive element for a: " << index_a_pos
- << '\n';
- auto[index_b_even, value_b_even] = Array::first_even(box_b);
- if (index_b_even != -1) cout
- << "\nThe value of the first even element for b: " << value_b_even
- << "\nThe index of the first even element for b: " << index_b_even;
- auto[index_b_pos, value_b_pos] = Array::last_pos(box_b);
- if (index_b_pos != -1) cout
- << "\nThe value of the last positive element for b: " << value_b_pos
- << "\nThe index of the last positive element for b: " << index_b_pos
- << '\n';
- Array::sort_increase(box_a, index_a_even + 1, index_a_pos);
- cout << "\nArray a: ";
- Array::show(box_a);
- Array::sort_increase(box_b, index_b_even + 1, index_b_pos);
- cout << "Array b: ";
- Array::show(box_b);
- cout << "The number of prime numbers in a: ";
- Array::show_prime(box_a);
- cout << "The number of prime numbers in b: ";
- Array::show_prime(box_b);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement