В вещественном массиве найти местоположение (индексы или указатели):
1) Четных элементов
2) Нечетных элементов
3) Элементов, кратных K
Использовать функцию, возвращающую указатель (или ссылку) на очередной найденный элемент, удовлетворяющий заданному условию
Другие языки программирования и технологии
Помогите с заданием, пожалуйста. C++.
#include <iostream>
using namespace std;
using p_unary = bool(*)(int a);
using p_binary = bool(*)(int a, int b);
bool is_odd(int num) { return num & 1; }
bool is_even(int num) { return! is_odd(num); }
bool is_aliquot(int num, int den) { return num % den == 0; }
int* get(int* begin, int* end, p_unary func) {
auto next = begin;
do if (func(*next)) return next; while (++next != end);
return end;
}
int* get(int* begin, int* end, int den, p_binary func) {
auto next = begin;
do if (func(*next, den)) return next; while (++next != end);
return end;
}
int main() {
const size_t size = 15;
int vec[size] = { 15, 22, 35, 42, 84, 66, 73, 45, 93, 20, 33, 81, 42, 28, 26 };
cout << "Array:";
for (auto item : vec) cout << ' ' << item;
cout << "\nEven:";
auto end = vec + size;
auto even = vec;
do {
even = get(even, end, is_even);
if (even == end) break;
cout << ' ' << *even;
} while (++even);
cout << "\nOdd:";
auto odd = vec;
do {
odd = get(odd, end, is_odd);
if (odd == end) break;
cout << ' ' << *odd;
} while (++odd);
auto den = 3;
cout << "\nAliquot " << den << ":";
auto aliquot = vec;
do {
aliquot = get(aliquot, end, den, is_aliquot);
if (aliquot == end) break;
cout << ' ' << *aliquot;
} while (++aliquot);
cout << endl;
cin.get();
}
using namespace std;
using p_unary = bool(*)(int a);
using p_binary = bool(*)(int a, int b);
bool is_odd(int num) { return num & 1; }
bool is_even(int num) { return! is_odd(num); }
bool is_aliquot(int num, int den) { return num % den == 0; }
int* get(int* begin, int* end, p_unary func) {
auto next = begin;
do if (func(*next)) return next; while (++next != end);
return end;
}
int* get(int* begin, int* end, int den, p_binary func) {
auto next = begin;
do if (func(*next, den)) return next; while (++next != end);
return end;
}
int main() {
const size_t size = 15;
int vec[size] = { 15, 22, 35, 42, 84, 66, 73, 45, 93, 20, 33, 81, 42, 28, 26 };
cout << "Array:";
for (auto item : vec) cout << ' ' << item;
cout << "\nEven:";
auto end = vec + size;
auto even = vec;
do {
even = get(even, end, is_even);
if (even == end) break;
cout << ' ' << *even;
} while (++even);
cout << "\nOdd:";
auto odd = vec;
do {
odd = get(odd, end, is_odd);
if (odd == end) break;
cout << ' ' << *odd;
} while (++odd);
auto den = 3;
cout << "\nAliquot " << den << ":";
auto aliquot = vec;
do {
aliquot = get(aliquot, end, den, is_aliquot);
if (aliquot == end) break;
cout << ' ' << *aliquot;
} while (++aliquot);
cout << endl;
cin.get();
}
Похожие вопросы
- Помогите с заданием по C#
- Помогите с заданием на C++
- Помогите решить задание по HTML/CSS в Dreamweaver
- Пожалуйста! Помогите выбрать: java или c
- microsoft visual C++, помогите с заданием УМОЛЯЮ
- Помогите с домашним заданием по c++
- помогите сделать задачку на c++ пожалуйста.
- Помогите решить задачу на C или C++, пожалуйста.
- Помогите написать программу в C# пожалуйста!
- пожалуйста,пожалуйста!!!!помогите с заданием в basic!!! я его не понимаю,а надо срочно сдать(
Попробую разобраться теперь)