Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- //global variable allowed once here
- string months[12] = {"January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"};
- //function prototypes. Some of these have arrays denoted by []
- void getData(double[], int);
- double totalRainfall(double [], int);
- double averageRainfall(double [], int);
- int driestMonth(double [], int);
- int wettestMonth(double [], int);
- void displayReport(double,int, double, int, int, double[]);
- int main()
- {
- const int SIZE = 12;
- double rainfall[SIZE];
- //call function getData
- getData(rainfall, SIZE);
- double totalRain = totalRainfall(rainfall, SIZE);
- double avgRain = averageRainfall(rainfall, SIZE);
- int lowRain = driestMonth(rainfall, SIZE);
- int highRain = wettestMonth(rainfall, SIZE);
- displayReport(totalRain, SIZE, avgRain, lowRain, highRain, rainfall);
- return 0;
- }
- void getData(double rainfall[], int SIZE)
- {
- double rainInput;
- cout << "Please enter the total rainfall for all twelve months. \n";
- for (int i=0; i< SIZE; i++)
- {
- do
- {
- cout << months[i] << ": ";
- cin >> rainInput;
- if (rainInput<0)
- cout << "Error. Please enter a rainfall amount greater than zero. \n";
- }
- while (rainInput < 0);
- rainfall[i] = rainInput;
- }
- }
- double totalRainfall(double rainfall[], int SIZE)
- {
- double total = 0;
- for (int i = 0; i < SIZE; i++)
- {
- total += rainfall[i];
- }
- return total;
- }
- double averageRainfall(double rainfall[], int SIZE)
- {
- return totalRainfall(rainfall, SIZE) / SIZE;
- }
- int driestMonth(double rainfall[], int SIZE)
- {
- //set the first month as the driest month.
- double driest = rainfall[0];
- int lowRain;
- //compare the first number to the other numbers in the array
- //if another number is lower than the current month
- //it will use that month instead.
- for (int i=0; i<SIZE; i++)
- {
- if(rainfall[i] < driest)
- {
- driest = rainfall[i];
- lowRain = i;
- }
- }
- return lowRain;
- }
- int wettestMonth(double rainfall[], int SIZE)
- {
- double wettest = rainfall[0];
- int highRain;
- for (int i = 0; i < SIZE; i++)
- {
- if (rainfall[i] > wettest)
- {
- wettest = rainfall[i];
- highRain = i;
- }
- }
- return highRain;
- }
- void displayReport(double totalRain, int SIZE, double avgRain, int lowRain, int highRain, double rainfall[])
- {
- cout << "\n 2015 Rain Report for Neversnows County \n";
- cout << fixed << showpoint << setprecision(2);
- cout << "Total rainfall: " << totalRain <<" inches \n";
- cout << "Average monthly rainfall: " << avgRain << " inches \n";
- cout << "The least rain fell in " << months[lowRain] << " with " << rainfall[lowRain] << " inches. \n";
- cout << "The most rain fell in " << months[highRain] << " with " << rainfall[highRain] << " inches. \n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement