Дан массив действительных чисел размерностью N. Подсчитать, сколько в нем отрицательных, положительных и нулевых элементов
c++
Ответы на вопрос
Ответил clinteastwood2
0
- #include <iostream>
- #include <array>
- #include <algorithm>
- #include <functional>
- template <std::size_t size>
- int count(std::array<int, size> const& arr_, std::function<bool(int)> const& f)
- {
- return std::count_if(arr_.begin(), arr_.end(), f);
- }
- int main()
- {
- std::array<int, 5> arr = { -2, 0, 5, 0, -1 };
- int positive = count(arr, [](int a){ return a > 0; });
- int negative = count(arr, [](int a){ return a < 0; });
- int zero = count(arr, [](int a){ return a == 0; });
- std::cout << positive << " " << negative << " " << zero;
- }
Новые вопросы