Advertisement
vencinachev

Matrix - diagonal

Dec 15th, 2020
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. #define EXAMPLE
  5.  
  6. using namespace std;
  7.  
  8. string acronym(string text);
  9.  
  10. int main()
  11. {
  12.     const int MAX = 20;
  13.     #ifdef EXAMPLE
  14.     int matrix[MAX][MAX] = {{1, 2, 3, 4},
  15.                             {5, 6 ,7 ,8},
  16.                             {9,10,11,12},
  17.                             {13,14,15,16}};
  18.     int n = 4;
  19.     #else
  20.     int matrix[MAX][MAX];
  21.     int n;
  22.     do
  23.     {
  24.         cout << "Enter n: ";
  25.         cin >> n;
  26.     }
  27.     while (n < 1 || n > MAX);
  28.  
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         for (int j = 0; j < n; j++)
  32.         {
  33.             cin >> matrix[i][j];
  34.         }
  35.     }
  36.     #endif
  37.  
  38.     for (int i = 0; i < n; i++)
  39.     {
  40.         for (int row = i, col = 0; row >= 0 ; row--, col++)
  41.         {
  42.             cout << matrix[row][col] << " ";
  43.         }
  44.         cout << endl;
  45.     }
  46.  
  47.     for (int i = 1; i < n ; i++)
  48.     {
  49.         for (int col = i, row = n - 1; col < n; row--, col++)
  50.         {
  51.             cout << matrix[row][col] << " ";
  52.         }
  53.         cout << endl;
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement