/* Rainfall Statistics - This program lets the user enter the totalrainfall for each of 12 months into an array of doubles. The programcalculates and displays:* The total rainfall for the year* The average monthly rainfall* The months with the highest and lowest amountsInput validation: No negative numbers are accepted for monthlyrainfall figures. */#include "Utility.h"/* Prototypes: Get rainfall data, Get month with highest rainfall, Getmonth with lowest rainfall, Calculate monthly averagerainfall, 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: getRainfallDataThis function accepts two arrays and their sizes as anargument. One for the rainfall data, the other as an aidduring data entry, containing the months names.It gets the amount of rainfall for each month of the yearas 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 userValidate: 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: getHighestThis function accepts an array and its size as arguments.It determines the month with the highest rainfall andreturns the value.********************************************************** */double getHighest(const double rainfall[], int months,int &nameHighest){/* Variable: Highest rainfall amount (gets rainfall[0] as afirst value to compare the amount of rainfallto) */double highestAmount = rainfall[0];/* Determine: The highest amount of rainfall */for (int counter = 0; counter < months; counter++){/* When the highest amount is found, highestAmountgets rainfall[counter] and nameHighest gets thecounter value of the month with the highest amountof rainfall */if (rainfall[counter] > highestAmount){highestAmount = rainfall[counter];nameHighest = counter;}}/* Return: highestAmount */return highestAmount;}/* **********************************************************Definition: getLowestThis function accepts an array and its size as arguments.It determines the month with the lowest rainfall andreturns the value.********************************************************** */double getLowest(const double rainfall[], int months,int &nameLowest){/* Variable: Lowest rainfall amount (gets rainfall[0] as afirst value to compare the amount of rainfallto) */double lowestAmount = rainfall[0];/* Determine: The highest amount of rainfall */for (int counter = 0; counter < months; counter++){/* When the lowest amount is found, lowestAmountgets rainfall[counter] and nameLowest gets thecounter value of the month with the lowest amountof rainfall */if (rainfall[counter] < lowestAmount){lowestAmount = rainfall[counter];nameLowest = counter;}}/* Return: highestAmount */return lowestAmount;}/* **********************************************************Definition: getTotalRainfallThis 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: calcMonthlyAverageThis function accepts an array and its size as arguments.It calculates the monthly average amount of rainfall, andreturns 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: showDataThis function accepts five double values containing asarguments:* Month with highest rainfall* Month with lowest rainfall* Average monthly rainfall* Total rainfallTwo int values:* The value for the name of the month with highestrainfall* The value for the name of the month with lowestrainfallAn array of string objects and its size, containing thenames 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";}
Sunday, January 22, 2017
Programming Challenge 7.2 - Rainfall Statistics
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment