// Kurt Feiereisel CSC5 Chapter 7, p.444, #4
/*******************************************************************************
*
* Calculate and Report Monkey Food Consumption
* _____________________________________________________________________________
* This program allows a user to enter the amount of food three monkeys eat
* over the course of a week. The program will then calculate and report the
* average amount of food consumed by the family of monkeys per day. The
* program will also find the least amount of food and most amount of food
* eaten by a monkey.
* _____________________________________________________________________________
* Formula:
* a = total / 7
*
* INPUT:
* food[M][DAYS] : Array to collect amount of food eaten by each
* monkey each day (3 x 7)
* total : Accumulator to collect amount eaten by each monkey
* each day
*
* OUTPUT:
* a : Average amount eaten by the monkeys in a day
* lowest : Least amount eaten by any monkey in a day
* most : Most amount eaten by any monkey in a day
* ****************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Global Constant
const int DAYS = 7; // CONSTANT : total number of days
// Function Prototypes
void getData(int food[][DAYS], int M);
void avg(int food[][DAYS], int M);
void leastFood(int food[][DAYS], int M);
void mostFood(int food[][DAYS], int M);
int main()
{
// Local Constant
const int M = 3; // INPUT : number of monkeys
// Declare Array
int food[M][DAYS]; // Input : Amount of food eaten by each
//monkey each day
// Function Calls
getData(food, M);
avg(food, M);
leastFood(food, M);
mostFood(food, M);
return 0;
}
/*
* Definition of getData
* This function allows a user to enter data on the amount of cans each monkey
* eats each day of the week.
*/
void getData(int f[][DAYS], int m)
{
// Input Food eaten per day, per monkey
cout << "Please enter the amount each monkey ate each day of the week:";
cout << endl;
for (int index = 0; index < m; index++)
{
cout << "Enter the amount of food eaten per day for monkey number "
<< (index + 1) << ":\n";
for(int count = 0; count < DAYS; count++)
{
cin >> f[index][count];
// Input Verification
while (f[index][count] < 0)
{
cout << "Please enter a positive number." << endl;
cin >> f[index][count];
}
}
}
}
/*
* Definition of avg
* This function calculates and reports the average amount all the monkeys eat
* together
*/
void avg(int food[][DAYS], int m)
{
// Initialize / Declare Local Variables
float a; // OUTPUT : Average lbs of food
// eaten by family per day
int total = 0;
// Accumulate total
for(int index = 0; index < m; index++)
{
for(int count = 0; count < DAYS; count++)
{
total += food[index][count];
}
}
// Calculate average food eaten by whole family in one day.
a = total / 7;
// Report Average
cout << "The average amount of food eaten per day by the family of "
<< "monkeys was " << a << " lbs." << endl;
}
/*
* Definition of leastFood
* This function displays the least amount of food eaten between all of the
* monkeys.
*/
void leastFood(int f[][DAYS], int m)
{
// Initialize Variables
float lowest = f[0][0]; // OUTPUT: Lowest amount of food
// eaten by a single monkey in a day
// Determine Lowest value
for(int index = 0; index > m; index++)
{
for (int count = 0; count < DAYS; count++)
{
if(f[index][count] < lowest)
lowest = f[index][count];
}
}
// Display least amount of food eaten
cout << "The least amount of food eaten was: " << lowest << " lbs.\n";
}
/*
* Definition of mostFood
* This function displays the most amount of food eaten between all of the
* monkeys
*/
void mostFood(int f[][DAYS], int m)
{
// Initialize Variable
float most = f[0][0]; // OUTPUT: most amount of food
// eaten by a monkey in a day
// Determine highest value
for (int index = 0; index < m; index++)
{
for (int count = 0; count < DAYS; count++)
{
if(f[index][count] > most)
most = f[index][count];
}
}
// Display most amount of food eaten
cout << "The most amount of food eaten was: " << most << " lbs.\n";
}