Advertisement
Sitisom

Matrix2

Nov 25th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cmath>
  4. using namespace std;
  5.  
  6. void show(int **x, int n)
  7. {
  8.     for (int i = 0; i < n; i++) {
  9.         for (int j = 0; j < n; j++)
  10.             cout << x[i][j] << ' ';
  11.         cout << endl;
  12.     }
  13. }
  14.  
  15. int** create(int n) {
  16.     int **x = new int*[n];
  17.     for (int i = 0; i < n; i++)
  18.         x[i] = new int[n];
  19.     return x;
  20. }
  21.  
  22. int** mass_from_file(ifstream &f, int n) {
  23.     int **x = create(n);
  24.     for (int i = 0; i < n; i++)
  25.         for (int j = 0; j < n; j++)
  26.             f >> x[i][j];
  27.     return x;
  28. }
  29.  
  30. bool check_on_simple(int a) {
  31.     if (a <= 2&&a>0) return true;
  32.     else if (a % 2 == 0) return false;
  33.     else {
  34.         for (int i = 3; i <= (int)sqrt(a); i+=2)
  35.             if (a%i == 0)
  36.                 return false;
  37.     }
  38.    
  39.     return true;
  40. }
  41.  
  42. bool simple_num(int**x, int n) {
  43.     bool k;
  44.     for (int i = 0; i < n; i++) {
  45.         k = true;
  46.         for (int j = 0; j < n&&k; j++) {
  47.             k = false;
  48.             for (int p = 0; p < 4 && !k; p++) {
  49.                 k = check_on_simple(x[i][j]+2 - p);
  50.                 if (j == n - 1 && k)
  51.                     return true;
  52.             }
  53.         }
  54.     }
  55.     return false;
  56. }
  57.  
  58. bool sumFrom5To8(int a) {
  59.     int sum = 0;
  60.     if (5 <= a && a <= 8) return true;
  61.     else if (a > 10) {
  62.         while (a > 10) {
  63.             sum += (a % 10);
  64.             a /= 10;
  65.         }
  66.         sum += a;
  67.         if (5 <= sum && sum <= 8) return true;
  68.         else return false;
  69.     }
  70.     else return false;
  71. }
  72.  
  73. bool sumOfNumbers(int** x, int n) {
  74.     bool t = false;
  75.     for (int i = 0; i < n; i++)
  76.         for (int j = 0; j < n && !t; j++) {
  77.             if (sumFrom5To8(x[i][j]))
  78.                 t = true;
  79.             else if (!sumFrom5To8(x[i][j]) && j == n - 1)
  80.                 return false;
  81.         }
  82.     return true;
  83. }
  84.  
  85. int MaximumNum(int**x, int n) {
  86.     int maximum = x[0][0];
  87.     for (int i = 0; i < n; i++)
  88.         for (int j = 1; j < n; j++)
  89.             if (maximum < x[i][j]) maximum = x[i][j];
  90.     return maximum;
  91. }
  92.  
  93. bool fibonacchiSearcher(int n) {
  94.     if (n <= 2 && n!=0) return true;
  95.     int x = 1;
  96.     int y = 1;
  97.     int ans = 1;
  98.     while (ans <= n) {
  99.         if (ans == n)
  100.             return true;
  101.         ans = x + y;
  102.         x = y;
  103.         y = ans;
  104.     }
  105.     return false;
  106. }
  107.  
  108. bool fibFunc(int** x, int n) {
  109.     for (int i = 0; i < n; i++)
  110.         for (int j = 0; j < n; j++)
  111.             if (!fibonacchiSearcher(x[i][j])) return false;
  112.     return true;
  113. }
  114.  
  115. bool thePowerOf3(int** x, int n) {
  116.     bool t = false;
  117.     for (int i = 0; i < n && !t; i++)
  118.         for (int j = 0; j < n && !t; j++)
  119.             while (x[i][j] > 1) {
  120.                 if (x[i][j] % 3 == 0) {
  121.                     x[i][j] /= 3;
  122.                     if (x[i][j] == 1)
  123.                         t = true;
  124.                 }
  125.                 else break;
  126.             }
  127.     return false;
  128. }
  129.  
  130. bool isEven(int **x, int n) {
  131.     bool t;
  132.     for (int i = 0; i < n; i++) {
  133.         t = true;
  134.         for (int j = 0; j < n && t; j++) {
  135.             if (x[i][j] % 2 != 0)
  136.                 t = false;
  137.             else if (x[i][j] % 2 == 0 && j == n - 1)
  138.                 return true;
  139.         }
  140.     }
  141.     return false;
  142. }
  143.  
  144. int** summ(int **a, int **b, int n) {
  145.     int **y = create(n);
  146.     for (int i = 0; i < n; i++)
  147.         for (int j = 0; j < n; j++)
  148.             y[i][j] = a[i][j] + b[i][j];
  149.     return y;
  150. }
  151.  
  152. int** sub(int **a, int **b, int n) {
  153.     int **y = create(n);
  154.     for (int i = 0; i < n; i++)
  155.         for (int j = 0; j < n; j++)
  156.             y[i][j] = a[i][j] - b[i][j];
  157.     return y;
  158. }
  159.  
  160. int** mult(int **a, int **b, int n) {
  161.     int **y = create(n + 1);
  162.  
  163.     for (int i = 0; i < n; i++)
  164.         for (int j = 0; j < n; j++)
  165.         {
  166.             y[i][j] = 0;
  167.             for (int k = 0; k < n; k++)
  168.                 y[i][j] += a[i][k] * b[k][j];
  169.         }
  170.     return y;
  171. }
  172.  
  173. int** mult_on_num(int k, int **a, int n) {
  174.     for (int i = 0; i < n; i++)
  175.         for (int j = 0; j < n; j++)
  176.             a[i][j] *= k;
  177.     return a;
  178. }
  179.  
  180. int** funcForCalculatingC(int**a, int**b, int n) {
  181.     int **c = create(n);
  182.     if (isEven(b, n)) {
  183.         c = mult(summ(a, b, n), sub(b, a, n),n);
  184.     }
  185.     else {
  186.         c = summ(mult(mult_on_num(2, b, n), a, n), a, n);
  187.     }
  188.     return c;
  189. }
  190.  
  191. bool EjAiStratForSimpleNum(int** x, int n) {
  192.     bool t;
  193.     for (int i = 0; i < n; i++) {
  194.         t = true;
  195.         for (int j = 0; j < n && t; j++) {
  196.             if (!check_on_simple(x[j][i]))
  197.                 t = false;
  198.             else if (check_on_simple(x[j][i]) && j == n - 1)
  199.                 return true;
  200.         }
  201.     }
  202.     return false;
  203. }
  204.  
  205.  
  206. int main()
  207. {
  208.     ifstream in1("in1.txt");
  209.     ifstream in2("in2.txt");
  210.     int n;
  211.     cout << "Enter n: "; cin >> n;
  212.     int **a = mass_from_file(in1,n);
  213.     int **b = mass_from_file(in2, n);
  214.     int **c = create(n);
  215.     c = funcForCalculatingC(a, b, n);
  216.     //cout << "Checking on simple number (EA)........ Answer is: " << simple_num(a, n) << endl;
  217.     //cout << "Checking on sums (AE)..........Answer is: " << sumOfNumbers(a, n) << endl;
  218.     //cout << "Checking on fibonacchi (AA).........Answer is: " << fibFunc(a, n) << endl;
  219.     //cout << "Checking on third degree (EE)........Answer is: " << thePowerOf3(a, n) << endl;
  220.     //cout << "Checking on even numbers (EA)........Answer is: " << isEven(a, n) << endl;
  221.     //cout << "Array C is: " << endl;
  222.     //show(c, n);
  223.     //cout << "Checking on simple number (EjAi).........Answer is: " << EjAiStratForSimpleNum(a, n) << endl;
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement