Task 2 Task 2.1 Write a struct whose purpose is to store daily temperature senso
ID: 3912737 • Letter: T
Question
Task 2
Task 2.1
Write a struct whose purpose is to store daily temperature sensor readings. Call it Reading. It will contain the following data members:
day - an integer representing the day number of the reading
high - a double representing the high temperature reading on the given day
low- a double representing the low temperature reading on the given day
Task 2.2
Overload the ostream operator << to accept a Reading as an argument such that when I write the following.
Reading r;
r.day = 1;
r.high = 100;
r.low = 0;
cout << r << endl ;
Then the following will be printed to the screen.
Furthermore, our sensor readings may be missing some or all of its temperature data. Let a high/low value of -1 denote a missing value. If a missing value is present, then omit output for the data in question. For example:
Reading r;
r.day = 50;
r.high = -1;
r.low = 20.0;
cout << r << endl;
Would give us
Task 2.3
Overload the istream operator >> to accept a Reading as an argument. We want to be able to read data of the form
{(Day: ## )(Low: ## )(High: ## )}
For example, given the following code and user input:
CODE:
Reading r;
cin >> r;
TERMINAL INPUT:
Would populate our reading r with r.day = 25, r.low = 5, and r.high = 50 . Additionally, you must handle possible input with missing low/high values. For example it should be able to handle an input of
Task 2.4
Now that we have the ability to read/write Readings with << and >>, let’s read a file containing Readings, readings.txt (listed at the end). Write a function called read_min_max. It will start by creating an empty vector (vector of readings). As we read the file one Reading at a time, we will add each reading to our vector.
After we have finished reading the file, find the Reading with the max high value and the Reading with the min low value and print those readings to the screen with cout.
Name your file task2.cpp
readings.txt:
{ ( Day: 1 ) ( High: 10 ) ( Low: 0 ) }
{ ( Day: 2 ) ( High: 20 ) ( Low: 5 ) }
{ ( Day: 2 ) ( High: 50 ) }
{ ( Day: 2 ) ( High: 100 ) ( Low: 22 ) }
{ ( Day: 2 ) ( Low: 50 ) }
{ ( Day: 2 ) ( High: 98 ) ( Low: 45 ) }
{ ( Day: 2 ) ( High: 80 ) ( Low: 60 ) }
{ ( Day: 2 ) }
{ ( Day: 2 ) ( High: 122 ) ( Low: 100 ) }
Explanation / Answer
here is your file : ------------>>>>>>>>>>>>>>
#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
using namespace std;
typedef struct ReadType{
int day;
double high;
double low;
}Reading;
ostream& operator<<(ostream& out,Reading r){
out<<"{(Day:"<<r.day<<")(High:"<<r.high<<")(Low:"<<r.low<<")}";
return out;
}
istream& operator>>(istream& in,Reading &r){
string line;
string word;
getline(in,line);
stringstream ss;
r.day = 0;
r.high = 0;
r.low = 0;
for(int i = 0;i<line.size();i++){
if(line[i] == '{'){
}else if(line[i] == '('){
}else if(line[i] == ':'){
ss<<" ";
}else if(line[i] == ')'){
ss<<" ";
ss>>word;
if(word == "Day"){
ss>>r.day;
}else if(word == "High"){
ss>>r.high;
}else if(word == "Low"){
ss>>r.low;
}
ss.clear();
}else if(line[i] == '}'){
}else{
ss<<line[i];
}
}
ss.clear();
return in;
}
Reading maximum(vector<Reading> reads){
Reading r;
r.day = 0;
r.high = 0;
r.low = 0;
if(reads.empty()){
return r;
}
for(int i = 0;i<reads.size();i++){
if(reads[i].high >= r.high){
r = reads[i];
}
}
return r;
}
Reading minimum(vector<Reading> reads){
Reading r;
r.day = 0;
r.high = 0;
r.low = 1200;
if(reads.empty()){
return r;
}
for(int i = 0;i<reads.size();i++){
if(reads[i].low < r.low){
r = reads[i];
}
}
return r;
}
void read_min_max(){
ifstream ifile;
vector<Reading> readings;
ifile.open("readings.txt");
if(!ifile.is_open()){
cout<<" Readings File Opening error !!! ";
exit(-1);
}
Reading r;
while(!ifile.eof()){
ifile>>r;
readings.push_back(r);
}
ifile.close();
Reading max = maximum(readings);
Reading min = minimum(readings);
cout<<" Maximum High : "<<max;
cout<<" Minimum Low : "<<min;
}
int main(){
read_min_max();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.