fork download
  1. //Mia Agramon CS1A Chapter 11, P.646, #4
  2. //
  3. /*******************************************************************************
  4.  * Weather Statistics
  5.  * _____________________________________________________________________________
  6.  * This program will display total rainfall, high temperature, low temperature,
  7.  * average temperature for the year.
  8.  *
  9.  * INPUT
  10.  * total rainfall
  11.  * highest temperature
  12.  * lowest temperature
  13.  *
  14.  * OUTPUT
  15.  * average temperature
  16.  * average rainfall
  17.  * total rainfall
  18.  * highest temperature
  19.  * lowest temperature
  20.  * month with the highest temperature
  21.  * month with the lowest temperature
  22.  ******************************************************************************/
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. struct weather
  27. {
  28. float totalRain; // month's total amount of rain
  29. float highTemp; // month's highest temperature
  30. float lowTemp; // month's lowest temperature
  31. float averageTemp; // month's average temperature
  32. int highestMonth; // month with the highest temperature
  33. int lowestMonth; // month with the lowest temperature
  34. float averageRain; // average monthly rain
  35. };
  36.  
  37. //Function Prototypes
  38. void data(weather* get, int SIZE);
  39. void calculate(const weather array[], int months, weather &annual);
  40.  
  41. int main()
  42. {
  43. // Initialize Variables
  44. const int numMONTHS = 12;
  45. weather monthly[numMONTHS];
  46. weather annualy;
  47.  
  48. //Input
  49. data(monthly, numMONTHS);
  50.  
  51. //Calculate Stats
  52. calculate(monthly, numMONTHS, annualy);
  53.  
  54. //Output
  55. cout << endl;
  56. cout << "Average Monthly Rain: " << annualy.averageRain << endl;
  57. cout << "Highest Temperature Month: " << annualy.highestMonth << endl;
  58. cout << "Lowest Temperature Month: " << annualy.lowestMonth << endl;
  59.  
  60. return 0;
  61. }
  62. void data(weather* get, int SIZE)
  63. {
  64. for (int i = 0; i < SIZE; i++)
  65. {
  66. cout << "Enter total rain for month " << (i + 1) << ": ";
  67. cin >> get[i].totalRain;
  68. cout << endl;
  69. cout << "Enter highest temperature for month " << (i + 1) << ": ";
  70. cin >> get[i].highTemp;
  71. cout << endl;
  72. cout << "Enter lowest temperature for month " << (i + 1) << ": ";
  73. cin >> get[i].lowTemp;
  74. cout << endl;
  75.  
  76. get[i].averageTemp = (get[i].highTemp + get[i].lowTemp) / 2;
  77. }
  78. }
  79.  
  80. void calculate(const weather array[], int months, weather &annual)
  81. {
  82. annual.averageRain = 0;
  83. annual.highestMonth = 0;
  84. annual.lowestMonth = 0;
  85.  
  86. for (int i = 0; i < months; i++)
  87. {
  88. annual.averageRain += array[i].totalRain;
  89.  
  90. if (array[i].highTemp > array[annual.highestMonth].highTemp)
  91. annual.highestMonth = i;
  92. if (array[i].lowTemp > array[annual.lowestMonth].lowTemp)
  93. annual.lowestMonth = i;
  94. }
  95. annual.averageRain /= months;
  96. }
Success #stdin #stdout 0.01s 5280KB
stdin
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
stdout
Enter total rain for month 1: 
Enter highest temperature for month 1: 
Enter lowest temperature for month 1: 
Enter total rain for month 2: 
Enter highest temperature for month 2: 
Enter lowest temperature for month 2: 
Enter total rain for month 3: 
Enter highest temperature for month 3: 
Enter lowest temperature for month 3: 
Enter total rain for month 4: 
Enter highest temperature for month 4: 
Enter lowest temperature for month 4: 
Enter total rain for month 5: 
Enter highest temperature for month 5: 
Enter lowest temperature for month 5: 
Enter total rain for month 6: 
Enter highest temperature for month 6: 
Enter lowest temperature for month 6: 
Enter total rain for month 7: 
Enter highest temperature for month 7: 
Enter lowest temperature for month 7: 
Enter total rain for month 8: 
Enter highest temperature for month 8: 
Enter lowest temperature for month 8: 
Enter total rain for month 9: 
Enter highest temperature for month 9: 
Enter lowest temperature for month 9: 
Enter total rain for month 10: 
Enter highest temperature for month 10: 
Enter lowest temperature for month 10: 
Enter total rain for month 11: 
Enter highest temperature for month 11: 
Enter lowest temperature for month 11: 
Enter total rain for month 12: 
Enter highest temperature for month 12: 
Enter lowest temperature for month 12: 

Average Monthly Rain: 3.66667
Highest Temperature Month: 3
Lowest Temperature Month: 3