Hollowfires

Untitled

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