Vous êtes sur la page 1sur 3

#include <iostream>

#include <iomanip>
using namespace std;
// constants
const int MONKEYS = 3;
const int DAYS = 7;
// function prototype.
void getFood(double arr[][DAYS], int rows);
void avgFood(double arr[][DAYS], int rows);
void leastAmount(double arr[][DAYS], int rows);
void greatestAmount(double arr[][DAYS], int rows);
int main()
{
// declarations.
double food[MONKEYS][DAYS];
// header
cout << "\tMonkey Business :::\n\n";
cout << "Enter the food each monkey eats (in pounds) during the week
.\n\n";
// get food.
getFood(food, MONKEYS);
cout << "Report:\n";
cout << "----------------------\n";
// get average.
avgFood(food, MONKEYS);
// get least amount in that week.
leastAmount(food, MONKEYS);
// get greatest amount in that week.
greatestAmount(food, MONKEYS);
system("pause");
// End program.
return 0;
}
// getFood function
// Takes three arguments. It asks the user to enter the food in
// pounds that each monkey eats, for every day in a single week.
void getFood(double arr[][DAYS], int rows)
{
for (int nRows = 0; nRows < rows; nRows++)
{
cout << "Monkey " << (nRows + 1) << "\n";
for (int nCols = 0; nCols < DAYS; nCols++)
{
cout << " Day " << (nCols + 1) << ": ";
cin >> arr[nRows][nCols];
// validate.
while (arr[nRows][nCols] < 0)
{
cout << "ERROR: food cannot be lower than 0,
try again: ";

cin >> arr[nRows][nCols];


}
}
// separate the fields.
cout << endl;
}
}
// avgFood function
// Gets the average food each monkey eats during the week. It
// will then show a message on the screen.
void avgFood(double arr[][DAYS], int rows)
{
double total = 0;
// accumulator.
cout << "Average food a day for " << MONKEYS;
cout << " monkeys: ";
for (int nRows = 0; nRows < rows; nRows++)
{
for (int nCols = 0; nCols < DAYS; nCols++)
total += arr[nRows][nCols];
}
// set formatting.
cout << fixed << setprecision(2);
cout << (total / MONKEYS) << " pounds.\n" << endl;
}
// leastAmount function
// Get from each monkey the least amount of food eaten
// during the week.
void leastAmount(double arr[][DAYS], int rows)
{
for (int nRows = 0; nRows < rows; nRows++)
{
double least = arr[0][0];
int day = 0;
// accumulator.
for (int nCols = 0; nCols < DAYS; nCols++)
{
if (arr[nRows][nCols] < least)
{
least = arr[nRows][nCols];
day += (nCols + 1);
}
}
cout << "On day " << day;
cout << " monkey " << (nRows + 1);
cout << " ate the least amount of food.\n";
}
// empty line
cout << endl;
}
// greatestAmount function
void greatestAmount(double arr[][DAYS], int rows)
{
for (int nRows = 0; nRows < rows; nRows++)
{
double greatest = arr[0][0];

int day = 0;
// accumulator.
for (int nCols = 0; nCols < DAYS; nCols++)
{
if (arr[nRows][nCols] > greatest)
{
greatest = arr[nRows][nCols];
day += (nCols + 1);
}
}
cout << "On day " << day;
cout << " monkey " << (nRows + 1);
cout << " ate the greatest amount of food.\n";
}
// empty line
cout << endl;
}

Vous aimerez peut-être aussi