Write a main program to run the following functions:
void inp
ut_array(double a[ ]); // to input an array
void count( double a[ ], int &odd, int &even) // count odd and even numbers, return by
reference
#include <iostream>
using namespace std;
void input_array(double a[]); // to input an array
void count(double a[], int &odd, int &even); // count odd and even numbers, return by reference
const int N = 5;
int main() {
double a[N];
cout << "Input " << N << " real numbers" << endl;
input_array(a);
int odd, even;
count(a, odd, even);
cout << "You have entered " << odd << " odd and " << even << " even numbers." << endl;
}
void input_array(double a[]) {
for (int i=0; i<N; i++) {
cin >> a[i];
}
}
void count(double a[], int &odd, int &even) {
odd = even = 0;
for (int i=0; i<N; i++) {
int x = static_cast<int>(a[i]);
if (x % 2 == 0) {
even++;
}
else {
odd++;
}
}
}
Comments
Leave a comment