1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
Purpose of program: Diplay hourly temperature fluctuations as well as the daily mean and standard deviation.
***************************************************************************/
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
const int MAX_HOURS = 24;
string getDate1 (ifstream& fin);// used to get the first string date
string getDate2 (ifstream& fin);// used to get the second string date
void tempDay1(ifstream& fin, double day1[], string& tempDate1);//fills day1 array with 24 temperature values
void tempDay2(ifstream& fin, double day2[], string& tempDate2);//fills day2 array with 24 temperature values
double findMean1 (const double day1[]);//calculates the mean temperature for day1 array
double findMean2 (const double day2[]);//calculates the mean temperature for day2 array
double stdDev1 (const double day1[], double mean1);//calculates the standard deviation for day1 array
double stdDev2 (const double day2[], double mean2);//calculates the standard deviation for day2 array
void showData (const double day1[], const double day2[], string tempDate1, string tempDate2);//shows data in desired format
int main()
{
ifstream fin;
string tempDate1, tempDate2;
double day1[MAX_HOURS], day2[MAX_HOURS];
tempDate1 = getDate1(fin);
tempDate2 = getDate2(fin);
double mean1 = findMean1(day1);
double mean2 = findMean2(day2);
double stdv1 = stdDev1(day1, mean1);
double stdv2 = stdDev2(day2, mean2);
tempDay1(fin, day1, tempDate1);
tempDay2(fin, day2, tempDate2);
showData(day1, day2, tempDate1, tempDate2);
cout << setw(20) << "Daily Mean:" << setw(25) << mean1 << setw(30) << mean2 << endl;
cout << setw(20) << "Standard Deviation:" << setw(25) << stdv1 << setw(30) << stdv2 << endl;
}
string getDate1 (ifstream& fin)
{
fin.open("weatherdata.txt");//opens data file
if(fin.fail( ))
{
cout << "File failed to open." << endl;
exit(1);//closes the program if .txt file fails to open
}
string tempDate1;
fin >> tempDate1;
return tempDate1;
}
string getDate2 (ifstream& fin)
{
string tempDate2;
fin >> tempDate2;
return tempDate2;
}
void tempDay1(ifstream& fin, double day1[], string& tempDate1)
{
int i = 0;
double tempFromFile;
for (int i = 0; i < MAX_HOURS; i++)
{
fin >> tempFromFile;
day1[i] = tempFromFile;
}
}
void tempDay2(ifstream& fin, double day2[], string& tempDate2)
{
int i = 0;
double tempFromFile;
for (int i = 0; i < MAX_HOURS; i++)
{
fin >> tempFromFile;
day2[i] = tempFromFile;
}
fin.close();
}
double findMean1 (const double day1[])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
double tempTotal = 0;
for(int i = 0; i < MAX_HOURS; i++)
{
tempTotal = tempTotal + day1[i];
}
return tempTotal/MAX_HOURS;
}
double findMean2 (const double day2[])
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
double totalTemp = 0;
for(int i = 0; i < MAX_HOURS; i++)
{
totalTemp = totalTemp + day2[i];
}
return totalTemp/MAX_HOURS;
}
double stdDev1 (const double day1[], double mean1)
{
double temp = 0;
double stdv1;
for (int i = 0; i < MAX_HOURS; i++)
{
temp += pow((day1[i] - mean1), 2);
}
stdv1 = sqrt(temp / (MAX_HOURS));
return stdv1;
}
double stdDev2 (const double day2[], double mean2)
{
double temp = 0;
double stdv2;
for (int i = 0; i < MAX_HOURS; i++)
{
temp += pow((day2[i] - mean2), 2);
}
stdv2 = sqrt(temp / (MAX_HOURS));
return stdv2;
}
void showData (const double day1[], const double day2[], string tempDate1, string tempDate2)
{
cout << setw(20) << "Hour of the Day" << setw(25) << tempDate1 << setw(30) << tempDate2 << endl;
cout << setw(20) << "________________" << setw(25) << "__________" << setw(30) << "__________" << "\n" << endl;
for (int i = 0; i < MAX_HOURS; i++)
{
cout << setw(20) << i << setw(25) << day1[i] << setw(30) << day2[i] << endl;
}
cout << "_____________________________________________________________________________" << endl;
}
| |