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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
//program to track rainfall through out a year
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// functions
double calculateTotalRainfall (double[], int);
double calculateAverageRainfall (double [], int);
double findHighestAmountRainfall(double[], int, int &);
double findlowestAmountRainfall(double[], int, int &);
//main funtion
int main()
{
const int TotalMonths = 12;
//variables
double totalRainfall;
double averageRainfall;
double HighestAmountRainfall;
double lowestAmountRainfall;
int monthIndexHigh = 0;
int monthIndexLow = 0;
//array to store the total rainfa1l of per month
double monthlyRainfall [TotalMonths];
//array to store names of the months
string namesofMonths[TotalMonths] = {"Jan.", "Feb.", "Mar.", "Apri1", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.","Dec." };
//loop to get amount of rainfall
for(int i = 0; i < TotalMonths; i++)
{
//enter amount of rainfall for each month
cout<< "Please enter the amount of rainfall for the month of " <<namesofMonths[i]<<": ";
cin>>monthlyRainfall[i];
//validation of entry
while(monthlyRainfall[i]<0)
{
cout<< "please enter positive numbers only!"<<endl;
cin>>monthlyRainfall[i];
}
}
//retrieve total rainfall with calc total rainfall function
totalRainfall= calculateTotalRainfall(monthlyRainfall, TotalMonths);
//retrieve Avg rainfall for the year
averageRainfall= calculateAverageRainfall(monthlyRainfall, TotalMonths);
//retrieve highest amount of rainfall
HighestAmountRainfall= findHighestAmountRainfall(monthlyRainfall, TotalMonths, monthIndexHigh);
//retrieve lowest amount of rainfall
lowestAmountRainfall= findlowestAmountRainfall(monthlyRainfall, TotalMonths, monthIndexLow);
//display results
cout<< fixed<< showpoint<< setprecision(2)<<endl;
cout<< "Total Rainfall for the year: "<<totalRainfall<<endl;
cout<< "Avg. Rainfall for the year: "<<averageRainfall<<endl;
cout<< "Highest Amount of Rainfall for the year: "<<HighestAmountRainfall<<"for the month of "<<namesofMonths[monthIndexHigh]<<endl;
cout<< "Lowest Amount of Rainfall for the year: "<<lowestAmountRainfall<<"for the month of "<<namesofMonths[monthIndexLow]<<endl;
system("pause");
return 0;
}
//total rainfall function
double calculateTotalRainfall (double amounts[], int n)
{
double totalAmount = 0;
//total rainfall
for (int i = 0; 1 < n; i++)
totalAmount += amounts[i];
return totalAmount;
}
//Avg rainfall function
double calculateAverageRainfall (double amounts[], int n)
{
double averageAmount = 0.0;
double totalAmount = 0;
//avg rainfall
for (int i = 0; 1 < n; i++)
totalAmount += amounts[i];
averageAmount = totalAmount/n;
return totalAmount;
}
//highest amount of rainfall function
double findHighestAmountRainfall(double amounts[], int n, int & monthlndex)
{
double highestAmount;
//get the highest amount of rainfall
int i = 0;
highestAmount = amounts[i];
while(i < n)
{
if (amounts[i] > highestAmount)
{
highestAmount = amounts [i];
monthlndex= i;
}
i++;
}
return highestAmount;
}
//Lowest amount of rainfall function
double findlowestAmountRainfall(double amounts[], int n, int & monthlndex)
{
double lowestAmount;
//get the lowest amount of rainfall
int i = 0;
lowestAmount = amounts[i];
while(i < n)
{
if (amounts[i] < lowestAmount)
{
lowestAmount = amounts [i];
monthlndex= i;
}
i++;
}
return lowestAmount;
}
|