# Spiral form of matrix

Given a 2d matrix which containing S rows and C columns. Our goal is to print spiral view of this matrix in clockwise direction. Let see an example.

Here given code implementation process.

``````/*
C Program
+ Spiral form of 2d array
*/
#include<stdio.h>
#define ROW 7
#define COL 6

void spiral(int data[ROW][COL],
int s_row,
int s_col,
int e_row,
int e_col,
int element)
{

//Left to right
for (int i = s_col; i <=e_col && element > 0 ; ++i)
{
element--;
printf(" %3d",data[s_row][i]);
}
//Top to down
for (int i = s_row+1; i <=e_row && element > 0  ; ++i)
{
element--;
printf(" %3d",data[i][e_col]);
}
//Bottom right to bottom-left
for (int i = e_col-1; i >=s_col && element > 0 ; --i)
{
element--;
printf(" %3d",data[e_row][i]);
}

//Bottom left to top
for (int i =e_row-1 ; i > s_row && element > 0 ; --i)
{
element--;
printf(" %3d",data[i][s_row]);
}

if(s_row+1 <= e_row-1 && element > 0)
{
//Recursive call
spiral(data,s_row+1,s_col+1,e_row-1,e_col-1,element);
}

}
int main(){

int arr[ROW][COL]={
{1, 2, 3, 4, 5, 6},
{22,23,24,25,26,7},
{21,36,37,38,27,8},
{20,35,42,39,28,9},
{19,34,41,40,29,10},
{18,33,32,31,30,11},
{17,16,15,14,13,12},
};
int element=COL*(ROW);

spiral(arr,0,0,ROW-1,COL-1,element);

return 0;
}``````

#### Output

``   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``
``````//C++ Program
//Spiral form of 2d array
#include <iostream>
using namespace std;
#define ROW 7
#define COL 6

class MyArray
{

public:

void spiral ( int data[ROW][COL] ,
int s_row ,
int s_col ,
int e_row ,
int e_col ,
int element)
{

//Left to right
for  ( int i = s_col ;  i  <=  e_col  &&  element > 0  ;  ++i)
{
element-- ;
cout<<"  "<< data[s_row][i] ;
}
//Top to down
for  ( int i = s_row+1 ;  i  <=  e_row  &&  element > 0 ;  ++i)
{
element-- ;
cout<<"  "<< data[i][e_col] ;
}
//Bottom right to bottom-left
for  ( int i = e_col-1 ;  i >= s_col  &&  element > 0 ;  --i)
{
element-- ;
cout<<"  "<< data[e_row][i] ;
}

//Bottom left to top
for  ( int i  = e_row-1  ;  i > s_row  &&  element > 0 ;  --i)
{
element-- ;
cout<<"  "<< data[i][s_row] ;
}

if ( s_row+1 <=  e_row-1  &&  element > 0)
{
//Recursive call
spiral ( data , s_row+1 , s_col+1 , e_row-1 , e_col-1 , element) ;
}

}
};

int main()
{

MyArray obj;

int arr[ROW][COL] = {
{1 ,  2 ,  3 ,  4 ,  5 ,  6} ,
{22 , 23 , 24 , 25 , 26 , 7} ,
{21 , 36 , 37 , 38 , 27 , 8} ,
{20 , 35 , 42 , 39 , 28 , 9} ,
{19 , 34 , 41 , 40 , 29 , 10} ,
{18 , 33 , 32 , 31 , 30 , 11} ,
{17 , 16 , 15 , 14 , 13 , 12}
};
int element = COL *  ROW ;

obj.spiral ( arr , 0 , 0 , ROW-1 , COL-1 , element) ;

return 0;
}``````

#### Output

``  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``
``````//Java program
//Spiral form of 2d array
public class MyArray
{

public void  spiral ( int [][]data ,
int s_row ,
int s_col ,
int e_row ,
int e_col ,
int element)
{

//Left to right
for ( int i = s_col ;  i  <=  e_col  &&  element > 0  ;  ++i)
{
element-- ;
System.out.print("  "+ data[s_row][i]);
}
//Top to down
for ( int i = s_row+1 ;  i  <=  e_row  &&  element > 0 ;  ++i)
{
element-- ;
System.out.print("  "+ data[i][e_col]) ;
}
//Bottom right to bottom-left
for ( int i = e_col-1 ;  i >= s_col  &&  element > 0 ;  --i)
{
element-- ;
System.out.print("  "+ data[e_row][i]) ;
}

//Bottom left to top
for ( int i  = e_row-1  ;  i > s_row  &&  element > 0 ;  --i)
{
element-- ;
System.out.print("  "+ data[i][s_row] );
}

if ( s_row+1 <=  e_row-1  &&  element > 0)
{
//Recursive call
spiral ( data , s_row+1 , s_col+1 , e_row-1 , e_col-1 , element) ;
}

}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//array elements
int [][]matrix ={
{1 ,  2 ,  3 ,  4 ,  5 ,  6} ,
{22 , 23 , 24 , 25 , 26 , 7} ,
{21 , 36 , 37 , 38 , 27 , 8} ,
{20 , 35 , 42 , 39 , 28 , 9} ,
{19 , 34 , 41 , 40 , 29 , 10} ,
{18 , 33 , 32 , 31 , 30 , 11} ,
{17 , 16 , 15 , 14 , 13 , 12}
};
int row =  matrix.length;
int col =  matrix[0].length;
//Get the size of array elements
int element=row*col;
obj.spiral ( matrix , 0 , 0 , row-1 , col-1 , element) ;

}
}``````

#### Output

``  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``
``````#Python Program
#Spiral form of 2d array
class MyArray:

def spiral (self,
data ,
s_row ,
s_col ,
e_row ,
e_col ,
element) :

#Left to right
i=s_col
while(i  <=  e_col   and   element > 0 ) :

element -= 1

print( data[s_row][i],end="  ")
i += 1

#Top to down

i = s_row + 1

while (i  <=  e_row   and   element > 0 ) :

element -= 1

print(data[i][e_col],end="  ")
i += 1

#Bottom right to bottom-left
i = e_col-1
while ( i >= s_col and element > 0   --i) :

element -= 1

print(data[e_row][i],end="  ")
i -= 1

#Bottom left to top
i  = e_row-1
while ( i > s_row  and element > 0   ) :

element -= 1

print( data[i][s_row],end="  ")
i -= 1

if ( s_row+1 <=  e_row-1 and element > 0) :

#Recursive call
self.spiral ( data , s_row+1 , s_col+1 , e_row-1 , e_col-1 , element)

def main():

obj = MyArray()
matrix = [
[1 ,  2 ,  3 ,  4 ,  5 ,  6] ,
[22 , 23 , 24 , 25 , 26 , 7] ,
[21 , 36 , 37 , 38 , 27 , 8] ,
[20 , 35 , 42 , 39 , 28 , 9] ,
[19 , 34 , 41 , 40 , 29 , 10] ,
[18 , 33 , 32 , 31 , 30 , 11] ,
[17 , 16 , 15 , 14 , 13 , 12]
]
row = len(matrix)
col = len(matrix[0])
element = row * col

obj.spiral ( matrix , 0 , 0 , row-1 , col-1 , element)

if __name__ =="__main__":
main()``````

#### Output

``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  42``
``````//C# program
//Spiral form of 2d array
using System;
public class MyArray
{

public void  spiral ( int [,]data ,
int s_row ,
int s_col ,
int e_row ,
int e_col ,
int element)
{

//Left to right
for ( int i = s_col ;  i  <=  e_col  &&  element > 0  ;  ++i)
{
element-- ;
Console.Write("  "+ data[s_row,i]);
}
//Top to down
for ( int i = s_row+1 ;  i  <=  e_row  &&  element > 0 ;  ++i)
{
element-- ;
Console.Write("  "+ data[i,e_col]) ;
}
//Bottom right to bottom-left
for ( int i = e_col-1 ;  i >= s_col  &&  element > 0 ;  --i)
{
element-- ;
Console.Write("  "+ data[e_row,i]) ;
}

//Bottom left to top
for ( int i  = e_row-1  ;  i > s_row  &&  element > 0 ;  --i)
{
element-- ;
Console.Write("  "+ data[i,s_row] );
}

if ( s_row+1 <=  e_row-1  &&  element > 0)
{
//Recursive call
spiral ( data , s_row+1 , s_col+1 , e_row-1 , e_col-1 , element) ;
}

}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
//array elements
int [,]matrix ={
{1 ,  2 ,  3 ,  4 ,  5 ,  6} ,
{22 , 23 , 24 , 25 , 26 , 7} ,
{21 , 36 , 37 , 38 , 27 , 8} ,
{20 , 35 , 42 , 39 , 28 , 9} ,
{19 , 34 , 41 , 40 , 29 , 10} ,
{18 , 33 , 32 , 31 , 30 , 11} ,
{17 , 16 , 15 , 14 , 13 , 12}
};
int row =  matrix.GetLength(0);
int col =  matrix.GetLength(1);
//Get the size of array elements
int element=row*col;
obj.spiral ( matrix , 0 , 0 , row-1 , col-1 , element) ;

}
}``````

