Tuesday, March 7, 2017

Programming Challenge 8.5 - Rainfall Statistics Modification

/* Rainfall Statistics Modification - 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.
 
   This program is a modification of Programming Challenge 7.2. It
   displays a list of months, sorted in order of rainfall, from highest
   to lowest. */

#include "Utility.h"

/* Function prototypes */
void displayHeader();
void getRainfallData(double[], const string[], int);
double getTotalAmount(const double[], int);
double getHighestAmount(const double[], const string[], string &, int);
double getLowestAmount(const double[], const string[], string &, int);
double getAverageAmount(const double[], int);
void displayRainfallData(const double[], const double, const double,
                         const string, const double, const string,
                         const double, int);
void sortRainfallData(double[], string[], int);
void displaySorted(const double[], const string[], int);


int main()
{
   const int NUM_MONTHS = 12;

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

   /* Array to hold the amount of rainfall for each month */
   double rainfall[NUM_MONTHS] = { 0.0 };

   double highestAmount = 0.0,
          lowestAmount = 0.0,
          monthlyAverage = 0.0,
          totalAmount = 0.0;

   string monthHighest = " ",
          monthLowest = " ";

   /* Displays the program header */
   displayHeader();

   /* Gets rainfall data */
   getRainfallData(rainfall, monthNames, NUM_MONTHS);

   /* Calculates the total amount of rainfall */
   totalAmount = getTotalAmount(rainfall, NUM_MONTHS);

   /* Gets month and month name with highest amount of rainfall */
   highestAmount = getHighestAmount(rainfall, monthNames, monthHighest,
                                    NUM_MONTHS);

   /* Gets month and month name with lowest amount of rainfall */
   lowestAmount = getLowestAmount(rainfall, monthNames, monthLowest,
                                  NUM_MONTHS);

   /* Gets the monthly average over a 12 month period */
   monthlyAverage = getAverageAmount(rainfall, NUM_MONTHS);

   /* Displays the data in formatted order */
   displayRainfallData(rainfall, totalAmount, highestAmount, monthHighest,
                       lowestAmount, monthLowest, monthlyAverage, NUM_MONTHS);

   /* Uses selection sort to sort and order rainfall and monthNames */
   sortRainfallData(rainfall, monthNames, NUM_MONTHS);

   /* Displays the sorted list */
   displaySorted(rainfall, monthNames, NUM_MONTHS);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayHeader

   Displays the program header along with information about
   the program.
   ********************************************************** */

void displayHeader()
{
   cout << "\t\tSTR Data Center: Annual Rainfall Statistics\n\n"
        << "\t\tEnter the precipitation data for the past 12 months\n"
        << "\t\tto get information about the following:\n\n"
        << "\t\t   * The month with the highest amount of rainfall\n"
        << "\t\t   * The month with the lowest amount of rainfall\n"
        << "\t\t   * The average amount of rainfall of months total\n"
        << "\t\t   * The total annual amount of rainfall.\n"
        << "\t\t   * Precipitation data displayed and sorted in order\n"
        << "\t\t     from highest to lowest amount of rainfall.\n\n";
}

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

   This function accepts rainfall, monthNames and NUM_MONTHS,
   containing the size of elements stored in the arrays, as
   its argument.

   It gets and stores the amount of precipitation for each
   month in a 12 month period and stores this information in
   rainfall.
   ********************************************************** */

void getRainfallData(double rainfall[], const string monthNames[],
                     int NUM_MONTHS)
{
   cout << "\n\t\tEnter the amount of rainfall for each month:\n\n";

   for (int index = 0; index < NUM_MONTHS; index++)
   {
      cout << "\t\tData for "
           << setw(9) << left << monthNames[index]
           << setw(5) << right << ": ";
      cin >> rainfall[index];

      /* Verify input */
      while (rainfall[index] < 0.0)
      {
         cout << "\n\t\tInput failure!\n"
              << "\n\t\tData for "
              << setw(9) << left << monthNames[index]
              << setw(5) << right << ": ";
         cin >> rainfall[index];
      }
   }
}

/* **********************************************************
   Definition: getTotalAmount

   This function accepts rainfall and NUM_MONTHS containing
   the size of the elements in the array as its arguments. It
   calculates and returns the total amount of rainfall over a
   12 month period.
   ********************************************************** */

double getTotalAmount(const double rainfall[], int NUM_MONTHS)
{
   double totalAmount = 0.0;

   for (int index = 0; index < NUM_MONTHS; index++)
   {
      totalAmount += rainfall[index];
   }

   return totalAmount;
}

/* **********************************************************
   Definition: getHighestAmount

   This function accepts rainfall and monthName arrays as its
   argument. It determines the month with the highest amount
   of rainfall and returns this value.
   ********************************************************** */

double getHighestAmount(const double rainfall[], const string monthNames[],
                        string &monthHighest, int NUM_MONTHS)
{
   double highestAmount = rainfall[0];

   /* Determine the highest amount of rainfall */
   for (int index = 0; index < NUM_MONTHS; index++)
   {
      if (rainfall[index] > highestAmount)
      {
         highestAmount = rainfall[index];
         monthHighest = monthNames[index];
      }
   }

   return highestAmount;
}

/* **********************************************************
   Definition: getLowestAmount

   This function accepts rainfall and monthName arrays as its
   argument. It determines the month with the lowest amount
   of rainfall and returns this value.
   ********************************************************** */

double getLowestAmount(const double rainfall[], const string monthNames[],
                       string &monthLowest, int NUM_MONTHS)
{
   double lowestAmount = rainfall[0];

   /* Determine the highest amount of rainfall */
   for (int index = 0; index < NUM_MONTHS; index++)
   {
      if (rainfall[index] < lowestAmount)
      {
         lowestAmount = rainfall[index];
         monthLowest = monthNames[index];
      }
   }

   return lowestAmount;
}

/* **********************************************************
   Definition: getAverageAmount

   This function accepts rainfall and NUM_MONTHS containing
   the number of elements of the array as its argument. It
   calculates the average amount of rainfall and returns the
   value.
   ********************************************************** */

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

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

   return monthlyAverage;
}

