fork download
  1. // Torrez, Elaine CS1A Chapter 8 P. 487, #5
  2. /********************************************************************************************
  3.  *
  4.  * SORT MONTHLY RAINFALL FROM HIGHEST TO LOWEST
  5.  *
  6.  * ------------------------------------------------------------------------------------------
  7.  * This program stores the total rainfall for each of the 12 months into an array.
  8.  * It uses a parallel array to keep track of month names. The program then sorts both
  9.  * arrays in descending order (from highest rainfall to lowest rainfall) using the
  10.  * selection sort algorithm. Finally, it displays the months and their rainfall in
  11.  * that order.
  12.  *
  13.  * Input validation ensures that no negative rainfall values are entered.
  14.  * ------------------------------------------------------------------------------------------
  15.  *
  16.  * INPUT
  17.  * rainfall[] : Monthly rainfall amounts entered by the user (in inches)
  18.  *
  19.  * OUTPUT
  20.  * A list of months with rainfall displayed from highest to lowest
  21.  *
  22.  ********************************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <string>
  27. using namespace std;
  28.  
  29. // Function prototype
  30. void selectionSortDesc(double rain[], string months[], int size);
  31.  
  32. int main()
  33. {
  34. const int SIZE = 12; // Number of months
  35. string months[SIZE] = { // Parallel array for month names
  36. "January", "February", "March", "April", "May", "June",
  37. "July", "August", "September", "October", "November", "December"
  38. };
  39.  
  40. double rainfall[SIZE]; // Rainfall amounts for each month
  41.  
  42. // ----------------------------------------------------
  43. // INPUT: Get rainfall for each month (no negatives)
  44. // ----------------------------------------------------
  45. for (int i = 0; i < SIZE; i++)
  46. {
  47. cout << "Enter rainfall for " << months[i] << " (in inches): ";
  48. cin >> rainfall[i];
  49.  
  50. while (cin.fail() || rainfall[i] < 0)
  51. {
  52. cin.clear();
  53. cin.ignore(1000, '\n');
  54. cout << "ERROR: Enter a non-negative number for " << months[i] << ": ";
  55. cin >> rainfall[i];
  56. }
  57. }
  58.  
  59. cout << endl;
  60.  
  61. // ----------------------------------------------------
  62. // PROCESSING: Sort both arrays from highest → lowest
  63. // ----------------------------------------------------
  64. selectionSortDesc(rainfall, months, SIZE);
  65.  
  66. // ----------------------------------------------------
  67. // OUTPUT: Display months sorted by rainfall
  68. // ----------------------------------------------------
  69. cout << left << setw(15) << "Month" << "Rainfall (inches)" << endl;
  70. cout << "--------------------------------" << endl;
  71.  
  72. for (int i = 0; i < SIZE; i++)
  73. {
  74. cout << left << setw(15) << months[i]
  75. << fixed << setprecision(2) << rainfall[i] << endl;
  76. }
  77.  
  78. return 0;
  79. }
  80.  
  81. /********************************************************************************************
  82.  * SELECTION SORT DESCENDING
  83.  * ------------------------------------------------------------------------------------------
  84.  * This function sorts both the rainfall and month arrays in descending order using
  85.  * the selection sort algorithm, so that the highest rainfall month appears first.
  86.  ********************************************************************************************/
  87. void selectionSortDesc(double rain[], string months[], int size)
  88. {
  89. int maxIndex;
  90. double maxValue;
  91. string tempMonth;
  92.  
  93. for (int start = 0; start < size - 1; start++)
  94. {
  95. maxIndex = start;
  96. maxValue = rain[start];
  97.  
  98. for (int index = start + 1; index < size; index++)
  99. {
  100. if (rain[index] > maxValue)
  101. {
  102. maxValue = rain[index];
  103. maxIndex = index;
  104. }
  105. }
  106.  
  107. // Swap rainfall values
  108. rain[maxIndex] = rain[start];
  109. rain[start] = maxValue;
  110.  
  111. // Swap corresponding month names
  112. tempMonth = months[maxIndex];
  113. months[maxIndex] = months[start];
  114. months[start] = tempMonth;
  115. }
  116. }
  117.  
Success #stdin #stdout 0s 5320KB
stdin
2.5
3.0
1.8
4.2
0.9
2.0
5.0
3.5
1.2
4.0
2.2
1.7
stdout
Enter rainfall for January (in inches): Enter rainfall for February (in inches): Enter rainfall for March (in inches): Enter rainfall for April (in inches): Enter rainfall for May (in inches): Enter rainfall for June (in inches): Enter rainfall for July (in inches): Enter rainfall for August (in inches): Enter rainfall for September (in inches): Enter rainfall for October (in inches): Enter rainfall for November (in inches): Enter rainfall for December (in inches): 
Month          Rainfall (inches)
--------------------------------
July           5.00
April          4.20
October        4.00
August         3.50
February       3.00
January        2.50
November       2.20
June           2.00
March          1.80
December       1.70
September      1.20
May            0.90