/* Monkey Business - A local zoo wants to keep track of how many pounds
of food each of its three monkeys eats each day during a typical week.
This program stores this information in a two-dimensional 3 x 5 array,
where each row represents a different monkey and each column represents
a different day of the week.
First the user has to input the data for each monkey. Then the program
creates a report that includes the following information:
* The average amount of food eaten per day by the whole family of
monkeys
* The least amount of food eaten during the week by any one monkey
* The greatest amount of food eaten during the week by any one monkey
Input Validation: No negative numbers for pounds of food eaten are
accepted. */
#include "Utility.h"
/* Constants: Global constant number of monkeys,
Global constant number of weekdays */
const int G_CONST_MONKEYS = 3;
const int G_CONST_DAYS = 5;
/* Prototypes: Get pounds, Get average amount, Get largest amount,
Get smallest amount, Show data */
void getPounds(double[][G_CONST_DAYS], const string[], const string[]);
void getAverageAmount(double[G_CONST_DAYS], const double[][G_CONST_DAYS]);
void getLargestAmount(double [G_CONST_DAYS], const double [][G_CONST_DAYS]);
void getSmallestAmount(double [G_CONST_DAYS], const double [][G_CONST_DAYS]);
void showData(const double[][G_CONST_DAYS], const double[], const double[],
const double [], const string[], const string[]);
int main()
{
/* Array variables: Monkey names, Day names, Pounds eaten,
Average eaten, Largest, Smallest */
const string monkeyNames[G_CONST_MONKEYS] = { "Sora", "Yuki", "Hana" };
const string dayNames[G_CONST_DAYS] = { "Mon", "Tue", "Wed", "Thu", "Fri" };
double poundsEaten[G_CONST_MONKEYS][G_CONST_DAYS] = { {0.0} };
double averageEaten[G_CONST_DAYS] = { 0.0 };
double largest[G_CONST_DAYS] = { 0.0 };
double smallest[G_CONST_DAYS] = { 0.0 };
/* Display: Introduction */
cout << "\t\tMirano Zoo - Monkey Valley - Feeding Statistics\n\n";
/* Call: getPounds, getAverage, getLarestAmount, getSmallestAmount,
showData */
getPounds(poundsEaten, dayNames, monkeyNames);
getAverageAmount(averageEaten, poundsEaten);
getLargestAmount(largest, poundsEaten);
getSmallestAmount(smallest, poundsEaten);
showData(poundsEaten, averageEaten, largest,
smallest, dayNames, monkeyNames);
pauseSystem();
return 0;
}
/* **********************************************************
Defintion: getPounds
This function accepts three arrays:
* poundsEaten[][]
* dayNames[]
* monkeyNames[]
It collects the amount eaten by each monkey on every day
of the week. The result is stored in poundsEaten[][].
********************************************************** */
void getPounds(double poundsEaten[][G_CONST_DAYS], const string dayNames[],
const string monkeyNames[])
{
/* Variables: Input control, Days, Monkeys (loop counters) */
int inpCtrl = 0,
days = 0,
monkeys = 0;
/* Get: Amount eaten by any one monkey on any one weekday */
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
{
cout << "" << monkeyNames[monkeys] << ":\n";
for (days = 0; days < G_CONST_DAYS; days++)
{
cout << "" << dayNames[days] << "\t" << " pounds: ";
cin >> poundsEaten[monkeys][days];
/* This loop takes care of the input validation. If the user
enters a value lower than or equal to null, he or she is
informed about this failure, and asked to repeat the input
as many times as necessary until a positive value is entered */
for (inpCtrl = 0; poundsEaten[monkeys][days] <= 0; inpCtrl++)
{
cout << "\nInput Failure: The amount of pounds eaten you entered\n"
<< "was negative and can not be accepted. All input has to\n"
<< "be positive. Ex.: 0.2, 1.25 ...\n"
<< "" << dayNames[days] << "\t" << " pounds: ";
cin >> poundsEaten[monkeys][days];
}
}
cout << endl;
}
}
/* **********************************************************
Definition: getAverageAmount
This function accepts two arrays as arguments:
* average[]
* poundsEaten[][]
It finds and stores the average amount eaten by the family
of monkeys during the week, and stores the found values in
average[].
********************************************************** */
void getAverageAmount(double average[G_CONST_DAYS],
const double poundsEaten[][G_CONST_DAYS])
{
/* Variable: Total (accumulator) */
double total = 0.0;
/* Variables: Days, Monkeys (loop counters) */
int days = 0,
monkeys = 0;
for (days = 0; days < G_CONST_DAYS; days++)
{
/* Reset: average[], total */
average[days] = 0.0;
total = 0.0;
/* The total amount of food for each day of the week is
accumulated by total, and average[] determines and
stores the average for all three monkeys for each
day of the week */
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
{
total += poundsEaten[monkeys][days];
average[days] = total / G_CONST_MONKEYS;
}
}
}
/* **********************************************************
Definition: getLargestAmount
This function accepts two arrays as arguments:
* largest[]
* poundsEaten[][]
It determines the largest amount eaten by each monkey and
stores the result in the array.
********************************************************** */
void getLargestAmount(double largest[G_CONST_DAYS],
const double poundsEaten[][G_CONST_DAYS])
{
/* Variable: larger (accumulator) */
double larger = poundsEaten[G_CONST_MONKEYS][0];
/* Variables: Days, Monkeys (loop counters) */
int days = 0,
monkeys = 0;
/* These loops determine the largest amount eaten by
each monkey during the whole week */
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
{
/* Reset: larger */
larger = 0.0;
/* When a value larger than poundsEaten[][0] is found,
larger gets this value and passes it to largest[monkeys]
which stores the three largest amounts eaten by each
monkey during the whole week */
for (days = 0; days < G_CONST_DAYS; days++)
{
if (larger < poundsEaten[monkeys][days])
larger = poundsEaten[monkeys][days];
largest[monkeys] = larger;
}
}
}
/* **********************************************************
Definition: getSmallestAmount
This function accepts two arrays as arguments:
* smallest[]
* poundsEaten[][]
It determines the smallest amount eaten by each monkey and
stores the result in the array.
********************************************************** */
void getSmallestAmount(double smallest[G_CONST_DAYS],
const double poundsEaten[][G_CONST_DAYS])
{
/* Variable: smaller (initialized to poundsEaten[][0]) */
double smaller = poundsEaten[G_CONST_MONKEYS][0];
/* Variables: Days, Monkeys (loop counters) */
int days = 0,
monkeys = 0;
/* These loops determine the smallest amount eaten by
each monkey during the whole week */
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
{
/* Variable: smaller (accumulator) */
double smaller = poundsEaten[G_CONST_MONKEYS][0];
/* Reset: smaller */
smaller = poundsEaten[monkeys][0];
/* When a value smaller than poundsEaten[][0] is found,
smaller gets this value and passes it to smallest[days]
which stores the three smallest amounts eaten by each
monkey during the whole week. */
for (days = 0; days < G_CONST_DAYS; days++)
{
if (poundsEaten[monkeys][days] < smaller)
smaller = poundsEaten[monkeys][days];
smallest[monkeys] = smaller;
}
}
}
/* **********************************************************
Definition: showData
This function accepts six arrays as arguments:
* poundsEaten[][]
* average[]
* largest[]
* smallest[]
* dayNames[]
* monkeyNames[]
It summarizes what each monkey has eaten, the average
amount eaten by the whole family of monkeys, the largest
and smallest amount eaten by each individual member of the
monkey family.
********************************************************** */
void showData(const double poundsEaten[][G_CONST_DAYS], const double average[],
const double largest[], const double smallest[],
const string dayNames[], const string monkeyNames[])
{
/* Variables: Days, Monkeys (loop counters)*/
int days = 0,
monkeys = 0;
/* Set up: Numeric output formatting */
cout << fixed << showpoint << setprecision(2);
/* Display: Total amount eaten, Average amount eaten,
Largest Amount eaten, Smallest amount eaten */
cout << "\tTotal Amount Eaten:\n"
<< "\t-------------------\n";
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
{
cout << "\n" << monkeyNames[monkeys] << ":\n";
cout << "----------------------------------------------"
<< "-------------------\n";
for (days = 0; days < G_CONST_DAYS; days++)
{
cout << "" << dayNames[days] << ":"
<< setw(5) << right << poundsEaten[monkeys][days]
<< setw(5) << right;
}
cout << "\n----------------------------------------------"
<< "-------------------\n";
}
cout << "\n\tAverage Amount Eaten:\n"
<< "\t--------------------\n";
for (days = 0; days < G_CONST_DAYS; days++)
cout << "\n" << setw(6) << left << dayNames[days] << ": "
<< setw(25) << right << average[days]
<< setw(5) << right << " lbs";
cout << "\n\n\tLargest Amount Eaten:\n"
<< "\t--------------------\n";
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
cout << "\n" << setw(6) << left << monkeyNames[monkeys] << ": "
<< setw(25) << right << largest[monkeys]
<< setw(5) << right << " lbs";
cout << "\n\n\tSmallest Amount Eaten:\n"
<< "\t--------------------\n";
for (monkeys = 0; monkeys < G_CONST_MONKEYS; monkeys++)
cout << "\n" << setw(6) << left << monkeyNames[monkeys] << ": "
<< setw(25) << right << smallest[monkeys]
<< setw(5) << right << " lbs";
}
Sunday, January 29, 2017
Programming Challenge 7.5 - Monkey Business
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment