Phoenix_x

Clockwise Matrix

Feb 2nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. void left();
  7. void bottom();
  8. void right();
  9. void top();
  10. void printFrame();
  11.  
  12. const int x = 5;
  13. const int y = 5;
  14.  
  15. int fillNumber = 0;
  16. int goInside = 0;
  17. int matrix[y][x] =
  18. {
  19.     { 1,   2,   3,   4,   5 },
  20.     { 16,   17, 18,  19,  6 },
  21.     { 15,   24, 25, 20,   7 },
  22.     { 14,   23,  22, 21,  8 },
  23.     { 13,   12,  11, 10,  9 }
  24. };
  25.  
  26. void main()
  27. {
  28.     while (fillNumber <= x * y) { // 5 x 5 = 25
  29.         printFrame();
  30.         goInside++;
  31.     }
  32. }
  33.  
  34. void left()
  35. {
  36.     for (int i = y - 2 -goInside; i >= 1 + goInside; i--)
  37.     {
  38.         cout << matrix[i][0 + goInside] << " ";
  39.         fillNumber++;
  40.     }
  41. }
  42.  
  43. void right() {
  44.     for (int i = 1 + goInside; i < y - goInside; i++) {
  45.         cout << matrix[i][x - 1 - goInside] << " ";
  46.         fillNumber++;
  47.     }
  48. }
  49.  
  50. void  top() {
  51.     for (int i = 0 + goInside; i < x - goInside; i++) {
  52.         cout << matrix[0 + goInside][i] << " ";
  53.         fillNumber++;
  54.     }
  55. }
  56.  
  57. void bottom() {
  58.     for (int i = x - goInside - 2; i >= 0 + goInside; i--) {
  59.         cout << matrix[y - 1 - goInside][i] << " ";
  60.         fillNumber++;
  61.     }
  62. }
  63.  
  64. void printFrame()
  65. {
  66.     top();
  67.     right();
  68.     bottom();
  69.     left();
  70. }
Add Comment
Please, Sign In to add comment