/* Rain Or Shine - An amateur meteorologist wants to keep track of weather
conditions during the past year's three-month summer season and has
designated each day as either rainy ('R'), cloudy ('C'), or sunny ('S').
This program stores this information in a 3 x 30 array of characters,
where the row indicates the month (0 = June, 1 = July, 2 = August) and
the column indicates the day of the month. (No data is collected for the
31st of any month.)
The program begins by reading the weather data in from a file. Then it
creates a report that displays, for each month and for the whole three-
month period, how many days were rainy, how many were cloudy, and how
many were sunny.
It also reports which of the three months had the largestAmount number
of rainy days. */
#include "Utility.h"
/* Global Constant Variables: Months, Days */
const int G_CONST_MONTHS = 3;
const int G_CONST_DAYS = 30;
/* Prototypes: Get meteorologic data, Get count, Get month with most rain,
Display report */
void getMetData(char[][G_CONST_DAYS]);
void getCount(const char rainOrShine[][G_CONST_DAYS]);
int getMostR(const int[]);
void displayReport(const int[], const int, const int[], const int,
const int[], const int, const int);
int main()
{
char rainOrShine[G_CONST_MONTHS][G_CONST_DAYS] = { };
/* Call: getMetData */
getMetData(rainOrShine);
pauseSystem();
return 0;
}
/* **********************************************************
Definition: getMetData
This function accepts an array as its argument. It creates
a file stream object, reads the data from the file called
"RainOrShine.txt", processes it, and stores the values in
the array rainOrShine[][].
********************************************************** */
void getMetData(char rainOrShine[][G_CONST_DAYS])
{
/* Declare: File stream object */
ifstream metData;
/* Variables: Months, Days */
int months = 0,
days = 0;
/* Open: File "RainOrShine.txt" */
metData.open("RainOrShine.txt");
/* If the file was opened successfully, the data will be read into the
array, and getCount is called to process the data. If there is an
error, a message is displayed indicating possible sources of errors,
and the user is asked to exit the program. */
if (metData)
{
for (months = 0; months < G_CONST_MONTHS; months++)
{
for (days = 0; days < G_CONST_DAYS; days++)
{
metData >> rainOrShine[months][days];
}
}
/* Call: getCount */
getCount(rainOrShine);
}
else
{
cout << "An error occured. The file could not be opened and/or\n"
<< "read. Make sure the file exists, the filename is\n"
<< "'RainOrShine.txt', and that the file is not damaged, and\n"
<< "the file has not been moved from the program directory.\n"
<< "\nPress Enter to close this window ...\n";
}
/* Close: File "RainOrShine.txt" */
metData.close();
}
/* **********************************************************
Definition: getCount
This function accepts four arrays as argument:
* getMetData[][]
* frequencyR[]
* frequencyS[]
* frequencyC[]
It processes the raw data read into the array from the
file "RainOrShine.txt". It counts how many days are sunny,
rainy, and cloudy These values are then stored in one of
the three arrays. It then accumulates the total sunny,
rainy and cloudy days.
********************************************************** */
void getCount(const char rainOrShine[][G_CONST_DAYS])
{
/* Array variables: Frequency sunny days, Frequency rainy days,
Frequency cloudy days */
int frequencyS[G_CONST_MONTHS] = { },
frequencyR[G_CONST_MONTHS] = { },
frequencyC[G_CONST_MONTHS] = { };
/* Variables: Total sunny days, Total rainy days, Total cloudy days,
Months, Days (loop counters), Month name */
int totalS = 0,
totalR = 0,
totalC = 0,
days = 0,
months = 0,
monthName = 0;
/* The ternary operator determines how many days are sunny, how many
days are rainy and how many are cloudy. Once either 'S', 'R', or
'C' are found, the arrays frequencyS, frequencyR and frequencyC
increment by 1 and store the amount in their subscripts. */
for (months = 0; months < G_CONST_MONTHS; months++)
{
for (days = 0; days < G_CONST_DAYS; days++)
{
rainOrShine[months][days] == 'S' ? frequencyS[months] += 1 :
rainOrShine[months][days];
rainOrShine[months][days] == 'R' ? frequencyR[months] += 1 :
rainOrShine[months][days];
rainOrShine[months][days] == 'C' ? frequencyC[months] += 1 :
rainOrShine[months][days];
}
totalS += frequencyS[months];
totalR += frequencyR[months];
totalC += frequencyC[months];
}
/* Call: getMostR, displayReport */
monthName = getMostR(frequencyR);
displayReport(frequencyS, totalS, frequencyR, totalR,
frequencyC, totalC, monthName);
}
/* **********************************************************
Definition: getMostR
This function accepts the following array as argument:
* frequencyR[]
It determines the month with most rain and returns this
value.
********************************************************** */
int getMostR(const int frequencyR[])
{
/* Variables: Month name, Largest amount, Months (loop counter) */
int monthName = 0,
largestAmount = 0,
months = 0;
/* This loop determines which month was the one with most
rainy days. Once it is found, largestAmount gets frequencyR[],
and monthName gets the value of that month. */
for (months = 0; months < G_CONST_MONTHS; months++)
{
if (frequencyR[months] > largestAmount)
{
largestAmount = frequencyR[months];
monthName = months;
}
}
/* Return: monthName */
return monthName;
}
/* **********************************************************
Definition: displayReport
This function accepts the following arrays as arguments:
* frequencyS[]
* frequencyR[]
* frequencyC[]
It displays a summary for each month, showing how many
days were sunny, rainy and cloudy, a summary of all
months, and it reports which month had the most rain.
********************************************************** */
void displayReport(const int frequencyS[], const int totalS,
const int frequencyR[], const int totalR,
const int frequencyC[], const int totalC,
const int monthName)
{
/* Constant: Character name */
const int CHAR_NAME = 3;
const string mNamae[G_CONST_MONTHS] = { "June", "July", "August" };
char charName[CHAR_NAME] = { 'S', 'R', 'C' };
/* Variables: Months, Count (loop counters) */
int months = 0,
cnt = 0;
/* Display: The report */
cout << "\t\tMETEOROLOGIC REPORT\n"
<< "\t\t-------------------\n\n\n"
<< "MONTHLY SUMMARY:\n";
for (months = 0; months < G_CONST_MONTHS; months++)
{
cout << "\n" << mNamae[months] << ":\n"
<< "\n-------------------------------------------------\n";
cout << "Sunny Days: " << setw(3) << right
<< frequencyS[months] << "\t";
for (cnt = 0; cnt < frequencyS[months]; cnt++)
{
cout << " " << charName[0];
}
cout << "\n-------------------------------------------------\n";
cout << "Rainy Days: " << setw(3) << right
<< frequencyR[months] << "\t";
for (cnt = 0; cnt < frequencyR[months]; cnt++)
{
cout << " " << charName[1];
}
cout << "\n-------------------------------------------------\n";
cout << "Cloudy Days: " << setw(2) << right
<< frequencyC[months] << "\t";
for (cnt = 0; cnt < frequencyC[months]; cnt++)
{
cout << " " << charName[2];
}
cout << "\n-------------------------------------------------\n";
}
cout << "\n\nTHREE MONTH SUMMARY:\n"
<< "\n-------------------------------------------------"
<< "--------------------------------------------\n";
cout << "Sunny Days: " << setw(3) << right << totalS << "\t";
for (cnt = 0; cnt < totalS; cnt++)
{
cout << " " << charName[0];
}
cout << "\n-------------------------------------------------"
<< "--------------------------------------------\n";
cout << "Rainy Days: " << setw(3) << right << totalR << "\t";
for (cnt = 0; cnt < totalR; cnt++)
{
cout << " " << charName[1];
}
cout << "\n-------------------------------------------------"
<< "--------------------------------------------\n";
cout << "Cloudy Days: " << totalC << "\t";
for (cnt = 0; cnt < totalC; cnt++)
{
cout << " " << charName[2];
}
cout << "\n-------------------------------------------------"
<< "--------------------------------------------\n\n";
cout << "\nMONTH WITH MOST RAIN:\n\n" << mNamae[monthName] << "\n"
<< "-------------------------------------------------\n";
for (months = 0; months < 1; months++)
{
cout << "Rainy Days: " << frequencyR[monthName] << "\t";
for (cnt = 0; cnt < frequencyR[monthName]; cnt++)
{
cout << " " << charName[1];
}
cout << "\n-------------------------------------------------\n";
}
}
Tuesday, January 31, 2017
Programming Challenge 7.6 - Rain Or Shine
Example File: RainOrShine.txt
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment