/* Weather Statistics - This program uses a structure to store the following
weather data for a particular month:
* Total Rainfall
* High Temperature
* Low Temperature
* Average Temperature
The program has an array of 12 structures to hold weather data for an
entire year. When the program runs, the user is asked to enter data for
each month. (The average temperature is calculated.) Once the data are
entered for all the months, the program calculates and displays the
average monthly rainfall, the total rainfall for the year, the highest
and lowest temperatures for the year (and the months they occurred in),
and the average of all the monthly average temperatures.
Input Validation: Only temperatures within the range between -100 and
+140 degrees Fahrenheit are accepted. */
#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 */
};
void getMetData(WeatherData [], const int);
void calcAvgTemps(WeatherData [], YearSummary &, const int);
void calcPpt(WeatherData[], YearSummary &, const int);
void getPptAmount(WeatherData[], YearSummary &, const int);
void displayMetData(const WeatherData [], const YearSummary &, const int);
int main()
{
const int MONTHS = 12;
/* 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, MONTHS);
calcAvgTemps(metData, yearly, MONTHS);
calcPpt(metData, yearly, MONTHS);
getPptAmount(metData, yearly, MONTHS);
displayMetData(metData, yearly, MONTHS);
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[], const int MONTHS)
{
cout << "\n\tSTR Data Center - Data Entry System\n\n";
for (int count = 0; count < MONTHS; count++)
{
cout << "\n\tEnter data for " << metData[count].monthNames << "\n";
cout << "\t" << setw(31) << right << setfill('±')
<< "\n" << setfill(' ');
cout << "\tTotal amount of rainfall" << setw(7) << right << ": ";
cin >> metData[count].totalRainfall;
while (metData[count].totalRainfall < 0)
{
cout << "\tInvalid input. The total amount of rainfall can't be "
<< "lower than 0.\n";
cout << "\tTotal amount of rainfall" << setw(7) << right << ": ";
cin >> metData[count].totalRainfall;
}
cout << "\tHighest measured temperature" << setw(3) << right << ": ";
cin >> metData[count].highTemp;
while (metData[count].highTemp < -100 || metData[count].highTemp > 140)
{
cout << "\n\tTemperatures above +140F or below -100F "
<< "are not accepted\n\n";
cout << "\tHighest measured temperature" << setw(3) << right << ": ";
cin >> metData[count].highTemp;
}
cout << "\tLowest measured temperature" << setw(4) << right << ": ";
cin >> metData[count].lowTemp;
while (metData[count].lowTemp < -100 || metData[count].lowTemp > 140)
{
cout << "\n\tTemperatures below -100F or above +140F are "
<< "not accepted.\n\n";
cout << "\tLowest measured temperature" << setw(3) << right << ": ";
cin >> metData[count].lowTemp;
}
cout << "\t" << setw(31) << right << setfill('±')
<< "\n" << setfill(' ') << "\n";
}
}
/* **********************************************************
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, const int MONTHS)
{
for (int index = 0; index < MONTHS; index++)
{
/* Calculates the monthly average temperatures */
metData[index].avgTempMonths = (metData[index].highTemp +
metData[index].lowTemp) / 2;
yearly.avgTempYear += metData[index].lowTemp +
metData[index].highTemp;
yearly.avgHighTemp += metData[index].highTemp / 12;
yearly.avgLowTemp += metData[index].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, const int MONTHS)
{
for (int index = 0; index < MONTHS; index++)
{
yearly.totalPpt += metData[index].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, const int MONTHS)
{
yearly.highestPpt = metData[0].totalRainfall;
yearly.lowestPpt = metData[0].totalRainfall;
for (int index = 0; index < MONTHS; index++)
{
if (metData[index].totalRainfall <= yearly.lowestPpt)
{
yearly.lowestPpt = metData[index].totalRainfall;
yearly.mNameLowest = metData[index].monthNames;
}
if (metData[index].totalRainfall >= yearly.highestPpt)
{
yearly.highestPpt = metData[index].totalRainfall;
yearly.mNameHighest = metData[index].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,
const int MONTHS)
{
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 (int count = 0; count < MONTHS; count++)
{
cout << "\t" << setw(9) << left << metData[count].monthNames
<< setw(20) << right << metData[count].totalRainfall
<< setw(20) << right << metData[count].highTemp
<< setw(20) << right << metData[count].lowTemp
<< setw(20) << right << metData[count].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";
}
Thursday, May 25, 2017
Programming Challenge 11.4 - Weather Statistics
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment