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
|
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const int NUM_MONKEYS = 3;
const int NUM_DAYS = 7;
void showData(string monkeys[NUM_MONKEYS],int table[][NUM_DAYS]);
void calculateDailyIntake(string monkeys[NUM_MONKEYS], int table[][NUM_DAYS]);
int main()
{
// 1-D array to hold monkeys names
string monkeys[NUM_MONKEYS] = { "Louie", "Bo-Bo", "Jimmy" };
int table[NUM_MONKEYS][NUM_DAYS] = { { 45,33,55,66,77,88,100 },
{ 88,77,66,55,44,33,22 },
{ 44,55,11,66,77,88,99 }};
showData(monkeys, table);
calculateDailyIntake(monkeys,table);
system("pause");
return 0;
}
// "prints the contents of the two arrays"
void showData(string monkeys[NUM_MONKEYS], int table[][NUM_DAYS])
{
cout << "Name \t\tDay 1\tDay 2\tDay 3\tDay 4\tDay 5\tDay 6\tDay 7 " << endl;
cout << "_________________________________________________________________\n";
for (int r = 0; r < NUM_MONKEYS; r++)
{
cout << monkeys[r];
for (int c = 0; c < NUM_DAYS; c++)
{
cout << "\t\t";
cout << table[r][c];
}
cout << endl;
}
cout << endl;
}
// "calculate and display the average daily food intake for each of the three monkeys"
void calculateDailyIntake(string monkeys[NUM_MONKEYS], int table[][NUM_DAYS])
{
cout << "Name \t\tAverage daily intake in pounds \n";
cout << "_________________________________________________________________\n";
double totalRow = 0;
cout << setprecision(1) << fixed;
for(int r = 0; r < NUM_MONKEYS; r++)
{
for(int c = 0; c < NUM_DAYS; c++)
totalRow = totalRow + table[r][c];
cout << monkeys[r] << "\t" << setw(1) << (totalRow/NUM_DAYS) << endl;
}
totalRow = 0;
}
|