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 <string>
using namespace std;
struct RainFallData
{
string month;
double rainFall;
RainFallData (string m = "")
{
month = m;
}
};
const int NUM_MONTHS = 12;
void inputRainfall (RainFallData[], int);
void sortArray (RainFallData[], int);
void displayArray (RainFallData[], int);
int main()
{
RainFallData array[NUM_MONTHS] = { "January", "Februrary", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December" };
inputRainfall(array, NUM_MONTHS);
sortArray(array, NUM_MONTHS);
displayArray(array, NUM_MONTHS);
return 0;
}
void inputRainfall (RainFallData passedarray[], int size)
{
for (int i = 0; i < size; i++)
{
cout << "Input the amount of rainfall in inches for "
<< passedarray[i].month << ": ";
cin >> passedarray[i].rainFall;
}
}
void sortArray (RainFallData passedarray[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = i+1; j < size; j++)
{
if (passedarray[i].rainFall < passedarray[j].rainFall)
{
RainFallData temp = passedarray[i];
passedarray[i] = passedarray[j];
passedarray[j] = temp;
}
}
}
}
void displayArray(RainFallData passedarray[], int size)
{
cout << "\n\nThe amount of rainfall for each month in descending order\n"
<< "---------------------------------------------------------";
for (int i = 0; i < size; i++)
{
cout << endl << passedarray[i].month << ": " << passedarray[i].rainFall;
}
}
|