Sunday, January 22, 2017

Programming Challenge 7.2 - Rainfall Statistics

/* Rainfall Statistics - This program lets the user enter the total
   rainfall for each of 12 months into an array of doubles. The program
   calculates and displays:
  
      * The total rainfall for the year
      * The average monthly rainfall
      * The months with the highest and lowest amounts
     
   Input validation: No negative numbers are accepted for monthly
   rainfall figures. */

#include "Utility.h"

/* Prototypes: Get rainfall data, Get month with highest rainfall, Get
               month with lowest rainfall, Calculate monthly average
               rainfall,  Get total rainfall, Show data */
void getRainfallData(double[], int, string[]);
double getHighest(const double[], int, int &);
double getLowest(const double[], int, int &);
double calcMonthlyAverage(const double[], int);
double getTotalRainfall(const double[], int);
void showData(double, double, double, double, string [], int, int);

int main()
{
   /* Constant: Number of months */
   const int NUM_MONTHS = 12;

   /* Variable: Month names */
   string monthNames[NUM_MONTHS] = { "January", "February", "March",
                                     "April", "May", "June", "July",
                                     "August", "September", "October",
                                     "November", "December" };

   /* Variables: Amount of rainfall, Month with highest rainfall,
                 Month with lowest rainfall, Monthly average,
                 Total amount of rainfall */
   double rainfallAmount[NUM_MONTHS],
          highestRainfall = 0.0,
          lowestRainfall = 0.0,
          monthlyAverage = 0.0,
          totalRainfall = 0.0;

   /* Variable: Name highest, Name lowest */
   int nameHighest = 0,
       nameLowest = 0;

   /* Display: Header */
   cout << "\t\tSTR Data Center: Annual Rainfall Storage\n\n"
        << "Enter the precipitation data for the past 12 months\n"
        << "to get information about the following:\n\n"
        << "\t* The month with the highest amount of rainfall\n"
        << "\t* The month with the lowest amount of rainfall\n"
        << "\t* The average amount of rainfall of months total\n"
        << "\t* The total annual amount of rainfall.\n\n";

   /* Call: getRainfallData, getHighest, getLowest,
            calcMonthlyAverage, getTotalRainfall,
            showData */
   getRainfallData(rainfallAmount, NUM_MONTHS, monthNames);

   highestRainfall = getHighest(rainfallAmount, NUM_MONTHS,
                                nameHighest);

   lowestRainfall = getLowest(rainfallAmount, NUM_MONTHS, nameLowest);

   monthlyAverage = calcMonthlyAverage(rainfallAmount, NUM_MONTHS);
   totalRainfall = getTotalRainfall(rainfallAmount, NUM_MONTHS);

   showData(highestRainfall, lowestRainfall, monthlyAverage,
            totalRainfall, monthNames, nameHighest, nameLowest);
  
   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getRainfallData

   This function accepts two arrays and their sizes as an
   argument. One for the rainfall data, the other as an aid
   during data entry, containing the months names.
   It gets the amount of rainfall for each month of the year
   as input from the user and stores the values in the array.
   ********************************************************** */

void getRainfallData(double rainfall[], int months,
                     string monthNames[])
{
   /* Get: The rainfall amount for each month from the user
      Validate: Input */
   cout << "Enter the amount of rainfall for each month:\n\n";

   for (int counter = 0; counter < months; counter++)
   {
      cout << "Data for "
         << setw(9) << left << (monthNames[counter])
         << setw(5) << right << ": " << right;
      cin >> rainfall[counter];

      while (rainfall[counter] < 0)
      {
         cout << "Input Failure: The amount of rainfall can not "
              << "be negative.\n"
              << "Data for "
            << setw(9) << left << (monthNames[counter])
            << setw(5) << right << ": " << right;
         cin >> rainfall[counter];
      }
   }
}

/* **********************************************************
   Definition: getHighest

   This function accepts an array and its size as arguments.
   It determines the month with the highest rainfall and
   returns the value.
   ********************************************************** */

double getHighest(const double rainfall[], int months,
                  int &nameHighest)
{
   /* Variable: Highest rainfall amount (gets rainfall[0] as a
                first value to compare the amount of rainfall
                to) */
   double highestAmount = rainfall[0];

   /* Determine: The highest amount of rainfall */
   for (int counter = 0; counter < months; counter++)
   {
      /* When the highest amount is found, highestAmount
         gets rainfall[counter] and nameHighest gets the
         counter value of the month with the highest amount
         of rainfall */
      if (rainfall[counter] > highestAmount)
      {
         highestAmount = rainfall[counter];
         nameHighest = counter;
      }
   }

   /* Return: highestAmount */
   return highestAmount;
}

/* **********************************************************
   Definition: getLowest

   This function accepts an array and its size as arguments.
   It determines the month with the lowest rainfall and
   returns the value.
   ********************************************************** */

double getLowest(const double rainfall[], int months,
                 int &nameLowest)
{
   /* Variable: Lowest rainfall amount (gets rainfall[0] as a
                first value to compare the amount of rainfall
                to) */
   double lowestAmount = rainfall[0];

   /* Determine: The highest amount of rainfall */
   for (int counter = 0; counter < months; counter++)
   {
      /* When the lowest amount is found, lowestAmount
         gets rainfall[counter] and nameLowest gets the
         counter value of the month with the lowest amount
         of rainfall */
      if (rainfall[counter] < lowestAmount)
      {
         lowestAmount = rainfall[counter];
         nameLowest = counter;
      }
   }

   /* Return: highestAmount */
   return lowestAmount;
}

/* **********************************************************
   Definition: getTotalRainfall

   This function accepts an array and its size as arguments.
   It calculates the total rainfall and returns the value.
   ********************************************************** */

double getTotalRainfall(const double rainfall[], int months)
{
   /* Variable: Total rainfall (accumulator) */
   double totalRainfall = 0.0;

   /* Get: The total annual amount of rainfall */
   for (int total = 0; total < months; total++)
   {
      totalRainfall += rainfall[total];
   }

   /* Return: totalRainfall */
   return totalRainfall;
}

/* **********************************************************
   Definition: calcMonthlyAverage

   This function accepts an array and its size as arguments.
   It calculates the monthly average amount of rainfall, and
   returns the value.
   ********************************************************** */

double calcMonthlyAverage(const double avgRainfall[], int months)
{
   /* Variable: Monthly average rainfall (accumulator) */
   double monthlyAverage = 0.0;

   /* Calculate: The average monthly amount of rainfall */
   for (int count = 0; count < months; count++)
   {
      monthlyAverage += avgRainfall[count] / months;
   }

   /* Return: monthlyAverage */
   return monthlyAverage;
}

/* **********************************************************
   Definition: showData

   This function accepts five double values containing as
   arguments:

      * Month with highest rainfall
      * Month with lowest rainfall
      * Average monthly rainfall
      * Total rainfall

   Two int values:

      * The value for the name of the month with highest
        rainfall
      * The value for the name of the month with lowest
        rainfall

   An array of string objects and its size, containing the
   names of the 12 months.
  
   It displays the data entered by the user.
   ********************************************************** */

void showData(double monthHighest, double lowestRainfall,
              double monthlyAverage, double totalRainfall,
              string monthNames[], int nameHighest, int nameLowest)
{
   /* Set up: Numeric output formatting */
   cout << fixed << showpoint << setprecision(2);

   cout << "\n\t\tSTR Data Center - Rainfall Data 2016\n\n"
        << "The month with the highest rainfall was "
        << monthNames[nameHighest] << ".\n"
        << "The amount of rainfall was: "
        << monthHighest << " inches.\n\n"
        << "The Month with lowest rainfall was "
        << monthNames[nameLowest] << ".\n"
        << "The amount of rainfall was: "
        << lowestRainfall << " inches.\n\n"
        << "The monthly average rainfall was "
        << monthlyAverage << " inches.\n"
        << "The yearly total rainfall was "
        << totalRainfall << " inches.\n";
}

Example Output: 



No comments:

Post a Comment