Question

1. Write a C++ program that reads daily weather observation data from a file and writes...

1. 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

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

------------------- -------------------

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

-------------------

3. The program must not print any output to the screen except for error messages.

4. The output file must be named wx_summary.txt and the format must match exactly the format of the sample output file.

The following code will compute the year, month, and day from the date format of the file: int date; inFile >> date; // date read from file, e.g., 20180101 int year = date / 10000; int month = (date % 10000) / 100; int day = date % 100;

It is highly recommended NOT to use arrays to complete this project, nor should you modify the .weather data file.

//wx_data.txt there are 365 lines and i cannot post it here because its long.

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

Code as text:

#include <iostream>

#include <fstream>

using namespace std;

// structure to read data

struct WeatherData {

int date;

float precipitation;

int maxTemp, minTemp;

};

int main() {

WeatherData data[366];

// open file and read data in the structure

ifstream inFile;

inFile.open("wx_data.txt");

int date, minTemp, maxTemp;

float ppt;

int i = 0;

while(inFile >> date >> ppt >> maxTemp >> minTemp) {

data[i].date = date;

data[i].precipitation = ppt;

data[i].minTemp = minTemp;

data[i].maxTemp = maxTemp;

i++;

}

int totalDays = i;

inFile.close(); // close the file

// an array for month names

string monthName[] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

int yearMaxTemp = -400, yearMaxMonth;

int yearMinTemp = 400, yearMinMonth;

float yearMaxPpt = -100;

int yearMaxPptMonth;

// open outfile

ofstream outFile;

outFile.open("wx_summary.txt");

i = 0;

// process the data and write to file

while (i != totalDays) {

int monthDays = 1;

date = data[i].date;

int curMonth = (date % 10000) / 100;

int curMinTemp = 400;

int curMaxTemp = -400;

float totalPpt = 0;

// process data for each month

while (true) {

i++;

date = data[i].date;

int month = (date % 10000) / 100;

if (month != curMonth)

break;

monthDays++;

totalPpt += data[i].precipitation;

curMaxTemp = max(data[i].maxTemp, curMaxTemp);

curMinTemp = min(data[i].minTemp, curMinTemp);

}

if (curMaxTemp > yearMaxTemp) {

yearMaxTemp = curMaxTemp;

yearMaxMonth = curMonth;

}

if (curMinTemp < yearMinTemp) {

yearMinTemp = curMinTemp;

yearMinMonth = curMonth;

}

if (totalPpt > yearMaxPpt) {

yearMaxPpt = totalPpt;

yearMaxPptMonth = curMonth;

}

// write data in file

string dash(15, '-');

outFile << dash << monthName[curMonth] << dash << endl;

outFile << "Precipitation\n";

outFile << "Total: " << totalPpt << endl;

outFile << "Average: " << totalPpt / monthDays << endl;

outFile << "Temperature\n";

outFile << "Maximum: " << curMaxTemp << endl;

outFile << "Minimum: " << curMinTemp << endl;

}

// write summary in file

outFile << "------------------" << endl;

outFile << "Max temp for year: " << yearMaxTemp << " in " << monthName[yearMaxMonth] << endl;

outFile << "Min temp for year: " << yearMinTemp << " in " << monthName[yearMinMonth] << endl;

outFile << "Max precip for year: " << yearMaxPpt << " in " << monthName[yearMaxPptMonth] << endl;

outFile.close();

return 0;

}

Sample run:

Sample in file:

Sample out file:

Add a comment
Know the answer?
Add Answer to:
1. Write a C++ program that reads daily weather observation data from a file and writes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a program, Weather.java, which loads an unsorted set of weather data for Pendleton, sorts the...

    Create a program, Weather.java, which loads an unsorted set of weather data for Pendleton, sorts the data by date (handling date errors), and performs some analysis of the data (e.g. min/max temperature), and outputs the sorted data set to a file. Sample data: 2015-12-12,47,43,38,32,12,39,0.09,7,Rain,205 2015-05-08,73,56,38,18,7,22,0,0,,75 2015-11-10,51,44,37,18,8,21,0,4,,261 2016-02-02,45,36,26,12,5,14,0,1,,111 2015-06-28,109,89,68,38,11,48,T,0,Rain-Thunderstorm,296 Write methods to calculate each of the following on the given data set: Max. temperature Min. temperature Max. wind gust Max. precipitation and what the 'events' were for that day Each result...

  • need help finiding slope and placement for the farenheit in the eaquation for a java.util.Scanner COMP163...

    need help finiding slope and placement for the farenheit in the eaquation for a java.util.Scanner COMP163 Greensboro Weather Trends There has been significant debate about global warming. Instead of answering the big question, your program is to determine if (and by how much) Greensboro has been warming for the past few decades. For this assignment you are to write a program that reads a file created with data from the National Climatic Data Center to determine the average annual minimum...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An...

    Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “see the dentist”) and a date. It writes a method occursOn (int year, int month, int day) that checks whether the appointmentoccurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then it will fill an array of Appointment objects with a mixture of appointments. When the user...

  • C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales...

    C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales for some number of weeks. It will create an output file with a file name you have gotten from the user that will hold a summary of sales data. Specifications: 1. You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc. 2. The program...

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Problem: A local amateur meteorologist has been recording daily high temperatures throughout the month of June....

    Problem: A local amateur meteorologist has been recording daily high temperatures throughout the month of June. He would like you to write a program to compute some statistics based on the data he has collected. He has placed each daily high temperature on a separate line in a file named “summer_temps.txt”. The high temperatures are in order so that the first one is for June 1, the second is for June 2, and so on. The statistics that the meteorologist...

  • The assignment : Assignment You will be developing a speeding ticket fee calculator. This program will...

    The assignment : Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate multiplier: 5.2252 Highway multiplier: 9.4412 Residential multiplier:...

  • IN C++ FORMAT it will have a txt file with January 0 -10 -20 -12 February...

    IN C++ FORMAT it will have a txt file with January 0 -10 -20 -12 February 0 20 -30 -10 March 5 45 20 32 April 25 65 45 55 May 12 75 32 66 June 18 85 65 72 July 6 98 88 90 August 18 102 82 91 September 16 85 65 73 October 12 72 45 59 November 5 62 30 45 December 0 46 -15 33 Write a program using structures to store the following weather...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT