fork download
  1. //Cameron Pham CS1A Chapter 5, p. 295, #10
  2. //
  3. /******************************************************************************
  4. * CALCULATE AVERAGE RAINFALL
  5. * ______________________________________________________________________________
  6. * This program accepts an integer value for years, and it accepts a value for
  7. * Inches of rain. It will calculate the total inches of rainfall over the years
  8. * And the average rainfall.
  9. * ______________________________________________________________________________
  10. * INPUT
  11. * Years // Number of years in desired time period
  12. * Rainfall // Inches of rainfall of particular month
  13. *
  14. * OUTPUT
  15. * Months // Total number of months
  16. * Rainfall total // Total inches of rainfall
  17. * Rainfall average // Average rainfall per month for the entire period
  18. *
  19. * INPUT VALIDATION
  20. * Years >= 1
  21. *******************************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. int main()
  27. {
  28. // Initialize Variables
  29. int years; // Number of years
  30. float rainfall; // Rainfall in inches for a particular month
  31. int months; // Total number of months in given years
  32. float rainfallTotal = 0; // Total inches of rainfall in time period
  33. float rainfallAverage; // Average rainfall per month for time period
  34.  
  35. // Ask the user for the number of years desired for calculating rainfall.
  36. cout << "I will calculate the average amount of rainfall in inches ";
  37. cout << "based off of the number of years and amount of rainfall you ";
  38. cout << "provide me." << endl;
  39. cout << "How many years of rainfall would you like me to calculate?";
  40. cin >> years;
  41.  
  42. // Validate the number of years.
  43. while (years < 1)
  44. {
  45. cout << "Please enter a number of years that is greater than 0./n";
  46. cin >> years;
  47. }
  48. cout << "You have entered " << years << " year(s)" << endl;
  49.  
  50. // Outer loop runs once for each year.
  51. for (int year = 1; year <= years; year++)
  52. {
  53. // Inner loop runs 12 times for each month in a year.
  54. for (int month = 1; month <= 12; month++)
  55. {
  56. cout << "Enter the rainfall in inches for month " << month;
  57. cin >> rainfall;
  58. cout << endl;
  59.  
  60. // Validate input for rainfall.
  61. while (rainfall < 0)
  62. {
  63. cout << "Enter a positive value for rainfall.";
  64. cin >> rainfall;
  65. }
  66.  
  67. // Add rainfall per month to total rainfall.
  68. rainfallTotal += rainfall;
  69. }
  70. }
  71.  
  72. // Calculate the total number of months and average rainfall per month.
  73. months = years * 12; // Total number of months
  74. rainfallAverage = rainfallTotal / months;
  75.  
  76. // Display the results.
  77. cout << fixed << setprecision(2);
  78. cout << "The total number of months in " << years << " year(s) is ";
  79. cout << months << " months." << endl;
  80. cout << "The total inches of rainfall in this period is ";
  81. cout << rainfallTotal << " inches" << endl;
  82. cout << "The average rainfall per month for the entire period is ";
  83. cout << rainfallAverage << " inches." << endl;
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 5316KB
stdin
1
34
7
12
22
5
6
7
8
9
10
11
12
stdout
I will calculate the average amount of rainfall in inches based off of the number of years and amount of rainfall you provide me.
How many years of rainfall would you like me to calculate?You have entered 1 year(s)
Enter the rainfall in inches for month 1
Enter the rainfall in inches for month 2
Enter the rainfall in inches for month 3
Enter the rainfall in inches for month 4
Enter the rainfall in inches for month 5
Enter the rainfall in inches for month 6
Enter the rainfall in inches for month 7
Enter the rainfall in inches for month 8
Enter the rainfall in inches for month 9
Enter the rainfall in inches for month 10
Enter the rainfall in inches for month 11
Enter the rainfall in inches for month 12
The total number of months in 1 year(s) is 12 months.
The total inches of rainfall in this period is 143.00 inches
The average rainfall per month for the entire period is 11.92 inches.