Saturday, May 27, 2017

Programming Challenge 11.5 - Weather Statistics Modification

/* Weather Statistics Modification - This is a modification of Programming
    Challenge 11.04. It defines an enumerated data type with enumerators for
    the months. The program uses the enumerated type to step through the
    elements of the array. */

#include "Utility.h"

struct WeatherData
{   
    string monthNames;        /* The month names                        */
    double totalRainfall;    /* The total amount of rainfall        */
    double highTemp;            /* The highest measured temperature    */
    double lowTemp;            /* The lowest measured temperature    */
    double avgTempMonths;    /* The average monthly temperatures */
};

struct YearSummary
{
    double totalPpt;           /* Year total amount of precipitation          */
    double avgPpt;                /* Average yearly precipitation                  */
    double highestPpt;        /* Highest amount of rainfall                      */
    double lowestPpt;            /* Lowest amount of rainfall                      */   
    string mNameHighest;        /* Name of month with highest precipitation */
    string mNameLowest;        /* Name of month with lowest precipitation  */
    double avgTempYear;        /* Average yearly temperature                      */   
    double avgHighTemp;        /* Average yearly high temperature              */
    double avgLowTemp;        /* Average yearly low temperature              */
    double highestTemp;        /* Highest measured temperature                  */
    double lowestTemp;        /* Lowest measured temperature                  */
};

enum monthNames
{
    JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,
    AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
} mNames;

void getMetData(WeatherData [], int);
bool validateInput(WeatherData[], int);
void calcAvgTemps(WeatherData [], YearSummary &, int);
void calcPpt(WeatherData[], YearSummary &, int);
void getPptAmount(WeatherData[], YearSummary &, int);
void displayMetData(const WeatherData [], const YearSummary &, int);

