General Principles of Intellectual Property: Concepts of Intellectual Proper...
Write a function that takes as a parameter an integer (as a long valu.docx
1. Write a function that takes as a parameter an integer (as a long
value) and returns the number of odd, even, and zero digits.
Also write a program to test your function.
Solution
#include "iostream"
#include "iomanip"
using namespace std;
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, nums;
int zeros;
int odds;
int evens;
char userAnswer;
2. bool finished = false;
do {
initialize (zeros, odds, evens);
cout << "enter an integer " << endl;
cin >> number;
cout << endl;
for (counter = 1; counter <= number; ++counter)
{
getNumber(nums);
// cout << number << " ";
classifyNumber(nums, zeros, odds, evens);
}// end for loop
cout << endl;
cout << "Would you like to go again? (y/n)" << endl;
cin >> userAnswer;
if (userAnswer == 'n')
finished = true;
} while (!finished);
printResults(zeros, odds, evens);
return 0;
}// Function main
void initialize(int& zeroCount, int& oddCount, int& evenCount)
{
zeroCount = 0;
oddCount = 0;
3. 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, "
4. << "which includes " << zeroCount << " zeros"
<< endl;
cout << "The number of odd numbers is: " << oddCount
<< endl;
}