1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <iostream>
using namespace std;
const int numRows = 3;
const int numCols = 5;
void getData(int array[][numCols], int);
double getAvg(int array[][numCols], int);
int getRowSum(int array[][numCols], int);
double getAvg(int array[][numCols], int);
int getSmall(int, int, int);
int getLarge(int, int, int);
int main()
{
int monkeys[numRows][numCols];
int monkey1 = 0, monkey2 = 1, monkey3 = 2, monk1Tot, monk2Tot, monk3Tot, small, large;
getData(monkeys, numRows);
monk1Tot = getRowSum(monkeys, monkey1);
monk2Tot = getRowSum(monkeys, monkey2);
monk3Tot = getRowSum(monkeys, monkey3);
small = getSmall(monk1Tot, monk2Tot, monk3Tot);
large = getLarge(monk1Tot, monk2Tot, monk3Tot);
cout << "Average amount of food eaten per day by the whole family of monkeys is... " << getAvg(monkeys, numRows) << ". \n" << endl;
cout << "The least amount of food eaten during the week by any one monkey is... " << small << ". \n" << endl;
cout << "The greatest amount of food eaten during the week by any one monkey is... " << large << ". \n" << endl;
}
void getData(int monkeys[][numCols], int numRows)
{
for (int rows = 0; rows < numRows; rows++)
{
cout << "Monkey " << (rows + 1) << "\n";
for (int cols = 0; cols < numCols; cols++)
{
cout << " Day " << (cols + 1) << ": ";
cin >> monkeys[rows][cols];
while (monkeys[rows][cols] < 0)
{
cout << " You need to enter a postivie number. ";
cin >> monkeys[rows][cols];
}
}
cout << endl;
}
}
int getRowSum(int monkeys[][numCols], int monkeyNum)
{
int total = 0;
for (int cols = 0; cols < numCols; cols++)
total += monkeys[monkeyNum][cols];
return total;
}
double getAvg(int monkeys[][numCols], int numRows)
{
double total = 0;
for (int cols = 0; cols < numCols; cols++)
{
for (int rows = 0; rows < numRows; rows++)
total += monkeys[rows][cols];
}
return (total / (numCols));
}
int getMost(int monkey1, int monkey2, int monkey3)
{
int array[3]{ monkey1, monkey2, monkey3 };
int high = array[0];
for (int count = 0; count < 3; count++)
{
if (array[count] > high)
{
high = array[count];
}
}
return high;
}
int getSmall(int monkey1, int monkey2, int monkey3)
{
int array[3]{ monkey1, monkey2, monkey3 };
int low = array[0];
for (int count = 0; count < 3; count++)
{
if (array[count] < low)
{
low = array[count];
}
}
return low;
}
|