In: Computer Science
Write a C++ program that reads daily weather observation data from a file and writes monthly and annual summaries of the daily data to a file.
a. The daily weather data will be contained in a file named wx_data.txt, with each line of the file representing the weather data for a single day.
b. For each day, the date, precipitation amount, maximum temperature, and minimum temperature will be provided as tab-separated data with the following format: 20180101 0.02 37 23 20180102 0.00 42 18 <...data for the remainder of the year here...>
c. For the first entry in the daily data above, the date is 20180101 (1 January 2018), the precipitation amount is 0.02 inches, the maximum temperature is 37°F, and the minimum temperature is 23°F.
d. You may assume that the weather data is complete (i.e., there is data for each day of every month of the year) and that the data contains no errors.
2. The program must read from file each daily weather observation and write a summary by month and year to a file named wx_summary.txt.
a. The output that is written to file must have the following format:
-------------------
January
-------------------
Precipitation
Total: 3.12
Average: 0.10
Temperature
Maximum: 62
Minimum: 39
-------------------
February
-------------------
Precipitation
Total: 10.18
Average: 0.36
Temperature
Maximum: 78
Minimum: 64
-------------------
March
-------------------
Precipitation
Total: 2.02
Average: 0.07
Temperature
Maximum: 73
Minimum: 53
-------------------
April
-------------------
Precipitation
Total: 3.99
Average: 0.13
Temperature
Maximum: 77
Minimum: 58
-------------------
May
-------------------
Precipitation
Total: 3.73
Average: 0.12
Temperature
Maximum: 87
Minimum: 77
-------------------
June
-------------------
Precipitation
Total: 8.47
Average: 0.28
Temperature
Maximum: 92
Minimum: 75
-------------------
July
-------------------
Precipitation
Total: 6.79
Average: 0.22
Temperature
Maximum: 89
Minimum: 74
-------------------
August
-------------------
Precipitation
Total: 7.54
Average: 0.24
Temperature
Maximum: 82
Minimum: 74
-------------------
September
-------------------
Precipitation
Total: 18.25
Average: 0.61
Temperature
Maximum: 89
Minimum: 74
-------------------
October
-------------------
Precipitation
Total: 3.75
Average: 0.12
Temperature
Maximum: 82
Minimum: 72
-------------------
November
-------------------
Precipitation
Total: 5.62
Average: 0.19
Temperature
Maximum: 77
Minimum: 63
-------------------
December
-------------------
Precipitation
Total: 16.55
Average: 0.53
Temperature
Maximum: 72
Minimum: 67
-------------------
Max temp for year: 98 in May
Min temp for year: 21 in January
Max precip for year: 18.25 in September
Code Screenshots:






