fork download
  1. // Torrez, Elaine CS1A Chapter 5, P. 298, #3
  2. /*******************************************************************************************
  3.  *
  4.  * AVERAGE RAINFALL
  5.  * ________________________________________________________________________________________
  6.  * This program calculates the average rainfall over a given number of years.
  7.  * The user enters the number of years, and for each year, the rainfall (in inches)
  8.  * for each of the 12 months is entered. The program then calculates and displays
  9.  * the total number of months, the total inches of rainfall, and the average rainfall
  10.  * per month for the entire period.
  11.  * ________________________________________________________________________________________
  12.  *
  13.  * INPUT:
  14.  * years : number of years
  15.  * rainfall : monthly rainfall in inches
  16.  *
  17.  * OUTPUT:
  18.  * totalMonths : total number of months (years * 12)
  19.  * totalRainfall : total rainfall in inches for the period
  20.  * averageRainfall: average rainfall per month
  21.  *
  22.  *******************************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. /***** VARIABLE DECLARATIONS *****/
  31. int years; // Number of years
  32. int totalMonths; // Total number of months
  33. double rainfall; // Rainfall for one month
  34. double totalRainfall; // Total rainfall for all months
  35. double averageRainfall; // Average rainfall per month
  36.  
  37. /***** INPUT SECTION *****/
  38. cout << "Enter the number of years: ";
  39. cin >> years;
  40.  
  41. // Validate number of years (must be at least 1)
  42. while (years < 1)
  43. {
  44. cout << "Invalid. Number of years must be at least 1. Try again: ";
  45. cin >> years;
  46. }
  47.  
  48. totalRainfall = 0.0;
  49. totalMonths = 0;
  50.  
  51. /***** PROCESSING SECTION *****/
  52. for (int year = 1; year <= years; year++)
  53. {
  54. cout << "\nYear " << year << endl;
  55.  
  56. for (int month = 1; month <= 12; month++)
  57. {
  58. cout << " Enter rainfall (in inches) for month " << month << ": ";
  59. cin >> rainfall;
  60.  
  61. // Validate rainfall (cannot be negative)
  62. while (rainfall < 0)
  63. {
  64. cout << " Invalid. Rainfall cannot be negative. Try again: ";
  65. cin >> rainfall;
  66. }
  67.  
  68. totalRainfall += rainfall;
  69. totalMonths++;
  70. }
  71. }
  72.  
  73. averageRainfall = totalRainfall / totalMonths;
  74.  
  75. /***** OUTPUT SECTION *****/
  76. cout << fixed << setprecision(2);
  77. cout << "\nNumber of months: " << totalMonths << endl;
  78. cout << "Total inches of rainfall: " << totalRainfall << endl;
  79. cout << "Average rainfall per month: " << averageRainfall << " inches" << endl;
  80.  
  81. return 0;
  82. }
  83.  
Success #stdin #stdout 0.01s 5320KB
stdin
2
3 4 2 1 0 5 2 3 4 3 1 2
2 3 1 0 4 3 2 1 5 4 3 2
stdout
Enter the number of years: 
Year 1
  Enter rainfall (in inches) for month 1:   Enter rainfall (in inches) for month 2:   Enter rainfall (in inches) for month 3:   Enter rainfall (in inches) for month 4:   Enter rainfall (in inches) for month 5:   Enter rainfall (in inches) for month 6:   Enter rainfall (in inches) for month 7:   Enter rainfall (in inches) for month 8:   Enter rainfall (in inches) for month 9:   Enter rainfall (in inches) for month 10:   Enter rainfall (in inches) for month 11:   Enter rainfall (in inches) for month 12: 
Year 2
  Enter rainfall (in inches) for month 1:   Enter rainfall (in inches) for month 2:   Enter rainfall (in inches) for month 3:   Enter rainfall (in inches) for month 4:   Enter rainfall (in inches) for month 5:   Enter rainfall (in inches) for month 6:   Enter rainfall (in inches) for month 7:   Enter rainfall (in inches) for month 8:   Enter rainfall (in inches) for month 9:   Enter rainfall (in inches) for month 10:   Enter rainfall (in inches) for month 11:   Enter rainfall (in inches) for month 12: 
Number of months: 24
Total inches of rainfall: 60.00
Average rainfall per month: 2.50 inches