fork download
  1. //Natalie Zarate CSC 5 Chapter 11, P.646 #4
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Weather Structure
  6. struct Weather
  7. {
  8. float totalRain;
  9. float tempHigh;
  10. float tempLow;
  11. float temp_avg;
  12. };
  13.  
  14. // Prototype for GetWeatherInfo
  15. void GetWeatherInfo(Weather month[], int num);
  16.  
  17. // Prototype for TempValid
  18. int TempValid(int temp);
  19.  
  20. // Prototype for CalcAverage
  21. float CalcAverage(int tempHigh, int tempLow);
  22.  
  23. // Prototype for TotalRainfall
  24. float TotalRainfall(Weather month[], int num);
  25.  
  26. // Prototype for AvgRainfall
  27. float AvgRainfall(float totalRain, int num);
  28.  
  29. // Prototype for YrAverageTemp
  30. float YrAverageTemp (Weather month[], int num);
  31.  
  32. // Prototype for SortValues
  33. void SortValues (Weather months[], int num);
  34.  
  35. int main()
  36. {
  37. int months = 12;
  38. Weather month[months];
  39. float averageTemp;
  40. float totalRainfall;
  41. float monthAvgRain;
  42. int lowestTemp;
  43. float yearAvgTemp;
  44.  
  45. // Call GetWeatherInfo
  46. GetWeatherInfo(month, months);
  47.  
  48. // Call CalcAverage
  49. for(int i = 0; i < months; i++)
  50. {
  51. month[i].temp_avg = CalcAverage(month[i].tempHigh, month[i].tempLow);
  52. }
  53.  
  54. // Call TotalRainfall
  55. totalRainfall = TotalRainfall(month, months);
  56.  
  57. // Call AvgRainfall
  58. monthAvgRain = AvgRainfall(totalRainfall, months);
  59.  
  60. // Call SortValues
  61. SortValues(month, months);
  62.  
  63. lowestTemp = month[0].tempLow;
  64. highestTemp = month[months]. tempHigh;
  65.  
  66. // YrAverageTemp
  67. yearAvgTemp = YrAverageTemp(month, months);
  68.  
  69. // DisplayClimate
  70. DisplayClimate(monthAvgRain, totalRainfall, lowestTemp)
  71. return 0;
  72. }
  73.  
  74. void GetWeatherInfo (Weather month[],int num )
  75. {
  76. for (int i = 0; i < num; i++)
  77. {
  78. // Prompt for total rainfall in month
  79. cout <<"Enter the total rainfall for Month " << i + 1 << ":" << endl;
  80. cin >> month[i].totalRain;
  81.  
  82. // Prompt for highest temp for month
  83. cout << "Enter highest temperature for Month " << i + 1 << ": " << endl;
  84. cin >> month[i].tempHigh;
  85.  
  86. // Call TempValid
  87. TempValid(month[i].tempHigh);
  88.  
  89. // Prompt user for lowest temp for month
  90. cout << "Enter lowest temperature for Month " << i + 1 << ": " << endl;
  91. cin >> month[i].tempLow;
  92.  
  93. // Call TempValid
  94. TempValid(month[i].tempLow);
  95.  
  96.  
  97.  
  98. }
  99. }
  100.  
  101. int TempValid(int temp)
  102. {
  103. int min_temp = -100;
  104. int max_temp = 140;
  105.  
  106. while (temp < min_temp || temp > max_temp)
  107. {
  108. cout << "Temperature must be between -100 and 140 degrees." << endl;
  109. cout << "Re-enter Temperature value: " << endl;
  110. cin >> temp;
  111. }
  112. return temp;
  113. }
  114.  
  115. float CalcAverage(int tempHigh, int tempLow)
  116. {
  117. float average;
  118. average = (tempHigh + tempLow) / 2;
  119.  
  120. return average;
  121. }
  122.  
  123. float TotalRainfall(Weather month[], int num)
  124. {
  125. float total;
  126.  
  127. for (int i = 0, i < num, i++)
  128. {
  129. total += month[i].totalRain;
  130. }
  131.  
  132. return total;
  133. }
  134.  
  135. float AvgRainfall(float totalRain, int num)
  136. {
  137. float average;
  138.  
  139. average = totalRain / num;
  140.  
  141. return average;
  142. }
  143.  
  144. void SortValues ( Weather month[], int num)
  145. {
  146. int start;
  147. int maxElem;
  148. int temp;
  149. int maxValue;
  150.  
  151. for (start = 0; start < (num - 1); start++)
  152. {
  153. maxElem = start;
  154. maxValue = months[start].tempLow;
  155. temp = months[start].tempHigh;
  156.  
  157. for(int i = start; i < (start + 1); i++)
  158. {
  159. if(months[is].tempLow > maxValue)
  160. {
  161. maxValue = months[i].tempLow;
  162. temp = months[i].tempHigh;
  163. maxElem = i;
  164. }
  165. }
  166.  
  167. months[maxElem].tempLow = months[start].tempLow;
  168. months[maxElem].tempHigh = months[start].tempHigh;
  169. months[start].tempLow = maxValue;
  170. months[start].tempHigh = temp;
  171. }
  172.  
  173. }
  174.  
  175. float YrAverageTemp(Weather month[], int num)
  176. {
  177. int total;
  178. float average;
  179.  
  180. for(int i = 0; i < num; i++)
  181. {
  182. total += month[i].tempLow;
  183. total += month[i].tempHigh;
  184. }
  185.  
  186. average = total / num;
  187.  
  188. return average;
  189. }
Success #stdin #stdout 0.01s 5280KB
stdin
stdout
Enter the total rainfall for Month 1:
Enter highest temperature for Month 1: 
Enter lowest temperature for Month 1: 
Enter the total rainfall for Month 2:
Enter highest temperature for Month 2: 
Enter lowest temperature for Month 2: 
Enter the total rainfall for Month 3:
Enter highest temperature for Month 3: 
Enter lowest temperature for Month 3: 
Enter the total rainfall for Month 4:
Enter highest temperature for Month 4: 
Enter lowest temperature for Month 4: 
Enter the total rainfall for Month 5:
Enter highest temperature for Month 5: 
Enter lowest temperature for Month 5: 
Enter the total rainfall for Month 6:
Enter highest temperature for Month 6: 
Enter lowest temperature for Month 6: 
Enter the total rainfall for Month 7:
Enter highest temperature for Month 7: 
Enter lowest temperature for Month 7: 
Enter the total rainfall for Month 8:
Enter highest temperature for Month 8: 
Enter lowest temperature for Month 8: 
Enter the total rainfall for Month 9:
Enter highest temperature for Month 9: 
Enter lowest temperature for Month 9: 
Enter the total rainfall for Month 10:
Enter highest temperature for Month 10: 
Enter lowest temperature for Month 10: 
Enter the total rainfall for Month 11:
Enter highest temperature for Month 11: 
Enter lowest temperature for Month 11: 
Enter the total rainfall for Month 12:
Enter highest temperature for Month 12: 
Enter lowest temperature for Month 12: