Question: C++ Write a program that will compare current hour... C++ Write a prog
ID: 3859148 • Letter: Q
Question
Question: C++ Write a program that will compare current hour...
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
//// C++ code attached below
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
//function to calculate mean
double calc_mean(double temp[], int days){
double mean=0;
for(int day=0; day<days; day++){
mean += temp[day];
}
mean = mean/days;
return mean;
}
//function to calculate standard deviation
double calc_std(double temp[], int days){
double std=0;
double mean=calc_mean(temp,days);
for(int day=0; day<days; day++){
std += (temp[day]-mean)*(temp[day]-mean);
}
std = std/days;
std = sqrt(std);
return std;
}
int main () {
string date1, date2;
ifstream infile("dailyTemps.txt");
getline(infile,date1); //first day date
getline(infile,date2); //second day date
double temp1[24]; // first day temeratures array
double temp2[24]; // second day temeratures array
//Reading temperatures into arrays
int days=0;
string t1,t2;
while(infile >> t1)
{
temp1[days]=atof(t1.c_str());
infile>>t2;
temp2[days]=atof(t2.c_str());
days++;
}
// printing all days temperetures
cout<<"Hour "<<date1<<" "<<date2<<endl;
for(int d=0; d<days; d++){
cout<<d+1<<" "<<temp1[d]<<" "<<temp2[d]<<endl;
}
//calculating mean
double mean_day1 = calc_mean(temp1,days);
double mean_day2 = calc_mean(temp2,days);
//calculating standard deviation
double std_day1 = calc_std(temp1,days);
double std_day2 = calc_std(temp2,days);
// Printing mean and standard deviation
cout<<"Mean Tempereture "<<mean_day1<<" "<<mean_day2<<endl;
cout<<"Standard Deviation "<<std_day1<<" "<<std_day2<<endl;
infile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.