fork download
  1. //Devin Scheu CS1A Chapter 7, P. 444, #2
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE RAINFALL STATISTICS
  6. * ____________________________________________________________
  7. * This program determines the total rainfall, average monthly
  8. * rainfall, and the months with the highest and lowest rainfall
  9. * for a year.
  10. * ____________________________________________________________
  11. * INPUT
  12. * monthlyRainfall : The rainfall amounts for each of the 12 months
  13. *
  14. * OUTPUT
  15. * totalRainfall : The total rainfall for the year
  16. * averageRainfall : The average monthly rainfall
  17. * highestMonth : The month with the highest rainfall
  18. * lowestMonth : The month with the lowest rainfall
  19. *
  20. **************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <string>
  25.  
  26. using namespace std;
  27.  
  28. int main () {
  29.  
  30. //Variable Declarations
  31. const int MONTHS = 12; //OUTPUT - Number of months in a year
  32. double monthlyRainfall[MONTHS]; //INPUT - The rainfall amounts for each of the 12 months
  33. double totalRainfall; //OUTPUT - The total rainfall for the year
  34. double averageRainfall; //OUTPUT - The average monthly rainfall
  35. int highestMonth; //OUTPUT - The month with the highest rainfall
  36. int lowestMonth; //OUTPUT - The month with the lowest rainfall
  37. string monthNames[MONTHS] = {"January", "February", "March", "April", "May", "June",
  38. "July", "August", "September", "October", "November", "December"};
  39.  
  40. //Prompt for Input
  41. for (int i = 0; i < MONTHS; i++) {
  42. cout << "Enter rainfall for " << monthNames[i] << " (in inches): ";
  43. cin >> monthlyRainfall[i];
  44. while (monthlyRainfall[i] < 0) {
  45. cout << "\nError: Please enter a non-negative number: ";
  46. cin >> monthlyRainfall[i];
  47. }
  48. cout << monthlyRainfall[i] << " inches" << endl;
  49. }
  50.  
  51. //Calculate Total and Average
  52. totalRainfall = 0;
  53. for (int i = 0; i < MONTHS; i++) {
  54. totalRainfall += monthlyRainfall[i];
  55. }
  56. averageRainfall = totalRainfall / MONTHS;
  57.  
  58. //Find Highest and Lowest Rainfall
  59. highestMonth = 0;
  60. lowestMonth = 0;
  61. for (int i = 1; i < MONTHS; i++) {
  62. if (monthlyRainfall[i] > monthlyRainfall[highestMonth]) {
  63. highestMonth = i;
  64. }
  65. if (monthlyRainfall[i] < monthlyRainfall[lowestMonth]) {
  66. lowestMonth = i;
  67. }
  68. }
  69.  
  70. //Separator and Output Section
  71. cout << "-------------------------------------------------------" << endl;
  72. cout << "OUTPUT:" << endl;
  73.  
  74. //Output Result
  75. cout << fixed << setprecision(2);
  76. cout << left << setw(25) << "Total Rainfall:" << right << setw(15) << totalRainfall << " inches" << endl;
  77. cout << left << setw(25) << "Average Rainfall:" << right << setw(15) << averageRainfall << " inches" << endl;
  78. cout << left << setw(25) << "Highest Rainfall:" << right << setw(15) << monthNames[highestMonth] << " (" << monthlyRainfall[highestMonth] << " inches)" << endl;
  79. cout << left << setw(25) << "Lowest Rainfall:" << right << setw(15) << monthNames[lowestMonth] << " (" << monthlyRainfall[lowestMonth] << " inches)" << endl;
  80.  
  81. } //end of main()
Success #stdin #stdout 0.01s 5320KB
stdin
6
12
3
4
6
7
10
17
4
1
3
8
stdout
Enter rainfall for January (in inches): 6 inches
Enter rainfall for February (in inches): 12 inches
Enter rainfall for March (in inches): 3 inches
Enter rainfall for April (in inches): 4 inches
Enter rainfall for May (in inches): 6 inches
Enter rainfall for June (in inches): 7 inches
Enter rainfall for July (in inches): 10 inches
Enter rainfall for August (in inches): 17 inches
Enter rainfall for September (in inches): 4 inches
Enter rainfall for October (in inches): 1 inches
Enter rainfall for November (in inches): 3 inches
Enter rainfall for December (in inches): 8 inches
-------------------------------------------------------
OUTPUT:
Total Rainfall:                    81.00 inches
Average Rainfall:                   6.75 inches
Highest Rainfall:                 August (17.00 inches)
Lowest Rainfall:                 October (1.00 inches)