fork download
  1. //Cameron Pham CS1A Chapter 7, p. 444, #2
  2. //
  3. /******************************************************************************
  4. * TRACK MONKEY DIET
  5. * ______________________________________________________________________________
  6. * This program accepts a float value for how many pounds of food each of three
  7. * monkeys eats each day for one week. It stores the data in a two-dimensional
  8. * array and displays a report containing the average food eaten per day by the
  9. * monkey family, as well as the least and greatest amounts eaten by any monkey.
  10. * ______________________________________________________________________________
  11. * INPUT
  12. * Food // Pounds of food eaten by each monkey each day
  13. *
  14. * OUTPUT
  15. * Average food // Average food eaten per day by the family
  16. * Least food // Least food eaten by one monkey in a week
  17. * Greatest food // Greatest food eaten by one monkey in a week
  18. *******************************************************************************/
  19. #include <iostream>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. int main()
  24. {
  25. // Initialize variables
  26. float food[3][7]; // Food eaten by 3 monkeys over 7 days
  27. float totalFood = 0.0; // Total food eaten by all monkeys
  28. float averageFood; // Average daily food eaten by the family
  29. float leastFood; // Least food eaten by any monkey in a week
  30. float greatestFood; // Greatest food eaten by any monkey in a week
  31.  
  32. // Store the names of the days of the week
  33. string days[7] =
  34. {
  35. "Monday", "Tuesday", "Wednesday", "Thursday",
  36. "Friday", "Saturday", "Sunday"
  37. };
  38.  
  39. // Ask the user to enter food amounts for each monkey
  40. for (int monkey = 0; monkey < 3; monkey++)
  41. {
  42. cout << endl << "Monkey #" << (monkey + 1) << endl;
  43.  
  44. for (int day = 0; day < 7; day++)
  45. {
  46. cout << "Enter pounds of food eaten on "
  47. << days[day] << ": ";
  48. cin >> food[monkey][day];
  49.  
  50. // Validate the food amount
  51. while (food[monkey][day] < 0)
  52. {
  53. cout << "Error: Food amount cannot be negative. ";
  54. cout << "Enter the amount for " << days[day] << " again: ";
  55. cin >> food[monkey][day];
  56. }
  57.  
  58. // Add the amount to the weekly total
  59. totalFood += food[monkey][day];
  60. }
  61. }
  62.  
  63. // Initialize the least and greatest food amounts
  64. leastFood = food[0][0];
  65. greatestFood = food[0][0];
  66.  
  67. // Find the least and greatest amounts eaten in a single day
  68. for (int monkey = 0; monkey < 3; monkey++)
  69. {
  70. for (int day = 0; day < 7; day++)
  71. {
  72. if (food[monkey][day] < leastFood)
  73. {
  74. leastFood = food[monkey][day];
  75. }
  76.  
  77. if (food[monkey][day] > greatestFood)
  78. {
  79. greatestFood = food[monkey][day];
  80. }
  81. }
  82. }
  83.  
  84. // Calculate the average amount eaten per day by the monkey family
  85. averageFood = totalFood / 7.0;
  86.  
  87. // Display the report
  88. cout << fixed << setprecision(2);
  89. cout << endl << "Monkey Business Report" << endl;
  90. cout << "----------------------" << endl;
  91. cout << "Average food eaten per day by the family: "
  92. << averageFood << " pounds" << endl;
  93. cout << "Least amount eaten by single monkey in one day: "
  94. << leastFood << " pounds" << endl;
  95. cout << "Greatest amount eaten by a single monkey in one day: "
  96. << greatestFood << " pounds" << endl;
  97.  
  98. return 0;
  99. }
Success #stdin #stdout 0s 5308KB
stdin
1 0.5 1 1.5 1 1 1
1.75 2 1.5 2.3 1.4 1.8 2
2 3 2.5 4 1 2 2.6
stdout
Monkey #1
Enter pounds of food eaten on Monday: Enter pounds of food eaten on Tuesday: Enter pounds of food eaten on Wednesday: Enter pounds of food eaten on Thursday: Enter pounds of food eaten on Friday: Enter pounds of food eaten on Saturday: Enter pounds of food eaten on Sunday: 
Monkey #2
Enter pounds of food eaten on Monday: Enter pounds of food eaten on Tuesday: Enter pounds of food eaten on Wednesday: Enter pounds of food eaten on Thursday: Enter pounds of food eaten on Friday: Enter pounds of food eaten on Saturday: Enter pounds of food eaten on Sunday: 
Monkey #3
Enter pounds of food eaten on Monday: Enter pounds of food eaten on Tuesday: Enter pounds of food eaten on Wednesday: Enter pounds of food eaten on Thursday: Enter pounds of food eaten on Friday: Enter pounds of food eaten on Saturday: Enter pounds of food eaten on Sunday: 
Monkey Business Report
----------------------
Average food eaten per day by the family: 5.26 pounds
Least amount eaten by single monkey in one day: 0.50 pounds
Greatest amount eaten by a single monkey in one day: 4.00 pounds