C/C++

Составить программу для обработки одномерных статических массивов.

#include <iostream>
#include <functional>
#include <iomanip>
#include <random>
using namespace std;
struct Array {
static size_t count(unsigned* box, const size_t n, function<bool(size_t, unsigned)> fn) {
auto cnt = 0U;
for (auto i = 0U; i < n; ++i) if (fn(i, box[i])) ++cnt;
return cnt;
}
static void show(unsigned* box, const size_t n, streamsize w) {
for (auto i = 0U; i < n; ++i) cout << setw(w) << box[i];
cout.put('\n');
}
static void random_fill(unsigned* box, const size_t n, unsigned a, unsigned b) {
if (a > b) swap(a, b);
uniform_int_distribution<unsigned> uid(a, b);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) box[i] = uid(gen);
}
};
int main() {
unsigned box[20];
auto n = size(box);
Array::random_fill(box, n, 10U, 99U);
Array::show(box, n, 3);
auto predicate = [](size_t i, unsigned x) { return i & 1 && x & 1; };
auto count = Array::count(box, n, predicate);
cout << " Result: " << count << '\n';
system("pause > nul");
}
MM
Marat Mara
68 898
Лучший ответ
Чилевич Игорь почему в виде структуры, а не класса?
#include < iostream >
#include < vector >

using namespace std;

signed main() {
int n;
cin >> n;
vector < int > a(n);
int cnt = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
cnt += (i % 2 == 1) && (a[i] % 2 == 1);
}
cout << cnt;
}
Олег Косарев
Олег Косарев
8 952