Pesterevaev

pesterevaev_week15_task1_1

Feb 17th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <random>
  4. #include <ctime>
  5.  
  6. typedef int myInt;
  7.  
  8. using namespace std;
  9.  
  10. int action(int* arrayA, int* arrayB, int sizeA, int sizeB, int(*func[3])(int*, int)) {
  11.     cout << "Enter the array you are going to work with(1 - arrayA or 2 - arrayB): ";
  12.     int numArray;
  13.     cin >> numArray;
  14.     cout << "Enter the action which you want to perform with the array: " << endl;
  15.     cout << "1 - to find maximum" << endl;
  16.     cout << "2 - to find minimum" << endl;
  17.     cout << "3 - to find average" << endl;
  18.     int numAct;
  19.     cin >> numAct;
  20.     switch (numArray) {
  21.         case 1: {
  22.             switch (numAct) {
  23.             case 1: return (*func[0])(arrayA, sizeA); break;
  24.             case 2: return (*func[1])(arrayA, sizeA); break;
  25.             case 3: return (*func[2])(arrayA, sizeA); break;
  26.             }
  27.             break;
  28.         }
  29.         case 2: {
  30.             switch (numAct) {
  31.             case 1: return (*func[0])(arrayB, sizeB); break;
  32.             case 2: return (*func[1])(arrayB, sizeB); break;
  33.             case 3: return (*func[2])(arrayB, sizeB); break;
  34.             }
  35.             break;
  36.         }
  37.     }
  38.  
  39. }
  40.  
  41. int max(int* array, int size) {
  42.     int max = INT_MIN;
  43.     for (int i = 0; i < size; ++i) {
  44.         if (array[i] > max) max = array[i];
  45.     }
  46.     cout << "Max = " << max;
  47.     return max;
  48. }
  49.  
  50. int min(int* array, int size) {
  51.     int min = INT_MAX;
  52.     for (int i = 0; i < size; ++i) {
  53.         if (array[i] < min) min = array[i];
  54.     }
  55.     cout << "Min = " << min;
  56.     return min;
  57. }
  58.  
  59. int avg(int* array, int size) {
  60.     double sum = 0.0;
  61.     for (int i = 0; i < size; ++i) {
  62.         sum += array[i];
  63.     }
  64.     double avg = sum / size;
  65.     cout << "Avg = " << fixed << setprecision(2) << avg;
  66.     return avg;
  67. }
  68.  
  69. void print(int* array, int size) {
  70.     for (int i = 0; i < size; ++i) {
  71.         cout << array[i] << " ";       
  72.     }  
  73.     cout << endl;
  74. }
  75.  
  76. int main()
  77. {
  78.     random_device rand;
  79.     mt19937 gen(time(NULL));
  80.     uniform_int_distribution<> result(1, 100);
  81.     const int sizeA = 10;
  82.     int arrayA[sizeA] = {};
  83.     for (int i = 0; i < sizeA; ++i) {
  84.         arrayA[i] = result(gen);
  85.     }
  86.     const int sizeB = 15;
  87.     int arrayB[sizeB] = {};
  88.     for (int i = 0; i < sizeB; ++i) {
  89.         arrayB[i] = result(gen);
  90.     }
  91.     print(arrayA, sizeA);
  92.     print(arrayB, sizeB);
  93.     int(*func[3])(int*, int) = { max, min, avg };
  94.     action(arrayA, arrayB, sizeA, sizeB, func);
  95.     return 0;
  96. }
Add Comment
Please, Sign In to add comment