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 76
|
// This program calculates and displays the total rainfall for a year.
// This is a modified version of BE0702.
// This version will sort data in addition to its original functions.
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <vector>
#include <cstring>
using namespace std;
int main(){
float rainfall[12], Total = 0, RainHolder;
int MostCount, LeastCount, MaxValue, MaxAddress;
int n1, n2;
char MonthHolder[10];
char month[12][10] = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
for (n1 = 0; n1 < 12; n1++){
cout.setf(ios::left);
cout << "Enter the rainfall for " << setw(10) << month[n1] << ": ";
cin >> rainfall[n1];
if (rainfall[n1] < 0){
cout << "Invalid Entry." << endl;
exit(0);
}
}
MostCount = 0;
LeastCount = 0;
for (n2 = 0; n2 < 12; n2++){
Total = Total + rainfall[n2];
if (rainfall[n2] > rainfall[MostCount])
MostCount = n2;
if (rainfall[n2] < rainfall[LeastCount])
LeastCount = n2;
}
cout << "The total rainfall for the year was " << Total << endl
<< "The average rainfall per month was " << Total / 12 << endl
<< "The month with the most rainfall was " << month[MostCount] << endl
<< "The month with the least rainfall was " << month[LeastCount] << endl;
for (n1 = 0; n1 < 11; n1++){
MaxValue = rainfall[n1];
MaxAddress = n1;
strcpy(MonthHolder, month[n1]);
RainHolder = rainfall[n1];
for (n2 = n1 + 1; n2 < 12; n2++){
if (rainfall[n2] > MaxValue){
strcpy(MonthHolder, month[n2]);
RainHolder = rainfall[n2];
MaxAddress = n2;
}
}
strcpy(month[MaxAddress], month[n1]);
strcpy(month[n1], MonthHolder);
rainfall[MaxAddress] = rainfall[n1];
rainfall[n1] = RainHolder;
}
cout << "The months sorted by rainfall follow from highest to lowest." << endl;
for (n1 = 0; n1 < 12; n1++)
cout << month[n1] << endl;
return 0;
}
|