Question
Use C++ program to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device
Use C++ program to modify the Classify Numbers programming example in this chapter. As 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.
CLASSIFY NUMBERS EXAMPLE:
In this example, using functions we rewrite the program that determines the number of odds and evens from a given list of integers. This program was first written in Chapter 5.
The main algorithm remains the same:
1. Initialize variables, zeros, odds, and evens to 0.
2. Read a number.
3. If the number is even, increment the even count, and if the number is also zero, increment the zero count; else increment the odd count.
4. Repeat Steps 2 and 3 for each number in the list.
To simplify the function main and further illustrate parameter passing, the program includes
A function, initialize, to initialize the variables, such as zeros, odds, and evens.
A function, getNumber, to get the number.
A function, classifyNumber, to determine whether the number is odd or even (and whether it is also zero). This function also increments the appropriate count.
A function, printResults, to print the results.
initialize
void initialize(int& zeroCount, int& oddCount,
int& evenCount)
{
zeroCount = 0;
oddCount = 0;
evenCount = 0;
}
getNumber
void getNumber(int& num)
{
cin>>num;
}
classifyNumber
void classifyNumber(int num, int& zeroCount,
int& oddCount, int& evenCount)
{
switch(num % 2)
{
case 0: evenCount++; // update even count
if(num == 0) // number is also zero
zeroCount++; // update zero count
break;
case 1:
case -1: oddCount++; // update odd count
} //end switch
}
printResults
void printResults(int zeroCount, int oddCount,
int evenCount)
{
cout<<"There are "< <<"which also includes "< <<" zeros"< cout<<"Total number of odds are: " < } Main Algorithm 1. Call function initialize to initialize variables. 2. Prompt the user to enter 20 numbers. 3. For each number in the list a. Call function getNumber to read a number b. Output the number. c. Call function classifyNumber to classify the number and increment the appropriate count. 4. Call function printResults to print the final results.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started