fork download
  1. //Roman Lara Delgado CSC5 Chapter 7, P.444, #2
  2. //
  3. /*******************************************************************************
  4. *
  5. * Calculate Rainfall Statistics
  6. *_______________________________________________________________________________
  7. * This program calculates the total rainfall of a year, the average monthly
  8. * rainfall, and determines the months with the highest and lowest rainfall of
  9. * that year.
  10. * ______________________________________________________________________________
  11. * INPUT
  12. * rainfall[MONTHS] : The amount of rainfall in a month
  13. *
  14. * OUTPUT
  15. * totalYear : The total amount of rainfall in the year
  16. * averageMonthly : The average monthly rainfall in the year
  17. * highestMonthName : The month with the highest rainfall
  18. * lowestMonthName : The month with the lowest rainfall
  19. *******************************************************************************/
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <string>
  23. using namespace std;
  24.  
  25. int main ()
  26. {
  27. /***************************************************************************
  28. * CONSTANTS
  29. * -------------------------------------------------------------------------
  30. * MONTHS : number of months in a year
  31. * ************************************************************************/
  32. //Declare Program Constants
  33. const int MONTHS = 12; //Number of months in a year
  34.  
  35. //Delcare Program Variables
  36. float rainfall[MONTHS]; //INPUT - The amount of rainfall in a month
  37. string nameMonth[MONTHS] = {"Jan.", "Feb.", "Mar.", "Apr.", "May ", "Jun.",
  38. "Jul.", "Aug.", "Sep.", "Oct.", "Nov.",
  39. "Dec."}; //Name of each month in the year
  40. float totalYear; //OUTPUT - The total amount of rainfall in the year
  41. float averageMonthly; //OUTPUT - The average monthly rainfall in the year
  42. float highestMonth; //CALC - The month with the highest rainfall
  43. float lowestMonth; //CALC - The month with the lowest rainfall
  44. string highestMonthName; //OUTPUT - Month with highest rainfall.
  45. string lowestMonthName; //OUTPUT - Month with lowest rainfall
  46.  
  47. //Input Rainfall for the Specified Month
  48. for (int count = 0; count < MONTHS; count++)
  49. {
  50. cout << "Enter the amount of rainfall for " << nameMonth[count] << ": ";
  51. cin >> rainfall[count];
  52. cout << endl;
  53. totalYear += rainfall[count];
  54. }
  55.  
  56. //Calculate Average Monthly Rainfall
  57. averageMonthly = totalYear / MONTHS;
  58.  
  59. highestMonthName = nameMonth[0];
  60. highestMonth = rainfall[0];
  61.  
  62. //Determine Month with Most Rainfall
  63. for (int count = 1; count < MONTHS; count++)
  64. {
  65. if (rainfall[count] > highestMonth)
  66. {
  67. highestMonth = rainfall[count];
  68. highestMonthName = nameMonth[count];
  69. }
  70. }
  71.  
  72. lowestMonthName = nameMonth[0];
  73. lowestMonth = rainfall[0];
  74.  
  75. //Determine Month with Least Rainfall
  76. for (int count = 1; count < MONTHS; count++)
  77. {
  78. if (rainfall[count] < lowestMonth)
  79. {
  80. lowestMonth = rainfall[count];
  81. lowestMonthName = nameMonth[count];
  82. }
  83. }
  84.  
  85. //Output Rainfall Statistics
  86. cout << fixed << setprecision(2) << endl;
  87. cout << "The total rainfall of the year was: " << totalYear;
  88. cout << " inches" << endl;
  89. cout << "The monthly average rainfall was: " << averageMonthly;
  90. cout << " inches" << endl;
  91. cout << "The month with the most rainfall was " << highestMonthName;
  92. cout << " with a rainfall of: " << highestMonth << " inches"<< endl;
  93. cout << "The month with the least rainfall was " << lowestMonthName;
  94. cout << " with a rainfall of: " << lowestMonth << " inches" << endl;
  95.  
  96. return 0;
  97. }
  98.  
  99.  
  100.  
Success #stdin #stdout 0.01s 5316KB
stdin
10
20
4
6
3
9
6
4
3
10
4
3
stdout
Enter the amount of rainfall for Jan.: 
Enter the amount of rainfall for Feb.: 
Enter the amount of rainfall for Mar.: 
Enter the amount of rainfall for Apr.: 
Enter the amount of rainfall for May : 
Enter the amount of rainfall for Jun.: 
Enter the amount of rainfall for Jul.: 
Enter the amount of rainfall for Aug.: 
Enter the amount of rainfall for Sep.: 
Enter the amount of rainfall for Oct.: 
Enter the amount of rainfall for Nov.: 
Enter the amount of rainfall for Dec.: 

The total rainfall of the year was: 82.00 inches
The monthly average rainfall was: 6.83 inches
The month with the most rainfall was Feb. with a rainfall of: 20.00 inches
The month with the least rainfall was May  with a rainfall of: 3.00 inches