fork download
  1. // Kurt Feiereisel CSC5 Chapter 7, p.444, #4
  2. /*******************************************************************************
  3.  *
  4.  * Calculate and Report Monkey Food Consumption
  5.  * _____________________________________________________________________________
  6.  * This program allows a user to enter the amount of food three monkeys eat
  7.  * over the course of a week. The program will then calculate and report the
  8.  * average amount of food consumed by the family of monkeys per day. The
  9.  * program will also find the least amount of food and most amount of food
  10.  * eaten by a monkey.
  11.  * _____________________________________________________________________________
  12.  * Formula:
  13.  * a = total / 7
  14.  *
  15.  * INPUT:
  16.  * food[M][DAYS] : Array to collect amount of food eaten by each
  17.  * monkey each day (3 x 7)
  18.  * total : Accumulator to collect amount eaten by each monkey
  19.  * each day
  20.  *
  21.  * OUTPUT:
  22.  * a : Average amount eaten by the monkeys in a day
  23.  * lowest : Least amount eaten by any monkey in a day
  24.  * most : Most amount eaten by any monkey in a day
  25.  * ****************************************************************************/
  26. #include <iostream>
  27. #include <iomanip>
  28. using namespace std;
  29.  
  30. // Global Constant
  31. const int DAYS = 7; // CONSTANT : total number of days
  32.  
  33. // Function Prototypes
  34. void getData(int food[][DAYS], int M);
  35. void avg(int food[][DAYS], int M);
  36. void leastFood(int food[][DAYS], int M);
  37. void mostFood(int food[][DAYS], int M);
  38.  
  39. int main()
  40. {
  41. // Local Constant
  42. const int M = 3; // INPUT : number of monkeys
  43.  
  44. // Declare Array
  45. int food[M][DAYS]; // Input : Amount of food eaten by each
  46. //monkey each day
  47.  
  48. // Function Calls
  49. getData(food, M);
  50. avg(food, M);
  51. leastFood(food, M);
  52. mostFood(food, M);
  53.  
  54.  
  55. return 0;
  56. }
  57.  
  58. /*
  59.  * Definition of getData
  60.  * This function allows a user to enter data on the amount of cans each monkey
  61.  * eats each day of the week.
  62.  */
  63. void getData(int f[][DAYS], int m)
  64. {
  65.  
  66. // Input Food eaten per day, per monkey
  67. cout << "Please enter the amount each monkey ate each day of the week:";
  68. cout << endl;
  69. for (int index = 0; index < m; index++)
  70. {
  71. cout << "Enter the amount of food eaten per day for monkey number "
  72. << (index + 1) << ":\n";
  73. for(int count = 0; count < DAYS; count++)
  74. {
  75. cin >> f[index][count];
  76.  
  77. // Input Verification
  78. while (f[index][count] < 0)
  79. {
  80. cout << "Please enter a positive number." << endl;
  81. cin >> f[index][count];
  82. }
  83. }
  84. }
  85. }
  86. /*
  87.  * Definition of avg
  88.  * This function calculates and reports the average amount all the monkeys eat
  89.  * together
  90.  */
  91. void avg(int food[][DAYS], int m)
  92. {
  93. // Initialize / Declare Local Variables
  94. float a; // OUTPUT : Average lbs of food
  95. // eaten by family per day
  96. int total = 0;
  97.  
  98. // Accumulate total
  99. for(int index = 0; index < m; index++)
  100. {
  101. for(int count = 0; count < DAYS; count++)
  102. {
  103. total += food[index][count];
  104. }
  105. }
  106.  
  107. // Calculate average food eaten by whole family in one day.
  108. a = total / 7;
  109.  
  110. // Report Average
  111. cout << "The average amount of food eaten per day by the family of "
  112. << "monkeys was " << a << " lbs." << endl;
  113. }
  114. /*
  115.  * Definition of leastFood
  116.  * This function displays the least amount of food eaten between all of the
  117.  * monkeys.
  118.  */
  119. void leastFood(int f[][DAYS], int m)
  120. {
  121. // Initialize Variables
  122. float lowest = f[0][0]; // OUTPUT: Lowest amount of food
  123. // eaten by a single monkey in a day
  124.  
  125. // Determine Lowest value
  126. for(int index = 0; index > m; index++)
  127. {
  128. for (int count = 0; count < DAYS; count++)
  129. {
  130. if(f[index][count] < lowest)
  131. lowest = f[index][count];
  132. }
  133. }
  134.  
  135. // Display least amount of food eaten
  136. cout << "The least amount of food eaten was: " << lowest << " lbs.\n";
  137. }
  138. /*
  139.  * Definition of mostFood
  140.  * This function displays the most amount of food eaten between all of the
  141.  * monkeys
  142.  */
  143. void mostFood(int f[][DAYS], int m)
  144. {
  145. // Initialize Variable
  146. float most = f[0][0]; // OUTPUT: most amount of food
  147. // eaten by a monkey in a day
  148.  
  149. // Determine highest value
  150. for (int index = 0; index < m; index++)
  151. {
  152. for (int count = 0; count < DAYS; count++)
  153. {
  154. if(f[index][count] > most)
  155. most = f[index][count];
  156. }
  157. }
  158.  
  159. // Display most amount of food eaten
  160. cout << "The most amount of food eaten was: " << most << " lbs.\n";
  161. }
  162.  
  163.  
  164.  
Success #stdin #stdout 0.01s 5304KB
stdin
2
3
4
6
3
12
6
4
8
7
32
6
4
3
6
5
4
7
8
9
5
stdout
Please enter the amount each monkey ate each day of the week:
Enter the amount of food eaten per day for monkey number 1:
Enter the amount of food eaten per day for monkey number 2:
Enter the amount of food eaten per day for monkey number 3:
The average amount of food eaten per day by the family of monkeys was 20 lbs.
The least amount of food eaten was: 2 lbs.
The most amount of food eaten was: 32 lbs.