SHARE
TWEET

wkejfbow

a guest Jan 24th, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iomanip>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.     int size;
  7.     cin >> size;
  8.     int m = 0; 
  9.     int n = 0; 
  10.     int** matrix;
  11.     matrix = new int* [size];
  12.     for (int i = 0; i < size; i++)
  13.         matrix[i] = new int[size];
  14.  
  15.     for (int i = 0; i < size; ++i)
  16.     {
  17.         for (int j = 0; j < size; ++j)
  18.         {
  19.             matrix[i][j] = 0;
  20.         }
  21.     }
  22.  
  23.  
  24.     for (int count = 0; count < size*size - 1;)
  25.     {
  26.         for (int i = n; i < size - n - 1; ++i)
  27.         {
  28.             matrix[m][i] = count++;
  29.         }
  30.         for (int i = m; i < size - m - 1; ++i)
  31.         {
  32.             matrix[i][size - n - 1] = count++;
  33.         }
  34.         for (int i = size - n - 1; i > m; --i)
  35.         {
  36.             matrix[size - m - 1][i] = count++;
  37.         }
  38.         for (int i = size - m - 1; i > n; --i)
  39.         {
  40.             matrix[i][n] = count++;
  41.         }
  42.         ++m;
  43.         ++n;
  44.     }
  45.  
  46.     if (size % 2 != 0)
  47.     {
  48.         matrix[size / 2][size / 2] = size*size - 1;
  49.     }
  50.  
  51.     for (int i = 0; i < size; ++i)
  52.     {
  53.         for (int j = 0; j < size; ++j)
  54.         {
  55.             cout << matrix[i][j] << ' ';
  56.  
  57.         }
  58.         cout << endl;
  59.     }
  60.     return 0;
  61. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top