#### Output

``````  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
``````
``````<?php
//Php program
//Spiral form of 2d array

class MyArray
{

function  spiral(\$data,
\$s_row,
\$s_col,
\$e_row,
\$e_col,
\$element)
{

//Left to right
for  (\$i= \$s_col;  \$i <=  \$e_col &&  \$element>0 ; ++ \$i)
{
\$element--;
echo "  ". \$data[\$s_row][\$i];
}

//Top to down
for  (\$i= \$s_row+1;  \$i <=  \$e_row &&  \$element>0; ++ \$i)
{
\$element--;
echo "  ". \$data[\$i][\$e_col];
}

//Bottom right to bottom-left
for(\$i= \$e_col-1;  \$i>= \$s_col &&  \$element>0; --\$i)
{
\$element--;
echo "  ". \$data[\$e_row][\$i];
}

//Bottom left to top
for (\$i = \$e_row-1 ; \$i> \$s_row &&  \$element>0; --\$i)
{
\$element--;
echo "  ". \$data[\$i][\$s_row];
}

if ( \$s_row+1<=  \$e_row-1 &&  \$element>0)
{

//Recursive call
\$this->spiral( \$data, \$s_row+1, \$s_col+1, \$e_row-1, \$e_col-1, \$element);
}

}
}
function main()
{

\$obj= new MyArray();
\$matrix = array(
array(1 ,  2 ,  3 ,  4 ,  5 ,  6) ,
array(22 , 23 , 24 , 25 , 26 , 7) ,
array(21 , 36 , 37 , 38 , 27 , 8) ,
array(20 , 35 , 42 , 39 , 28 , 9) ,
array(19 , 34 , 41 , 40 , 29 , 10) ,
array(18 , 33 , 32 , 31 , 30 , 11) ,
array(17 , 16 , 15 , 14 , 13 , 12)
);

\$row = count(\$matrix);
\$col =count(\$matrix[0]);
\$element=\$row*\$col;
\$obj->spiral ( \$matrix , 0 , 0 , \$row-1 , \$col-1 , \$element) ;
}
main();
?>``````

#### Output

``  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``
``````# Ruby Program
# Spiral form of 2d array
class MyArray
def spiral(data, s_row, s_col, e_row, e_col, element)
# Left to right
i = s_col
while (i <= e_col and element > 0)
element -= 1
print(" ", data[s_row][i])
i += 1
end
# Top to down
i = s_row + 1
while (i <= e_row and element > 0)
element -= 1
print(" ", data[i][e_col])
i += 1
end
# Bottom right to bottom-left
i = e_col - 1
while (i >= s_col and element > 0)
element -= 1
print(" ", data[e_row][i])
i -= 1
end
# Bottom left to top
i = e_row - 1
while (i > s_row and element > 0)
element -= 1
print(" ", data[i][s_row])
i -= 1
end
if (s_row + 1 <= e_row - 1 and element > 0)
self.spiral(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element)
end
end
end
def main()
obj = MyArray.new()
matrix = [
[1, 2, 3, 4, 5, 6],
[22, 23, 24, 25, 26, 7],
[21, 36, 37, 38, 27, 8],
[20, 35, 42, 39, 28, 9],
[19, 34, 41, 40, 29, 10],
[18, 33, 32, 31, 30, 11],
[17, 16, 15, 14, 13, 12]
]
row = matrix.length
col = matrix[0].length
element = row * col
obj.spiral(matrix, 0, 0, row - 1, col - 1, element)
end
main()```
```

#### Output

`` 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``
``````/*
Swift 4 Program
Spiral form of 2d array
*/

