
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Spiral Matrix in C++
Suppose we have a matrix and we have to print the matrix elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion. So if the matrix is like −
1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 |
Then the output will be like [1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 15 16]
To solve this, we will follow these steps −
currRow := 0 and currCol := 0
while currRow and currCol are in the matrix range
for i in range currCol and n-1,
display mat[currRow, i]
increase currRow by 1
for i in range currRow and m-1, do
display mat[i, n-1]
decrease n by 1
if currRow < m, then
for i := n-1 down to currCol, do
display mat[m-1, i]
decrease m by 1
if currCol < n, then
for i := m-1 down to currRow, do
display mat[i, currCol]
increase currCol by 1
Example(C++)
Let us see the following implementation to get a better understanding −
#include <iostream> #define ROW 3 #define COL 6 using namespace std; int array[ROW][COL] = {{1, 2, 3, 4, 5, 6}, {7, 8, 9, 10, 11, 12}, {13, 14, 15, 16, 17, 18}}; void dispSpiral(int m, int n){ int i, currRow = 0, currCol = 0; while (currRow < ROW && currCol < COL){ for (i = currCol; i < n; i++){ //print the first row normally cout << array[currRow][i]<<" "; } currRow++; //point to next row for (i = currRow; i < m; ++i){ //Print the last column cout << array[i][n-1]<<" "; } n--; //set the n-1th column is current last column if ( currRow < m){ //when currRow is in the range, print the last row for (i = n-1; i >= currCol; --i){ cout << array[m-1][i]<<" "; } m--; //decrease the row range } if (currCol < n){ //when currCol is in the range, print the fist column for (i = m-1; i >= currRow; --i){ cout << array[i][currCol]<<" "; } currCol++; } } } int main(){ dispSpiral(ROW, COL); }
Input
[[1,2,3,4,5,6] [7,8,9,10,11,12] [13,14,15,16,17,18]]
Output
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 15 16
- Related Articles
- Spiral Matrix III in C++
- Spiral Matrix II in Python
- Print a given matrix in reverse spiral form in C++
- Print Matrix in spiral way\n
- Print a given matrix in counter-clockwise spiral form in C++
- Print a matrix in a spiral form starting from a point in C++
- Program to print matrix elements in spiral order in python
- Print n x n spiral matrix using O(1) extra space in C Program.
- How to print a matrix of size n*n in spiral order using C#?
- Java program to print a given matrix in Spiral Form.
- Maximum spiral sum in Binary Tree in C++
- C program to represent the numbers in spiral pattern
- C program to represent the alphabets in spiral pattern
- Anti Clockwise spiral traversal of a binary tree in C++?
- C++ Program to Print Spiral Pattern of Numbers
