//Natalie Zarate CSC 5 Chapter 11, P.646 #4
#include <iostream>
using namespace std;
// Weather Structure
struct Weather
{
float totalRain;
float tempHigh;
float tempLow;
float temp_avg;
};
// Prototype for GetWeatherInfo
void GetWeatherInfo(Weather month[], int num);
// Prototype for TempValid
int TempValid(int temp);
// Prototype for CalcAverage
float CalcAverage(int tempHigh, int tempLow);
// Prototype for TotalRainfall
float TotalRainfall(Weather month[], int num);
// Prototype for AvgRainfall
float AvgRainfall(float totalRain, int num);
// Prototype for YrAverageTemp
float YrAverageTemp (Weather month[], int num);
// Prototype for SortValues
void SortValues (Weather months[], int num);
int main()
{
int months = 12;
Weather month[months];
float averageTemp;
float totalRainfall;
float monthAvgRain;
int lowestTemp;
float yearAvgTemp;
// Call GetWeatherInfo
GetWeatherInfo(month, months);
// Call CalcAverage
for(int i = 0; i < months; i++)
{
month[i].temp_avg = CalcAverage(month[i].tempHigh, month[i].tempLow);
}
// Call TotalRainfall
totalRainfall = TotalRainfall(month, months);
// Call AvgRainfall
monthAvgRain = AvgRainfall(totalRainfall, months);
// Call SortValues
SortValues(month, months);
lowestTemp = month[0].tempLow;
highestTemp = month[months]. tempHigh;
// YrAverageTemp
yearAvgTemp = YrAverageTemp(month, months);
// DisplayClimate
DisplayClimate(monthAvgRain, totalRainfall, lowestTemp)
return 0;
}
void GetWeatherInfo (Weather month[],int num )
{
for (int i = 0; i < num; i++)
{
// Prompt for total rainfall in month
cout <<"Enter the total rainfall for Month " << i + 1 << ":" << endl;
cin >> month[i].totalRain;
// Prompt for highest temp for month
cout << "Enter highest temperature for Month " << i + 1 << ": " << endl;
cin >> month[i].tempHigh;
// Call TempValid
TempValid(month[i].tempHigh);
// Prompt user for lowest temp for month
cout << "Enter lowest temperature for Month " << i + 1 << ": " << endl;
cin >> month[i].tempLow;
// Call TempValid
TempValid(month[i].tempLow);
}
}
int TempValid(int temp)
{
int min_temp = -100;
int max_temp = 140;
while (temp < min_temp || temp > max_temp)
{
cout << "Temperature must be between -100 and 140 degrees." << endl;
cout << "Re-enter Temperature value: " << endl;
cin >> temp;
}
return temp;
}
float CalcAverage(int tempHigh, int tempLow)
{
float average;
average = (tempHigh + tempLow) / 2;
return average;
}
float TotalRainfall(Weather month[], int num)
{
float total;
for (int i = 0, i < num, i++)
{
total += month[i].totalRain;
}
return total;
}
float AvgRainfall(float totalRain, int num)
{
float average;
average = totalRain / num;
return average;
}
void SortValues ( Weather month[], int num)
{
int start;
int maxElem;
int temp;
int maxValue;
for (start = 0; start < (num - 1); start++)
{
maxElem = start;
maxValue = months[start].tempLow;
temp = months[start].tempHigh;
for(int i = start; i < (start + 1); i++)
{
if(months[is].tempLow > maxValue)
{
maxValue = months[i].tempLow;
temp = months[i].tempHigh;
maxElem = i;
}
}
months[maxElem].tempLow = months[start].tempLow;
months[maxElem].tempHigh = months[start].tempHigh;
months[start].tempLow = maxValue;
months[start].tempHigh = temp;
}
}
float YrAverageTemp(Weather month[], int num)
{
int total;
float average;
for(int i = 0; i < num; i++)
{
total += month[i].tempLow;
total += month[i].tempHigh;
}
average = total / num;
return average;
}