Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<ctime>
- #include<windows.h>
- using namespace std;
- //Memory block
- int **memoryAllocation2(int **a, int &n, int &m) {
- cout << "Memory is starting allocating";
- a = new int*[n];
- for (int i = 0; i < n; i++)
- a[i] = new int[m];
- cout << " -- Memmory allocated successfully" << endl;
- return a;
- }
- int *memoryAllocation1(int *a, int &n) {
- cout << "Memory is starting allocating";
- a = new int[n];
- cout << " -- Memmory allocated successfully" << endl;
- return a;
- }
- int **memoryFreeing2(int **a, int &n) {
- cout << "Memory is starting freeing";
- for (int i = 0; i < n; i++)
- delete[] a[i];
- delete[] a;
- cout << " -- Memmory freed successfully" << endl;
- return a;
- }
- int *memoryFreeing1(int *a) {
- cout << "Memory is starting freeing";
- delete[] a;
- cout << " -- Memmory freed successfully" << endl;
- return a;
- }
- int *AddPtr(int *pp, int n, int elem){
- if (n == 0)
- pp = new int [n + 1];
- else {
- int *copy = new int[n + 1];
- for (int i = 0; i < n; i++) {
- copy[i] = pp[i];
- }
- delete[] pp;
- pp = new int[n + 1];
- pp = copy;
- }
- pp[n] = elem;
- return pp;
- }
- //Working with arrays
- void arrayFilling2(int **a, int &n, int &m, int mode) {
- if (mode) {
- cout << "Array is starting filling";
- srand(time(0));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- a[i][j] = rand() % 100 + 1;
- cout << " -- Array filled successfully" << endl;
- }
- else
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> a[i][j];
- }
- void arrayShowing1(int *a, int &n) {
- cout << endl;
- for (int i = 0; i < n; i++)
- cout << "[" << a[i] << "] ";
- cout << endl;
- }
- void arrayShowing2(int **a, int &n, int &m) {
- cout << endl;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++)
- cout << "[" << a[i][j] << "]\t";
- cout << endl;
- }
- }
- bool isCompos(int a) {
- return (a != 0 && a != 1 && a != 2 && a!= 3 && a != 5 && a != 7 && a != 11 )
- && ((a % 2 == 0) || (a % 3 == 0) || (a % 5 == 0) || (a % 7 == 0) || (a % 11 == 0));
- }
- int arrayFindMin(int *a, int &m) {
- int min = a[0];
- for (int j = 0; j < m; j++)
- if (a[j] < min)
- min = a[j];
- return min;
- }
- int *arrayFindRowCompos(int **a, int *minA, int &n, int &m, int &k) {
- bool flag = true;
- for (int i = 0; i < n; i ++) {
- for (int j = 0; j < m; j++) {
- if (!isCompos(a[i][j])) {
- flag = false;
- break;
- }
- }
- if (flag) {
- minA = AddPtr(minA, k, arrayFindMin(a[i], m));
- k++;
- }
- flag = true;
- }
- return minA;
- }
- int gcd_2(int a, int b) {
- int t;
- while (b != 0) {
- t = b;
- b = a % b;
- a = t;
- }
- return a;
- }
- int getNOKab(int a, int b)
- {
- cout << "GCD -\t " << gcd_2(a, b) << endl;
- return a*b / gcd_2(a, b);
- }
- void showArrayReDone(int *minA, int &n) {
- int M = 1;
- for (int i = 0; i < n; i++) {
- cout << "--------\nM before - \t" << M << endl;
- cout << "minA["<< i <<"] - \t" << minA[i] << endl;
- M = getNOKab(M, minA[i]);
- cout << "M after - \t" << M << endl;
- }
- cout << "HOK = " << ((M == 1) ? minA[0] : M) << endl;
- }
- int main() {
- int **a = 0;
- int *minA = 0;
- int n, m, k = 0, mode = 0; /*
- cout << "Enter n of rows :";
- cin >> n;
- cout << "Enter n of colums :";
- cin >> m;
- cout << "0 - to fill by your self, 1 to fill random: ";
- cin >> mode;*/
- while (1) {
- a = 0;
- minA = 0;
- k = 0, mode = 0;
- n = m = mode = 5;
- a = memoryAllocation2(a, n, m);
- arrayFilling2(a, n, m, mode);
- minA = arrayFindRowCompos(a, minA, n, m, k);
- arrayShowing2(a, n, m);
- arrayShowing1(minA, k);
- if (k > 0)
- showArrayReDone(minA, k);
- else
- cout << "No such numbers" << endl;
- a = memoryFreeing2(a, n);
- minA = memoryFreeing1(minA);
- Sleep(1000);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement