Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sort Temperatures Lab This assignment utilizes a class in an application that might be useful in the real world. It requires the sorting of, and

Sort Temperatures Lab

This assignment utilizes a class in an application that might be useful in the real world. It requires the sorting of, and computation with, data involving temperatures.

Requirements:

You are working for a meteorologist to maintain a list of temperatures and some related statistics for a month.

Class and Data members:

Create a class called Temperature that stores temperature readings (integers) in a vector (do not use an array). The class should have data members that store the month name and year of when the temperature readings were collected.

Constructor(s):

The class should have a 2-argument constructor that receives the month name and year as parameters and sets the appropriate data members to these values.

Member Functions:

The class should have functions as follows:

  1. Member functions to set and get the month and year variables.
  2. A member function that adds a single temperature to the vector. Both negative and positive temperatures are allowed. Call this function AddTemperature.
  3. A member function to sort the vector in ascending order.
  4. A member function to compute the average (x) of the temperatures in the vector. The formula for calculating an average is

x = xi / n

where xi is the value of each temperature reading and

n is the total number of temperature readings in the vector.

  1. A member function to determine the lowest temperature for the month. [Note that to receive credit for this function, it must contain an algorithm to search through the vector to determine the minimum value. You cannot simply sort the vector and return the first data member.]
  2. A member function to determine the highest temperature for the month. [Note that to receive credit for this function, it must contain an algorithm to search through the vector to determine the maximum value. You cannot simply sort the vector and return the last data member.]
  3. A member function to return the number of temperatures that were read in for the month.
  4. A member function to display the sorted temperatures.

Write a program (client) that uses the class by creating a Temperatures object and prompting the user for a file name. Appropriate error checking is required to ensure that the file exists and can be opened successfully.

The client should read in the file contents and store them in the object. The file will be formatted such that the first line contains the month name, the second line contains the year, and each successive line contains a temperature. A typical input file might contain:

June

2019

90

85

97

91

87

86

88

82

83

85

Note that the file may contain any number of temperatures for a given month. Therefore, you need to read in and store each temperature until you reach the end of the file.

The client (i.e. main()) should read in the contents of the file. After each temperature is read in, it should call the member function in the Temperatures class to add the new temperature (i.e. one temperature at a time) to the vector.

Main() should then produce a report that displays the month and year of the data, the total number of observations (temperatures) in the file, the lowest temperature, the highest temperature, the average temperature, and finally, a listing of all of the temperatures that were read in. The listing of all of the temperatures must be displayed in sorted order (ascending from lowest to highest).

All output should be labeled appropriately, and validity checking should be done on input of the filename and also the temperatures that are read in.

If a non-numeric value is encountered in reading in the temperatures, the program should output an error message indicating that a non-numeric value was found in the file, and the entire program should then terminate. If a non-numeric is found, consider the entire file to be corrupted and dont try to produce any calculations nor display the contents of the vector just end the program with an appropriate error message. (Make sure the error message is displayed long enough for the user to read it before ending the program.) Again, negative temperatures are permitted, but non-numeric temperatures should be caught.image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Use good coding style (modular, separate .h and .cpp class files, no globals, meaningful comments, etc.) throughout your program. Finally, make sure that you do not misspell the word temperature in your output to the user.

File Tools View Document1 - Compatibility Mode - Word o 0 X Sort Temperatures Lab This assignment utilizes a class in an application that might be useful in the "real world. It requires the sorting of, and computation with, data involving tempera- 4. A member function to compute the average (x) of the temperatures in the vector. The formula for calculating an average is x= xi where Xi is the value of each temperature reading and n is the total number of temperature readings in the vector. tures. Requirements: You are working for a meteorologist to maintain a list of temperatures and some related statistics for a month. Class and Data members: Create a class called Temperature that stores temperature readings (integers) in a vector (do not use an array). The class should have data members that store the month name and year of when the temperature readings were collected. 5. A member function to determine the lowest temperature for the month. [Note that to receive credit for this function, it must contain an algorithm to search through the vector to determine the minimum value. You cannot simply sort the vector and return the first data member.] 6. A member function to determine the highest temperature for the month. [Note that to receive credit for this function, it must contain an algorithm to search through the vector to determine the maximum value. You cannot simply sort the vector and return the last data member.] 7. A member function to return the number of temperatures that were read in for the month 8. A member function to display the sorted temperatures. Constructor(s): The class should have a 2-argument constructor that receives the month name and year as parameters and sets the appropriate data members to these values. Member Functions: The class should have functions as follows: Write a program (client) that uses the class by creating a Temperatures object and prompting the user for a file name. Appropriate error checking is required to en- sure that the file exists and can be opened successfully. 1. Member functions to set and get the month and year variables. 2. A member function that adds a single temperature to the vector. Both negative and positive temperatures are allowed. Call this function AddTemperature. 3. A member function to sort the vector in ascending order. The client should read in the file contents and store them in the object. The file will be formatted such that the first line contains the month name, the second line contains the year, and each successive line contains a temperature. A typical input file might contain: Screens 1-2 of 7 Focus 23 108% Type here to search Ca ) * ENG 8:51 PM 2/8/2021 File Tools View Document1 - Compatibility Mode - Word o 0 X All output should be labeled appropriately, and validity checking should be done on input of the filename and also the temperatures that are read in. June 2019 90 85 97 91 87 86 88 82 83 85 If a non-numeric value is encountered in reading in the temperatures, the program should output an error message indicating that a non-numeric value was found in the file, and the entire program should then terminate. If a non-numeric is found, consider the entire file to be corrupted and don't try to produce any calculations nor display the contents of the vector - just end the program with an appropriate error message. (Make sure the error message is displayed long enough for the user to read it before ending the program.) Again, negative temperatures are permit- ted, but non-numeric temperatures should be caught. The executing program should look something like this: Note that the file may contain any number of temperatures for a given month. Therefore, you need to read in and store each temperature until you reach the end of the file. The client (i.e. main() should read in the contents of the file. After each temper- ature is read in, it should call the member function in the Temperatures class to add the new temperature (i.e. one temperature at a time) to the vector. Main() should then produce a report that displays the month and year of the data, the total number of observations (temperatures) in the file, the lowest temperature, the highest temperature, the average temperature, and finally, a listing of all of the temperatures that were read in. The listing of all of the temperatures must be dis- played in sorted order (ascending - from lowest to highest). Screens 3-4 of 7 C Focus 108% Type here to search la ) ** ENG 8:51 PM 2/8/2021 5 File Tools View Document1 - Compatibility Mode - Word o 0 X Enter the name of the temperature file: Temps.txt Enter the name of the temperature file: Temps.txt 10 temperatures were recorded for this month. Statistics Average: Lowest temperature: Highest temperature: A non-numeric temperature was found in the file. Please correct the problem and restart the application. Press any key to continue ... 87.4 82 97 The temperatures for June 2019 are: 82 83 85 In the screen above, notice that the program pauses long enough to read the error message that is displayed. Use good coding style (modular, separate h and .cpp class files, no globals, meaningful comments, etc.) throughout your program. Finally, make sure that you do not misspell the word "temperature in your output to the user. 88 90 91 97 Press any key to continue .. To give you an idea of the general criteria that will be used for grading, here is a checklist that you might find helpful: In the screen above, notice that the average temperature is displayed with decimal points. Whenever you are calculating a statistic like an average, never truncate the decimal portion until you are told to do so. In this assignment, you are not to truncate the decimal portions of the average you calculate. If a non-numeric score is read in, the program might look something like this: Compiles and Executes without crashing Style: No globals, Files are closed after use, code is modular Appropriate Internal Documentation Temperature Class Created: Temperature.h (header file) Temperature.cpp (class file) Temperature Driver.cpp (Driver file) Data members: Appropriate data members are declared Pre-processing directives: Appropriate Pre-processing and using directives Constructors: Two argument constructor initializes data members Member functions are correct and used correctly: Set and get functions for data members Screens 5-6 of 7 D Focus 108% Type here to search la c* ENG 8:52 PM 2/8/2021 File Tools View Document1 - Compatibility Mode - Word o X Get function for total number of temperatures addTemperaturel) -- adds a single temperature to the vector sortTemperatures() - sorts vector in ascending order averageTemperaturel) findMin Temperature) and find MaxTemperature) displayTemperatures) Protection: Member functions and variables are declared with appropriate protection (i.e. private or public) Object Creation: Temperatures object created correctly User Interaction: Prompts user for file name Appropriate error checking is in place to ensure that the file exists Temperatures are correctly read in from the file and stored in the temperatures object Ouput is labeled and formatted appropriately Error checking is performed on input data: it outputs appropriate error message; does not close the program until the error message can be read; indicates the problem with the invalid item. Ouput consists of the number of temperatures read in, the month and year of the data, the sorted vector of temperatures, the average of the temperatures, and the low and high temperatures for the month - all formatted appropriately End of document I Screen 7 of 7 Focus + 108% Type here to search W fra 1)) * ENG 8:52 PM 2/8/2021 2

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

New Trends In Databases And Information Systems Adbis 2019 Short Papers Workshops Bbigap Qauca Sembdm Simpda M2p Madeisd And Doctoral Consortium Bled Slovenia September 8 11 2019 Proceedings

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Robert Wrembel ,Mirjana Ivanovic ,Johann Gamper ,Mikolaj Morzy ,Theodoros Tzouramanis ,Jerome Darmont

1st Edition

3030302776, 978-3030302771

More Books

Students also viewed these Databases questions

Question

How have your competitors responded to them?

Answered: 1 week ago