fork download
  1. //Xinnuo Wang CS1A chapter7 #4
  2. /*******************************************************************************
  3.  * Display Amount of Food
  4.  * _____________________________________________________________________________
  5.  * The program accepts pounds of food each of its three monkeys eats each day
  6.  * during a typical week and diaplays average amount of food eaten per day by
  7.  * the whole family of monkeys, the least amount of food eaten during the week
  8.  * by any one monkey and the greatest amount of food eaten during the week by
  9.  * any one monkey.
  10.  *
  11.  * _____________________________________________________________________________
  12.  * Input:
  13.  * food : The accepted amount of food of different monkey in the dimentional array.
  14.  * Output:
  15.  * ave : Average amount of food eaten per day by the whole family of monkeys.
  16.  * Largest : The greatest amount of food eaten during the week by any one monkey.
  17.  * Smallest : The least amount of food eaten during the week by any one monkey.
  18.  *
  19.  * ****************************************************************************/
  20.  
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int main()
  25. {
  26. double food[3][7];
  27. double monkey[3];//The amount of food eaten during the week by any one monkey.
  28. double sum = 0, ave = 0;
  29. double Largest = 0;
  30. double Smallest = 0;
  31.  
  32. for (int r = 0; r < 3; r++)
  33. {
  34. for (int c = 0; c< 7; c++)
  35. {
  36. cout << "Enter amount of food of the monkey "<< (r+1)<< " on day ";
  37. cout << (c + 1);
  38. cin >> food[r][c];
  39. cout << food[r][c]<< endl;
  40. //validate check
  41. while(food[r][c] < 0)
  42. {
  43. return 0;
  44. }
  45. sum +=food[r][c];
  46. monkey[r]+=food[r][c];
  47. }
  48. }
  49. ave = sum / 7;
  50. Largest = monkey[0];
  51. Smallest =monkey[0];
  52. for(int i = 0; i < 3; i++ )
  53. {
  54. if (Largest < monkey[i])
  55. Largest = monkey[i];
  56. if (Smallest > monkey[i])
  57. Smallest= monkey[i];
  58. }
  59. // OUTPUT RESULT
  60. cout << "Average amount of food eaten per day by the whole family of monkeys";
  61. cout<< "is: " << ave << endl;
  62. cout << "The least amount of food eaten during the week by any one monkey is";
  63. cout<< ": " << Smallest << endl;
  64. cout << "The greatest amount of food eaten during the week by any one monkey ";
  65. cout << "is: " << Largest << endl;
  66. return 0;
  67. }
Success #stdin #stdout 0.01s 5324KB
stdin
123.3 223.2 111.2 111 113 114 144 
221 221.2 112 112.3 111.1 222 111
134 234 333 222 111 222.2 111.1
222.1 222 222 111 110 88.8 112
stdout
Enter amount of food of the monkey 1 on day 1123.3
Enter amount of food of the monkey 1 on day 2223.2
Enter amount of food of the monkey 1 on day 3111.2
Enter amount of food of the monkey 1 on day 4111
Enter amount of food of the monkey 1 on day 5113
Enter amount of food of the monkey 1 on day 6114
Enter amount of food of the monkey 1 on day 7144
Enter amount of food of the monkey 2 on day 1221
Enter amount of food of the monkey 2 on day 2221.2
Enter amount of food of the monkey 2 on day 3112
Enter amount of food of the monkey 2 on day 4112.3
Enter amount of food of the monkey 2 on day 5111.1
Enter amount of food of the monkey 2 on day 6222
Enter amount of food of the monkey 2 on day 7111
Enter amount of food of the monkey 3 on day 1134
Enter amount of food of the monkey 3 on day 2234
Enter amount of food of the monkey 3 on day 3333
Enter amount of food of the monkey 3 on day 4222
Enter amount of food of the monkey 3 on day 5111
Enter amount of food of the monkey 3 on day 6222.2
Enter amount of food of the monkey 3 on day 7111.1
Average amount of food eaten per day by the whole family of monkeysis: 488.229
The least amount of food eaten during the week by any one monkey is: 939.7
The greatest amount of food eaten during the week by any one monkey is: 1367.3