To save the weather data with month and day, it would be the easiest to expand the structure.
typedef struct {
int highTemp;
int lowTemp;
double avgTemp;
int month;
} WeatherStats;
In order to use the advantages of C ++ and to improve the handling, it would also be advantageous to manage the weather data with the methods together in one object.
class Weather {
public:
Weather() {}; void SetData(WeatherStats& w);
void GetData(int i, WeatherStats& w);
int Highest();
int Lowest();
double YearlyAvgTemp();
private:
vector <WeatherStats> m_w;
};
void Weather::SetData(WeatherStats& w)
{
w.avgTemp = (double)(w.lowTemp + w.highTemp) / 2;
m_w.push_back(w);
}
void Weather::GetData(int i, WeatherStats& w)
{
w = m_w.at(i);
}
int Weather::Highest() {
int highIndex = 0, high = m_w.at(highIndex).highTemp;
for (size_t i = 0; i < m_w.size(); i++) {
if (m_w.at(i).highTemp > high) {
high = m_w.at(i).highTemp;
highIndex = i;
}
}
return highIndex;
}
You could call it that way
int main()
{
Weather weather;
WeatherStats w;
for (int i = 0; i < NUM_MONTHS; i++) {
cout << "Enter data for " << monthNames[i] << endl;
w.month = i;
do {
cout << "Enter high temperature within the range ";
cout << "[-100 to +140]: ";
cin >> w.highTemp;
} while (w.highTemp < -100 || w.highTemp > 140);
do {
cout << "Enter low temperature within the range";
cout << "[-100 to +140]: ";
cin >> w.lowTemp;
} while (w.lowTemp < -100 || w.lowTemp > 140);
weather.SetData(w);
cout << endl << endl;
}
int hi = weather.Highest();
weather.GetData(hi, w);
int highTemp = w.highTemp;
cout << "\nThe yearly highest temperature was " << highTemp;
cout << ", and it occurred in " << monthNames[hi] << endl;
int lo = weather.Lowest();
weather.GetData(lo, w);
int lowTemp = w.lowTemp;
cout << "\nThe yearly lowest temperature was " << lowTemp;
cout << ", and it occurred in " << monthNames[lo] << endl;
cout << "Yearly average temperature: ";
cout << weather.YearlyAvgTemp() << endl;
return 0;
}
As Original Griff has already indicated, with so many data records it should be possible to save and load them.