I need help in answer about array

Hello.. what is answer in code??

Write a function that will return count of even number and the odd number in an array of int? for example : when call the function in the main for the following array: int a[5]={2,5,9,-8,85}; and the output is even=2 odd=3
What have you tried ?
Can you help me in code answer.? ,, i have no idea but im student because this question is my homework
ok, read on:
* Any number that gives you a 0 as remainder when divided by 2 is considered an even number OTHERWISE it is an odd number.
in c++, the modulus (%) operator makes this check simple for us.
For example 4%2 = 0 as a remainder, so it is an even number
while 7 % 2 = 1 as a remainder , so it is an odd number.

possible code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

void checkNumber(int arr[], int arraySize, int &numOfEven, int &numOfOdd)
{
	for(int x = 0; x < arraySize; ++x)
	{
		if(arr[x] % 2)
		{
			// if element at index x divided by 2 gives me a non-zero number,
			// then it is an odd number
			++numOfOdd;
		}
		else
		{
			// otherwise it is an even number
			++numOfEven;
		}
	}
}
int main()
{
    const int arraySize = 5;
    int arr[arraySize] = {4, 43, 4, 8, 2};
    int numOfEven = 0;
    int numOfOdd = 0;
    checkNumber(arr, arraySize, numOfEven, numOfOdd);
    cout << "Number Of Even: " << numOfEven << endl;
    cout << "Number Of Odd: " << numOfOdd << endl;
   return 0;
}
Last edited on
Thanks for your helping ... best luck and wishes for you :) i understand now and thanks alot.. :)
Topic archived. No new replies allowed.