Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ requirements The numbers need to be read in as type double. The calculations need to be done using type double as well. You must

C++ requirements

The numbers need to be read in as type double. The calculations need to be done using type double as well.

You must properly handle end of file conditions.

You must properly open and close any files.

Failure to follow the C++ requirements could reduce the points received from passing the tests.

General overview

In this program you will be reading numbers from a file. You will validate the numbers and calculate the average of all of the valid numbers.

Your program will read in a file with numbers. The numbers will be of type double.

The numbers should be in the range from 0 to 110 (inclusive). You need to count all of the numbers between 0 and 110. You also need to calculate the average of these numbers.

If a number is not valid (that is, it is less than 0 or greater than 110) you need to count it (as a count of invalid values) and you need to write out the number to a file called "invalid-numbers.txt". Values written to file invalid-numbers.txt should be in fixed format with four digits to the right of the decimal point.

As you did in lab lesson 7 part 1 you need to read in the input file name using cin.

You need to properly handle end of file conditions. See lab lesson 7 part 1 for details on reading files. Also see Demo of file input and output of data in this unit.

The output from your program will be written to cout. The output must contain the file being processed, the total number of values read in from the file, the number of invalid values read in, the number of valid values read in.

The last thing you need to output is either the average of the valid values or an error message. The average must have three digits of precision to the right of the decimal point and must be in fixed format. If there is not valid average you should output the message:

An average cannot be calculated 

In what case would you display this message?

If the input file cannot be opened, you will need to output a message. Assume the input file name is badinput.txt and it cannot be opened. You will display the following error message to cout

File "badinput.txt" could not be opened 

Here is an example of a working program:

Assume the file name read in from cin is:

input.txt 

and that input.txt contains:

-12 0 98.5 100 105.5 93.5 88 75 -3 111 89 -12 

Your program would output the following:

Reading from file "input.txt" Total values: 12 Invalid values: 4 Valid values: 8 Average of valid values: 81.188 

The contents written out to file invalid-numbers.txt are:

-12.0000 -3.0000 111.0000 -12.0000 

You are reading from an input file and you are writing to an output file. Make sure you close both files after you are finished using them. You must do this in your program, you cannot just let the operating system close the files for you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

=+2 Is the decision sustainable in the long run?

Answered: 1 week ago