C++ Write a program that will compare current hourly temperatures with the previ
ID: 3856511 • Letter: C
Question
C++ Write a program that will compare current hourly temperatures with the previous day's hourly temperatures. Specifications:
1. The program will read data from a file named "dailyTemps.txt." The first six items in the file are strings corresponding to dates of the data. The strings that start the file will look (for example) like this:
June 23, 2017
June 24, 2017
Following the dates will be doubles that represent data pertaining to hourly temperature readings for two days. The doubles correspond to readings from 12:00am to 11:00pm for the two days. Note that the temperature for 12:00am of day 1 is followed by the temperature for 12:00am of day two, etc. In other words, you must read any given hourly temperature for both days consecutively.
You will read the values into two one dimensional arrays to hold them so you do not have to keep reading the file. This assignment DOES NOT require use of a two-dimensional array and you should not use one as it will complicate your life unnecessarily. You will display all the temperatures under the appropriate day heading, something like this after you have read them: Hour June 23, 2017 June 24, 2017
1 55 57
2 54 57
... ... ...
24 67 43
******************************************************************
Mean Temperature 79 74
Standard deviation 18 16
ALL output is to go to the console window in JGrasp. DO NOT write the output data to a data file. You will need to compute the mean (average) temperature for each day and the standard deviation (variability) in temperatures for each day. The mean temperature is computed by adding up all the hourly temperatures and dividing by the total number of temperatures (24) for the day. The standard deviation is computed by:
1. computing the mean temperature.
2. going through a loop for each hourly temperature in which you - subtract the mean temperature from the current temperature - square that result - accumulate that result into a total (these steps compute the numerator in the expression under the radical below)
3. Divide the number computed in the loop above by the number of temperatures (N). This step provides the value under the radical in the formula.
4. Take the square root of that result - this is the standard deviation of the list of temperatures.
Explanation / Answer
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
using namespace std;
/**
*
* @author Sam
*/
int main() {
int time = 0;
double total1 = 0;
double total2 = 0;
double *day1 = new double[24];
double *day2 = new double[24];
ifstream sc;
sc.open("dailyTemps.txt");
string date1, date2,date;
sc >> date1;
sc >> date;
date1 = date1 + " " + date;
sc >> date;
date1 = date1 + " " + date;
sc >> date2;
sc >> date;
date2 = date2 + " " + date;
sc >> date;
date2 = date2 + " " + date;
cout << "Hour " << date1 << " " << date2 << endl;
while (!sc.eof()) {
sc >> day1[time] ;
sc >> day2[time] ;
total1 += day1[time];
total2 += day2[time];
cout << time << " " << day1[time] << " " << day2[time] << endl;
time ++;
}
cout << "******************************************************************" << endl;
double mean1 = total1/time;
double mean2 = total2/time;
cout << "Mean Temperatuer " << mean1 << " " << mean2 << endl;
double sd1 = 0,sd2 = 0;
for (int i = 0; i < time; i++) {
sd1 += pow(day1[i] - mean1, 2);
sd2 += pow(day2[i] - mean2, 2);
}
sd1/=time;
sd2/=time;
sd1 = sqrt(sd1);
sd2 = sqrt(sd2);
cout << "Standard deviation " << sd1 << " " << sd2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.