Advertisement
Anglis

Untitled

Nov 24th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. #include<windows.h>
  4.  
  5. using namespace std;
  6.  
  7. //Memory block
  8.  
  9. int **memoryAllocation2(int **a, int &n, int &m) {
  10.     cout << "Memory is starting allocating";
  11.     a = new int*[n];
  12.     for (int i = 0; i < n; i++)
  13.         a[i] = new int[m];
  14.     cout << " -- Memmory allocated successfully" << endl;
  15.     return a;
  16. }
  17.  
  18. int *memoryAllocation1(int *a, int &n) {
  19.     cout << "Memory is starting allocating";
  20.     a = new int[n];
  21.     cout << " -- Memmory allocated successfully" << endl;
  22.     return a;
  23. }
  24.  
  25. int **memoryFreeing2(int **a, int &n) {
  26.     cout << "Memory is starting freeing";
  27.     for (int i = 0; i < n; i++)
  28.         delete[] a[i];
  29.     delete[] a;
  30.     cout << " -- Memmory freed successfully" << endl;
  31.     return a;
  32. }
  33.  
  34. int *memoryFreeing1(int *a) {
  35.     cout << "Memory is starting freeing";
  36.     delete[] a;
  37.     cout << " -- Memmory freed successfully" << endl;
  38.     return a;
  39. }
  40.  
  41. int *AddPtr(int *pp, int n, int elem){
  42.     if (n == 0)
  43.         pp = new int [n + 1];
  44.     else {
  45.         int *copy = new int[n + 1];
  46.         for (int i = 0; i < n; i++) {
  47.             copy[i] = pp[i];
  48.         }
  49.         delete[] pp;
  50.         pp = new int[n + 1];
  51.         pp = copy;
  52.     }
  53.     pp[n] = elem;
  54.     return pp;
  55. }
  56.  
  57. //Working with arrays
  58.  
  59. void arrayFilling2(int **a, int &n, int &m, int mode) {
  60.     if (mode) {
  61.         cout << "Array is starting filling";
  62.         srand(time(0));
  63.         for (int i = 0; i < n; i++)
  64.             for (int j = 0; j < m; j++)
  65.                 a[i][j] =  rand() % 100 + 1;
  66.         cout << " -- Array filled successfully" << endl;
  67.     }
  68.     else
  69.         for (int i = 0; i < n; i++)
  70.             for (int j = 0; j < m; j++)
  71.                 cin >> a[i][j];
  72. }
  73.  
  74. void arrayShowing1(int *a, int &n) {
  75.     cout << endl;
  76.     for (int i = 0; i < n; i++)
  77.             cout << "[" << a[i] << "]  ";
  78.     cout << endl;
  79. }
  80.  
  81. void arrayShowing2(int **a, int &n, int &m) {
  82.     cout << endl;
  83.     for (int i = 0; i < n; i++) {
  84.         for (int j = 0; j < m; j++)
  85.             cout << "[" << a[i][j] << "]\t";
  86.         cout << endl;
  87.     }
  88. }
  89.  
  90. bool isCompos(int a) {
  91.     return (a != 0 && a != 1 && a != 2 && a!= 3 && a != 5 && a != 7 && a != 11 )
  92.         && ((a % 2 == 0) || (a % 3 == 0) || (a % 5 == 0) || (a % 7 == 0) || (a % 11 == 0));
  93. }
  94.  
  95. int arrayFindMin(int *a, int &m) {
  96.     int min = a[0];
  97.     for (int j = 0; j < m; j++)
  98.         if (a[j] < min)
  99.             min = a[j];
  100.     return min;
  101.  
  102. }
  103.  
  104. int *arrayFindRowCompos(int **a, int *minA, int &n, int &m, int &k) {
  105.     bool flag = true;
  106.     for (int i = 0; i < n; i ++) {
  107.         for (int j = 0; j < m; j++) {
  108.             if (!isCompos(a[i][j])) {
  109.                 flag = false;
  110.                 break;
  111.             }
  112.         }
  113.         if (flag) {
  114.             minA = AddPtr(minA, k, arrayFindMin(a[i], m));
  115.             k++;
  116.         }
  117.         flag = true;
  118.     }
  119.     return minA;
  120. }
  121.  
  122. int gcd_2(int a, int b) {
  123.     int t;
  124.     while (b != 0) {
  125.         t = b;
  126.         b = a % b;
  127.         a = t;
  128.     }
  129.     return a;
  130. }
  131.  
  132. int getNOKab(int a, int b)
  133. {
  134.     cout << "GCD -\t " << gcd_2(a, b) << endl;
  135.     return a*b / gcd_2(a, b);
  136. }
  137.  
  138. void showArrayReDone(int *minA, int &n) {
  139.    
  140.     int M = 1;
  141.     for (int i = 0; i < n; i++) {
  142.         cout << "--------\nM before - \t" << M << endl;
  143.         cout << "minA["<< i <<"] - \t" << minA[i] << endl;
  144.         M = getNOKab(M, minA[i]);
  145.         cout << "M after - \t" << M << endl;
  146.     }
  147.    
  148.     cout << "HOK = " << ((M == 1) ? minA[0] : M) << endl;
  149. }
  150.  
  151.  
  152.  
  153.  
  154.  
  155. int main() {
  156.     int **a = 0;
  157.     int *minA = 0;
  158.     int n, m, k = 0, mode = 0; /*
  159.     cout << "Enter n of rows :";
  160.     cin >> n;
  161.     cout << "Enter n of colums :";
  162.     cin >> m;
  163.     cout << "0 - to fill by your self, 1 to fill random: ";
  164.     cin >> mode;*/ 
  165.     while (1) {
  166.         a = 0;
  167.         minA = 0;
  168.         k = 0, mode = 0;
  169.  
  170.         n = m = mode = 5;
  171.         a = memoryAllocation2(a, n, m);
  172.  
  173.         arrayFilling2(a, n, m, mode);
  174.         minA = arrayFindRowCompos(a, minA, n, m, k);
  175.  
  176.         arrayShowing2(a, n, m);
  177.         arrayShowing1(minA, k);
  178.  
  179.         if (k > 0)
  180.             showArrayReDone(minA, k);
  181.         else
  182.             cout << "No such numbers" << endl;
  183.  
  184.  
  185.         a = memoryFreeing2(a, n);
  186.         minA = memoryFreeing1(minA);
  187.         Sleep(1000);
  188.     }
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement