C++ Programming Assignment 3 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...

1 answer below »

C++ Programming Assignment 3</o:p>

Sort Temperatures Lab</o:p>

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.</o:p>

</o:p>

Requirements:</o:p>

</o:p>

You are working for a meteorologist to maintain a list of temperatures and some related statistics for a month. </o:p>

</o:p>

Class and Data members:</o:p>

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. </o:p>

Constructor(s):</o:p>

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. </o:p>

</o:p>

Member Functions:</o:p>

The class should have functions as follows:</o:p>

</o:p>

1. Member functions to set and get the month and year variables.</o:p>

</o:p>

2. A member function that adds a single temperature to the vector. Both negative and positive temperatures are allowed. Call this function AddTemperature.</o:p>

</o:p>

3. A member function to sort the vector in ascending order. </o:p>

</o:p>

Feel free to use the sort function that is available in the algorithm library for sorting vectors.</o:p>

Or, if you would prefer to write your own sort code, you may find this site to be helpful:</o:p>

http://www.cplusplus.com/articles/NhA0RXSz/</o:p>

</o:p>

4. A member function to compute the average (x̄) of the temperatures in the vector. The formula for calculating an average is </o:p>

x̄ = ∑xi / n </o:p>

where xi is the value of each temperature reading and </o:p>

n is the total number of temperature readings in the vector.</o:p>

</o:p>

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.]</o:p>

</o:p>

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.]</o:p>

</o:p>

7. A member function to return the number of temperatures that were read in for the month.</o:p>

</o:p>

8. A member function to display the sorted temperatures. </o:p>

</o:p>

</o:p>

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. </o:p>

</o:p>

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:</o:p>

</o:p>

</o:p>

June</o:p>

2019</o:p>

90</o:p>

85</o:p>

97</o:p>

91</o:p>

87</o:p>

86</o:p>

88</o:p>

82</o:p>

83</o:p>

85</o:p>

</o:p>

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.</o:p>

</o:p>

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. </o:p>

</o:p>

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). </o:p>

</o:p>

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. </o:p>

</o:p>

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 permitted, but non-numeric temperatures should be caught.</o:p>

</o:p>

The executing program should look something like this:</o:p>

</o:p>

</o:p>

</o:p>

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.</o:p>

</o:p>

If a non-numeric score is read in, the program might look something like this:</o:p>

</o:p>

</o:p>

</o:p>

In the screen above, notice that the program pauses long enough to read the error message that is displayed. </o:p>

</o:p>

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.</o:p>

</o:p>

To give you an idea of the general criteria that will be used for grading, here is a checklist that </o:p>

you might find helpful:</o:p>

</o:p>

Compiles and Executes without crashing</o:p>

Style: No globals, Files are closed after use, code is modular</o:p>

Appropriate Internal Documentation</o:p>

Temperature Class Created:</o:p>

Temperature.h (header file)</o:p>

Temperature.cpp (class file)</o:p>

TemperatureDriver.cpp (Driver file)</o:p>

Data members:</o:p>

Appropriate data members are declared </o:p>

Pre-processing directives:</o:p>

Appropriate Pre-processing and using directives</o:p>

Constructors:</o:p>

Two argument constructor initializes data members</o:p>

Member functions are correct and used correctly:</o:p>

Set and get functions for data members </o:p>

Get function for total number of temperatures</o:p>

addTemperature() -- add s a single temperature to the vector</o:p>

sortTemperatures() - sorts vector in ascending order</o:p>

averageTemperature()</o:p>

findMinTemperature() and findMaxTemperature()</o:p>

displayTemperatures()</o:p>

Protection:</o:p>

Member functions and variables are declared with appropriate
protection (i.e. private or public)</o:p>

Object Creation:</o:p>

Temperatures object created correctly</o:p>

User Interaction:</o:p>

Prompts user for file name</o:p>

Appropriate error checking is in place to ensure that the file exists </o:p>

Temperatures are correctly read in from the file and stored in the temperatures object</o:p>

Ouput is labeled and formatted appropriately</o:p>

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.</o:p>

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</o:p>

</o:p>

Review the submission instructions document prior to uploading your work to Blackboard.</o:p>

</o:p>

Submit this assignment by 11:59 p.m. (ET) on Monday of Module/Week 3.</o:p>

Answered Same DayFeb 01, 2021

Answer To: C++ Programming Assignment 3 Sort Temperatures Lab This assignment utilizes a class in an...

Rohith answered on Feb 02 2021
79 Votes
50188/.DS_Store
__MACOSX/50188/._.DS_Store
50188/TemperatureDriver.cpp
#include #include #include #include "Temperature.h"
int main(int argc, cha
r const *argv[])
{
string fpath, line, month, choice;
float number;
int count, year, temp, e
or;
while (true)
{
e
or = 0;
cout
"Enter name of the temperature file: ";
cin
fpath;
fstream tempFile(fpath, ios_base::in);

Checking for file existence
if (!tempFile.good())
{
cout
"The file doesn't exist"
endl;
cout
"Please co
ect the problem and restart the application"
endl;
}
count = 1;
while (tempFile
line)
{

First line to get month
if (count == 1)
{
month = line;
}

Second line to get yea
if (count == 2)
{
year = stoi(line);
}

Rest all lines for the temperatures
if (count > 2)
{
Temperature temperature(month, year);
temperature.addTemperature(stoi(line));
while (tempFile
line)
{

Checking for integers
try
{
temperature.addTemperature(stoi(line));
}
catch (const std::exception &e)
{
e
or = 1;
}
}
if (e
or == 0)
{
temperature.displayStats();
temperature.displayTemperatures();
}
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here