fork download
  1. //Marvyn Jesse De La Torre CS1A Chapter 7, pg.444, #2
  2. //
  3. /******************************************************************************
  4.  * CALCULATE RAINFALL
  5.  *
  6.  * This program asks the user to enter the rainfall for each month.
  7.  * It stores the amounts in an array and calculates the total and average
  8.  * rainfall. It also finds the months with the highest and lowest rainfall.
  9.  *
  10.  * INPUT
  11.  * rainfall - Rainfall amount for each month
  12.  *
  13.  * OUTPUT
  14.  * total - Total rainfall for the year
  15.  * average - Average rainfall per month
  16.  * highestMonth - Month with the most rainfall
  17.  * lowestMonth - Month with the least rainfall
  18.  ******************************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <string>
  23. #include <limits>
  24. using namespace std;
  25.  
  26. int main()
  27. {
  28. const int MONTHS = 12;
  29.  
  30. double rainfall[MONTHS];
  31. double total = 0.0;
  32. int highestMonth = 0;
  33. int lowestMonth = 0;
  34.  
  35. // List of month names.
  36. string months[MONTHS] =
  37. {
  38. "January", "February", "March", "April",
  39. "May", "June", "July", "August",
  40. "September", "October", "November", "December"
  41. };
  42.  
  43. // Get the rainfall amount for each month.
  44. for (int i = 0; i < MONTHS; i++)
  45. {
  46. cout << "Enter rainfall for " << months[i] << ": ";
  47. cin >> rainfall[i];
  48.  
  49. // Make sure the rainfall amount is valid.
  50. while (cin.fail() || rainfall[i] < 0)
  51. {
  52. cout << "Invalid amount. Enter a positive number: ";
  53. cin.clear();
  54. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  55. cin >> rainfall[i];
  56. }
  57.  
  58. total += rainfall[i];
  59. }
  60.  
  61. // Find the months with the highest and lowest rainfall.
  62. for (int i = 1; i < MONTHS; i++)
  63. {
  64. if (rainfall[i] > rainfall[highestMonth])
  65. {
  66. highestMonth = i;
  67. }
  68.  
  69. if (rainfall[i] < rainfall[lowestMonth])
  70. {
  71. lowestMonth = i;
  72. }
  73. }
  74.  
  75. // Display the rainfall results.
  76. cout << fixed << setprecision(2);
  77.  
  78. cout << "\nRainfall Statistics\n";
  79. cout << "--------------------\n";
  80.  
  81. cout << "Total rainfall: " << total << " inches\n";
  82. cout << "Average rainfall: " << total / MONTHS << " inches\n";
  83.  
  84. cout << "Month with the most rainfall: "
  85. << months[highestMonth] << " ("
  86. << rainfall[highestMonth] << " inches)\n";
  87.  
  88. cout << "Month with the least rainfall: "
  89. << months[lowestMonth] << " ("
  90. << rainfall[lowestMonth] << " inches)\n";
  91.  
  92. return 0;
  93. }
Success #stdin #stdout 0s 5320KB
stdin
2 14 2 0 17 7 6 8 10 12 5 18
stdout
Enter rainfall for January: Enter rainfall for February: Enter rainfall for March: Enter rainfall for April: Enter rainfall for May: Enter rainfall for June: Enter rainfall for July: Enter rainfall for August: Enter rainfall for September: Enter rainfall for October: Enter rainfall for November: Enter rainfall for December: 
Rainfall Statistics
--------------------
Total rainfall: 101.00 inches
Average rainfall: 8.42 inches
Month with the most rainfall: December (18.00 inches)
Month with the least rainfall: April (0.00 inches)