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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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-

1 #include <iostream> 2 using namespace std; 3 //define the function average 4 //takes in the matrix monkey Food[3][5] and di

39 40 //display the least consumed and the monkey number cout<<\nThe least amount of food eaten during the week by any one m

71 72 74 67 //main method 68 int main() 69-{ 70 1/2D array to store monkey Food int monkeyFood[3][5]; //prompt the user to en

OUTPUT-

Enter the data for each monkey week wise - Enter food consumed for monkey 1 : 25 36 74 55 Enter food consumed for monkey 2: 1

If this answer helps, please give an up vote and feel free to comment for any query.

Add a comment
Know the answer?
Add Answer to:
C++ Monkey Business A local zoo wants to keep track of how many pounds of food...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java 1. Monkey Business (10 points) A local zoo wants to keep track of how...

    In java 1. Monkey Business (10 points) 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 x 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...

  • Pseudocode please Special Problem for Swap A local zoo wants to keep track of how many...

    Pseudocode please Special Problem for Swap 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 x 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"....

  • In C++ Rewrite Modularity section to clarify functions should be separate. Assignment 6 - Monkey Food...

    In C++ Rewrite Modularity section to clarify functions should be separate. Assignment 6 - Monkey Food In the Gaddis textbook read Chapter 8 sections 8.1-8.9 before starting this assignment. This assignment is Programming Challenge 4 from Chapter 8 of the textbook. 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 x 7 arrray,...

  • What are the 8 errors in this C++ program below: /* CSE 100 Professor Feng Learn...

    What are the 8 errors in this C++ program below: /* CSE 100 Professor Feng Learn to read data from user and fill a two-dimensional array. Learn how to compute the sum of one row or one column in a 2D array Learn how to compute the sum, average of a 2D array */ #include<iostream> #include<cmath> #include<iomanip> using namespace std; double bananaMon=0; //Global Variables to be used in the program double bananaSat=0; double totalSum=0.0; //Global variable total sum double findGroupTotal();...

  • Please write a Java program that ask the user how many beers he or she expects...

    Please write a Java program that ask the user how many beers he or she expects to consume each day, on average, as well as the average amount of money he or she spends on a single 12-ounce can of beer. Studies have shown that, on average, someone who consumes a single 12-ounce can of beer every day without compensating for the calorie intake through diet modifications or extra exercise, will gain approximately 15 pounds in one year. You can...

  • can someone please comment through this code to explain me specifically how the variables and arrays...

    can someone please comment through this code to explain me specifically how the variables and arrays are working? I am just learning arrays code is below assignment C++ Programming from Problem Analysis to Program Design by D. S. Malik, 8th ed. Programming Exercise 12 on page 607 Lab9_data.txt Jason, Samantha, Ravi, Sheila, and Ankit are preparing for an upcoming marathon. Each day of the week, they run a certain number of miles and write them into a notebook. At the...

  • Requires Python to answer A client wishes to keep track of his investment in shares. Write...

    Requires Python to answer A client wishes to keep track of his investment in shares. Write a program to help him manage his stock portfolio. You are to record both the shares that he is holding as well as shares that he has sold. For shares be is curreatly holding, record the following data: .a 3-character share code, share name, last purchase date, volume currently held and average purchase price (refer to description under part cii) option 2) For shares...

  • C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include ...

    C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include <stdio.h> int main() { int fries; // A flag denoting whether they want fries or not. char bacon; // A character for storing their bacon preference. double cost = 0.0; // The total cost of their meal, initialized to start at 0.0 int choice; // A variable new to version 2, choice is an int that will store the // user's menu choice. It will also serve as our loop control...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

  • 10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated...

    10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated sludge operation that adversely affect effluent quality with origins in the engineering, hydraulic and microbiological components of the process. The real "heart" of the activated sludge system is the development and maintenance of a mixed microbial culture (activated sludge) that treats wastewater and which can be managed. One definition of a wastewater treatment plant operator is a "bug farmer", one who controls the aeration...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT