C++ programming language Monkey Business A local zoo wants to keep track of how
ID: 3917498 • Letter: C
Question
C++ programming language
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. Write a program that stores this information in a two-dimensional 3 × 7 array, where each row represents a different monkey and each column represents a different day of the week. The monkeys are represented by integers 1, 2, and 3; the weekdays are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday". The program should first prompt the user to input the data for each monkey, starting with "Sunday" for monkey #1, then monkeys #2 and #3, followed by "Monday" for monkey #1, then monkeys #2 and #3 and so on, through "Saturday". The program then creates a report that includes the following information, each properly labeled (see below): 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: Do not accept negative numbers for pounds of food eaten. When a negative value is entered, the program outputs "invalid (negative) food quantity -- re-enter" and attempts to reread the value. NOTE: Decimal values should be displayed using default precision, i.e. do not specify precision.
Explanation / Answer
#include <iostream>
using namespace std;
void average(double food[3][7])
{
double avg[3];
int i,j;
for(i=0;i<3;i++)
avg[i] = 0; //initialize avg for each monkey
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
{
avg[i] = avg[i] + food[i][j]; // sum of food eaten whole week by each monkey
}
}
for(i=0;i<3;i++)
{
cout<<avg[i]/7<<endl; // avg for each monkey
}
}
void least(double food[3][7])
{
double smallest[3];
int i,j;
for(i=0;i<3;i++)
{
smallest[i] = food[0][0]; // assign smallest food eaten by each monkey to first day of week
for(j=0;j<7;j++)
{
if(smallest[i]>food[i][j])
smallest[i] = food[i][j];
}
}
for(i=0;i<3;i++)
{
cout<<smallest[i]<<endl;
}
}
void greatest(double food[3][7])
{
double highest[3];
int i,j;
for(i=0;i<3;i++)
{
highest[i] = food[0][0];// assign greatest food eaten by each monkey to first day of week
for(j=0;j<7;j++)
{
if(highest[i]<food[i][j])
highest[i] = food[i][j];
}
}
for(i=0;i<3;i++)
{
cout<<highest[i]<<endl;
}
}
void input(double food[3][7])
{
int i,j;
string days[7] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
for(i=0;i<3;i++)
{
for(j=0;j<7;j++)
{
cout<<" Enter the food eaten by monkey on "<<days[j];
cin>>food[i][j];
if(food[i][j]<0)
{
cout<<" Food should be positive number. Try again";
j--;
}
}
}
}
int main() {
double food[3][7];
cout<<"Enter the food eaten by three monkeys in 7 days : ";
input(food);
cout<<" Average food eaten per day by 3 monkeys : ";
average(food);
cout<<" Smallest amount of food eaten by each monkey in the week : ";
least(food);
cout<<" Greatest amount of food eaten by each monkey in the week : ";
greatest(food);
return 0;
}
Output:
Enter the food eaten by three monkeys in 7 days :
Enter the food eaten by monkey on Monday 3.4
Enter the food eaten by monkey on Tuesday5.6
Enter the food eaten by monkey on Wednesday4.2
Enter the food eaten by monkey on Thursday3.1
Enter the food eaten by monkey on Friday4.2
Enter the food eaten by monkey on Saturday3.5
Enter the food eaten by monkey on Sunday-5.6
Food should be positive number. Try again
Enter the food eaten by monkey on Sunday5.6
Enter the food eaten by monkey on Monday3.1
Enter the food eaten by monkey on Tuesday2.1
Enter the food eaten by monkey on Wednesday2.8
Enter the food eaten by monkey on Thursday4.2
Enter the food eaten by monkey on Friday4.1
Enter the food eaten by monkey on Saturday4.8
Enter the food eaten by monkey on Sunday4.6
Enter the food eaten by monkey on Monday4.5
Enter the food eaten by monkey on Tuesday4.1
Enter the food eaten by monkey on Wednesday2.6
Enter the food eaten by monkey on Thursday2.9
Enter the food eaten by monkey on Friday3.6
Enter the food eaten by monkey on Saturday3.9
Enter the food eaten by monkey on Sunday4.9
Average food eaten per day by 3 monkeys :
4.22857
3.67143
3.78571
Smallest amount of food eaten by each monkey in the week :
3.1
2.1
2.6
Greatest amount of food eaten by each monkey in the week :
5.6
4.8
4.9
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.