Advertisement
axyd

Lab 7, part 3

May 5th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const int rows = 3;
  6. const int cols = 5;
  7.  
  8. void err(int);
  9. void mega(int[rows][cols], double&, double&, double&);
  10.  
  11. int main()
  12. {
  13.     cout << "\t\t\t<<< Monkey Food Tracker >>>\n";
  14.     char again = 'y';
  15.     while (again == 'y' || again == 'Y'){
  16.         int megaRay[rows][cols], meat = 0;
  17.  
  18.         //Input Block
  19.         for (int r = 0; r < rows; r++){
  20.             cout << "\n\t\t\t ## Monkey " << r + 1 << " Food Eaten ##\n";
  21.             for (int c = 0; c < cols; c++){
  22.                 cout << "Day " << c + 1 << ": ";
  23.                 cin >> meat;
  24.  
  25.                 //check meat input for letters and negatives
  26.                 while (cin.fail() || meat < 1){
  27.                     err(meat);
  28.                     cout << "Day " << c + 1 << ": ";
  29.                     cin >> meat;
  30.                 }
  31.                 megaRay[r][c] = meat; //assign meat to array position
  32.             }
  33.         }
  34.  
  35.         double avgm = 0, least = 0, greatest = 0;
  36.         mega(megaRay, avgm, least, greatest);
  37.         cout << "\nAverage amount of food eaten: " << avgm << endl
  38.             << "The least amount of food eaten: " << least << endl
  39.             << "The greatest amount of food eaten: " << greatest << endl;
  40.  
  41.         cout << "\n\t\tEnter y or Y to go again: ";
  42.         cin >> again;
  43.     }
  44. }
  45.  
  46. void err(int meatIn){ //fix letter or negative number
  47.     if (cin.fail())
  48.         cout << "\t\t/!\\ Not a number /!\\\n";
  49.     else if (meatIn < 1)
  50.         cout << "\t\t/!\\ Negative number /!\\\n";
  51.     cin.clear();
  52.     fflush(stdin);
  53. }
  54.  
  55. void mega(int mRay[rows][cols], double& avgOut, double& min, double& max){ //number of days is scalable
  56.     double m1 = 0, m2 = 0, m3 = 0;
  57.     for (int i = 0; i < rows; i++){
  58.         if (i == 0){
  59.             for (int j = 0; j < cols; j++)
  60.                 m1 += mRay[i][j];
  61.         }
  62.         else if (i == 1){
  63.             for (int k = 0; k < cols; k++)
  64.                 m2 += mRay[i][k];
  65.         }
  66.         else if (i == 2){
  67.             for (int l = 0; l < cols; l++)
  68.                 m3 += mRay[i][l];
  69.         }
  70.     }
  71.     avgOut = (m1 + m2 + m3) / (cols * 3);
  72.  
  73.     //min
  74.     if (m1 < m2 && m1 < m3)
  75.         min = m1;
  76.     else if (m2 < m1 && m2 < m3)
  77.         min = m2;
  78.     else if (m3 < m1 && m3 < m2)
  79.         min = m3;
  80.  
  81.     //max
  82.     if (m1 > m2 && m1 > m3)
  83.         max = m1;
  84.     else if (m2 > m1 && m2 > m3)
  85.         max = m2;
  86.     else if (m3 > m1 && m3 > m2)
  87.         max = m3;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement