fork download
  1. //Ryan Shahriyarpour CS1A Carl Argila CH7 HW Question 4
  2. //
  3. /*****************************************************************************
  4.  *
  5.  * Monkey Business
  6.  *
  7.  *********************
  8.  *
  9.  * This program stores the pounds of food eaten by 3 monkeys over 7 days in a
  10.  * 3 x 7 array (rows = monkey and columns = days). It displays the average food
  11.  * eaten per day by all the monkeys, and the least and greatest amount eaten
  12.  * during the week by any one monkey
  13.  *
  14.  * INPUT
  15.  * Pounds of food eaten by each monkey on each day
  16.  *
  17.  * OUTPUT
  18.  * Average food per day, least and greatest weekly amount
  19.  * eaten by one monkey
  20.  ****************************************************************************/
  21.  
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. const int MONKEYS = 3;
  26. const int DAYS = 7;
  27.  
  28. int main()
  29. {
  30. double food[MONKEYS][DAYS];
  31. double weekTotal;
  32. double allTotal = 0;
  33. double average;
  34. double least;
  35. double greatest;
  36.  
  37. // Input (no negative numbers allowed)
  38. for (int m = 0; m < MONKEYS; m++)
  39. {
  40. for (int d = 0; d < DAYS; d++)
  41. {
  42. cout << "Monkey " << m + 1 << ", day " << d + 1 << " pounds: ";
  43. cin >> food[m][d];
  44. while (food[m][d] < 0)
  45. {
  46. cout << "Cannot be negative. Re-enter: ";
  47. cin >> food[m][d];
  48. }
  49. }
  50. }
  51.  
  52. // Total each monkey's week, then find the least and greatest
  53. for (int m = 0; m < MONKEYS; m++)
  54. {
  55. weekTotal = 0;
  56. for (int d = 0; d < DAYS; d++)
  57. weekTotal = weekTotal + food[m][d];
  58.  
  59. allTotal = allTotal + weekTotal;
  60.  
  61. if (m == 0 || weekTotal < least)
  62. least = weekTotal;
  63. if (m == 0 || weekTotal > greatest)
  64. greatest = weekTotal;
  65. }
  66. average = allTotal / DAYS;
  67.  
  68. // Output
  69. cout << "\nAverage food per day (all monkeys): " << average << endl;
  70. cout << "Least eaten by one monkey: " << least << endl;
  71. cout << "Greatest eaten by one monkey: " << greatest << endl;
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Monkey 1, day 1 pounds: Monkey 1, day 2 pounds: Monkey 1, day 3 pounds: Monkey 1, day 4 pounds: Monkey 1, day 5 pounds: Monkey 1, day 6 pounds: Monkey 1, day 7 pounds: Monkey 2, day 1 pounds: Monkey 2, day 2 pounds: Monkey 2, day 3 pounds: Monkey 2, day 4 pounds: Monkey 2, day 5 pounds: Monkey 2, day 6 pounds: Monkey 2, day 7 pounds: Monkey 3, day 1 pounds: Monkey 3, day 2 pounds: Monkey 3, day 3 pounds: Monkey 3, day 4 pounds: Monkey 3, day 5 pounds: Monkey 3, day 6 pounds: Monkey 3, day 7 pounds: 
Average food per day (all monkeys): 4.75202e-310
Least eaten by one monkey: 6.80214e-310
Greatest eaten by one monkey: 1.85262e-309