Advertisement
Hollowfires

Untitled

Nov 18th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. //global variable allowed once here
  9. string months[12] = {"January", "February", "March", "April", "May", "June",
  10. "July", "August", "September", "October", "November", "December"};
  11.  
  12. //function prototypes. Some of these have arrays denoted by []
  13. void getData(double[], int);
  14. double totalRainfall(double [], int);
  15. double averageRainfall(double [], int);
  16. int driestMonth(double [], int);
  17. int wettestMonth(double [], int);
  18.  
  19. void displayReport(double,int, double, int, int, double[]);
  20.  
  21.  
  22.  
  23.  
  24.  
  25. int main()
  26. {
  27. const int SIZE = 12;
  28. double rainfall[SIZE];
  29. //call function getData
  30. getData(rainfall, SIZE);
  31.  
  32. double totalRain = totalRainfall(rainfall, SIZE);
  33. double avgRain = averageRainfall(rainfall, SIZE);
  34.  
  35. int lowRain = driestMonth(rainfall, SIZE);
  36. int highRain = wettestMonth(rainfall, SIZE);
  37.  
  38. displayReport(totalRain, SIZE, avgRain, lowRain, highRain, rainfall);
  39.  
  40. return 0;
  41. }
  42.  
  43.  
  44. void getData(double rainfall[], int SIZE)
  45. {
  46.  
  47. double rainInput;
  48.  
  49. cout << "Please enter the total rainfall for all twelve months. \n";
  50. for (int i=0; i< SIZE; i++)
  51. {
  52. do
  53. {
  54. cout << months[i] << ": ";
  55. cin >> rainInput;
  56.  
  57. if (rainInput<0)
  58. cout << "Error. Please enter a rainfall amount greater than zero. \n";
  59.  
  60. }
  61. while (rainInput < 0);
  62.  
  63. rainfall[i] = rainInput;
  64. }
  65.  
  66. }
  67.  
  68.  
  69. double totalRainfall(double rainfall[], int SIZE)
  70. {
  71. double total = 0;
  72.  
  73. for (int i = 0; i < SIZE; i++)
  74. {
  75. total += rainfall[i];
  76. }
  77.  
  78. return total;
  79. }
  80.  
  81.  
  82. double averageRainfall(double rainfall[], int SIZE)
  83. {
  84. return totalRainfall(rainfall, SIZE) / SIZE;
  85.  
  86. }
  87.  
  88. int driestMonth(double rainfall[], int SIZE)
  89. {
  90. //set the first month as the driest month.
  91. double driest = rainfall[0];
  92. int lowRain;
  93.  
  94. //compare the first number to the other numbers in the array
  95. //if another number is lower than the current month
  96. //it will use that month instead.
  97. for (int i=0; i<SIZE; i++)
  98. {
  99. if(rainfall[i] < driest)
  100. {
  101. driest = rainfall[i];
  102. lowRain = i;
  103. }
  104. }
  105. return lowRain;
  106.  
  107. }
  108.  
  109. int wettestMonth(double rainfall[], int SIZE)
  110. {
  111. double wettest = rainfall[0];
  112. int highRain;
  113.  
  114. for (int i = 0; i < SIZE; i++)
  115. {
  116. if (rainfall[i] > wettest)
  117. {
  118. wettest = rainfall[i];
  119. highRain = i;
  120. }
  121. }
  122. return highRain;
  123. }
  124.  
  125. void displayReport(double totalRain, int SIZE, double avgRain, int lowRain, int highRain, double rainfall[])
  126. {
  127.  
  128. cout << "\n 2015 Rain Report for Neversnows County \n";
  129. cout << fixed << showpoint << setprecision(2);
  130. cout << "Total rainfall: " << totalRain <<" inches \n";
  131. cout << "Average monthly rainfall: " << avgRain << " inches \n";
  132. cout << "The least rain fell in " << months[lowRain] << " with " << rainfall[lowRain] << " inches. \n";
  133. cout << "The most rain fell in " << months[highRain] << " with " << rainfall[highRain] << " inches. \n";
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement