In this program, you will learn how to print spiral matrix on the output console. The spiral matrix or circular matrix of order n*n in both clockwise and anti-clockwise directions. see the following example:

Example: Program to Display Spiral Matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#include <vector> // for auto_ptr #include <memory> // for the ceil and log10 and floor functions #include <cmath> #include <iostream> // for the setw function #include <iomanip> using namespace std; typedef vector<int> IntRow; typedef vector<IntRow> IntTable; auto_ptr<IntTable> getSpiralArray(int dimension) { auto_ptr<IntTable> spiralArrayPtr(new IntTable( dimension, IntRow(dimension))); int numConcentricSquares = static_cast< int >( ceil( static_cast< double >( dimension ) / 2.0)); int j; int sideLen = dimension; int currNum = 0; for (int i = 0; i < numConcentricSquares; i++) { // do top side for (j = 0; j < sideLen; j++) (*spiralArrayPtr)[i][i + j] = currNum++; // do right side for (j = 1; j < sideLen; j++) (*spiralArrayPtr)[i + j][dimension - 1 - i] = currNum++; // do bottom side for (j = sideLen - 2; j > -1; j--) (*spiralArrayPtr)[dimension - 1 - i][i + j] = currNum++; // do left side for (j = sideLen - 2; j > 0; j--) (*spiralArrayPtr)[i + j][i] = currNum++; sideLen -= 2; } return spiralArrayPtr; } void printSpiralArray(const auto_ptr<IntTable> &spiralArrayPtr) { size_t dimension = spiralArrayPtr->size(); int fieldWidth = static_cast< int >( floor(log10( static_cast< double >( dimension * dimension - 1 )))) + 2; size_t col; for (size_t row = 0; row < dimension; row++) { for (col = 0; col < dimension; col++) cout << setw(fieldWidth) << (*spiralArrayPtr)[row][col]; cout << endl; } } int main() { printSpiralArray(getSpiralArray(5)); return 0; } |
1 2 3 4 5 |
0 1 2 3 4 15 16 17 18 5 14 23 24 19 6 13 22 21 20 7 12 11 10 9 8 |