Sample input file (wx_data.txt):
20180101 0.00 39 26
20180102 0.00 44 23
20180103 0.00 46 32
20180104 0.00 47 27
20180105 0.00 53 29
20180106 0.00 58 30
20180107 0.00 56 38
20180108 0.60 63 48
20180109 0.06 66 57
20180110 0.04 65 58
20180111 0.54 67 63
20180112 0.02 68 35
20180113 0.00 45 29
20180114 0.00 52 26
20180115 0.00 60 34
20180116 0.00 62 33
20180117 0.04 35 22
20180118 0.00 47 21
20180119 0.00 58 26
20180120 0.00 64 37
20180121 0.00 66 47
20180122 0.18 70 56
20180123 0.00 67 46
20180124 0.00 57 41
20180125 0.00 66 39
20180126 0.00 62 47
20180127 0.82 64 58
20180128 0.82 70 60
20180129 0.00 67 47
20180130 0.00 55 37
20180131 0.00 62 39
20180201 0.00 69 59
20180202 0.00 62 42
20180203 0.00 69 59
20180204 0.00 62 42
20180205 0.00 69 59
20180206 0.00 62 42
20180207 0.00 69 59
20180208 0.00 62 42
20180209 0.00 69 59
20180210 0.00 62 42
20180211 0.00 69 59
20180212 0.00 62 42
20180213 0.00 69 59
20180214 0.00 62 42
20180215 0.00 69 59
20180216 0.00 62 42
20180217 0.00 69 59
20180218 0.00 62 42
20180219 0.00 69 59
20180220 0.00 62 42
20180221 0.00 69 59
20180222 0.00 62 42
20180223 0.00 69 59
20180224 0.00 69 59
20180225 0.00 62 42
20180226 0.00 69 59
20180227 0.00 69 59
20180228 0.00 69 59
20180301 0.00 39 26
20180302 0.00 44 23
20180303 0.00 46 32
20180304 0.00 47 27
20180305 0.00 53 29
20180306 0.00 58 30
20180307 0.00 56 38
20180308 0.60 63 48
20180309 0.06 66 57
20180310 0.04 65 58
20180311 0.54 67 63
20180312 0.02 68 35
20180313 0.00 45 29
20180314 0.00 52 26
20180315 0.00 60 34
20180316 0.00 62 33
20180317 0.04 35 22
20180318 0.00 47 21
20180319 0.00 58 26
20180320 0.00 64 37
20180321 0.00 66 47
20180322 0.18 70 56
20180323 0.00 67 46
20180324 0.00 57 41
20180325 0.00 66 39
20180326 0.00 62 47
20180327 0.82 64 58
20180328 0.82 70 60
20180329 0.00 67 47
20180330 0.00 55 37
20180331 0.00 62 39
20180401 0.00 39 26
20180402 0.00 44 23
20180403 0.00 46 32
20180404 0.00 47 27
20180405 0.00 53 29
20180406 0.00 58 30
20180407 0.00 56 38
20180408 0.60 63 48
20180409 0.06 66 57
20180410 0.04 65 58
20180411 0.54 67 63
20180412 0.02 68 35
20180413 0.00 45 29
20180414 0.00 52 26
20180415 0.00 60 34
20180416 0.00 62 33
20180417 0.04 35 22
20180418 0.00 47 21
20180419 0.00 58 26
20180420 0.00 64 37
20180421 0.00 66 47
20180422 0.18 70 56
20180423 0.00 67 46
20180424 0.00 57 41
20180425 0.00 66 39
20180426 0.00 62 47
20180427 0.82 64 58
20180428 0.82 70 60
20180429 0.00 67 47
20180430 0.00 55 37
20180501 0.00 39 26
20180502 0.00 44 23
20180503 0.00 46 32
20180504 0.00 47 27
20180505 0.00 53 29
20180506 0.00 58 30
20180507 0.00 56 38
20180508 0.60 63 48
20180509 0.06 66 57
20180510 0.04 65 58
20180511 0.54 67 63
20180512 0.02 68 35
20180513 0.00 45 29
20180514 0.00 52 26
20180515 0.00 60 34
20180516 0.00 62 33
20180517 0.04 35 22
20180518 0.00 47 21
20180519 0.00 58 26
20180520 0.00 64 37
20180521 0.00 66 47
20180522 0.18 70 56
20180523 0.00 67 46
20180524 0.00 57 41
20180525 0.00 66 39
20180526 0.00 62 47
20180527 0.82 64 58
20180528 0.82 70 60
20180529 0.00 67 47
20180530 0.00 55 37
20180531 0.00 62 39
20180601 0.00 39 26
20180602 0.00 44 23
20180603 0.00 46 32
20180604 0.00 47 27
20180605 0.00 53 29
20180606 0.00 58 30
20180607 0.00 56 38
20180608 0.60 63 48
20180609 0.06 66 57
20180610 0.04 65 58
20180611 0.54 67 63
20180612 0.02 68 35
20180613 0.00 45 29
20180614 0.00 52 26
20180615 0.00 60 34
20180616 0.00 62 33
20180617 0.04 35 22
20180618 0.00 47 21
20180619 0.00 58 26
20180620 0.00 64 37
20180621 0.00 66 47
20180622 0.18 70 56
20180623 0.00 67 46
20180624 0.00 57 41
20180625 0.00 66 39
20180626 0.00 62 47
20180627 0.82 64 58
20180628 0.82 70 60
20180629 0.00 67 47
20180630 0.00 55 37
20180701 0.00 39 26
20180702 0.00 44 23
20180703 0.00 46 32
20180704 0.00 47 27
20180705 0.00 53 29
20180706 0.00 58 30
20180707 0.00 56 38
20180708 0.60 63 48
20180709 0.06 66 57
20180710 0.04 65 58
20180711 0.54 67 63
20180712 0.02 68 35
20180713 0.00 45 29
20180714 0.00 52 26
20180715 0.00 60 34
20180716 0.00 62 33
20180717 0.04 35 22
20180718 0.00 47 21
20180719 0.00 58 26
20180720 0.00 64 37
20180721 0.00 66 47
20180722 0.18 70 56
20180723 0.00 67 46
20180724 0.00 57 41
20180725 0.00 66 39
20180726 0.00 62 47
20180727 0.82 64 58
20180728 0.82 70 60
20180729 0.00 67 47
20180730 0.00 55 37
20180731 0.00 62 39
20180801 0.00 39 26
20180802 0.00 44 23
20180803 0.00 46 32
20180804 0.00 47 27
20180805 0.00 53 29
20180806 0.00 58 30
20180807 0.00 56 38
20180808 0.60 63 48
20180809 0.06 66 57
20180810 0.04 65 58
20180811 0.54 67 63
20180812 0.02 68 35
20180813 0.00 45 29
20180814 0.00 52 26
20180815 0.00 60 34
20180816 0.00 62 33
20180817 0.04 35 22
20180818 0.00 47 21
20180819 0.00 58 26
20180820 0.00 64 37
20180821 0.00 66 47
20180822 0.18 70 56
20180823 0.00 67 46
20180824 0.00 57 41
20180825 0.00 66 39
20180826 0.00 62 47
20180827 0.82 64 58
20180828 0.82 70 60
20180829 0.00 67 47
20180830 0.00 55 37
20180831 0.00 62 39
20180901 0.00 39 26
20180902 0.00 44 23
20180903 0.00 46 32
20180904 0.00 47 27
20180905 0.00 53 29
20180906 0.00 58 30
20180907 0.00 56 38
20180908 0.60 63 48
20180909 0.06 66 57
20180910 0.04 65 58
20180911 0.54 67 63
20180912 0.02 68 35
20180913 0.00 45 29
20180914 0.00 52 26
20180915 0.00 60 34
20180916 0.00 62 33
20180917 0.04 35 22
20180918 0.00 47 21
20180919 0.00 58 26
20180920 0.00 64 37
20180921 0.00 66 47
20180922 0.18 70 56
20180923 0.00 67 46
20180924 0.00 57 41
20180925 0.00 66 39
20180926 0.00 62 47
20180927 0.82 64 58
20180928 0.82 70 60
20180929 0.00 67 47
20180930 0.00 55 37
20181001 0.00 39 26
20181002 0.00 44 23
20181003 0.00 46 32
20181004 0.00 47 27
20181005 0.00 53 29
20181006 0.00 58 30
20181007 0.00 56 38
20181008 0.60 63 48
20181009 0.06 66 57
20181010 0.04 65 58
20181011 0.54 67 63
20181012 0.02 68 35
20181013 0.00 45 29
20181014 0.00 52 26
20181015 0.00 60 34
20181016 0.00 62 33
20181017 0.04 35 22
20181018 0.00 47 21
20181019 0.00 58 26
20181020 0.00 64 37
20181021 0.00 66 47
20181022 0.18 70 56
20181023 0.00 67 46
20181024 0.00 57 41
20181025 0.00 66 39
20181026 0.00 62 47
20181027 0.82 64 58
20181028 0.82 70 60
20181029 0.00 67 47
20181030 0.00 55 37
20181031 0.00 62 39
20181101 0.00 39 26
20181102 0.00 44 23
20181103 0.00 46 32
20181104 0.00 47 27
20181105 0.00 53 29
20181106 0.00 58 30
20181107 0.00 56 38
20181108 0.60 63 48
20181109 0.06 66 57
20181110 0.04 65 58
20181111 0.54 67 63
20181112 0.02 68 35
20181113 0.00 45 29
20181114 0.00 52 26
20181115 0.00 60 34
20181116 0.00 62 33
20181117 0.04 35 22
20181118 0.00 47 21
20181119 0.00 58 26
20181120 0.00 64 37
20181121 0.00 66 47
20181122 0.18 70 56
20181123 0.00 67 46
20181124 0.00 57 41
20181125 0.00 66 39
20181126 0.00 62 47
20181127 0.82 64 58
20181128 0.82 70 60
20181129 0.00 67 47
20181130 0.00 55 37
20181201 0.00 39 26
20181202 0.00 44 23
20181203 0.00 46 32
20181204 0.00 47 27
20181205 0.00 53 29
20181206 0.00 58 30
20181207 0.00 56 38
20181208 0.60 63 48
20181209 0.06 66 57
20181210 0.04 65 58
20181211 0.54 67 63
20181212 0.02 68 35
20181213 0.00 45 29
20181214 0.00 52 26
20181215 0.00 60 34
20181216 0.00 62 33
20181217 0.04 35 22
20181218 0.00 47 21
20181219 0.00 58 26
20181220 0.00 64 37
20181221 0.00 66 47
20181222 0.18 70 56
20181223 0.00 67 46
20181224 0.00 57 41
20181225 0.00 66 39
20181226 0.00 62 47
20181227 0.82 64 58
20181228 0.82 70 60
20181229 0.00 67 47
20181230 0.00 55 37
20181231 0.00 62 39
Sample output file (wx_summary.txt):



