A perfect square spiral (spiral matrix) fills an n×n grid with 1..n² by walking clockwise around shrinking border rings — starting at the top-left.
Remember
Rule: fill top → right → bottom ← left ↑, then shrink boundaries
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7 ← n = 4
Unlike Program 61 (1D digit loop), this finale uses a 2D array and four moving boundaries — a classic interview spiral-matrix fill.
Approach
How to Solve It
Allocate an n×n array. While boundaries are valid, fill four sides of the current ring, then move inward. Print with setw(4).
Method
Idea
Best for
Four boundaries
top / bottom / left / right rings
Learning, interviews, any n
low / high rings
Symmetric square layers (classic 10×10)
Fixed even sizes
Pseudocode
Pseudocode
top = 0, bottom = n-1, left = 0, right = n-1, val = 1
while top <= bottom and left <= right:
fill top row left→right; top++
fill right column top→bottom; right--
if top <= bottom: fill bottom row right→left; bottom--
if left <= right: fill left column bottom→top; left++
print grid with width 4
1. Four walls.top, bottom, left, and right describe the current ring.
2. Guard single-row rings. The if (top <= bottom) / if (left <= right) checks avoid double-filling the last row or column.
3. Validate in real apps. Prefer checking cin failure and capping n to the array size (tip below).
Safer input tip
if (!(cin >> n) || n < 1 || n > 20)
{
cout << "Please enter an integer from 1 to 20.\n";
return 1;
}
Example 3 — Compact 4×4 Trace
Sixteen cells — easy to dry-run every boundary move on paper.
C++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n = 4;
int a[4][4];
int top = 0, bottom = 3, left = 0, right = 3, val = 1;
int i, j;
while (top <= bottom && left <= right)
{
for (j = left; j <= right; j++)
a[top][j] = val++;
top++;
for (i = top; i <= bottom; i++)
a[i][right] = val++;
right--;
if (top <= bottom)
{
for (j = right; j >= left; j--)
a[bottom][j] = val++;
bottom--;
}
if (left <= right)
{
for (i = bottom; i >= top; i--)
a[i][left] = val++;
left++;
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
cout << setw(4) << a[i][j];
cout << "\n";
}
return 0;
}
Output
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
How It Works
1. Two rings only. Outer 1..12, then inner 13..16 — perfect for a paper dry-run.
2. Same formula. Nothing changes except n — proving the spiral scales.
3. Center closes. The last values land at 13 14 / 16 15 before boundaries cross.
Edge Cases & Pitfalls
Check these before calling the solution done.
Double fill
Guard bottom and left sides
Without if (top <= bottom) / if (left <= right), a single-row or single-column ring can be filled twice.
Alignment
Include <iomanip> for setw
Without fixed width, multi-digit numbers break the grid visually.
Array size
Cap n to your declared max
Example 2 uses a[20][20] — reject sizes above 20 to avoid out-of-bounds writes.
n = 1
One cell is still a spiral
A 1×1 grid prints a single 1 — the while loop fills only the top side once.
Analysis
Time and Space Complexity
Program
Time
Extra space
Spiral fill + print (Examples 1–3)
O(n²)
O(n²) for the array
Every cell is written once and printed once — quadratic in n. The 2D array uses O(n²) memory; streaming print without storage is possible but harder to follow.
Remember
Key Takeaways
Rule: fill top → right → bottom → left, then shrink boundaries.
Guard rings: check before filling bottom and left to avoid double writes.
Align:setw(4) keeps multi-digit columns neat.
Complexity:O(n²) time and array space.
One line: walk each border clockwise, tighten the walls, repeat until the center is filled.
Frequently Asked Questions
A perfect square spiral (spiral matrix): an n×n grid filled with 1..n² in a clockwise spiral. For n=10 that is 1 through 100.
They mark the current layer. After filling top, right, bottom, and left, you move them inward and fill the next ring.
cout << setw(4) << value (from <iomanip>) prints each number in a fixed width of 4 characters.
Program 61 uses a while loop on one number. Program 62 fills an n×n grid with a 2D array and boundary-based spiral loops.
Yes. This is the classic spiral matrix generation exercise: fill the grid while shrinking boundaries.
Yes. The boundary approach works for any positive n, odd or even.
Printing a padded number stays on the same line. Printing a newline ends the current row after the column loop.
O(n²) for an n×n grid because each cell is filled and printed once.
🤔
Did you know?
A perfect square spiral fills an n×n grid with numbers 1..n² by walking each border layer clockwise and tightening boundaries — runtime is O(n²).