Monkey Business.


SUBMITTED BY: Guest

DATE: Nov. 12, 2013, 2:55 a.m.

FORMAT: Text only

SIZE: 4.1 kB

HITS: 1372

  1. // Monkey Business.
  2. #include <iostream>
  3. #include <iomanip>
  4. using namespace std;
  5. // constants
  6. const int MONKEYS = 3;
  7. const int DAYS = 7;
  8. // function prototype.
  9. void getFood(double arr[][DAYS], int rows);
  10. void avgFood(double arr[][DAYS], int rows);
  11. void leastAmount(double arr[][DAYS], int rows);
  12. void greatestAmount(double arr[][DAYS], int rows);
  13. int main()
  14. {
  15. // declarations.
  16. double food[MONKEYS][DAYS];
  17. // header
  18. cout << "\tMonkey Business :::\n\n";
  19. cout << "Enter the food each monkey eats (in pounds) during the week.\n\n";
  20. // get food.
  21. getFood(food, MONKEYS);
  22. cout << "Report:\n";
  23. cout << "----------------------\n";
  24. // get average.
  25. avgFood(food, MONKEYS);
  26. // get least amount in that week.
  27. leastAmount(food, MONKEYS);
  28. // get greatest amount in that week.
  29. greatestAmount(food, MONKEYS);
  30. // End program.
  31. return 0;
  32. }
  33. //*******************************************************************
  34. // getFood function
  35. // Takes three arguments. It asks the user to enter the food in
  36. // pounds that each monkey eats, for every day in a single week.
  37. //*******************************************************************
  38. void getFood(double arr[][DAYS], int rows)
  39. {
  40. for (int nRows = 0; nRows < rows; nRows++)
  41. {
  42. cout << "Monkey " << (nRows + 1) << "\n";
  43. for (int nCols = 0; nCols < DAYS; nCols++)
  44. {
  45. cout << " Day " << (nCols + 1) << ": ";
  46. cin >> arr[nRows][nCols];
  47. // validate.
  48. while (arr[nRows][nCols] < 0)
  49. {
  50. cout << "ERROR: food cannot be lower than 0, try again: ";
  51. cin >> arr[nRows][nCols];
  52. }
  53. }
  54. // separate the fields.
  55. cout << endl;
  56. }
  57. }
  58. //*******************************************************************
  59. // avgFood function
  60. // Gets the average food each monkey eats during the week. It
  61. // will then show a message on the screen.
  62. //*******************************************************************
  63. void avgFood(double arr[][DAYS], int rows)
  64. {
  65. double total = 0; // accumulator.
  66. cout << "Average food a day for " << MONKEYS;
  67. cout << " monkeys: ";
  68. for (int nRows = 0; nRows < rows; nRows++)
  69. {
  70. for (int nCols = 0; nCols < DAYS; nCols++)
  71. total += arr[nRows][nCols];
  72. }
  73. // set formatting.
  74. cout << fixed << setprecision(2);
  75. cout << (total / MONKEYS) << " pounds.\n" << endl;
  76. }
  77. //*******************************************************************
  78. // leastAmount function
  79. // Get from each monkey the least amount of food eaten
  80. // during the week.
  81. //*******************************************************************
  82. void leastAmount(double arr[][DAYS], int rows)
  83. {
  84. for (int nRows = 0; nRows < rows; nRows++)
  85. {
  86. double least = arr[0][0];
  87. int day = 0; // accumulator.
  88. for (int nCols = 0; nCols < DAYS; nCols++)
  89. {
  90. if (arr[nRows][nCols] < least)
  91. {
  92. least = arr[nRows][nCols];
  93. day += (nCols + 1);
  94. }
  95. }
  96. cout << "On day " << day;
  97. cout << " monkey " << (nRows + 1);
  98. cout << " ate the least amount of food.\n";
  99. }
  100. // empty line
  101. cout << endl;
  102. }
  103. //*******************************************************************
  104. // greatestAmount function
  105. //
  106. //*******************************************************************
  107. void greatestAmount(double arr[][DAYS], int rows)
  108. {
  109. for (int nRows = 0; nRows < rows; nRows++)
  110. {
  111. double greatest = arr[0][0];
  112. int day = 0; // accumulator.
  113. for (int nCols = 0; nCols < DAYS; nCols++)
  114. {
  115. if (arr[nRows][nCols] > greatest)
  116. {
  117. greatest = arr[nRows][nCols];
  118. day += (nCols + 1);
  119. }
  120. }
  121. cout << "On day " << day;
  122. cout << " monkey " << (nRows + 1);
  123. cout << " ate the greatest amount of food.\n";
  124. }
  125. // empty line
  126. cout << endl;
  127. }

comments powered by Disqus