C++ Monkey Business A local zoo wants to keep track of how many pounds of food…
The following solution is suggested to handle the subject “C++ Monkey Business A local zoo wants to keep track of how many pounds of food…“. Let’s keep an eye on the content below!
Question “C++ Monkey Business A local zoo wants to keep track of how many pounds of food…”
C++
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 × 5
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.
Write functions
- average()
- least()
- greatest()
- main()
Include a header comment with your name and a description and
footer comment with test data.
No magic numbers!
Answer
The above problem can be solved in the following steps-
STEP 1
– First define the average() function that takes
the food consumed 2D array and then prints the average food
consumed per day.
STEP 2
– Define the function least() which takes the food
consumed 2D array and then displays the least food consumed during
week by any monkey.
STEP
3 – Define the function greatest() which takes the
food consumed 2D array and then displays the greatest food consumed
during week by any monkey.
STEP
4- Write the main method which asks for user input
for 2D matrix and then calls all the above defined functions which
display the result.
The comments in the code will help in understanding the
code in a better way.
C++CODE-
#include <iostream>
using namespace std;
//define the function average
//takes in the matrix monkeyFood[3][5] and displays the average
food consumed per day
void average(int monkeyFood[3][5]){
//variable to store the sum of total food
int sumFood;
//iterate over the matrix and find our the sum of monkeyFood
for(int i=0;i<3;i++){
for(int j=0;j<5;j++){
sumFood = sumFood + monkeyFood[i][j];
}
}
//print the average amount of food eaten per day
//since, number of days = 5. therfore divide the sumFood by 5 to
get the average
cout<<“\nAverage amount of food eaten per day by the whole
family of monkeys = “<<sumFood/5.0<<endl;
}
//define the funciton least
//takes in the matrix monkeyFood[3][5] and displays the least food
consumed by any monkey during the week
void least(int monkeyFood[3][5]){
//initialize min with a large number
int min = 99999;
int monkeyNo; //to store the monkeyNo with least consumed
food
//iterate over the monkeyFood[3][5] matrix
for(int i=0;i<3;i++){
//variable to store the food consumed this week for monkey
‘i’
int sumFoodWeek = 0;
//iterate over the week
for(int j=0;j<5;j++){
sumFoodWeek = sumFoodWeek + monkeyFood[i][j];
}
//check if sumFoodWeek is less than min
if(sumFoodWeek < min){
//set min to sumFoodWeek
min = sumFoodWeek;
monkeyNo = i+1; //and monkeyNo to i+1 (‘i’ is index no, so add 1 to
it)
}
}
//display the least consumed and the monkey number
cout<<“\nThe least amount of food eaten during the week by
any one monkey = “<<min<<endl;
cout<<“The monkey number was
“<<monkeyNo<<endl;
}
//define the funciton greatest
//takes in the matrix monkeyFood[3][5] and displays the greatest
food consumed by any monkey during the week
void greatest(int monkeyFood[3][5]){
//initialize max with a small number
int max = 0;
int monkeyNo; //to store the monkeyNo with greatest consumed
food
//iterate over the monkeyFood[3][5] matrix
for(int i=0;i<3;i++){
//variable to store the food consumed this week for monkey
‘i’
int sumFoodWeek = 0;
//iterate over the week
for(int j=0;j<5;j++){
sumFoodWeek = sumFoodWeek + monkeyFood[i][j];
}//check if sumFoodWeek is more than max
if(sumFoodWeek > max){
//set max to sumFoodWeek
max = sumFoodWeek;
monkeyNo = i+1;
}
}
//display the greatest food consumed and monkeyNo
cout<<“\nThe least amount of food eaten during the week by
any one monkey = “<<max<<endl;
cout<<“The monkey number was
“<<monkeyNo<<endl;
}
//main method
int main()
{
//2D array to store monkeyFood
int monkeyFood[3][5];
//prompt the user to enter the food consumed data
cout<<“Enter the data for each monkey week wise – \n”;
//iterate for 3 monkeys
for(int i=0;i<3;i++){
cout<<“Enter food consumed for monkey “<<i+1<<” :
“<<endl;
//iterate for 5 week days
for(int j=0;j<5;j++){
int input;
//while loop which breaks when input is positive
while(1){
cin>>input;
if(input<0){
//when input is Negative, ask the user to enter again
cout<<“Wrong input. Negative not allowed.Enter again
\n”;
continue;
}
else{
break;
}
}
//set the value in matrix
monkeyFood[i][j] = input;
}
}
//call the three funcitons
average(monkeyFood);
least(monkeyFood);
greatest(monkeyFood);
return 0;
}
IMAGE OF
CODE-
OUTPUT-
If this answer helps, please give an up vote and
feel free to comment for any query.
Conclusion
Above is the solution for “C++ Monkey Business A local zoo wants to keep track of how many pounds of food…“. We hope that you find a good answer and gain the knowledge about this topic of engineering.