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
|
/* FirstName_LastName
* February 27, 2018
* Section 02
* Lab 07
*/
//I included comments as to what each thing does, and to my understanding of it.
#include <iostream>
//This is needed for opening the txt file (though I am unsure exactly what it does).
#include <fstream>
using namespace std;
ifstream infile; //I don't know what this is for. It was in the project description.
//Function Prototypes
double HighestTemp(double highArray[], int size);
double LowestTemp(double lowArray[], int size);
double AverageHigh(double highArray[], int size);
double AverageLow(double lowArray[], int size);
int main()
{
//This opens the file that I'm getting the weather data for.
infile.open("weather.txt");
int i;
//These are the arrays.
int dateArray[31];
double highArray[31];
double lowArray[31];
if (infile.is_open())
{
for (int i = 0; i < 31; i++)
{
infile >> dateArray[i];
infile >> highArray[i];
infile >> lowArray[i];
}
cout << "Dates Highs Lows\n";
for (int i = 0; i < 31; i++)
{
cout << dateArray[i] << " ";
cout << highArray[i] << " ";
cout << lowArray[i] << " " << endl;
}
}
//This closes the file, because the data is already seeded into the arrays. I no longer need it, I believe.s
infile.close();
double Max;
Max = HighestTemp(highArray, 31);
cout << "\nThe highest temperature in January, 2016 was: " << Max << endl;
double Min;
Min = LowestTemp(lowArray, 31);
cout << "\nThe lowest temperature in January, 2016 was: " << Min << endl;
double AvgHigh;
AvgHigh = AverageHigh(highArray, 31);
cout << "\nThe average of all high temperatures in January, 2016 was: " << AvgHigh << endl;
double AvgLow;
AvgLow = AverageLow(lowArray, 31);
cout << "\nThe average of all low temperatures in January, 2016 was: " << AvgLow << endl;
//This is to give my program a nice clean exit.
cout << "\n\nPress enter to exit.";
cin.ignore();
return 0;
}
double HighestTemp(double highArray[], int size)
{
double Max = 0;
for (int i = 0; i < size; i++)
{
if (highArray[i] > Max)
{
Max = highArray[i];
}
}
return Max;
}
double LowestTemp(double lowArray[], int size)
{
double Min = 0;
for (int i = 0; i < size; i++)
{
if (lowArray[i] < Min)
{
Min = lowArray[i];
}
}
return Min;
}
double AverageHigh(double highArray[], int size)
{
double sum = 0;
double AvgHigh = 0;
for (int i = 0; i < size; i++)
{
sum = highArray[i] + sum;
}
AvgHigh = sum / size;
return AvgHigh;
}
double AverageLow(double lowArray[], int size)
{
double sum = 0;
double AvgLow = 0;
for (int i = 0; i < size; i++)
{
sum = lowArray[i] + sum;
}
AvgLow = sum / size;
return AvgLow;
}
|