In: Computer Science
Tony Gaddis 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 × 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.
#include <iostream> using namespace std; const int NUM_MONKEYS = 3; const int NUM_DAYS = 7; const string DAYS[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; double findGroupTotal(double food[NUM_MONKEYS][NUM_DAYS]) { double total = 0; for(int i=0; i<NUM_MONKEYS; i++) { for(int j=0; j<NUM_DAYS; j++) { total += food[i][j]; } } return total; } double findLeastAmtFood(double food[NUM_MONKEYS][NUM_DAYS]) { double leastAmt = food[0][0]; for(int i=0; i<NUM_MONKEYS; i++) { for(int j=0; j<NUM_DAYS; j++) { if(leastAmt > food[i][j]) { leastAmt = food[i][j]; } } } return leastAmt; } double findGreatestAmtFood(double food[NUM_MONKEYS][NUM_DAYS]){ double greatestAmt = food[0][0]; for(int i=0; i<NUM_MONKEYS; i++) { for(int j=0; j<NUM_DAYS; j++) { if(greatestAmt < food[i][j]) { greatestAmt = food[i][j]; } } } return greatestAmt; } void fillData(double food[NUM_MONKEYS][NUM_DAYS]) { for(int j=0; j<NUM_DAYS; j++) { for(int i=0; i<NUM_MONKEYS; i++) { cout << "Enter food for Monkey:" << (i+1) << ", " << DAYS[j] << " : "; cin >> food[i][j]; while(food[i][j] < 0) { cout << "Invalid input. Try again: " << endl; cin >> food[i][j]; } } } } int main() { double food[NUM_MONKEYS][NUM_DAYS]; fillData(food); double groupTotal = findGroupTotal(food); double leastAmtFood = findLeastAmtFood(food); double greatestAmtFood = findGreatestAmtFood(food); cout << "Average food ate per day: " << (groupTotal/(NUM_DAYS*NUM_MONKEYS)) << endl; cout << "Least amount of food ate on a day: " << leastAmtFood << endl; cout << "Greatest amount of food ate on a day: " << greatestAmtFood << endl; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.