Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "cmath"
  3. #include <iostream>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. bool func(float a, float b, float c, float d, float S)
  8. {
  9.     if ((a + b + c + d) < S) { return true; }
  10.     else { return false; }
  11. }
  12.  
  13. int main()
  14. {
  15.     srand(time(NULL));
  16.  
  17.     int n, m;
  18.     float S;
  19.  
  20.     cout << "Enter n and m" << endl;
  21.     cin >> n >> m;
  22.     cout << "Enter S" << endl;
  23.     cin >> S;
  24.  
  25.     float **a = new float*[n];
  26.     for (int i = 0; i < n; i++)
  27.         a[i] = new float;
  28.     for (int i = 0; i < n; i++)
  29.         for (int j = 0; j < m; j++)
  30.             a[i][j] = rand()%500;
  31.  
  32.     cout << "Initial array" << endl;
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         for (int j = 0; j < m; j++)
  36.             cout << a[i][j] << " ";
  37.         cout << endl;
  38.     }
  39.  
  40.     bool **b = new bool*[n];
  41.     for (int i = 0; i < n; i++)
  42.         b[i] = new bool;
  43.     for (int i = 0; i < n; i++)
  44.         for (int j = 0; j < m; j++)
  45.             b[i][j] = false;
  46.  
  47.     for (int i = 0; i < n; i++)
  48.         for (int j = 0; j < m; j++)
  49.             if ((i == 0) || (i == (n - 1)) || (j == 0) || (j == (m - 1)))
  50.             {
  51.                 if ((i == 0) && (j == 0)) { if ((a[i + 1][j] + a[i][j + 1]) < S) b[i][j] = true; }
  52.                 else if ((i == 0) && (j == (m - 1))) { if ((a[i + 1][j] + a[i][j - 1]) < S) b[i][j] = true; }
  53.                 else if ((i == (n - 1)) && (j == 0)) { if ((a[i - 1][j] + a[i][j + 1]) < S) b[i][j] = true; }
  54.                 else if ((i == (n - 1)) && (j == (m - 1))) { if ((a[i - 1][j] + a[i][j - 1]) < S) b[i][j] = true; }
  55.                 else if (i == 0) { if ((a[i + 1][j] + a[i][j - 1] + a[i][j + 1]) < S) b[i][j] = true; }
  56.                 else if (j == 0) { if ((a[i - 1][j] + a[i + 1][j] + a[i][j + 1]) < S) b[i][j] = true; }
  57.                 else if (i == (n - 1)) { if ((a[i - 1][j] + a[i][j - 1] + a[i][j + 1]) < S) b[i][j] = true; }
  58.                 else if (j == (m - 1)) { if ((a[i - 1][j] + a[i][j - 1] + a[i + 1][j]) < S) b[i][j] = true; }
  59.             }
  60.             else { b[i][j] = func(a[i - 1][j], a[i + 1][j], a[i][j - 1], a[i][j + 1], S); }
  61.  
  62.     cout << "Resulting array:" << endl;
  63.     for (int i = 0; i < n; i++)
  64.     {
  65.         for (int j = 0; j < m; j++)
  66.             cout << b[i][j] << " ";
  67.         cout << endl;
  68.     }
  69.  
  70.     system("pause");
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement