fork download
  1. // Andres Guzman CSC5 Chapter 7, P. 444, #4
  2. /******************************************************************************
  3.  *
  4.  * CALCULATE MONKEY FOOD
  5.  * ____________________________________________________________________________
  6.  * This program will display the food intake of 3 monkeys and calculate their
  7.  * daily average consumption as a family, and find the highest and lowest
  8.  * intake amongst each monkey
  9.  *
  10.  * Computation
  11.  *
  12.  * total / (MONKEY * DAYS)
  13.  * ____________________________________________________________________________
  14.  *
  15.  * INPUT
  16.  * MONKEY = Size of rows
  17.  * DAYS = Size of Colomns
  18.  * week[MONKEY][DAYS] = Subscripts
  19.  * weeklytotal[MONKEY] = Subscript Accumulator
  20.  *
  21.  * OUTPUT
  22.  * highest = Displays highest amount of food eaten
  23.  * lowest = Displays lowest amount of food eaten
  24.  * total = accumulated total
  25.  *
  26.  ******************************************************************************/
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. int main()
  31. {
  32. const int MONKEY = 3; //Size of row
  33. const int DAYS = 7; //Size of Colomns
  34. int week[MONKEY][DAYS]; //Subscripts
  35. int m; //counter for month
  36. int d; //counter for days
  37. double total = 0; //accumulator
  38. double weeklytotal[MONKEY] = {0}; //subscript accumulator
  39. //
  40. //Initializing For Loop for subscript inputs in rows
  41. for(m = 0; m < MONKEY; m++)
  42. {
  43. cout << "Monkey " << (m + 1) << " Weekly Consumption" << endl;
  44. //
  45. //Nested For Loop for colomns
  46. for(d = 0; d < DAYS; d++)
  47. {
  48. cout << "Day " << (d + 1) << ": ";
  49. cin >> week[m][d];
  50. while(week[m][d] < 0) //Input Validation
  51. {
  52. cout << "Invalid Amount Eaten, Please Try Again.";
  53. cout << "\nDay " << (d + 1) << ": ";
  54. cin >> week[m][d];
  55. }
  56. total += week[m][d]; //Adds each weeks total to variable
  57. weeklytotal[m] += week[m][d];//Adds week total to row subscript
  58. cout << endl;
  59. }
  60. }
  61. double highest = weeklytotal[0]; //Assigns subscript [0] to highest
  62. double lowest = weeklytotal[0]; //Assigns subscript [0] to lowest
  63. //
  64. //Initializing For Loop
  65. for (m = 0; m < MONKEY; m++)
  66. {
  67. if(weeklytotal[m] > highest) //Determines which subscript is
  68. //greater than highest
  69. {
  70. highest = weeklytotal[m];//Assigns subscript to highest
  71. }
  72. if(weeklytotal[m] < lowest) //Determines which subscript is
  73. //lower than lowest
  74. {
  75. lowest = weeklytotal[m];//Assigns subscript to lowest
  76. }
  77. }
  78. cout << "Average Amount of Food Eaten: " << (total / (MONKEY * DAYS));
  79. cout << "\nLeast Amount of Food Eaten during the Week: " << lowest << endl;
  80. cout << "Most Amount of Food Eaten during the Week: " << highest << endl;
  81. return 0;
  82. }
Success #stdin #stdout 0.01s 5328KB
stdin
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
stdout
Monkey 1 Weekly Consumption
Day 1: 
Day 2: 
Day 3: 
Day 4: 
Day 5: 
Day 6: 
Day 7: 
Monkey 2 Weekly Consumption
Day 1: 
Day 2: 
Day 3: 
Day 4: 
Day 5: 
Day 6: 
Day 7: 
Monkey 3 Weekly Consumption
Day 1: 
Day 2: 
Day 3: 
Day 4: 
Day 5: 
Day 6: 
Day 7: 
Average Amount of Food Eaten: 9
Least Amount of Food Eaten during the Week: 21
Most Amount of Food Eaten during the Week: 21