fork download
  1. //Ryan Robateau CSC5 Chapter 7, P. 444, #2
  2. //
  3. /*******************************************************************************
  4.  * Compute Rainfall Statistics
  5.  * _____________________________________________________________________________
  6.  * This program prompts the user for total rainfall for 12
  7.  * months and computes total rainfall for the year, average
  8.  * monthly rainfall, and the months with the highest and lowest
  9.  * rainfall. This program does not accept negative numbers for
  10.  * monthly rainfall.
  11.  * _____________________________________________________________________________
  12.  ******************************************************************************/
  13. #include <iostream>
  14. #include <string>
  15. using namespace std;
  16.  
  17. int main()
  18. {
  19. const int NUM_MONTHS = 12;
  20. double rainfall[NUM_MONTHS]; // INPUT - Holds amountf of Rainfall
  21. double rainTotal; // OUTPUT - Total amount of rainfall
  22. double rainAverage; // OUTPUT - Average amount of rainfall
  23. double rainMax; // OUTPUT - Highest amount of rain
  24. double rainMin; // OUTPUT - Lowests amount of rain
  25. string monthMax;
  26. string monthMin;
  27. int count;
  28. string monthNames[NUM_MONTHS] = { "January", "February", "March",
  29. "April", "May", "June", "July",
  30. "August", "September", "October",
  31. "November", "December" };
  32.  
  33. // Loop for input for all months
  34. for (count = 0; count < NUM_MONTHS; count++)
  35. {
  36. cout << "Enter the amount of rainfall for month " << count + 1 << ":\n";
  37. cin >> rainfall[count];
  38.  
  39. // Input validation
  40. while (rainfall[count] < 0)
  41. {
  42. cout << "\n<ERROR: Please input a valid rainfall value greater than 0>\n";
  43. cout << "Enter the amount of rainfall for month " << count + 1 << ":\n";
  44. cin >> rainfall[count];
  45. }
  46. }
  47.  
  48. // Compute total
  49. for (count = 0; count < NUM_MONTHS; count++)
  50. rainTotal += rainfall[count];
  51.  
  52. // Compute average
  53. rainAverage = rainTotal / NUM_MONTHS;
  54.  
  55. // Determine highest value
  56. rainMax = rainfall[0];
  57. monthMax = monthNames[0];
  58. for (count = 0; count < NUM_MONTHS; count++)
  59. {
  60. if (rainfall[count] > rainMax)
  61. {
  62. rainMax = rainfall[count];
  63. monthMax = monthNames[count];
  64. }
  65. }
  66.  
  67. // Determine lowest number
  68. rainMin = rainfall[0];
  69. monthMin = monthNames[0];
  70. for (count = 0; count < NUM_MONTHS; count++)
  71. {
  72. if (rainfall[count] < rainMin)
  73. {
  74. rainMin = rainfall[count];
  75. monthMin = monthNames[count];
  76. }
  77. }
  78.  
  79. // Display findings
  80. cout << "\nTotal rainfall for the year: " << rainTotal << endl;
  81. cout << "Average monthly rainfall: " << rainAverage << endl;
  82. cout << "Month with most rainfall: " << monthMax << endl;
  83. cout << "Month with least rainfall: " << monthMin << endl;
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0.01s 5304KB
stdin
1
2
3
4
5
6
7
8
9
10
11
12
stdout
Enter the amount of rainfall for month 1:
Enter the amount of rainfall for month 2:
Enter the amount of rainfall for month 3:
Enter the amount of rainfall for month 4:
Enter the amount of rainfall for month 5:
Enter the amount of rainfall for month 6:
Enter the amount of rainfall for month 7:
Enter the amount of rainfall for month 8:
Enter the amount of rainfall for month 9:
Enter the amount of rainfall for month 10:
Enter the amount of rainfall for month 11:
Enter the amount of rainfall for month 12:

Total rainfall for the year: 78
Average monthly rainfall: 6.5
Month with most rainfall: December
Month with least rainfall: January