1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <iomanip>
using namespace std;
//Function protoypes
void sort(int[], int[], int);
void show(int[], int[], int);
int main ()
{
const int Num_Months = 12;
int rainfall[Num_Months];
int months[Num_Months];
//input the 12 months rainfall
for (int months = 0; months < Num_Months; months++)
{
cout << "Enter the rainfall (in inches) for month #";
cout << (months + 1) << ": ";
cin >> rainfall[months];
//input validation
while (rainfall[months] < 0)
{
cout << "Rainfall input must be grater than or equal to 0, Please re-enter: ";
cin >> rainfall[months];
}
}
// sort the values
sort (months, rainfall, Num_Months);
//display the values
show (rainfall, months, Num_Months);
return 0;
}
void sort (int months[], int array[], int size)
{
int startscan, maxIndex, tempmonth, maxValue;
for (startscan = 0; startscan < (size - 1);startscan++)
{
maxIndex = startscan;
maxValue = array[startscan];
tempmonth = months[startscan];
for(int index = startscan + 1; index < size; index++)
{
if(array[index] > maxValue)
{
maxValue = array[index];
tempmonth = months[index];
maxIndex = index;
}
}
array[maxIndex] = array[startscan];
months[maxIndex] = months[startscan];
array[startscan] = maxValue;
months[startscan] = tempmonth;
}
}
void show(int array[], int months[], int num)
{
for(int index = 0; index < num; index++)
{
cout << "Month # " << months[index] << " ";
cout << array[index] << endl;
}
}
|