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
|
#include <iostream>
#include <fstream>
using namespace std;
const int months = 12;
void getData(double [][ 2 ], int);
double averageHigh(double [] [ 2 ], int);
double averageLow(double [] [ 2 ], int);
int indexHighTemp(double [] [ 2 ], int);
int indexLowTemp(double [] [ 2 ], int);
int main()
{
double temperatures[months][2];
getData(temperatures, months);
cout << "\n\nThe average high temp. for the year: "
<< averageHigh(temperatures, months) << endl;
cout << "\n\nThe average low temp. for the year: "
<< averageLow(temperatures, months) << endl;
cout << "\n\nIndex of highest temp. for the year: "
<< indexHighTemp(temperatures, months) << endl;
cout << "\n\nIndex of lowest temp. for the year: "
<< indexLowTemp(temperatures, months) << endl;
system("PAUSE");
return 0;
}
void getData(double t[][2], int m)
{
int i;
ifstream inFile;
ofstream outFile;
inFile.open("tempsFile.txt");
if (!inFile)
{
cout << "Cannot open input file."
<< endl;
}
for (int i=0; i<m; i++)
inFile >> t[i][0];
cout << endl;
inFile >> t[i][1];
cout << endl;
}
double averageHigh(double t[] [2], int m)
{
double sum = 0;
for (int i=0; i<m; i++)
sum += t[i][0];
return (sum/m);
}
double averageLow(double t[][2], int m)
{
double sum = 0;
for (int i=0; i<m; i++)
sum += t[i][1];
return (sum/m);
}
int indexHighTemp(double t[][2], int m)
{
int ind = 0;
double highest = t[0][0];
for (int i=1; i<m; i++)
if (t[i][0] > highest)
{
highest = t[i][0];
ind = i;
}
return ind;
}
int indexLowTemp(double t[][2], int m)
{
int ind = 0;
double lowest = t[0][1];
for (int i=1; i<m; i++)
if (t[i][1] < lowest)
{
lowest = t[i][1];
ind = i;
}
return ind;
}
|