Question: https://maryash.github.io/135/labs/lab_03.html Please help with C++ Task B! Data on Ashokan water levels In this lab, we will be studying the Ashokan water levels for the
https://maryash.github.io/135/labs/lab_03.html
Please help with C++ Task B!
Data on Ashokan water levels
In this lab, we will be studying the Ashokan water levels for the year 2018. It is available from NYC Open Data. Please follow these instructions to download the dataset:
- Follow the link NYC Open Data Current Reservoir Levels.
- Choose the View Data menu option (it will reload the page).
- Filter data including only the year 2018. To do that:
- Click the blue button Filter
- In its submenu Filter, click Add a New Filter Condition
- Choose Date is between January 1, 2018 and December 31, 2018
- Sort entries by Date in the Ascending order.
- Export the data:
- Click the light blue button Export
- Choose TSV for Excel (it will produce a plain text data file in tab-separated valuesformat).
- Save obtained file Current_Reservoir_Levels.tsv on your hard drive.
Data format
Dont open the datafile in Excel (that can mess up its formatting). Instead, you can open it with your text editor (gedit).
The datafile is a plain text file whose first line is a header followed by rows of data. The entries in each row are separated by the tab symbol, hence the name of the file format: TSV (tab-separated-values). It is the most convenient format for reading by our C++ program.
Each row has five fields: Date, Storage (in billions of gallons) and Elevation (in feet) for the East basin and for the West basin of the reservoir:
Date EastStorage EastElevation WestStorage WestElevation 01/01/2018 59.94 574 32.67 574.33 01/02/2018 59.89 573.99 32.57 574.29 01/03/2018 59.89 573.97 32.44 574.24 01/04/2018 59.9 573.97 32.22 574.07 ...
To read the datafile, we have to open an input file stream (represented by an object of type ifstream, here we called it fin):
ifstream fin("Current_Reservoir_Levels.tsv"); if (fin.fail()) { cerr << "File cannot be opened for reading." << endl; exit(1); // exit if failed to open the file } Remember that the first line in the file is a header line. We have to skip it before we get to process the actual data. We can do that by reading that line into a temporary variable that we can call junk:
string junk; // new string variable getline(fin, junk); // read one line from the file
After that, the file can be read line by line. The most idiomatic C++ way to read such well-formatted file until the end would be the following:
while(fin >> date >> eastSt >> eastEl >> westSt >> westEl) { // this loop reads the file line-by-line // extracting 5 values on each iteration fin.ignore(INT_MAX, ' '); //skips to the end of line, //ignorring the remaining columns // for example, to print the date and East basin storage: cout << date << " " << eastSt << endl; } Here, variable date can be of type string, and the others are numeric variables of type double extracting the storage and elevation in East and West basins.
After you are done reading the file, close the stream:
fin.close();
Need to include additional header files
The above code is using a new function exit and a stream class ifstream. To make them work, we have to include two new headers at the beginning of the program:
#include #include #include
***Task B. Minimum and maximum storage in 2018***
Write a program minmax.cpp that finds the minimum and maximum storage in East basin in 2018.
Example (using made up numbers):
$ ./minmax minimum storage in East basin: 59.88 billion gallons MAXimum storage in East basin: 81.07 billion gallons
Hint:
The program should read the file line by line, while keeping track of what is the highest and the lowest storage level in the basin so far. In the end, after reading the entire file, the found values will be the minimum and the maximum storage levels for the entire year.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
