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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <iomanip>
using namespace std;
double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double[], const int, int);
int main() {
const int months = 12;
double inchesOfRain[months];
double sumOfAllMonths=0;
int monthPosition;
monthPosition=searchHighestMonth(inchesOfRain, months, 12);
for (int count = 0; count < months; count++)
{
cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<": ";
cin>>inchesOfRain[count];
sumOfAllMonths += inchesOfRain[count];
if(inchesOfRain[count] < 0){
cout <<"Rainfall must be 0 or more.\n";
cout<<"please re-enter: "<<endl;
cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<": ";
cin>>inchesOfRain[count];
}
}
cout << fixed << showpoint << setprecision(2) << endl;
cout<<"the total rainfall for the year is "<<sumOfAllMonths<<" inches"<<endl;
cout<<"the average is "<<yearlyRainAverage(inchesOfRain, 12)<<" inches"<<endl;
// cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
// cout<<"in month "<<endl;
cout<<"The largest amount of rainfall was: "<<largestRainfall(inchesOfRain, 12)<<" inches ";
cout<<"in month "<<(monthPosition+1)<<endl;
return 0;
}
double yearlyRainAverage(double inchesofrain[], const int months){
double sum=0;
for(int i=0;i<months; i++){
sum+=inchesofrain[i];
}
return sum/months;
}
double smallestRainfall(double inchesofrain[], const int months){
double smallest;
int i;
smallest=inchesofrain[0];
for(i=0; i < months; i++){
if(inchesofrain[i] < smallest){
smallest = inchesofrain[i];
}
}
return smallest;
}
double largestRainfall(double inchesofrain[], const int months){
double largest;
int i;
largest=inchesofrain[0];
for(i=0; i < months; i++){
if(inchesofrain[i] > largest){
largest = inchesofrain[i];
}
}
return largest;
}
int searchHighestMonth(double inchesofrain[], const int months, int value){
int index=0;
int position = -1;
bool found = false;
while(index < months && !found){
if(inchesofrain[index] == value)
{
found = true;
position = index;
}
index++;
}
return position;
}
}
|