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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int MAX = 12;
void promptUser(string & fileLocation);
void readFile(double rainAry, string fileLocation);
double computeAverage(double rainAry, double average);
void computeDifference(double rainAry, double average, double aryDiff);
void printResults(double rainAry, double average, double aryDiff, string aryMonth);
int main()
{
string fileAt;
double rainGauge[MAX];
double averageRain;
double diffAry[MAX];
string monthAry[MAX] = {"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"};
promptUser(fileAt);
cout << fileAt;
readFile(rainGauge[MAX], fileAt);
computeAverage(rainGauge[MAX], averageRain);
computeDifference(rainGauge[MAX], averageRain, diffAry[MAX]);
printResults(rainGauge[MAX], averageRain, diffAry[MAX], monthAry[MAX]);
return 0;
}
void promptUser(string & fileLocation)
{
cout <<"Enter the file location of your rain measurements: ";
cin >> fileLocation;
}
void readFile(double rainAry[MAX], string fileLocation)
{
fstream inFile;
fstream outFile;
inFile.open(fileLocation.c_str());
for (int i = 0; i < MAX; i++)
{
inFile >> rainAry[i];
}
}
double computeAverage(double rainAry[MAX], double average)
{
double total;
for(int i = 0; i < MAX; i++)
{
total = total + rainAry[i];
}
average = total / MAX;
return average;
}
void computeDifference(double rainAry[MAX], double average, double aryDiff[MAX])
{
for (int i = 0; i < MAX; i++)
{
aryDiff[i] = rainAry[i] - average;
}
}
void printResults(double rainAry[MAX], double average, double aryDiff[MAX], string aryMonth[MAX])
{
cout << setprecision(10) << "Month" << setprecision(10) << "Rain Fall" << setprecision(10) << "Average" <<
setprecision(10) << "Difference" << endl;
for (int i = 0; i < MAX; i++)
{
cout << setprecision(10) << aryMonth[i] << setprecision(10) << rainAry[i] << setprecision(10) << average <<
setprecision(10) << aryDiff[i] << endl;
}
}
| |