fork download
  1. //Cameron Pham CS1A Chapter 8, p. 488, #5
  2. //
  3. /******************************************************************************
  4. * CALCULATE RAINFALL STATISTICS
  5. * ______________________________________________________________________________
  6. * This program accepts a double value for the total rainfall for each of 12
  7. * months and stores the value in an array of doubles. It calculates and displays
  8. * the total rainfall, average monthly rainfall, and it sorts the months by
  9. * rainfall.
  10. * ______________________________________________________________________________
  11. * INPUT
  12. * Rainfall // Rainfall amount for each month
  13. *
  14. * OUTPUT
  15. * Total rainfall // Total rainfall for the year
  16. * Average rainfall // Average monthly rainfall
  17. *******************************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <string>
  21. using namespace std;
  22. int main()
  23. {
  24. // Initialize Variables
  25. const int MONTHS = 12;
  26. string monthNames[MONTHS] =
  27. {
  28. "January", "February", "March", "April",
  29. "May", "June", "July", "August",
  30. "September", "October", "November", "December"
  31. };
  32. double rainfall[MONTHS]; // Rainfall for each month
  33. double totalRainfall = 0.0; // Total rainfall for the year
  34. double averageRainfall; // Average monthly rainfall
  35. double tempRainfall; // Temporary rainfall value
  36. string tempMonth; // Temporary month name
  37. int maxIndex; // Index of highest rainfall
  38. // Get the rainfall for each month
  39. for (int count = 0; count < MONTHS; count++)
  40. {
  41. cout << "Enter rainfall for " << monthNames[count] << ": ";
  42. cin >> rainfall[count];
  43. // Validate the rainfall amount
  44. while (rainfall[count] < 0)
  45. {
  46. cout << "Rainfall cannot be negative. Enter again: ";
  47. cin >> rainfall[count];
  48. }
  49. totalRainfall += rainfall[count];
  50. }
  51. // Calculate the average monthly rainfall
  52. averageRainfall = totalRainfall / MONTHS;
  53. // Sort rainfall from highest to lowest using selection sort
  54. for (int start = 0; start < MONTHS - 1; start++)
  55. {
  56. maxIndex = start;
  57. for (int count = start + 1; count < MONTHS; count++)
  58. {
  59. if (rainfall[count] > rainfall[maxIndex])
  60. {
  61. maxIndex = count;
  62. }
  63. }
  64. // Swap the rainfall amounts
  65. tempRainfall = rainfall[start];
  66. rainfall[start] = rainfall[maxIndex];
  67. rainfall[maxIndex] = tempRainfall;
  68. // Swap the corresponding month names
  69. tempMonth = monthNames[start];
  70. monthNames[start] = monthNames[maxIndex];
  71. monthNames[maxIndex] = tempMonth;
  72. }
  73. // Display the rainfall statistics
  74. cout << fixed << setprecision(2);
  75. cout << "\nRainfall Statistics" << endl;
  76. cout << "------------------------------" << endl;
  77. cout << "Total rainfall: " << totalRainfall << " inches" << endl;
  78. cout << "Average rainfall: " << averageRainfall << " inches" << endl;
  79. // Display months in descending order of rainfall
  80. cout << "\nMonths Sorted by Rainfall" << endl;
  81. cout << "------------------------------" << endl;
  82. for (int count = 0; count < MONTHS; count++)
  83. {
  84. cout << monthNames[count] << ": "
  85. << rainfall[count] << " inches" << endl;
  86. }
  87. return 0;
  88. }
Success #stdin #stdout 0s 5348KB
stdin
3.5
1.2
5.8
2.4
4.1
0.8
6.3
3.0
1.7
7.2
4.5
2.9
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: 43.40 inches
Average rainfall: 3.62 inches

Months Sorted by Rainfall
------------------------------
October: 7.20 inches
July: 6.30 inches
March: 5.80 inches
November: 4.50 inches
May: 4.10 inches
January: 3.50 inches
August: 3.00 inches
December: 2.90 inches
April: 2.40 inches
September: 1.70 inches
February: 1.20 inches
June: 0.80 inches