Before coding the example of a matrix spiral, we need to see how to create a matrix in C++. To create a matrix in C++, we use a 2D array, and we can declare it as the following.

int mat[M][N] ={ { 1, 2, 3, 4, 5}, {16, 17, 18, 19, 6}, {15, 24, 25, 20, 7}, {14, 23, 22, 21, 8}, {13, 12, 11, 10, 9}};

So here, we declare a matrix of M rows and N columns and fill in the values row by row. The numbers in 2 curly braces, separated by a comma, depict a row. The numbers in the matrix will be of integer type because the matrix is an integer 2d array. Before we start with the code, let’s first talk about the algorithm to understand it better.

- The algorithm starts from the top left corner of the array and traverses the first row from left to right, and increments the value of the top corner index
- Then it traverses the rightmost column top to bottom. Once this completes, it decrements the right corner index.
- The algorithm traverses the last row and decrements the bottom corner index.
- Lastly, the algorithm traverses the leftmost column, incrementing the left corner index.
- These steps work in continuation until the left index is greater than the right index and the bottom index is greater than the top index.

Now let’s begin with the real deal and write the code to print a spiral matrix of the above-shown matrix. The code is written below:

#include <iostream> using namespace std; #define M 5 #define N 5 void printSpiralOrder(int mat[M][N]) { int top = 0, bottom = M - 1; int left = 0, right = N - 1; while (1) { if (left > right) { break; } // print top row for (int i = left; i <= right; i++) { cout << mat[top][i] << " "; } top++; if (top > bottom) { break; } // Since we have traversed the whole first // row, move down to the next row. // print right column for (int i = top; i <= bottom; i++) { cout << mat[i][right] << " "; } right--; if (left > right) { break; } // print bottom row for (int i = right; i >= left; i--) { cout << mat[bottom][i] << " "; } bottom--; if (top > bottom) { break; } // print left column for (int i = bottom; i >= top; i--) { cout << mat[i][left] << " "; } left++; } } int main() { int mat[M][N] = { { 1, 2, 3, 4, 5}, {16, 17, 18, 19, 6}, {15, 24, 25, 20, 7}, {14, 23, 22, 21, 8}, {13, 12, 11, 10, 9} }; printSpiralOrder(mat); return 0; }

So in the code, above we have first written the function to print the matrix in spiral order. In the main function, we have declared a matrix passed to the printSpiralOrder() function. The function first declares the variables (left, right, top, bottom) for easy traversal in the matrix. Then it has an infinite while loop and breaks conditions were necessary to jump to other loops.

So, we start with the first row where we are looping until the row reaches the maximum index, i.e., 4, and print the whole row. Then we move forward and then do the same thing for the bottom row and so on until we are finished printing the matrix in a spiral form. When the matrix reaches the spiral form, one of the if meets the condition, and the loop breaks. ConclusionThe above solution is just one way of printing a spiral matrix. However, there can be other ways of doing the same thing using stack data structures or vectors.

Source: https://www.codeunderscored.com/printing-matrix-spiral-in-c/