Professional Documents
Culture Documents
Spiral Print
Spiral Print
Problem Description
You are given a 2D vector of integers. You need to print the
elements of the vector in a spiral -like pattern.
There must be one question arising in your mind which is
what is a Spiral??
A long curved line which moves round and round away from
the central point.
Here this means it starts from the top-left corner of the matrix
and moves in a clockwise direction, visiting each element
once.
Example: -
INPUT - 2D Vector = {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20};
OUTPUT– 1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12
So this will be our pattern or the flow of the code int this
matrix.
We are going to initialise 8 variables to solve this question.
19. Now last loop of the spiral function for printing the
starting column of the matrix. Which prints from the
starting element of the updated ending row to updated
starting row’s element. After this it increments the starting
column. And vice- versa as the other loop did above.