// Torrez, Elaine CS1A Chapter 8 P. 487, #5
/********************************************************************************************
*
* SORT MONTHLY RAINFALL FROM HIGHEST TO LOWEST
*
* ------------------------------------------------------------------------------------------
* This program stores the total rainfall for each of the 12 months into an array.
* It uses a parallel array to keep track of month names. The program then sorts both
* arrays in descending order (from highest rainfall to lowest rainfall) using the
* selection sort algorithm. Finally, it displays the months and their rainfall in
* that order.
*
* Input validation ensures that no negative rainfall values are entered.
* ------------------------------------------------------------------------------------------
*
* INPUT
* rainfall[] : Monthly rainfall amounts entered by the user (in inches)
*
* OUTPUT
* A list of months with rainfall displayed from highest to lowest
*
********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function prototype
void selectionSortDesc(double rain[], string months[], int size);
int main()
{
const int SIZE = 12; // Number of months
string months[SIZE] = { // Parallel array for month names
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
double rainfall[SIZE]; // Rainfall amounts for each month
// ----------------------------------------------------
// INPUT: Get rainfall for each month (no negatives)
// ----------------------------------------------------
for (int i = 0; i < SIZE; i++)
{
cout << "Enter rainfall for " << months[i] << " (in inches): ";
cin >> rainfall[i];
while (cin.fail() || rainfall[i] < 0)
{
cin.clear();
cin.ignore(1000, '\n');
cout << "ERROR: Enter a non-negative number for " << months[i] << ": ";
cin >> rainfall[i];
}
}
cout << endl;
// ----------------------------------------------------
// PROCESSING: Sort both arrays from highest → lowest
// ----------------------------------------------------
selectionSortDesc(rainfall, months, SIZE);
// ----------------------------------------------------
// OUTPUT: Display months sorted by rainfall
// ----------------------------------------------------
cout << left << setw(15) << "Month" << "Rainfall (inches)" << endl;
cout << "--------------------------------" << endl;
for (int i = 0; i < SIZE; i++)
{
cout << left << setw(15) << months[i]
<< fixed << setprecision(2) << rainfall[i] << endl;
}
return 0;
}
/********************************************************************************************
* SELECTION SORT DESCENDING
* ------------------------------------------------------------------------------------------
* This function sorts both the rainfall and month arrays in descending order using
* the selection sort algorithm, so that the highest rainfall month appears first.
********************************************************************************************/
void selectionSortDesc(double rain[], string months[], int size)
{
int maxIndex;
double maxValue;
string tempMonth;
for (int start = 0; start < size - 1; start++)
{
maxIndex = start;
maxValue = rain[start];
for (int index = start + 1; index < size; index++)
{
if (rain[index] > maxValue)
{
maxValue = rain[index];
maxIndex = index;
}
}
// Swap rainfall values
rain[maxIndex] = rain[start];
rain[start] = maxValue;
// Swap corresponding month names
tempMonth = months[maxIndex];
months[maxIndex] = months[start];
months[start] = tempMonth;
}
}