Write a function for a function \"count\" that counts the number of odd and even
ID: 3621356 • Letter: W
Question
Write a function for a function "count" that counts the number of odd and even numbers in an integer array. Note that the function has to return two results. Note that the four formal parameters have been left blank.Hint: There should be four parameters to the function with the following names:
int input [size]; --->integer array of some size (specific size does not matter)
int size; --->the size of the array
int odd; --->number of odd numbers
int even; --->number of even numbers
Skeleton Code:
void count ( , , , )
{
}
Explanation / Answer
The following is C / C++ code. a % 2 is the remainder of dividing a by 2. If it's equal to zero, the number is even. If not, it's odd. You should pass even and odd, the counters, as pointers, so the function that calls count could get the answer.
void count (int n, int *array, int *even, int *odd)
{
*even = 0;
*odd = 0;
int i;
for (i = 0; i < n; i++)
{
// The number is even.
if (array[i] % 2 == 0)
*even += 1;
// The number is odd.
else
*odd += 1;
};
};
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.