C/C++

Помогите написать программу на c++

Задан одномерный массив А(10). Найти произведение отрицательных элементов массива. Заданный массив отсортировать методом обмена по возрастанию.
#include "iostream"
#include "iomanip"
#include "ctime"
#include "cstdlib"
#include "algorithm"
using namespace std;
int main() {
const int n=10; double a[n],p=1; bool b=false; srand(time(NULL));
for(double &i:a)cout<<(i=(rand()%1999-999)/100.)<<' ';
for(double &i:a)if(i<0)p*=i,b=true; cout<< fixed<< setprecision(2);
if(b)cout<<"\np="<< p<<'\n'; else cout<<"\nNo negatives\n";
for(double &i:a)for_each(a,a+n-1,[](double &x){if(x>*(&x+1))swap(x,*(&x+1));});
for(double &i:a)cout<< i<<' '; cout<< endl;}
Михаил Юсупов
Михаил Юсупов
85 673
Лучший ответ
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
void bubble_sort(int* box, int n) {
auto first = box;
auto last = box + n;
while (first < --last) {
for (auto begin = first; begin < last; ++begin) {
if (*(begin + 1) < *begin) {
swap(*begin, *(begin + 1));
}
}
}
}
void show(int* box, const size_t n, const streamsize w) {
for (auto i = 0U; i < n; ++i) {
cout << setw(w) << box[i];
}
puts("");
}
void fill_random(int* box, const size_t n, int a, int b) {
if (a > b) swap(a, b);
uniform_int_distribution<> uid(a, b);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) {
box[i] = uid(gen);
}
}
long long product_negative(int* box, const size_t n) {
auto m = 1LL;
auto count = 0U;
for (auto i = 0U; i < n; ++i) {
if (box[i] < 0) {
m *= box[i];
++count;
}
}
return count ? m : 0LL;
}
int main() {
const auto n = 10U;
int box[n]{};
fill_random(box, n, -9, 9);
const streamsize w = 4U;
show(box, n, w);
auto m = product_negative(box, n);
if (m) cout << "m: " << m << '\n';
else puts("Negative not found!");
bubble_sort(box, n);
show(box, n, w);
}