C++ program to print a spiral matrix
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
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