I'v only started to learn c++ this year so maybe this is just a simple problem.
here is the question I was given:
Write a function of the form int countEvens(int[], int) that returns the number of even ints in a given array, when passed the array and the number of elements in the array. Remember the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
countEvens({2, 1, 2, 3, 4},5) → 3
countEvens({2, 2, 0},3) → 3
countEvens({1, 3, 5},3) → 0
I passed the array and used the "mod" operator to identify which elements are even.
my problem is that I don't know how to add up how many even elements are in the array. Can anyone help me?
my code so far just displays which numbers in the array are even, I want to display how many are even. Here is my code
Code:#include <cstdlib> #include <iostream> using namespace std; int countEvens(int a[], int size) { int i; for (i=0; i<size; i++) { if ((a[i]%2) == 0) { cout << a[i] << " is even" << endl; } } } int main() { int array[5] = {2,3,4,5,2}; countEvens(array,5); system("PAUSE"); return EXIT_SUCCESS; }