CPP Please, SEAT Num ints, 32 minutes. 2a) A file, CP2015wind.dat, contains weat
ID: 3594339 • Letter: C
Question
CPP Please,
SEAT Num ints, 32 minutes. 2a) A file, CP2015wind.dat, contains weather data taken from Central Park for the year 8 Each line represents data from one day. Readings were not taken every day, so we don't know how many 01 here are. Each line consists of a string of four digits, two for the month, two for the day (0101January 1 0107 == January 7 , etc.), a one word description of the wind condition followed bythe high temperature tor the day in Fahrenheit. The date, description and temperature are separated with tabs. The beginning of the le looks like this 0101 breezy 0102 calm 0104 calm 0107 windy 25 29 41 Write a program that reads the file and creates two new fles. CAL.M.dat and coLD data where CALM.dat contains a copy of those lines in the original file that have data for CALM days, and COLD.data has a copy of those lines in the original file that have data for days where the temperature was 32 or lower. 2b) Another file CP2015sky dat contains weather data from Central Park for 2015 of the form: 0104 overcast 0101 sunny 0102 cloudy 0107 overcast Where the first string on each row represents the date (exactly like the previous question), and the second string represents a description of the sky. Write a program that combines the two files (lets call them simply WIND and SKY from now on) and creates a third file called CP2015all.dat which will contain the information from both files. The first four lines of CP2015all.dat would look something like this: 0101 breezy 0102 calm 0104 calm 0107 windy 25 sunny 29 cloudy 41 XXxxx 44 overcast There are exactly the same number of lines in the WIND file and in the SKY file and for every date in the WIND file there is a corresponding date in the SKY file, but the order of the lines in the file are different. The output file should be in the same order as the WIND fileExplanation / Answer
#include <iostream>
#include<fstream>//for file operations
#include<sstream> //for string stream, to read line from file
#include<vector> //for vector, to store file date
using namespace std;
int main(){
//initialize input filestreams and open input files
ifstream windFile,skyFile;
windFile.open("CP2015wind.dat");
skyFile.open("CP2015sky.dat");
//initialize output filestreams and open output files
ofstream calmFile,coldFile,allFile;
calmFile.open("CALM.dat");
coldFile.open("COLD.dat");
allFile.open("CP2015all.dat");
//vectors to store date data and wind condition
vector<string> date_arr,desc_arr;
vector<int> tempr_arr; //vector to store temperature data
//string variables to store date, wind condition and line to read line from file
string date,desc,line;
int i,tempr; //tempr to store temperature
//Read wind file
while (getline(windFile, line))
{
istringstream ss(line);
ss >> date >> desc >> tempr;
//store corresponding values in corresponding vectors
date_arr.push_back(date);
desc_arr.push_back(desc);
tempr_arr.push_back(tempr);
//store row in calm file if wind condition is calm
if(desc=="calm"){
calmFile<<date<<" "<<desc<<" "<<tempr<<endl;
}
//store row in cold file if temperature<=32
if(tempr<=32){
coldFile<<date<<" "<<desc<<" "<<tempr<<endl;
}
}
//initialize a vector to store sky condition
vector<string> sky_arr(date_arr.size());
//Read sky file and store corresponding date's sky coniditon in respective index of sky vector
while(getline(skyFile,line)){
istringstream ss(line);
ss >> date >> desc;
for(i=0;i<date_arr.size();i++)
if(date == date_arr[i])
sky_arr[i] = desc;
}
//print all vectors in to final file
for(i=0;i<date_arr.size();i++){
allFile<<date_arr[i]<<" "<<desc_arr[i]<<" "<<tempr_arr[i]<<" "<<sky_arr[i]<<endl;
}
//close all files
windFile.close();
skyFile.close();
calmFile.close();
coldFile.close();
allFile.close();
return 0;
}
Comments: Keep CP2015wind.dat and CP2015sky.dat files in same folder with cpp file.
Please comment if extra help required.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.