Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Question- Spiral Print a Matrix

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.

Our memory is allocated like this.

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

What are we going to do??


We are going to print it in this way…...

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.

The variables are for: -


1. Number of rows in the matrix.
2. Number of columns in the matrix.
3. Total number of elements in the matrix.
4. Starting row index.
5. Ending col index.
6. Ending row index.
7. Starting column index.
8. Count variable for keeping track of elements traversed so
far.
By using this we are going to solve this question.

What will be our solution approach??


1. First we will make a 2D vector.
2. We will make one function. And then call it.
3. Now we will move to our spiral print function.
4. We will initialise a vector to store the answer.
5. Initialise a variable for number of rows in the matrix.
6. Initialise a variable for number of columns in the matrix.
7. Initialise a variable for total number of elements in the
matrix.
8. Initialise a variable for starting row index.
9. Initialise a variable for ending row index.
10. Initialise a variable for ending column index.
11. Initialise a variable for starting column index.
12. Initialise a count variable for keeping track of elements
traversed so far.
13. We will use a while loop now. Which says that if the
count is less than the total elements of the matrix.
14. Now we will have a loop for the first row of the matrix.
In this loop the elements of the matrix in the first row will
be printed. The elements will be pushed in the answer
vector made previously. The count variable which keeps
the check over the elements of the matrix pushed in is also
incremented.
15. The starting row is incremented an updated to the next.
16. Now there is a check that if the count of the variable
which keep the check over the elements of the matrix
pushed is greater than or equal to of the total elements of
the matrix. If it is true, it will immediately break the while
loop.
17. Now coming to another loop to print downwards from
the last element of the updated starting row to the last
row’s last element. After this decrements the ending
column. And does all the same as the first loop.
18. Now coming to the third loop. It is for printing the
ending column’s last element to starting column’s last
element. After this it will decrement the last row. And
same as the other loops above did.

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.

20. Print the answer vector.


So, this was the explanation of my spiral function’s
explanation. I hope it sound clear for you.

Till then Byee…….

You might also like