Question: C++ In this lab, you will write a program to analyze files of temperature data. 1. Declare a struct or a class named Reading, whose
C++
In this lab, you will write a program to analyze files of temperature data.
1. Declare a struct or a class named Reading, whose purpose is to hold a temperature
reading that consists of an hour at which the temperature reading was taken, as well as a
temperature.
2. Read in all the temperature reading data from the input files using the ifstream. (Each
line of the input file is used to construct an object of Reading type.)
3. You havent written any programs that process command-line parameter. So Ive built
that capability into the main() function.
4. Functions for major subtasks of the program:
a. An output operator for the Reading data type. It simply outputs the hours and the
temperature.
b. Function get_temps() to read in the temperature data into the Reading data type
and store those Reading objects into a vector.
c. Function check_adjust_temp(). It must check the input, making sure that the
temperature is legitimate:
The scale must either F, f, C, or c
The temperature must not below absolute zero
If either of these conditions is not met, use error() to emit an appropriate error
message. It must also adjust the temperature, meaning that if the temperature was
input as Celsius, it should be converted to Fahrenheit.
d. Function c_to_f() to convert a Celsius temperature to Fahrenheit, used by the
input operator.
e. Function mean() to calculate the mean of all the temperature data
f. Function median() to calculate the median of all the temperature data
g. Function print_results() to print the final results
5. Your program should print the following information:
a. The readings (i.e. hours and temperatures), sorted by increasing order of
temperature
b. The mean (average) temperature, and
c. The median temperature
6. One important part of the program will be sorting the data. You can sort a
vector
sort(v);
You might expect that one could sort a vector
sort(temps);
This is almost right. The problem is that since Reading is a user-defined data type, the
computer has no idea how to compare two readings, i.e., how to determine r1 < r2 (where
r1 and r2 are Readings). So you need to teach the Reading class how to compare two
Readings.
You can do this by overloading the < operator to work with Readings. (Operator
overloading was introduced in Chapter 11.2). This overloaded < operator has the name
operator <, and is declared as
bool operator < (const Reading &r1, const Reading &r2);
So suppose that r1 and r2 are Readings. What should it mean for r1 < r2 to be true? This
certainly is true if r1.temperature < r2.temperature. The interesting case is when the
temperatures are equal. In that case, it would make sense to say that r1 < r2 when r1.hour
< r2.hour.
For example, consider the input set
1 20 C
2 20 c
0 68 F
3 98.6 F
4 32 F
5 0 c
Suppose that we only considered the temperature, and not the hour at which the reading
was taken. Since 20 C = 68 F, the output would be as follows:
The sorted temperatures are:
4: 32.00 F
5: 32.00 F
1: 68.00 F
2: 68.00 F
0: 68.00 F
3: 98.60 F
The mean temperature is 61.0 F
The median temperature is 68.0 F
Would be valid output. But the output
4: 32.00 F
5: 32.00 F
0: 68.00 F
1: 68.00 F
2: 68.00 F
3: 98.60 F
The mean temperature is 61.0 F
The median temperature is 68.0 F
is more natural. Thats why we resolve a pair of Readings having equal temperature in
favor of the one having the smaller hour.
7. Sample output:
The input file temps04:
-40c 68 f 32 f 0 -40 f
In your command line you type: ./a.out temps04
Then wed have The sorted temperatures are: -40.00 f -40.00 f 0.00 f 32.00 f 68.00 f The mean temperature is 4.00 F. The median temperature is 0.00 F.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
