Modify the classify numbers programming

Assignment Help Computer Engineering
Reference no: EM132155900

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 "<<evenCount<<" evens, "

<<"which also includes "<<zeroCount

<<" zeros"<<endl;

cout<<"Total number of odds are: "

<<oddCount<<endl;

}

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.

Reference no: EM132155900

Questions Cloud

What is the probability that at least 3 of the 5 trees : What is the probability that at least 3 of the 5 trees that he measures are really Douglas-firs?
What code can be added : The ADT UnsortedType List function ‘DeleteItem(ItemType item)' creates an endless loop error when trying to delete a word or key that is not in the list.
Confidence interval for the true proportion : Construct a 95% confidence interval for the true proportion, mean, of Phantol users who got relief from using the drug.
How many would you expect to weigh between 3 and 8 pounds : The weights for newborn babies is approximately normally distributed with a mean of 5.5 pounds and a standard deviation of 1.1 pounds.
Modify the classify numbers programming : 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.
What is the efficiency rating : The expected value with no survey is $62,000 and the EVPI is $4,000. What is the efficiency rating?
Reads in a series of lines of input character by character : Write a program that reads in a series of lines of input character by character (using getchar()).
Write a program that exits when the user inputs character : Write a program that exits when the user inputs the character 'y At the start of the program, prompt the user to input a character.
What is the probability that : If one buys 10 of these tires from the distributor, what is the probability that

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd