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
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void getdata(ifstream & citydata, string & name, double temps[][2]);
void averagehigh(double temps[][2], double sum, double & average);
void averagelow(double temps[][2], double sum, double & average);
void indexhightemp(double temps[][2], double highest, double & index);
void indexlowtemp(double temps[][2], double lowest, double & index);
int main()
{
string city;
double temperatures[12][2], temp1[12][2], average1, sum1=0, temp2[12][2], average2, sum2=0, temp3[12][2], highest1 = 0 , index1, temp4[12][2], lowest1 = 0, index2;
ifstream data;
//***************************************
data.open("averagetemps.txt");
//**************************************
getdata(data, city, temperatures);
//******************************************
cout << city << endl;
for (int i = 0; i<12; i++)
{
for (int j = 0; j<2; j++)
cout << temperatures[i][j] << " ";
cout << endl;
}
//**********************************
data.close();
//*********************************************
averagehigh(temp1, sum1, average1);
cout << average1 << endl;
averagelow(temp2, sum2, average2);
cout << average2 << endl;
indexhightemp(temp3, highest1, index1);
cout << index1 << endl;
indexlowtemp(temp4, lowest1, index2);
cout << index2 << endl;
//**********************************************
return 0;
}
void getdata(ifstream & citydata, string & name, double temps[][2])
{
citydata >> name;
for (int i = 0; i<12; i++)
{
for (int j = 0; j<2; j++)
citydata >> temps[i][j];
}
}
void averagehigh(double temps[][2], double sum, double & average)
{
sum = 0;
for (int i = 0; i<12; i++)
{
sum = sum + temps[i][1];
average = (sum / 12);
}
}
void averagelow(double temps[][2], double sum, double & average)
{
sum = 0;
for (int i = 0; i < 12; i++)
{
sum = sum + temps[i][0];
average = (sum / 12);
}
}
void indexhightemp(double temps[][2],double highest, double & index)
{
for (int i = 0; i < 12; i++)
{
if (temps[i][1] > highest)
{
highest = temps[i][1];
index = i;
}
}
}
void indexlowtemp(double temps[][2], double lowest, double & index)
{
for (int i = 0; i < 12; i++)
{
if (temps[i][0] > lowest)
{
lowest = temps[i][0];
index = i;
}
}
}
|