fork download
  1. // Kurt Feiereisel CSC5 Chapter 7, p.444, #2
  2. /*******************************************************************************
  3.  *
  4.  * Calculate Rainfall Statistics
  5.  * _____________________________________________________________________________
  6.  * This program accepts input as the amount of rainfall each month for a year.
  7.  * The program will then calculate and report the total rainfall for the year,
  8.  * average rainfall for the year, the month with the highest rainfall, and the
  9.  * month with the lowest rainfall.
  10.  * _____________________________________________________________________________
  11.  * FORMULA:
  12.  * averageRain = tr / months
  13.  * INPUT:
  14.  * rain[] : Rainfall per month of the year
  15.  *
  16.  * OUTPUT:
  17.  * totalRain : Total rainfall for the year
  18.  * averageRain : Average Rainfall for the year
  19.  * highest : Most amount of rainfall in a month
  20.  * lowest : Least amount of rainfall in a month
  21.  * mostRain : Month with the most rain
  22.  * leastRain : Month with the least rain
  23.  *
  24.  * ****************************************************************************/
  25. #include <iostream>
  26. #include <string>
  27. #include <iomanip>
  28. using namespace std;
  29.  
  30. // Function Prototypes
  31. void rainfall(float rain[], int MONTHS);
  32. float total(float rain[], int MONTHS);
  33. float average(float totalRain, int MONTHS);
  34. float highestRain(float rain[], int MONTHS);
  35. float lowestRain(float rain[], int MONTHS);
  36. void display(float totalRain, float averageRain, float highest, float lowest,
  37. string mostRain, string leastRain);
  38. string highestMonth(float rain[], int MONTHS, float highest);
  39. string lowestMonth (float rain[], int MONTHS, float lowest);
  40.  
  41. int main()
  42. {
  43. // Initialize Constant
  44. const int MONTHS = 12;
  45.  
  46. // Declare Array
  47. float rain[MONTHS];
  48.  
  49. // Declare Variables
  50. float averageRain; // Output: Average amount of rainfall in a year
  51. float totalRain; // Output: Total rainfall for the year
  52. float highest; // Output: Most amount of rain in a month
  53. float lowest; // Output: Least amount of rain in a month
  54. string mostRain; // Output: Month with the most rain
  55. string leastRain; // Output: Month with the least rain
  56.  
  57. // Call Functions
  58. rainfall(rain, MONTHS);
  59. totalRain = total(rain, MONTHS);
  60. averageRain = average(totalRain, MONTHS);
  61. highest = highestRain(rain, MONTHS);
  62. lowest = lowestRain(rain, MONTHS);
  63. mostRain = highestMonth(rain, MONTHS, highest);
  64. leastRain = lowestMonth(rain, MONTHS, lowest);
  65. display(totalRain, averageRain, highest, lowest, mostRain, leastRain);
  66.  
  67. return 0;
  68. }
  69.  
  70. /*******************************************************************************
  71.  * Definition of function rainfall
  72.  * This function accepts input as the amount of rain per month of the year
  73.  ******************************************************************************/
  74. void rainfall(float r[], int months)
  75. {
  76. // Input the Rainfall for the month
  77. cout << "Please enter the rainfall for each month"
  78. << " this past year." << endl;
  79. for(int index = 0; index < months; index++)
  80. {
  81. cin >> r[index];
  82.  
  83. // Input Verification
  84. while (r[index] < 0)
  85. {
  86. cout << "Please enter a positive number." << endl;
  87. cin >> r[index];
  88. }
  89. }
  90. }
  91.  
  92. /*******************************************************************************
  93.  * Definition of function total
  94.  * This function accumulates the total rainfall for the year
  95.  ******************************************************************************/
  96. float total(float r[], int months)
  97. {
  98. // Initialize Accumulator
  99. float t = 0; // Accumulator: Sums up all the rainfall
  100. // per month of the year
  101. // Implement for loop with accumulator
  102. for(int index = 0; index < months; index++)
  103. t += r[index];
  104. return t;
  105. }
  106.  
  107. /*******************************************************************************
  108.  * Definition of function average
  109.  * This function calculates the average amount of rainfall for the year
  110.  ******************************************************************************/
  111. float average(float tr, int months)
  112. {
  113. // Initialize Variable
  114. float avg = 0;
  115. // Calculate the average amount of rainfall for the year
  116. avg = tr / months;
  117. return avg;
  118. }
  119.  
  120. /*******************************************************************************
  121.  * Definition of function highestRain
  122.  * This function determines the highest amount of rainfall during a month for
  123.  * the year.
  124.  ******************************************************************************/
  125. float highestRain(float r[], int months)
  126. {
  127. // Initialize Variable
  128. float highest = r[0];
  129.  
  130. // For loop to find the highest value
  131. for(int index = 0; index < months; index++)
  132. {
  133. if (r[index] > highest)
  134. highest = r[index];
  135. }
  136. return highest;
  137. }
  138.  
  139. /*******************************************************************************
  140.  * Definition of function lowestRain
  141.  * This function determines the lowest amount of rainfall in a month for the
  142.  * year
  143.  ******************************************************************************/
  144. float lowestRain(float r[], int months)
  145. {
  146. // Initialize Variable
  147. float lowest = r[0];
  148.  
  149. // For loop to determine lowest value
  150. for(int index = 0; index < months; index++)
  151. {
  152. if(r[index] < lowest)
  153. lowest = r[index];
  154. }
  155. return lowest;
  156. }
  157.  
  158. /*******************************************************************************
  159.  * Definition of function highestMonth
  160.  * This function determines which month has the highest amount of rainfall
  161.  ******************************************************************************/
  162. string highestMonth(float r[], int months, float highest)
  163. {
  164. // Declare Variable
  165. string month;
  166.  
  167. // Determine the element that is equal to the highest amount of rainfall
  168. for (int index = 0; index < months; index++)
  169. {
  170. if(highest == r[index])
  171. {
  172. // Inialize month with correspoinding subscript
  173. switch(index)
  174. {
  175. case 0: month = "January";
  176. break;
  177. case 1: month = "February";
  178. break;
  179. case 2: month ="March";
  180. break;
  181. case 3: month = "April";
  182. break;
  183. case 4: month = "May";
  184. break;
  185. case 5: month = "June";
  186. break;
  187. case 6: month = "July";
  188. break;
  189. case 7: month = "August";
  190. break;
  191. case 8: month = "September";
  192. break;
  193. case 9: month = "October";
  194. break;
  195. case 10: month = "November";
  196. break;
  197. case 11: month = "December";
  198. break;
  199. default: month = "Not a valid month";
  200. break;
  201. }
  202. }
  203. }
  204. return month;
  205. }
  206.  
  207. /*******************************************************************************
  208.  * Definition of function lowestMonth
  209.  * This function determines which month has the lowest amount of rainfall
  210.  ******************************************************************************/
  211. string lowestMonth(float r[], int months, float lowest)
  212. {
  213. // Initialize Variable
  214. string month;
  215. for (int index = 0; index < months; index++)
  216. {
  217. // Determine the element that is equal to the highest value
  218. if(lowest == r[index])
  219. {
  220.  
  221. // Initalize month with correspoinding subscript
  222. switch(index)
  223. {
  224. case 0: month = "January";
  225. break;
  226. case 1: month = "February";
  227. break;
  228. case 2: month ="March";
  229. break;
  230. case 3: month = "April";
  231. break;
  232. case 4: month = "May";
  233. break;
  234. case 5: month = "June";
  235. break;
  236. case 6: month = "July";
  237. break;
  238. case 7: month = "August";
  239. break;
  240. case 8: month = "September";
  241. break;
  242. case 9: month = "October";
  243. break;
  244. case 10: month = "November";
  245. break;
  246. case 11: month = "December";
  247. break;
  248. default: month = "Not a valid month";
  249. }
  250. }
  251. }
  252. return month;
  253. }
  254. /*******************************************************************************
  255.  * Definition of function display
  256.  * This function displays the total rainfall, average rainfall, lowest rainfall
  257.  * highest rainfall, and the month with the lowest and highest rainfall amounts
  258.  ******************************************************************************/
  259. void display(float t, float a, float h, float l, string m, string low)
  260. {
  261. cout << fixed << setprecision(2);
  262. // Display Outputs
  263. cout << "The total rainfall this past year is " << t << " inches.\n";
  264. cout << "The average rainfall this past year is " << a << " inches.\n";
  265. cout << low << " had the least amount of rainfall with " << l
  266. << " inches.\n";
  267. cout << m << " had the most amount of rainfall with " << h << " inches.\n";
  268. }
  269.  
  270.  
Success #stdin #stdout 0.01s 5272KB
stdin
1
2
3
4
5
6
7
8
9
10
11
12
stdout
Please enter the rainfall for each month this past year.
The total rainfall this past year is 78.00 inches.
The average rainfall this past year is 6.50 inches.
January had the least amount of rainfall with 1.00 inches.
December had the most amount of rainfall with 12.00 inches.