A concentric number diamond peels layers from outer value k down to 1 at the center, then mirrors back out to k — a square grid of size 2k - 1 on each side.
Remember
Rule: same cell as Program 46 — (j > i ? j : i)
Plus a bottom half: i = 2..k after the top half
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5 ← k = 5
Unlike Program 46 (top half only), this shape adds a second outer loop so the diamond is complete and symmetric.
Approach
How to Solve It
Reuse Program 46’s row builder twice: peel i = k..1, then mirror i = 2..k.
Method
Idea
Best for
Two outer loops
Top k..1, bottom 2..k, same inner halves
Learning, interviews, demos
Ternary cells
cout << (j > i ? j : i) in both halves
Compact code once the rule clicks
Pseudocode
Pseudocode
for i from k down to 1: // top half
print left half and right half with (j > i ? j : i)
print newline
for i from 2 to k: // bottom half (skip center)
print left half and right half with (j > i ? j : i)
print newline
// left: j from k down to 1
// right: j from 2 to k
Cheat sheet
Goal
Pattern
Top half
for (i = k; i >= 1; i--)
Bottom half
for (i = 2; i <= k; i++)
Left / right
j = k..1 / j = 2..k
Cell value
j > i ? j : i
Print cell
cout << value << " "
End the row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << n << " "
Stays on the same line
Each cell
cout << "\n"
Ends the current line
After both inner loops
Print cells without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.
Try it
Live Preview
Change outer value k and the full concentric diamond updates instantly.
Use k from 2 to 7. Tap a chip or type a value — the preview redraws as you go.
1. Same two-pass structure. Only the source of k changes from a literal to cin.
2. Grid size follows k. Entering 4 yields a 7 × 7 diamond (2k - 1).
3. Validate in real apps. Prefer checking cin failure and requiring k >= 1 (tip below).
Safer input tip
if (!(cin >> k) || k < 1)
{
cout << "Please enter a positive integer.\n";
return 1;
}
Example 3 — Compact k = 3
Smaller outer value for quick tracing — five rows, easy to dry-run.
C++
#include <iostream>
using namespace std;
int main()
{
int k = 3;
int i, j;
for (i = k; i >= 1; i--)
{
for (j = k; j >= 1; j--)
cout << (j > i ? j : i) << " ";
for (j = 2; j <= k; j++)
cout << (j > i ? j : i) << " ";
cout << "\n";
}
for (i = 2; i <= k; i++)
{
for (j = k; j >= 1; j--)
cout << (j > i ? j : i) << " ";
for (j = 2; j <= k; j++)
cout << (j > i ? j : i) << " ";
cout << "\n";
}
return 0;
}
Output
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
How It Works
1. Five rows total. Top prints three rows (i = 3..1); bottom adds two more (i = 2..3).
2. Same formula. Nothing changes except k — proving the pattern scales.
3. Center stays 1. The middle row is always the deepest layer before the mirror begins.
Edge Cases & Pitfalls
Check these before calling the solution done.
Bottom starts at 2
Do not start the bottom loop at 1
Starting at 1 duplicates the center row. Use for (i = 2; i <= k; i++).
Missing bottom
Forgetting the second outer loop
That is Program 46 (square / top half only). Add the bottom pass for the full diamond.
Right starts at 2
Do not start the right half at 1
Starting at 1 duplicates the center cell on every row.
Bad cin
Validate input
Check cin >> k and require k >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Full diamond (Examples 1–3)
O(k²)
O(1)
The grid is (2k - 1) × (2k - 1), so work is quadratic in k. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print j when j > i, otherwise print i.
Two passes: top i = k..1, bottom i = 2..k — size 2k - 1.
Break the row: call cout << "\n" only after both inner loops.
Complexity:O(k²) time; O(1) extra space.
One line: print Program 46’s square for i = k..1, then repeat for i = 2..k to finish the diamond.
Frequently Asked Questions
A full concentric number diamond where the outer layer is k (e.g. 5), values decrease to 1 at the center, then increase back to k — a 9×9 grid when k = 5.
The first prints the top half (i = k down to 1). The second prints the bottom half (i = 2 up to k) so the center row is not duplicated.
Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
When column j is outside the current layer i, print j (outer ring). Otherwise print i (inner plateau).
Each row has 2*k - 1 numbers. With k = 5, the grid is 9 columns and 9 rows.
Printing a number (and space) stays on the same line. Printing a newline ends the current line. Cells use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(k²) because the grid has (2k-1)² cells and each is printed once.
The center prints 1 — the deepest layer of the concentric pattern.
🤔
Did you know?
Top half: i = k..1. Bottom half: i = 2..k (skip center). Grid size = (2k - 1) × (2k - 1) — for k = 5 that is 9 × 9 = 81 prints.