class MyArray {
func spiral(_ data: [
[Int]
], _ s_row: Int, _ s_col: Int, _ e_row: Int, _ e_col: Int, _ size: Int) {
//Left to right
var i: Int = s_col;
var element : Int = size;
while (i <= e_col && element > 0) {
element -= 1;
print(" ", data[s_row][i],terminator:"");
i += 1;
}
//Top to down
i = s_row + 1;
while (i <= e_row && element > 0) {
element -= 1;
print(" ", data[i][e_col],terminator:"");
i += 1;
}
//Bottom right to bottom-left
i = e_col - 1;
while (i >= s_col && element > 0) {
element -= 1;
print(" ", data[e_row][i],terminator:"");
i -= 1;
}
//Bottom left to top
i = e_row - 1;
while (i > s_row && element > 0) {
element -= 1;
print(" ", data[i][s_row],terminator:"");
i -= 1;
}
if (s_row + 1 <= e_row - 1 && element > 0) {
self.spiral(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
}
}
}
func main() {
let obj: MyArray = MyArray();
let matrix: [
[Int]
] = [
[1, 2, 3, 4, 5, 6],
[22, 23, 24, 25, 26, 7],
[21, 36, 37, 38, 27, 8],
[20, 35, 42, 39, 28, 9],
[19, 34, 41, 40, 29, 10],
[18, 33, 32, 31, 30, 11],
[17, 16, 15, 14, 13, 12]
];
let row: Int = matrix.count;
let col: Int = matrix[0].count;
let element: Int = row * col;
obj.spiral(matrix, 0, 0, row - 1, col - 1, element);
}
main();```
```

#### Output

`` 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``
``````/*
Node Js Program
Spiral form of 2d array
*/

class MyArray {
spiral(data, s_row, s_col, e_row, e_col, element) {
//Left to right

for (var i = s_col; i <= e_col && element > 0; ++i) {
element--;
process.stdout.write(" " + data[s_row][i]);
}
//Top to down

for (var i = s_row + 1; i <= e_row && element > 0; ++i) {
element--;
process.stdout.write(" " + data[i][e_col]);
}
//Bottom right to bottom-left

for (var i = e_col - 1; i >= s_col && element > 0; --i) {
element--;
process.stdout.write(" " + data[e_row][i]);
}
//Bottom left to top

for (var i = e_row - 1; i > s_row && element > 0; --i) {
element--;
process.stdout.write(" " + data[i][s_row]);
}
if (s_row + 1 <= e_row - 1 && element > 0) {
//Recursive call
this.spiral(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
}
}
}

function main() {
var obj = new MyArray();
//array elements
var matrix = [
[1, 2, 3, 4, 5, 6],
[22, 23, 24, 25, 26, 7],
[21, 36, 37, 38, 27, 8],
[20, 35, 42, 39, 28, 9],
[19, 34, 41, 40, 29, 10],
[18, 33, 32, 31, 30, 11],
[17, 16, 15, 14, 13, 12]
];
var row = matrix.length;
var col = matrix[0].length;
//Get the size of array elements
var element = row *col;
obj.spiral(matrix, 0, 0, row - 1, col - 1, element);
}

main();```
```

#### Output

`` 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``
``````/*
Scala Program
Spiral form of 2d array
*/

class MyArray {
def spiral(data: Array[Array[Int]], s_row: Int, s_col: Int, e_row: Int, e_col: Int, size: Int) {

var i: Int = s_col;

var element : Int = size;
//Left to right
while (i <= e_col && element > 0) {
element -= 1;
print(" "+ data(s_row)(i));
i += 1;
}
//Top to down
i = s_row + 1;
while (i <= e_row && element > 0) {
element -= 1;
print(" "+data(i)(e_col));
i += 1;
}
//Bottom right to bottom-left
i = e_col - 1;
while (i >= s_col && element > 0) {
element -= 1;
print(" "+ data(e_row)(i));
i -= 1;
}
//Bottom left to top
i = e_row - 1;
while (i > s_row && element > 0) {
element -= 1;
print(" "+ data(i)(s_row));
i -= 1;
}
if (s_row + 1 <= e_row - 1 && element > 0) {
this.spiral(data, s_row + 1, s_col + 1, e_row - 1, e_col - 1, element);
}
}
}
object Main {
def main(args: Array[String])() {
var obj: MyArray = new MyArray();
var matrix: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5, 6), Array(22, 23, 24, 25, 26, 7), Array(21, 36, 37, 38, 27, 8), Array(20, 35, 42, 39, 28, 9), Array(19, 34, 41, 40, 29, 10), Array(18, 33, 32, 31, 30, 11), Array(17, 16, 15, 14, 13, 12));
var row: Int = matrix.length;
var col: Int = matrix(0).length;
var element: Int = row * col;
obj.spiral(matrix, 0, 0, row - 1, col - 1, element);
}
}```
```

#### Output

`` 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``

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.