A local zoo wants to keep track of how many pounds of food each of its three mon
ID: 3723156 • Letter: A
Question
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 3x7 array, where each row represents a different monkey and each column represents a different day of the week. The program should first have the user input the data for each monkey. Then it should create a report that includes the following information:
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.
Note:
In addition to some arrays, please use an array of string objects to hold the seven days’ names, i.e. “Monday”, “Tuesday”, etc, so that you can use it to prompt the user when the user input the data for each monkey.
in C++
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.