#include #include "conditions.h" // include the possible conditions using namespace std; // NOW we only write ONE print function, for all possible conditions // for whether to print an array element. The condition will be passed // IN to the function, as a functor. template void PrintArray(const int* array, unsigned int size, Condition cond) { bool first = true; cout << "{"; for (unsigned int i = 0; i < size; i++) { if (cond(array[i])) // if the condition is true { // then print the item if (!first) cout << ", "; cout << array[i]; first = false; } } cout << "}\n"; } int main() { int list[15] = {-2, 5, 0, 14, 123, -67, -90, 85, 32, 200, 50, -21, 80, 77, -123}; cout << "Print the array:\n"; PrintArray(list, 15, Always()); cout << "\nPrint the positive elements of the array:\n"; PrintArray(list, 15, IsPos()); cout << "\nPrint the negative elements of the array:\n"; PrintArray(list, 15, IsNeg()); cout << "\nPrint the odd elements of the array:\n"; PrintArray(list, 15, IsOdd()); cout << "\nPrint the even elements of the array:\n"; PrintArray(list, 15, IsEven()); cout << "\nPrint the elements of the array divisible by 3:\n"; PrintArray(list, 15, IsDivisBy3()); cout << '\n'; }