Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <ctime>
- #include <algorithm>
- #include <numeric>
- #include <string>
- using namespace std;
- void set_arr(double *);
- void f(double *);
- void show(double *);
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- srand(time(NULL));
- system("color 0A");
- auto arr = new double[10u + rand() % 11u];
- set_arr(arr);
- cout << "Исходный массив" << endl;
- show(arr);
- f(arr);
- cout << "Обработанный массив" << endl;
- show(arr);
- system("pause");
- return 0;
- }
- void set_arr(double *a)
- {
- auto len = _msize(a) / (2u * sizeof(a));
- auto filler = []()
- {
- return -10 + 20 * (rand() / static_cast<double>(RAND_MAX));
- };
- generate(a, a + len, filler);
- a[rand() % len] = rand() % 11;
- }
- void f(double *a)
- {
- auto len = _msize(a) / (2u * sizeof(a));
- auto count_positive = 0u;
- auto max = a[0u];
- auto collection = [&count_positive, &max](double p, double t) -> double
- {
- if (t > 0)
- {
- if (t > max)
- max = t;
- ++count_positive;
- return p *= t;
- }
- else
- {
- return p;
- }
- };
- auto p = accumulate(a, a + len, 1., collection);
- auto avg_geom = pow(p, 1. / count_positive);
- auto swp = [max, avg_geom](double &t)
- {
- if (t == max)
- {
- t = avg_geom;
- }
- };
- for_each(a, a + len, swp);
- }
- void show(double *a)
- {
- auto len = _msize(a) / (2u * sizeof(a));
- for (auto it = a; it != a + len; ++it)
- {
- cout << *it << endl;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement