Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <iomanip>
- #include <vector>
- #include <iterator>
- #include <cmath>
- #include <algorithm>
- #include <numeric>
- using namespace std;
- float get_rand()
- {
- return rand() / (rand() + 0.1f) - rand() / (rand() + 1);
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- int N = 0;
- cout << "Кол-во в массиве: ";
- cin >> N;
- if (N < 2)
- {
- cout << "\nНеобходимо больше элементов";
- return 0;
- }
- srand(time(0));
- vector<float> a(N, 0.0f);
- generate_n(a.begin(), N, get_rand);
- cout << "Элементы: " << endl;
- cout << fixed << setprecision(2); // %0.2f
- for (int i = 0; i < N; i++)
- {
- cout << a[i] << " ";
- }
- int c = 0;
- for (int i = 0; i < N; ++i)
- {
- c += a[i] < 0;
- }
- vector<float>::const_iterator itMin = a.begin();
- for (vector<float>::const_iterator it = a.begin() + 1; it != a.end(); ++it)
- {
- if (abs(*it) < abs(*itMin))
- {
- itMin = it;
- }
- }
- const float summa = accumulate(a.begin(), a.end(), 0.0f);
- cout << endl << "1) Количество отрицательных элементов массива: " << c;
- cout << endl << "2) Сумма модулей элементов массива, расположенных после минимального по модулю элемента = " << summa;
- cout << endl << "3) Отфильтрованный массив: " << endl;
- for (int i = 0; i < N; i++)
- {
- if (a[i] < 0) a[i] = a[i] * a[i];
- }
- sort(a.begin(), a.end());
- for (int i = 0; i < N; i++)
- {
- cout << a[i] << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement