Sunday, December 11, 2016

Programming Challenge 5.10 - Average Rainfall

/* Average Rainfall - This program uses nested loops to collect data and calculate
the average rainfall over a period of years.

The outer loop will iterate once for each year. The inner loop will iterate 12
times, once for each month. Each iteration of the inner loop asks the user for
the inches of rainfall for that month.

After all iterations, the program displays the number of months, the total
inches of rainfall, and the average rainfall per month for the entire period.

Input Validation: Numbers less than 1 for years, and negative numbers for the
monthly rainfalls are rejected. */

#include "Utility.h"

int main()
{
    /* Constant: Months in Year */
    const int MONTHS_IN_YEAR = 12;

    /* Variables: Data for Month, Rainfall, AverageRainfall, Precipitation
       total (accumulator variable) */
    double dataMonths = 0,
           rainfall = 0,
           precipitationTotal = 0,
           averageRainfall = 0,
           yearTotal = 0;
           
    /* Variables: Data for Years, Months (counter variable), Years
       (counter variable), Months total (accumulator variable),
       Years total (accumulator variable) */
    int dataYears = 0,
        months = 0,
        monthsTotal = 0,
        years = 0;
   
    /* Ask the user for the number of years */
    cout << "Please enter the number of years to enter data for: ";
    cin >> dataYears;

    /* While loop::Input Validation: The user must enter a number above
       0 */
    while (dataYears <= 0)
    {
        /* Display: Error message */
        cout << "Sorry, but you seem to have entered 0 or any other\n"
            << "non-positive value for years. Only values 1 and above\n"
            << "are allowed.\n";

        /* Ask the user again */
        cout << "Please enter the number of years to enter data for: ";
        cin >> dataYears;
    }

    /* For loop: As long as years is smaller than or equal to the number
       of years the user entered, the outer loop will iterate */
    for (years = 1; years <= dataYears; years++)
    {
        cout << "\nYear: " << years << endl;

        /* Formatting the display */
        cout << fixed << showpoint << setprecision(2);

        /* Nested For loop: As long as months is smaller than or equal to
           the number of MONTHS_IN_YEAR (12), the outer loop will increment
           the year by 1, and the inner loop iterates again */
        for (months = 1; months <= MONTHS_IN_YEAR; months++)
        {
            /* Nested While loop: While months is smaller than or equal to
               MONTHS_IN_YEAR (12), and years is smaller than or equal to
               data years entered by the user, this loop will iterate */
            while (months <= MONTHS_IN_YEAR)
            {
                /* Ask the user for precipitation amount per month */
                cout << "\nEnter the amount of precipitation for month "
                    << months << ": ";
                cin >> dataMonths;

                /* Nested While loop::Check: While the user enters an
                   invalid amount for rainfall, this loop will iterate */
                while (dataMonths < 0)
                {
                    /* Explaining the error */
                    cout << "There was a problem with your data entry\n"
                         << "that caused this message to appear. Only\n"
                         << "positive values 0 and above are allowed\n";

                    /* Ask the user again */
                    cout << "\nEnter the amount of precipitation for month "
                        << months << ": ";
                    cin >> dataMonths;
                }

                /* Accumulating: The data entered by the user in rainfall,
                   precipitation total */           
                rainfall += dataMonths;
                precipitationTotal += dataMonths;

                /* If Statement: If the number of months is greater than
                   or equal to 12, and months equals 13 year total gets
                   the data of rainfall of months 1 through 12, rainfall
                   will be reset to 0, and year total accumulates the
                   rainfall for years 2 and up */
                if (months >= 12 || months == 13)
                {
                    yearTotal = rainfall;
                    rainfall = 0;       
                    yearTotal += rainfall;
                }

                /* Display: Information during data entry */
                cout << "The amount of precipitation in year " << years
                    << " month " << months
                    << " was: " << dataMonths << " inches\n";

                /* Display: To display months during data entry */
                months++;
            }

            /* Calculation: Average rainfall, Amount of months */
            averageRainfall = (precipitationTotal / MONTHS_IN_YEAR);
            monthsTotal = years * MONTHS_IN_YEAR;

            /* Display: Report for each year */
            cout << "\nThe total rainfall in year " << years
                 << " was: " << yearTotal << " inches.\n";
   
            cout << "\nThe total rainfall during year " << years
                << " was:\t" << setw(6) << right << precipitationTotal
                << " inches\n";
            cout << "The average rainfall during year " << years
                << " was:\t"
                << setw(6) << right << averageRainfall << " inches\n";
            }

        }

    /* Display: After the data is entered successfully, this short report
    will be displayed each year */
    cout << "\nThe total rainfall during " << monthsTotal << " months was: "
        << setw(6) << right << precipitationTotal << " inches\n";
    cout << "The average rainfall was:\t\t"
        << setw(7) << right << averageRainfall << " inches\n";

    pauseSystem();
    return 0;
}

No comments:

Post a Comment