/* **********************************************************
   Definition: displayRainfallData

   This function displays the following items:

      * Month and month name with highest amount of rainfall
      * Month and month name with lowest amount of rainfall
      * Average amount of rainfall over a 12 month period
      * Total amount of rainfall over a 12 month period
   ********************************************************** */

void displayRainfallData(const double rainfall[], const double totalAmount,
                         const double highestAmount, const string monthHighest,
                         const double lowestAmount, const string monthLowest,
                         const double monthlyAverage, int NUM_MONTHS)
{
      /* Set up: Numeric output formatting */
   cout << fixed << showpoint << setprecision(2);

   cout << "\n\n\t\tSTR Data Center - Rainfall Statistic Data 2016\n\n"
        << "\t\tThe month with the highest rainfall was "
        << monthHighest << ".\n"
        << "\t\tThe amount of rainfall was: "
        << highestAmount << " inches.\n\n"
        << "\t\tThe Month with lowest rainfall was "
        << monthLowest << ".\n"
        << "\t\tThe amount of rainfall was: "
        << lowestAmount << " inches.\n\n"
        << "\t\tThe monthly average rainfall was "
        << monthlyAverage << " inches.\n\n"
        << "\t\tThe yearly total rainfall was "
        << totalAmount << " inches.\n\n";
}

/* **********************************************************
   Definition: sortRainfallData

   This function accepts rainfall and monthNames as arguments
   on which it performs an descending selection sort.
   ********************************************************** */

void sortRainfallData(double rainfall[], string monthNames[],
                      int NUM_MONTHS)
{
   int startScan = 0,
       maxIndex = 0,
       index = 0;

   string tempMonths = " ";
   double maxValue = 0.0;

   for (startScan = 0; startScan < (NUM_MONTHS - 1); startScan++)
   {
      maxIndex = startScan;
      maxValue = rainfall[startScan];
      tempMonths = monthNames[startScan];

      for (index = startScan + 1; index < NUM_MONTHS; index++)
      {
         if (rainfall[index] > maxValue)
         {
            maxValue = rainfall[index];
            tempMonths = monthNames[index];
            maxIndex = index;
         }
      }

      rainfall[maxIndex] = rainfall[startScan];
      monthNames[maxIndex] = monthNames[startScan];
      rainfall[startScan] = maxValue;
      monthNames[startScan] = tempMonths;
   }
}

/* **********************************************************
   Definition: displaySorted

   This function accepts rainfall and monthNames arrays as
   arguments. The function displays a header and the sorted
   list of rainfall data and month names in descending order.
   ********************************************************** */

void displaySorted(const double rainfall[], const string monthNames[],
                   int NUM_MONTHS)
{
   cout << "\n\t\tStatistics In Descending Order\n"
        << "\t\t(Highest -> Lowest Amount Of Rainfall)\n\n"
        << "\t\tMonth name\tAmount Rainfall\n";

   cout << fixed << showpoint << setprecision(2);

   for (int index = 0; index < NUM_MONTHS; index++)
   {
      cout << "\n\t\t" << setw(9) << left << monthNames[index] << ": "
                       << setw(19) << right << rainfall[index] << setw(5) << right;
   }

   cout << "\n";
}

Example Output:




No comments:

Post a Comment