Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++: Files: May2017Rain.txt: ___________________________________________________

ID: 3846298 • Letter: C

Question

C++:

Files:

May2017Rain.txt:

_________________________________________________________

April2017Rain.txt:

Observed data is often collected and stored for batch processing. One example could be rainfall or other weather data. For this assignment, you will read rainfall data for a month from a file. As you read each day's value, you will add it to a running total so that you can output A table of daily rainfall The average daily rainfall The total rainfall for the month This output is to be saved to a file (although your program can also output it to the screen if you like) The input file consists of lines, one for each day of the month, with a single number on each: the measured rainfall for that day. You do not know how many lines are in the file: not all months have the same number of days AND it is possible that a file only contains data for part of a month. So you must read and process "while not end-of-file'' Outline: Ask your user to enter (from the keyboard) the month and year for the data file you are using. Use getline to accept this into a single string variable! Open your input and output files. Use use .fail() and exit (1) to halt processing if both files don't open correctly. Assuming they DO o output the month and year as part of a heading for the "report", something like Rainfall for April 2017 o Read in the first line of data While you are not at the end of the file o Output the date (since each line is a day, count the lines and output the ne number" as the date) and rainfall for that day on a line. Rainfall must be output with 2-digit precision (even those that are 0.00) Use setw to make sure all dates and rainfall values are in good (right-justified) columns. o Add the rainfall for this day to the total rainfall for the month o Read the next line of data When all data has been read o output the total rain for the month and the average daily rainfall for the month o Close both files and you are done! I will not tell you which functions to write this time, but your main() should not do any of the file opening or output. Input and processing are also best done outside of main(). If main() is totally-or almost totally a caller of other functions, that will be a good thing (and worth a point or 2 extra). On the other hand, if main() does much "work" at all, expect it to cost a few points here and there If your program is just main(), your score will be 0 And remember to ALWAYS pass data using parameters and/or return If you use ANY global variables, your score will be

Explanation / Answer

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

struct Rainfall
{
   string header;
   double total_rain;
   vector<double> rain;
};

void write_rain(string filename, Rainfall r1)
{
   // Open a file.
   ofstream ofile(filename);
   if (ofile.fail())
       exit(1);

   ofile << "Rainfall for " << r1.header << endl;

   for (int i = 0; i < r1.rain.size(); ++i)
   {
       // index dailyrainfall
       ofile << i << " " << r1.rain.at(i)           
           << endl;
   }

   ofile << "Total rainfall: " << r1.total_rain
       << endl;

   ofile.close();
}

Rainfall read_rain(string filename)
{
   Rainfall r1;
   char *head = new char[100];  

   cout << "Enter Year and month of the data file: ";
   cin.getline(head, 99);
   r1.header = string(head);

   // // Open a file.
   ifstream ifile(filename);
   if (ifile.fail())
       exit(1);

   double daily_fall{};

   while (! ifile.eof())
   {      
       ifile >> daily_fall;
       r1.rain.push_back(daily_fall);
       r1.total_rain += daily_fall;      
   }

   ifile.close();
   return r1;
}

int main()
{
   string filename1 = "May2017Rain.txt";
   Rainfall rf_data1 = read_rain(filename1);

   // ====
   // TESTING
   cout << "You entered: " << rf_data1.header << endl;
   cout << "Read rainfall is: " << endl;
   for (int i = 0; i < rf_data1.rain.size(); ++i)
       cout << rf_data1.rain.at(i) << endl;
   cout << "Total Rain: " << rf_data1.total_rain << endl;
   // ====

   string filename2 = "May2017RainOP.txt";
   write_rain(filename2, rf_data1);

   return 0;
}

Output file:

Rainfall for May 2017
0 0.03
1 0.13
2 0.11
3 0.44
4 0.25
5 0.02
6 0
7 0
8 0
9 0
10 0.47
11 0.16
12 0.03
13 0.04
14 0.33
15 0.15
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
Total rainfall: 2.16

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote