Garey

Matrix-CPP

Feb 4th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. // Матриь.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. void print_array(int [], int [5][5]);
  10.  
  11. int main()
  12. {
  13.     int arr[25] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  14.                     16, 17, 18, 19, 20, 21, 22, 23, 24, 25 };
  15.     int mat[5][5];
  16.  
  17.     print_array(arr, mat);
  18.    
  19.     for (int i = 0; i < 5; i++)
  20.     {
  21.         for (int j = 0; j < 5; j++)
  22.             cout << setw(3) << mat[i][j];
  23.         cout << endl;
  24.     }
  25.    
  26.     return 0;
  27. }
  28.  
  29.  
  30. void print_array(int arr[], int mat[5][5])
  31. {
  32.     int top = 0, bottom = 5 - 1;
  33.     int left = 0, right = 5 - 1;
  34.  
  35.     int index = 0;
  36.  
  37.     while (1)
  38.     {
  39.         if (top > bottom)
  40.             break;
  41.         // lqva kolona
  42.         for (int i = top; i <= bottom; i++)
  43.             mat[i][left] = arr[index++];
  44.         left++;
  45.  
  46.         if (left > right)
  47.             break;
  48.         // dolen red
  49.         for (int i = left; i <= right; i++)
  50.             mat[bottom][i] = arr[index++];
  51.         bottom--;
  52.  
  53.         if (top > bottom)
  54.             break;
  55.         // dqsna kolona
  56.         for (int i = bottom; i >= top; i--)
  57.             mat[i][right] = arr[index++];
  58.         right--;
  59.  
  60.         if (left > right)
  61.             break;
  62.         // goren red
  63.         for (int i = right; i >= left; i--)
  64.             mat[top][i] = arr[index++];
  65.         top++;
  66.     }
  67. }
Add Comment
Please, Sign In to add comment