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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double getTotal(double [], int);
void getRainfall(string [], double [], int);
double getLowest(double [], int, int&);
double getHighest(double [], int, int&);
double getAverage(double [], int);
int main()
{
// Inititalize variables
const int MAXRECS = 12;
int lrIndex;
int hrIndex;
string monthNames[MAXRECS] = { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
double rainFall[MAXRECS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
double total, average, lowRain, highRain;
average = getAverage(rainFall, MAXRECS);
getRainfall(monthNames, rainFall, MAXRECS);
total = getTotal(rainFall, MAXRECS);
lowRain = getLowest(rainFall, MAXRECS, lrIndex);
highRain = getHighest(rainFall, MAXRECS, hrIndex);
cout << "\n Highest rain is " << highRain;
cout << "\n whick occurred in the month of : " << monthNames[hrIndex] << endl;
cout << "\nLowest rain is " << lowRain;
cout << "\n whick occurred in the month of : " << monthNames[lrIndex] << endl;
cout << "\nthe total is : " <<total << endl;
cout << "\nthe average is : " <<average << endl;
return 0;
}
double getHighest(double rainfall[], int MAX, int& index)
{
double highest = rainfall[0];
index = 0; // in case the first is the highest kind of a "reset"
for (int x = 0; x < MAX; ++x)
{
if (rainfall[x] > highest){
highest = rainfall[x];
index = x;
}
}
return highest;
}
double getLowest(double rainfall[], int MAX, int& index)
{
double lowest = rainfall[0];
index = 0; // in case the first is the lowest kind of a "reset"
for (int x = 0; x < MAX; ++x)
{
if (rainfall[x] < lowest){
lowest = rainfall[x];
index = x;
}
}
return lowest;
}
double getAverage(double rainfall [], int MAX)
{
double total = 0;
double average = 0;
for (int x = 0; x < MAX; ++x)
{
total += rainfall[x];
average = total / MAX;
}
return average;
}
double getTotal(double rainfall[], int MAX)
{
double total = 0;
for (int x = 0; x < MAX; ++x)
total += rainfall[x];
return total;
}
void getRainfall(string months[], double rainfall[], int MAX)
{
for(int x = 0; x < MAX; ++x)
{
cout << "Enter rainfall amont for " << months[x] << ":";
cin >> rainfall[x];
}
}
|