-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Refer to the following program, "Classify Numbers"
#include <iostream>
#include <iomanip>
using namespace std;
const int N = 20;
//Function prototypes
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() {
//Variable declaration
int counter; //loop control variable
int number; //variable to store the new number
int zeros; //variable to store the number of zeros
int odds; //variable to store the number of odd integers
int evens; //variable to store the number of even integers
initialize(zeros, odds, evens); //Step 1
cout << "Please enter " << N << " integers." <<
endl; //Step 2
cout << "The numbers you entered are: " <<
endl;
for (counter = 1; counter <= N; counter++) //Step 3
{
getNumber(number); //Step 3a
cout << number << " "; //Step 3b
classifyNumber(number, zeros, odds, evens); //Step 3c
} // end for loop
cout << endl;
printResults(zeros, odds, evens); //Step 4
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++;
} //end switch
} //end classifyNumber
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;
} //end printResultsAs written, the program inputs the data from the
standard input device (keyboard) and outputs the results on the standard
output device (screen). The program can process only 20 numbers. Rewrite
the program to incorporate the following requirements:
a. Data to the program is input from a file of an unspecified length; that is, the
program does not know in advance how many numbers are in the file.
b. Save the output of the program in a file.
c. Modify the function getNumber so that it reads a number from the
input file (opened in the function main), outputs the number to the
output file (opened in the function main), and sends the number read to
the function main. Print only 10 numbers per line.
d. Have the program find the sum and average of the numbers.
e. Modify the function printResult so that it outputs the final results to
the output file (opened in the function main). Other than outputting the
appropriate counts, this new definition of the function printResult
should also output the sum and average of the numbers.
Notes:
The prompt more so involves the input and output devices used to run the program. You do not need to include the input/output files in the pull request, but please identify the file names one should use if they are running the program on their own client.
You may name the program as you please, for example, "ClassifyNumbers.cpp"
Please make sure the program compiles and runs as it should!
(You do not need to include the prompt before the code for this program, it is very long :( )