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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <math.h>
#define MAX 30
using namespace std;
struct cityInfo
{
char cityName[25];
int lowTemp;
int highTemp;
};
void calcStats(cityInfo[], int, double, double, double, double);
int main()
{
int numCities = 0;
double averagelow, averagehigh, stdlow, stdhigh;
cityInfo city[MAX];
calcStats(city, numCities, averagelow, averagehigh , stdlow, stdhigh );
cout << "\nTemperature Statistics" << endl
<< "----------------------" << endl
<< "Average Temperature - Low" << setw(15) << averagelow << endl
<< "Average Temperature - High" << setw(15) << averagehigh << endl
<< endl
<< "Standard Deviation - Low Temperatures" << setw(15) << stdlow << endl
<< "Standard Deviation - High Temperatures" << setw(15) << stdhigh << endl;
cout << endl;
system ("pause");
return 0;
}
void calcStats(cityInfo cities[], int numCities, double &averageLow, double &averageHigh, double &stdDevLow, double &stdDevHigh)
{
int sumLow, sumHigh, lowSquared, highSquared;
for( int i = 0; i < numCities; i++)
{
sumLow += cities[i].lowTemp;
sumHigh += cities[i].highTemp;
lowSquared += sumLow * sumLow;
highSquared += sumHigh * sumHigh;
averageLow = sumLow / numCities;
averageHigh = sumHigh / numCities;
stdDevLow = sqrt( lowSquared - ((sumLow *sumLow)/ numCities) / (numCities - 1));
stdDevHigh = sqrt( highSquared - ((sumHigh * sumHigh)/ numCities) / (numCities - 1));
}
}
|