C++ program to print a spiral matrix
A spiral matrix is a matrix consist of natural numbers up to n^2. Starting from one the numbers start increasing around the corner matrix then goes inside up to the middle element making the shape of the spiral or jalebi
For example: A spiral matrix for n=5
Algorithm:
In this algorithm the size of the array decreases by 2 for every iteration.
We starts with filling the first row then last column then last row after that the first column that means we will fill array around the corner and then for next iteration we will take the array inside the last array and fill it around the corner and so on until we reach the centre.
For first iteration array will be:
Here the empty or un-accessed elements are denoted by "0".
For next iteration array will be:
Here "_" represents the already accessed elements and “0”represents the un-accessed elements.
The size of the array goes on decreasing until you reach the centre most array element.
Consider the program:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer