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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 20;
void initialize(int& zeroCount, int& oddCount, int& evenCount);
void getNumber(int& num);
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount);
void printResults(int zeroCount, int oddCount, int evenCount);
int main ()
{
int counter;
int number;
int zeros;
int odds;
int evens;
initialize(zeros, odds, evens);
cout << "Please enter " << N << " integers."
<< endl;
cout << "The numbers you entered are: "
<< endl;
for (counter = 1; counter <= N; counter++)
{
getNumber(number);
cout << number << " ";
classifyNumber(number, zeros, odds, evens);
}
cout << endl;
printResults(zeros, odds, evens);
return 0;
}
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
void getNumber(int& num)
{
cin >> num;
}
void classifyNumber(int num, int& zeroCount, int& oddCount,
int& evenCount)
{
switch (num % 2)
{
case 0:
evenCount++;
if (num == 0)
zeroCount++;
break;
case 1:
case -1:
oddCount++;
}
}
void printResults(int zeroCount, int oddCount, int evenCount)
{
cout << "There are " << evenCount << " evens, "
<< "which includes " << zeroCount << " zeros"
<< endl;
cout << "The number of odd numbers is: " << oddCount
<< endl;
}
|