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");
}
#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");
}
#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;}
#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;}
#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;
}
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;
}
Александр Ярмак
Исправил=)

Похожие вопросы
- Срочно!!! Помогите пожалуйста написать код к задачке на с++! Заранее спасибо!
- Задачки по С++ (помогите пожалуйста кто какие сможет)
- Помогите пожалуйста решить задачку по C++, напишите полный текст кода пожалуйста
- Помогите пожалуйста решить задачки c++
- Помогите пожалуйста не понимаю как делать. Буду очень благодарен
- Помогите решить задачу, пожалуйста. Сам не понимаю. (Язык Си)
- Помоги пожалуйста с программой в С++
- Помогите пожалуйста написать программу на Си
- Помогите пожалуйста уже голову сломал, ничего не получается, когда перегружаю шаблонный оператор << ,
- Помогите пожалуйста с Массивами .В языке С .Заполнил 2-мерный массив N и M случайными числами дальше не понимаю.