// Andres Guzman CSC5 Chapter 8, P. 488, #5
/******************************************************************************
*
* DISPLAY RAINFALL STATISTICS
* ____________________________________________________________________________
* This program will calculate and display the total, average, and highest and
* lowest months of rainfall as well as display the highest to lowest months of
* rain in order.
*
* Computation:
* Uses a selection sort to sort the values inside the months for rainfall in
* order from highest to lowest.
* total / 12
* ____________________________________________________________________________
*
* INPUT
*
* [SIZE] = Array Size
* rainfall[SIZE] = Holds the inputed values of rainfall for each month
*
* OUTPUT
*
* total = Displays the total accumulated rainfall
* months[highestmonth] = Displays highest month of rainfall
* months[lowestmonth] = Display lowest month of rainfall
*
******************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//
//Function Prototype
void selectionsort(double a[], string b[], int c);
int main()
{
const int SIZE = 12; //Array size
string months[SIZE] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"}; //Initialization List for month
double rainfall[SIZE]; //Subscript to input values for rainfall
double total = 0; //Accumulator for rainfall
int i; //Cycles through subscript in for loops
//
//Initializing For Loop
for(i = 0; i < SIZE; i++)
{
cout << months[i] << " Rainfall: ";
cin >> rainfall[i];
cout << endl;
while(rainfall[i] < 0) //Input Validiation
{
cout << "Invalid Rainfall Amount, Please Try Again." << endl;
cout << months[i] << " Rainfall: ";
cin >> rainfall[i];
cout << endl;
}
total += rainfall[i]; //Adds rainfall input to total
}
double highest = rainfall[0]; //Assigns beginning of subscript to highest
double lowest = rainfall [0]; //Assigns beginning of subscript to lowest
int highestmonth = 0; //Assigns variable to subscript as a name
int lowestmonth = 0; //Assigns variable to subscript as a name
//
//Initializing For Loop
for(i = 0; i < SIZE; i++)
{
if(rainfall[i] > highest) //Finds which subscript is bigger than [0]
{
highest = rainfall[i]; //Assigns found subscript to highest
highestmonth = i; //Assigns highestmonth to subscript [0]
}
if(rainfall[i] < lowest) //Finds which subscirpt is lower than [0]
{
lowest = rainfall[i]; //Assigns found subscript to lower
lowestmonth = i; //Assigns lowestmonth to subscript [0]
}
}
cout << "\nTotal Rainfall: " << total << endl;
cout << fixed << setprecision(2); //Allows 2 decimal points for average
cout << "Average Monthly Rainfall: " << (total / 12) << endl;
cout << "Highest Month of Rainfall: " << months[highestmonth] << endl;
cout << "Lowest Month of Rainfall: " << months[lowestmonth] << endl;
//
//Display Next section contents
cout << "\nMonths Sorted in Order of Rainfall" << endl;
//
//Function Call
selectionsort(rainfall, months, SIZE);
//
//Output Each Month in order using for loop
for(i = 0; i < SIZE; i++)
{
cout << months[i] << ": " << rainfall[i] << endl;
}
return 0;
}
/*******************************************************************************
* Adding definition to Function selectionsort. This function sorts the rainfall
* amounts from highest to lowest while keeping each month paired with its
* corresponding rainfall amount. Uses selection sort to find the highest
* rainfall value and moves it into the correct position.
******************************************************************************/
void selectionsort(double a[], string b[], int c)
{
int startscan, minIndex; //Used to scan through the array
double minValue; //Holds highest value found
string temp; //Holds found value during swap
//
//Initializing For Loop
for (startscan = 0; startscan < (c - 1); startscan++)
{
minIndex = startscan; //Assigns starting position as highest
minValue = a[startscan]; //Assigns starting rainfall as highest
//
//Searches for highest rainfall value
for (int index = startscan + 1; index < c; index++)
{
if(a[index] > minValue)
{
minValue = a[index]; //Updates highest rainfall value
minIndex = index; //stores position of highest value
}
}
//
//Swaps Values
a[minIndex] = a[startscan];
a[startscan] = minValue;
//
//Swaps months to match swapped values
temp = b[minIndex];
b[minIndex] = b[startscan];
b[startscan] = temp;
}
}