int main()
{
    const int MONTHS = 12;

    /* Cast the first enumerator to int */
    int mCount = static_cast<monthNames>(mNames);

    /* Array of structures. The monthNames member is initialized. */
    WeatherData metData[MONTHS] = { { "JANUARY" }, { "FEBRUARY" }, { "MARCH" },
                                            { "APRIL" },   { "MAY" },      { "JUNE" },
                                            { "JULY" },    { "AUGUST" },   { "SEPTEMBER" },
                                            { "OCTOBER" }, { "NOVEMBER" }, { "DECEMBER" } };

    /* Structure variable definition, with all struct
       members initialized to default values */
    YearSummary yearly = { 0.0, 0.0, 0.0, 0.0, "\0", "\0", 0.0, 0.0, 0.0, 0.0, 0.0 };

    getMetData(metData, mCount);
    calcAvgTemps(metData, yearly, mCount);
    calcPpt(metData, yearly, mCount);
    getPptAmount(metData, yearly, mCount);
    displayMetData(metData, yearly, mCount);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getMetData
   
    This function uses an array of structs as parameter, to
    get the following information from the user:

        * The total amount of rainfall for each month
        * The highest measured temperature
        * The lowest measured temperature

    This information is then stored in the appropriate member
    variables.
   ********************************************************** */

void getMetData(WeatherData metData[], int mCount)
{
    bool valid = false;

    cout << "\n\tSTR Data Center - Data Entry System\n\n";

    for (; mCount <= DECEMBER; mCount++)
    {
        cout << "\n\tEnter data for " << metData[mCount].monthNames << "\n";
        cout << "\n";
        cout << "\t"                        << setw(31) << right << setfill('±')
              << "\n"                        << setfill(' ');

        cout << "\tTotal amount of rainfall" << setw(7) << right << ": ";
        cin >> metData[mCount].totalRainfall;

        valid = validateInput(metData, mCount);

        cout << "\tHighest measured temperature" << setw(3) << right << ": ";
        cin >> metData[mCount].highTemp;
       
        valid = validateInput(metData, mCount);

        cout << "\tLowest measured temperature" << setw(4) << right << ": ";
        cin >> metData[mCount].lowTemp;

        valid = validateInput(metData, mCount);
   
        cout << "\t" << setw(31) << right << setfill('±')
              << "\n" << setfill(' ') << "\n";
    }
}

/* **********************************************************
   Definition: validate Input

    This function accepts an array of structures as parameter.
    It validates the input.
   ********************************************************** */

bool validateInput(WeatherData metData[], int mCount)
{
    bool valid = false;

    while (metData[mCount].totalRainfall < 0)
    {
        cout << "\n\tInvalid input. The total amount of "
              << " rainfall can't be lower than 0.\n\n";

        cout << "\tTotal amount of rainfall" << setw(7) << right << ": ";
        cin >> metData[mCount].totalRainfall;
    }

    while (metData[mCount].highTemp < -100 ||
            metData[mCount].highTemp > 140)
    {
        cout << "\n\tTemperatures above +140F or below -100F "
            << "are not accepted\n\n";

        cout << "\tHighest measured temperature" << setw(3) << right << ": ";
        cin >> metData[mCount].highTemp;
    }

    while (metData[mCount].lowTemp < -100 ||
            metData[mCount].lowTemp > 140)
    {
        cout << "\n\tTemperatures below -100F or above +140F are "
              << "not accepted.\n\n";

        cout << "\tLowest measured temperature" << setw(3) << right << ": ";
        cin >> metData[mCount].lowTemp;
    }

    return valid;
}

/* **********************************************************
    Definition: calcAvgTemps

    This function uses an array of structs and a YearSummary
    reference variable, to calculate:
   
        * The monthly average temperatures
        * The yearly total average temperature of all average
          temperatures (Are you confuzzled now? ;-))
       * The highest average temperature
        * The lowest average temperature

    The results are stored in the appropriate struct members.
   ********************************************************** */

void calcAvgTemps(WeatherData metData[], YearSummary &yearly, int mCount)
{
    for (; mCount <= DECEMBER; mCount++)
    {
        /* Calculates the monthly average temperatures */
        metData[mCount].avgTempMonths = (metData[mCount].highTemp +
                                                      metData[mCount].lowTemp) / 2;

        yearly.avgTempYear += metData[mCount].lowTemp +
                                     metData[mCount].highTemp;
        yearly.avgHighTemp += metData[mCount].highTemp / 12;
        yearly.avgLowTemp  += metData[mCount].lowTemp  / 12;
    }

    yearly.avgTempYear /= 24;
}

/* **********************************************************
    Definition: calcPpt

   This function uses an array of structs and a YearSummary
    reference variable, to calculate the total and average
    precipitation amounts for the whole year.
   ********************************************************** */

void calcPpt(WeatherData metData[], YearSummary &yearly, int mCount)
{
    for (; mCount <= DECEMBER; mCount++)
    {
        yearly.totalPpt += metData[mCount].totalRainfall;
        yearly.avgPpt = yearly.totalPpt / 12;
    }
}

/* **********************************************************
   Definition: getPptAmount

    This function uses an array of structs and a YearSummary
    reference variable, to determine the month with the lowest
    and highest amounts of rainfall. The month names and the
   amounts of precipitation are stored in their appropriate
    member variables.
   ********************************************************** */

void getPptAmount(WeatherData metData[], YearSummary &yearly, int mCount)
{
    yearly.highestPpt = metData[0].totalRainfall;
    yearly.lowestPpt =  metData[0].totalRainfall;

    for (; mCount <= DECEMBER; mCount++)
    {
        if (metData[mCount].totalRainfall <= yearly.lowestPpt)
        {
            yearly.lowestPpt = metData[mCount].totalRainfall;
            yearly.mNameLowest = metData[mCount].monthNames;
        }

        if (metData[mCount].totalRainfall >= yearly.highestPpt)
        {
            yearly.highestPpt = metData[mCount].totalRainfall;
            yearly.mNameHighest = metData[mCount].monthNames;
        }
    }
}

/* **********************************************************
   Definition: displayMetData

    This function uses an array of structs and a const
    reference parameter to display their contents.
   ********************************************************** */

void displayMetData(const WeatherData metData[], const YearSummary &yearly,
                          int mCount)
{
    cout << "\n\n\tSTR Data Center - Weather Statistic Summary 2016\n\n\n\n\t";

    cout << setw(9)  << left  << "MONTH NAME"
          << setw(19) << right << "Rainfall Amount"
          << setw(20) << right << "Highest Temp"
          << setw(20) << right << "Lowest Temp"
          << setw(20) << right << "Average Temp" << "\n";

    cout << "\t" << setw(90) << right << setfill('±')
          << "\n"                             << setfill(' ');
   
    cout << fixed << showpoint << setprecision(2);

    for (; mCount <= DECEMBER; mCount++)
    {
        cout << "\t" << setw(9)  << left  << metData[mCount].monthNames
                         << setw(20) << right << metData[mCount].totalRainfall
                         << setw(20) << right << metData[mCount].highTemp
                         << setw(20) << right << metData[mCount].lowTemp
                         << setw(20) << right << metData[mCount].avgTempMonths << "\n";
    }

    cout << "\t" << setw(90) << right << setfill('±')
          << "\n"                             << setfill(' ');

    cout << "\n\n\tSTR Data Center - Additional Weather Statistic "
              "Information\n\n";

    cout << "\tThe total amount of rainfall was: "
          << setw(25) << right << yearly.totalPpt << " mm"
          << "\n\tThe average amount of rainfall was: "   
          << setw(23) << right << yearly.avgPpt << " mm"
          << "\n\n\tThe month with the highest amount of rainfall was: "
         << setw(10) << right << yearly.mNameHighest
          << "\n\tThe highest measured amount of rainfall was:  "              
          << setw(13) << right << yearly.highestPpt << " mm"
          << "\n\n\tThe month with the lowest amount of rainfall was: " 
          << setw(11) << right << yearly.mNameLowest
          << "\n\tThe lowest measured amount of rainfall was: "
          << setw(15) << right << yearly.lowestPpt << " mm" << "\n";
   
    cout << "\n\tThe average of all monthly average temperatures was: "      
          << setw(6) << right << yearly.avgTempYear << "  F"
          << "\n\tThe highest average yearly temperature was: "
          << setw(15) << right << yearly.avgHighTemp << "  F"
          << "\n\tThe lowest average yearly temperature was: " 
          << setw(16) << right << yearly.avgLowTemp  << "  F" << "\n";
}


Example Output:




No comments:

Post a Comment