Advertisement
Washi

Untitled

Oct 15th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. static const int DIRECTION_UP = 0;
  7. static const int DIRECTION_DOWN = 1;
  8. static const int DIRECTION_LEFT = 2;
  9. static const int DIRECTION_RIGHT = 3;
  10.  
  11. int main()
  12. {
  13.     int matrix[10][10], rows, columns;
  14.  
  15.     // Ask for matrix dimensions.
  16.     cout << "Enter the amount of rows: ";
  17.     cin >> rows;
  18.     cout << "Enter the amount of columns: ";
  19.     cin >> columns;
  20.  
  21.     // Ask for matrix elements.
  22.     for (int row = 0; row < rows; row++)
  23.     {
  24.         cout << "Enter values for row " << (row + 1) << ": ";
  25.         for (int column = 0; column < columns; column++)
  26.         {
  27.             cin >> matrix[row][column];
  28.         }
  29.     }
  30.  
  31.     // Define current movement direction in the matrix.
  32.     int direction = DIRECTION_RIGHT;
  33.    
  34.     // Define current position in matrix.
  35.     int currentRow = 0, currentColumn = 0;
  36.  
  37.     // Define valid boundaries of current position. Initially this spans the entire matrix
  38.     // and will shrink in size during the spiral movement.
  39.     int minColumn = 0, maxColumn = columns - 1;
  40.     int minRow = 0, maxRow = rows - 1;
  41.  
  42.     // Perform spiral.
  43.     for (int i = 0; i < rows*columns; i++)
  44.     {
  45.         // Print current element.
  46.         cout << matrix[currentRow][currentColumn] << " ";
  47.        
  48.         // Move in current direction, and see for each case if we have to change direction and shrink the boundaries.
  49.         switch (direction)
  50.         {
  51.         case DIRECTION_RIGHT:
  52.             currentColumn++;
  53.             if (currentColumn == maxColumn)
  54.             {
  55.                 minRow++;
  56.                 direction = DIRECTION_DOWN;
  57.             }
  58.             break;
  59.         case DIRECTION_DOWN:
  60.             currentRow++;
  61.             if (currentRow == maxRow)
  62.             {
  63.                 maxColumn--;
  64.                 direction = DIRECTION_LEFT;
  65.             }
  66.             break;
  67.         case DIRECTION_LEFT:
  68.             currentColumn--;
  69.             if (currentColumn == minColumn)
  70.             {
  71.                 maxRow--;
  72.                 direction = DIRECTION_UP;
  73.             }
  74.             break;
  75.         case DIRECTION_UP:
  76.             currentRow--;
  77.             if (currentRow == minRow)
  78.             {
  79.                 minColumn++;
  80.                 direction = DIRECTION_RIGHT;
  81.             }
  82.             break;
  83.         }
  84.     }
  85.  
  86.     // Prevent from exiting.
  87.     char x;
  88.     cin >> x;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement