# NWS Rainfall

## Problem

• NWS wants to study rainfall in Utah.
• Each day they record the average daily rainfall (in inches) for the state.
• Assume that rainfall for Utah ranges randomly between 0 and .75 inches per day.
• They have no data yet, so they would like you to simulate 20 years of daily average rainfall (don’t worry about leap years).
• They would like to have a summary each year of the total rainfall, as well as the average, max, and min daily rainfall.
• Follow the problem solving steps to design a solution

## Solution

/*
Test case 1: 1 Year, Fix daily rain fall to 1.0 (Test everything except the random num generator)
Expected output: 365 inches rain, avg 1 in, min 1, max 1
Actual:

Test case 2: 1 Year, Set MIN_RAIN and MAX_RAIN to 1.0 (Test everything plus the random num generator, but in a controlled way)
Expected output: 365 inches rain, avg 1 in, min 1, max 1
Actual:

Test case 3: 20 Years with correct values for constants (Test multiple years)
Expected output: Expect variability in results
Actual:
*/

#include <iostream>
#include <ctime>

using namespace std;

const double MAX_RAIN = 0.75;
const double MIN_RAIN = 0.00;
const int DAYS_IN_YEAR = 365;
const int YEARS_TO_SIMULATE = 20;

int main()
{
// only set the seed once (not inside a loop)
srand(time(0));

// Simulate 20 years
for (int year = 1; year <= YEARS_TO_SIMULATE; year++)
{
// Simulate 1 year
double total_rainfall = 0.0;
double max_rain_fall_so_far = 0.0;
double min_rain_fall_so_far = MAX_RAIN;

// Generate 365 random daily rainfall values
for (int day = 1; day <= DAYS_IN_YEAR; day++)
{
// To get a random double, get a double between 0 and 1, then adjust the range, then add an offset
double avg_daily_rainfall = (rand() * 1.0 / RAND_MAX) * (MAX_RAIN - MIN_RAIN) + MIN_RAIN;
cout << avg_daily_rainfall << endl;

// Keep a running total
total_rainfall = total_rainfall + avg_daily_rainfall;

// Check if the current avg daily rainfall is bigger than the max...
if (avg_daily_rainfall > max_rain_fall_so_far)
{
max_rain_fall_so_far = avg_daily_rainfall;
}

// ... or smaller than the min
if (avg_daily_rainfall < min_rain_fall_so_far)
{
min_rain_fall_so_far = avg_daily_rainfall;
}
}

// Calculate and output total and average
cout << "YEAR " << year << endl;
cout << "Total rainfall for year is " << total_rainfall << " inches" << endl;
cout << "Average rainfall was " << (total_rainfall / DAYS_IN_YEAR) << " inches" << endl;
cout << "Max rainfall was " << max_rain_fall_so_far << " inches" << endl;
cout << "Min rainfall was " << min_rain_fall_so_far << " inches" << endl;
cout << endl;
}

system("pause");
return 0;
} 