Code to Copy:
//Include the required
//header files.
#include <iostream>
#include <fstream>
#include <iomanip>
//Use the standard
//namespace.
using namespace std;
//Define a function to check
//if it is a leap year or not.
int is_leap(int year)
{
//Check if the year
//is divisible by 4.
if(year % 4 == 0)
{
//Check if the year
//is divisible by 100.
if(year % 100 == 0)
{
//Check if the year
//is divisible by 400.
if(year % 400 == 0)
{
//If the year is divisible
//by 100 as well as
//400 then, true.
return true;
}
//Otherwise, if the year is
//divisible by 100 but not
//by 400 then, return false.
return false;
}
//If the year is divisible
//by 4 but not by 100
//then, return true.
return true;
}
//Otherwise, if the year
//is not divisible by 4
//then, return false.
return false;
}
//Define the function get_num_days().
int get_num_days(int year, int month)
{
//Check if the month is 2.
if(month == 2)
{
//If the month is 2 and
//the year is a leap
//year then, return 29.
if(is_leap(year))
{
return 29;
}
//Otherwise, return 28.
else
{
return 28;
}
}
//Return the number of
//days as per the month.
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
}
return 0;
}
//Define the
//main() function.
int main() {
//Define the objects
//to open the files.
ifstream in_file_obj;
ofstream out_file_obj;
double total;
double average;
int max_temp;
int min_temp;
int max_temp_month = 0;
int min_temp_month = 0;
double max_precip = 0;
int max_precip_month = 0;
int max_month;
int min_month;
//Define an array to
//store the name of the months.
string month_name[12] = {"January", "February",
"March", "April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
//Open the files.
in_file_obj.open("wx_data.txt");
out_file_obj.open("wx_summary.txt");
out_file_obj << fixed << setprecision(2);
//Run the loop till
//the end of input file.
while(!in_file_obj.eof())
{
//Declare the
//required variables.
total = 0;
int year = 0;
int month = 0;
int day = 0;
int input;
float prec;
int curr_min_temp;
int curr_max_temp;
//Read the input.
in_file_obj >> input;
//Get the day
//from the input.
day = input%100;
input = input/100;
//Get the month.
month = input%100;
//Get the year.
year = input/100;
//Get the number of days
//in the current month.
int no_days = get_num_days(year, month);
//Read the first line
//of the month.
in_file_obj >> prec;
in_file_obj >> curr_max_temp;
in_file_obj >> curr_min_temp;
max_temp_month = curr_max_temp;
min_temp_month = curr_min_temp;
total += prec;
int days_read = 1;
if(month == 1)
{
min_temp = min_temp_month;
max_temp = max_temp_month;
max_month = 1;
min_month = 1;
}
//Read the data
//for current month.
while(days_read < no_days)
{
in_file_obj >> input;
in_file_obj >> prec;
in_file_obj >> curr_max_temp;
in_file_obj >> curr_min_temp;
if(curr_max_temp > max_temp_month)
{
max_temp_month = curr_max_temp;
}
if(curr_min_temp < min_temp_month)
{
min_temp_month = curr_min_temp;
}
total += prec;
days_read++;
}
if(max_temp_month > max_temp)
{
max_temp = max_temp_month;
max_month = month;
}
if(min_temp_month < min_temp)
{
min_month = month;
min_temp = min_temp_month;
}
if(total > max_precip)
{
max_precip =total;
max_precip_month = month;
}
average = total/no_days;
//Write the summary of the
//current month to the output file.
out_file_obj << "-------------------" << endl;
out_file_obj << month_name[month - 1] << endl;
out_file_obj << "-------------------" << endl;
out_file_obj << "Precipitation" << endl;
out_file_obj << "Total: " << total << endl;
out_file_obj << "Average: " << average
<< endl;
out_file_obj << "Temperature"
<< endl;
out_file_obj << "Maximum: " << max_temp_month
<< endl;
out_file_obj << "Minimum: " << min_temp_month
<< endl;
}
//Write the summary of
//the year to the output file.
out_file_obj << "-------------------"
<< endl;
out_file_obj << "Max temp for year: "
<< max_temp << " in "
<< month_name[max_month - 1] << endl;
out_file_obj << "Min temp for year: "
<< min_temp << " in "
<< month_name[min_month - 1] << endl;
out_file_obj << "Max precip for year: "
<< max_precip << " in "
<< month_name[max_precip_month - 1];
//Close the file objects.
in_file_obj.close();
out_file_obj.close();
//Return from the
//main() function.
return 0;
}