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

Engineering Problem solving with C++ OBJECTIVE Use an input file with weather da

ID: 3728374 • Letter: E

Question

Engineering Problem solving with C++

OBJECTIVE

Use an input file with weather data and write to an output file and the console only those records whose average temperature (F) is less than or equal 45 degrees. Also, for all records, compute the minimum, maximum, and average for the average temperature (F)

Sample Data (WeatherData.txt)

This file contains weather data from May 2015. There are 6 fields per record. The fields are: Day, Max Temp (F), Min Temp (F), Avg Temp (F), Precipitation (water), Precipitation (snow). There are 31 records – one for every day of May.

WeatherData.txt

Instructions

1. Use the WeatherData.txt file for input. Do not ask the user for the file name.

2. Make sure you add a system(“pause”) before the return and before any exits.

3. You must check for fail on both the input and output files.

4. Compute the Average, Max, Min of the Avg Temp (F) for all records.

5. You will print to the output file only those records with average temperature <= 45. Also, you will output these records to the console (cout), nicely formatted.

6. You will display the Average, Max, and Min temperatures to the console (cout) only.

7. Make sure all your comments are accurate

Explanation / Answer

#include<iostream>
#include<string>
#include<fstream>
#include<limits>
#include<iomanip>
using namespace std;

int main(){

// read a file weatherData.txt make sure you have placed it at correct location. And provide the complete path for the weatherData.txt
ifstream infile("E:\weatherData.txt");
// Check whether file exists and you are able to open it. If not return from the program by printing proper message
if(infile.is_open()){
  // Created Five variables to read five data in a line in the file
  int day;
  double maxTemp;
  double minTemp;
  double avg;
  double waterPercipation;
  double snowPercipation;
  // This variable will be used to read the file line by line
  string str;
  
  int finalMin = INT_MAX; // This will represent the minimum of all the minimum temprature of all the record (initalized to INT_MAX)
  int finalMax = INT_MIN;; // This will represent the maximum of all the maximum temprature of all the record (initalized to INT_MIN)
  double finalAvg = 0.0; // This will represent the average of all the average average temperature of all the records
  double avgSum = 0.0; // As to calulate average we have to sum up all the average temperature
  int count = 0; // To count number of records
  ofstream outputFile; // Output File
  outputFile.open("output.txt"); // Create and open the output file
  
  while (infile >>day >> maxTemp >> minTemp >> avg >> waterPercipation >> snowPercipation){ // Read the data from weatherData.txt line by line
   if(avg <=45){ // If average is less than equal to 45 for a record print to the console as well as to file
    cout<<day<<" "<<maxTemp<<" "<<minTemp<<" "<<avg<<" "<<waterPercipation<<" "<<snowPercipation<<endl; // TO the console
    if(outputFile.is_open()){
     outputFile<<day<<" "<<maxTemp<<" "<<minTemp<<" "<<avg<<" "<<waterPercipation<<" "<<snowPercipation<<" "; // to the file
    }else{
     cout<<"Unable to create and open output.txt file"<<endl;
    }
    
   }
   
   if(minTemp < finalMin){
    finalMin = minTemp; // Calculating the min of all minimum temperature
   }if(maxTemp > finalMax){
    finalMax = maxTemp; // Calculating the max of all maximum temperature
   }
   
   avgSum += avg;   // Total average sum
   ++count;        // number of records
  }
  
  cout<<"Minimum temperature of all the records: "<<finalMin<<endl;
  cout<<"Maximum temperature of all the records: "<<finalMax<<endl;
  cout<<"Average temperature of all the records: " <<(avgSum)/(count) <<endl;
}else{
  cout<<"weatherData.txt File doesn't exist"<<endl;
}
system("pause");
return 0;
}