Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print a matrix of size n*n in spiral order using C#?
Printing a matrix in spiral order means traversing the matrix elements layer by layer from the outermost to the innermost layer. We start from the top-left corner and move in a clockwise direction: right, down, left, and up, then repeat for the inner layers.
Algorithm Steps
Step 1 − Print all elements of the top row from left to right
Step 2 − Print all elements of the rightmost column from top to bottom
Step 3 − Print all elements of the bottom row from right to left
Step 4 − Print all elements of the leftmost column from bottom to top
Step 5 − Repeat the process for the inner matrix layers
Syntax
Following is the basic structure for spiral matrix traversal −
while (top <= bottom && left <= right) {
// Print top row
// Print right column
// Print bottom row (if exists)
// Print left column (if exists)
}
Example
using System;
public class Matrix {
public void PrintMatrixInSpiralOrder(int rows, int cols, int[,] matrix) {
int top = 0, bottom = rows - 1;
int left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
// Print top row from left to right
for (int i = left; i <= right; i++) {
Console.Write(matrix[top, i] + " ");
}
top++;
// Print right column from top to bottom
for (int i = top; i <= bottom; i++) {
Console.Write(matrix[i, right] + " ");
}
right--;
// Print bottom row from right to left (if we still have rows)
if (top <= bottom) {
for (int i = right; i >= left; i--) {
Console.Write(matrix[bottom, i] + " ");
}
bottom--;
}
// Print left column from bottom to top (if we still have columns)
if (left <= right) {
for (int i = bottom; i >= top; i--) {
Console.Write(matrix[i, left] + " ");
}
left++;
}
}
}
}
class Program {
static void Main(string[] args) {
Matrix m = new Matrix();
int rows = 3;
int cols = 6;
int[,] matrix = {
{ 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 }
};
Console.WriteLine("Matrix in spiral order:");
m.PrintMatrixInSpiralOrder(rows, cols, matrix);
}
}
The output of the above code is −
Matrix in spiral order: 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
Square Matrix Example
Here's an example with a square matrix (n×n) −
using System;
class Program {
static void PrintSpiral(int[,] matrix, int n) {
int top = 0, bottom = n - 1;
int left = 0, right = n - 1;
while (top <= bottom && left <= right) {
// Top row
for (int i = left; i <= right; i++) {
Console.Write(matrix[top, i] + " ");
}
top++;
// Right column
for (int i = top; i <= bottom; i++) {
Console.Write(matrix[i, right] + " ");
}
right--;
// Bottom row
if (top <= bottom) {
for (int i = right; i >= left; i--) {
Console.Write(matrix[bottom, i] + " ");
}
bottom--;
}
// Left column
if (left <= right) {
for (int i = bottom; i >= top; i--) {
Console.Write(matrix[i, left] + " ");
}
left++;
}
}
}
static void Main() {
int[,] matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
Console.WriteLine("4x4 Matrix in spiral order:");
PrintSpiral(matrix, 4);
}
}
The output of the above code is −
4x4 Matrix in spiral order: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
How It Works
The algorithm uses four boundary variables: top, left, and right. In each iteration, we traverse one complete layer of the matrix and adjust these boundaries inward. The process continues until all elements are visited.
Conclusion
Spiral matrix traversal is achieved by systematically moving through matrix layers from outside to inside. The key is maintaining proper boundary conditions and updating them after each directional traversal to ensure all elements are printed exactly once.
