In: Computer Science
Develop a C++ function to find the number of even and odd integers in a given array of integers
Pass in an array of integers
Pass in the size of the array of integers
Pass in a reference parameter for the number of even values
Pass in a reference parameter for the number of odd values
The function doesn't return anything
Develop a C++ program to test your function
C++ code:
#include <iostream>
using namespace std;
//initializing oddEven function for finding odd and even number
count
void oddEven(int numbers[],int n,int &even,int &odd){
//looping till n
for(int i=0;i<n;i++){
//checking if the number is even
if(numbers[i]%2==0)
//incrementing even number count
even++;
else
//incrementing odd number count
odd++;
}
}
int main()
{
//initializing even and odd number number count as 0
int even=0,odd=0;
//initializing a sample numbers list
int numbers[] {1,2,3,4,5,6,7,8,9};
//calling oddEven function
oddEven(numbers,9,even,odd);
//printing Even number count
cout<<"Even number count="<<even<<endl;
//printing Odd number count
cout<<"Odd number count="<<odd<<endl;
return 0;
}
Screenshot:
Output: