C/C++
Программа в C++
Дан массив чисел В [0:N]. Вывести на печать первый отрицательный элемент массива и его порядковый номер. Получить массив положительных и массив отрицательных элементов. Заранее спасибо за ответ
#include <algorithm>
#include <iostream>
#include <random>
#include <iomanip>
using namespace std;
int main() {
uniform_int_distribution<> uid(-5, 25);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int x) { cout << setw(4) << x; };
auto negate = [](int x) { return x < 0; };
auto positive = [](int x) { return x > 0; };
cout << "N: ";
size_t n;
cin >> n;
auto box = new int[n];
generate(box, box + n, rand);
for_each(box, box + n, show);
puts("");
auto target = find_if(box, box + n, negate);
if (target != box + n) {
auto position = target - box + 1;
cout << "Position: " << position << '\n';
auto nn = count_if(box, box + n, negate);
auto neg = new int[nn];
copy_if(box, box + n, neg, negate);
for_each(neg, neg + nn, show);
puts("");
delete[] neg;
} else {
puts("Not found!");
}
if (find_if(box, box + n, positive) != box + n) {
auto np = count_if(box, box + n, positive);
auto pos = new int[np];
copy_if(box, box + n, pos, positive);
for_each(pos, pos + np, show);
puts("");
delete[] pos;
} else {
puts("Not found!");
}
delete[] box;
system("pause > nul");
}
#include <iostream>
#include <random>
#include <iomanip>
using namespace std;
int main() {
uniform_int_distribution<> uid(-5, 25);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int x) { cout << setw(4) << x; };
auto negate = [](int x) { return x < 0; };
auto positive = [](int x) { return x > 0; };
cout << "N: ";
size_t n;
cin >> n;
auto box = new int[n];
generate(box, box + n, rand);
for_each(box, box + n, show);
puts("");
auto target = find_if(box, box + n, negate);
if (target != box + n) {
auto position = target - box + 1;
cout << "Position: " << position << '\n';
auto nn = count_if(box, box + n, negate);
auto neg = new int[nn];
copy_if(box, box + n, neg, negate);
for_each(neg, neg + nn, show);
puts("");
delete[] neg;
} else {
puts("Not found!");
}
if (find_if(box, box + n, positive) != box + n) {
auto np = count_if(box, box + n, positive);
auto pos = new int[np];
copy_if(box, box + n, pos, positive);
for_each(pos, pos + np, show);
puts("");
delete[] pos;
} else {
puts("Not found!");
}
delete[] box;
system("pause > nul");
}
Похожие вопросы
- Помогите написать программу на C++
- Написать программу на c++
- Помоги пожалуйста написать программу на C++
- Помогите с программой на C++
- ПОМОГИТЕ СОСТАВИТЬ ПРОГРАММУ НА C++.
- Короткая программа на C++
- Написать программу на C++.Создать класс vector3d, задаваемый тройкой координат. Создать конструктор...
- Помогите написать программу на C++
- Составить программу на c++ УСПОЛЬЗУЯ МАССИВ!!!
- Помогите пожалуйста составить правильную программу на C++ 12 вариант