C/C++

Помогите пожалуйста. Я не понимаю как решать подобные задачки.

Дан одномерный массив A[20]. Создать программу на C++ Найдите произведение элементов, имеющих значение меньше -2 (минус два).
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
double task(int* box, size_t n, int m) {
auto x = 0U;
for (auto i = 0U; i < n; ++i) if (box[i] == m) ++x;
return pow(m, x);
}
void print(int* box, size_t n) {
for (auto i = 0U; i < n; ++i) cout << setw(3) << box[i];
puts("");
}
int main() {
uniform_int_distribution<> uid(-2, 2);
mt19937 gen{ random_device()() };
const auto n = 20U;
int a[n];
for (auto& x : a) x = uid(gen);
print(a, n);
const auto m = task(a, n, -2);
cout << ' ' << m << '\n';
system("pause > nul");
}
ВК
Владимир Кожевников
56 045
Лучший ответ
#include "iostream"
#include "ctime"
#include "cstdlib"
#include "algorithm"
using namespace std;
int main(){
const int n=20; double a[n]; srand(time(NULL));
for(double &i:a)i=(rand()%100-50)/10.,cout<< i<<' ';
cout<< endl<< accumulate(a,a+n,1.,
[](double l, double r){return r<-2?l*r:l;})<< endl;}
Андрей Опрышко
Андрей Опрышко
66 782
#include < iostream >
using namespace std;

int main()
{
   int A[20] = {-1, 2, -3, 4, 5, -6, 7, 8, 9, 10, 1, -2, 3, -4, -5, -6, 7, 8, 9, -10};
   long long count = 1;
   bool flag = false;
   for(int i = 0; i < 20; i++)
   {
      if (A[i] < -2)
      {
         count *= A[i];
         flag = true;
      }
   }
   cout << ((flag) ? count : 0) << endl;
}
Евгений Селин
Евгений Селин
73 465
Дима .
Дима .
68 826