Grid Size
2n × 2n−1
Width 2 * rows, height 2 * rows - 1 — a wide frame for the diamond.

This pattern frames a hollow diamond inside solid top and bottom bars: fixed width 2 * rows, height 2 * rows - 1, and mirrored middle rows built from left stars, a gap, and right stars. This tutorial covers the grid size, border vs inner logic, a live preview, algorithm steps, worked C examples, edge cases, and complexity.
2n × 2n−1
Width 2 * rows, height 2 * rows - 1 — a wide frame for the diamond.
Top & bottom
First and last lines are full runs of * with no interior gap.
Inner rows
Equal star blocks on both sides with a hollow gap between them.
iSymmetry
Map each line to i so the gap grows to the waist, then shrinks.
1–10 rows
Pick a size and draw the framed hollow diamond in the browser.
Complexity
2n-1 lines × 2n columns — O(n²) time, O(1) extra space.
A hollow diamond inside a square is a framed console figure: solid star bars on the first and last lines, and a hollow diamond carved through the middle rows.
Unlike Program 9 (standalone hollow diamond) or Program 10 (filled diamond), every line here is exactly 2 * rows characters wide, and middle rows are built as left stars + gap + right stars.
It trains fixed-width grids, border special cases, and mirrored indices in one figure — a strong capstone after simpler triangles and diamonds.
Every line is 2 * rows characters.
Top and bottom bars close the “square” frame.
Left stars, hollow gap, right stars.
i grows then shrinks with line position.
In short: solid bars on the ends; elsewhere print left stars, gap spaces, left stars — with i mirrored so the hollow diamond opens and closes.
Given a positive integer rows, print a framed hollow diamond with width 2 * rows and height 2 * rows - 1.
// rows = 5 (conceptual shape; spaces shown as ·)
// **********
// ****··****
// ***····***
// **······**
// *········*
// **······**
// ***····***
// ****··****
// ********** | Item | Type | Description |
|---|---|---|
rows | int | Size parameter (typically ≥ 1). Controls both width and height. |
| Printed output | text | 2*rows-1 lines, each exactly 2*rows characters before the newline. |
height = 2 * rows - 1
width = 2 * rows
for line from 1 to height:
if line is first or last:
print width stars
else:
i = line if line <= rows else (2 * rows - line)
left = rows - i + 1
gap = 2 * (i - 1)
print left stars, gap spaces, left stars
newline | Approach | Idea | Best for |
|---|---|---|
| Three-segment loops | Left / gap / right on inner rows | Learning and interviews |
print_chars helper | Build each segment as a string | Shorter demos after formulas click |
| Goal | Pattern |
|---|---|
| Dimensions | height = 2 * rows - 1, width = 2 * rows |
| Solid bar | if (line == 1 || line == height) print width stars |
Map line → i | i = (line <= rows) ? line : (2 * rows - line) |
| Left / right stars | left = rows - i + 1 |
| Hollow gap | gap = 2 * (i - 1) |
| Width check | 2 * left + gap == width |
Three diamond-related patterns — different frames and fills.
framed hollowSolid bars + left/gap/right; width 2n
hollow aloneOutline only; fixed width 2n-1 per line
solid diamondFull star runs; tip rows shorter than middle
state dimensionsSay width/height first, then border vs inner cases
Reach for this figure when you need a fixed-width frame with a hollow diamond interior.
Often the last numbered exercise after triangles and diamonds.
Every line must close at the same column count.
Special-case first/last rows; formula-drive the middle.
Practice folding a line index into a mirrored i.
Terminal teaching pattern — not how you build framed widgets in apps.
Key benefit: one pattern that combines dimensions, border cases, segment math, and mirrored indices.
Choose a size between 1 and 10 and draw the framed hollow diamond in the browser.
Three complete C programs — fixed size, console input, and a print_chars helper. Click View Output to reveal sample console results.
Print the classic rows = 5 framed diamond with nested loops.
rows = 5Solid bars on the ends; left / gap / right on every other line.
#include <stdio.h>
int main(void) {
int rows = 5;
int line, j;
int height = 2 * rows - 1;
int width = 2 * rows;
for (line = 1; line <= height; ++line) {
if (line == 1 || line == height) {
for (j = 1; j <= width; ++j) {
printf("*");
}
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (j = 1; j <= left; ++j) {
printf("*");
}
for (j = 1; j <= gap; ++j) {
printf(" ");
}
for (j = 1; j <= left; ++j) {
printf("*");
}
}
printf("\n");
}
return 0;
} Lines 1 and 9 are solid 10-star bars. On line 5 (the waist), i = 5, so left = 1 and gap = 8 — a single star on each side with a wide hollow center. Lines above and below mirror that formula via the ternary for i.
Let the user choose the size at runtime.
Read rows with scanf("%d", &rows) (check the return value in real apps).
#include <stdio.h>
int main(void) {
int rows;
int line, j;
int height, width;
printf("Enter the number of rows: ");
scanf("%d", &rows);
height = 2 * rows - 1;
width = 2 * rows;
for (line = 1; line <= height; ++line) {
if (line == 1 || line == height) {
for (j = 1; j <= width; ++j) {
printf("*");
}
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (j = 1; j <= left; ++j) {
printf("*");
}
for (j = 1; j <= gap; ++j) {
printf(" ");
}
for (j = 1; j <= left; ++j) {
printf("*");
}
}
printf("\n");
}
return 0;
} Same grid logic as Example 1; only the source of rows changes. For rows = 4 you get 7 lines × 8 columns. Non-numeric input leaves rows unset if you ignore scanf’s return value — always check it in safer labs.
Same figure with a reusable print_chars helper for each segment.
print_chars HelperEncode each segment once with print_chars; the outer loop only picks counts.
#include <stdio.h>
void print_chars(char ch, int n) {
int j;
for (j = 1; j <= n; ++j) {
putchar(ch);
}
}
int main(void) {
int rows = 5;
int line;
int height = 2 * rows - 1;
int width = 2 * rows;
for (line = 1; line <= height; ++line) {
if (line == 1 || line == height) {
print_chars('*', width);
putchar('\n');
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
print_chars('*', left);
print_chars(' ', gap);
print_chars('*', left);
putchar('\n');
}
}
return 0;
} Same formulas as Example 1; print_chars replaces the three inner loops. Keep the loop version for exams that want every bound visible.
height = 2 * rows - 1 lines; width = 2 * rows characters per line. For rows = 4: 7 × 8.
When line == 1 or line == height, print a solid run of width stars — the closed horizontal edges.
Map line → i, then left = rows - i + 1 and gap = 2 * (i - 1). Print left stars, gap spaces, left stars again.
printf("\n") after the bar or the three segments. Every line is exactly width characters before the newline.
O(n²) for n = rows (2n-1 lines × up to 2n cells), O(1) extra space.
rows = 4Trace each line: whether it is a solid bar, and if not, the values of i, left, and gap.
| line | Kind | i | left | gap | Printed row |
|---|---|---|---|---|---|
1 | Bar | — | — | — | ******** |
2 | Inner | 2 | 3 | 2 | *** *** |
3 | Inner | 3 | 2 | 4 | ** ** |
4 | Inner | 4 | 1 | 6 | * * |
5 | Inner | 3 | 2 | 4 | ** ** |
6 | Inner | 2 | 3 | 2 | *** *** |
7 | Bar | — | — | — | ******** |
Check: on every inner row, 2 * left + gap = 8 = width. Lines 3 and 5 share the same i because of mirroring.
Where this framed hollow diamond (and its grid thinking) shows up beyond the homework prompt.
Combines borders, segments, and mirroring in one program.
Example: final lab after Programs 9 and 10.
Assert 2*left + gap == width while debugging.
Example: print lengths before printf("\n").
Side-by-side with Program 9 shows why structures differ.
Example: same rows, different width rules.
One loop over columns with border/left/right predicates.
Example: for (j = 1; j <= width; j++) with booleans.
Swap border vs interior characters once geometry works.
Example: # on bars, * on sides.
Fixed-size grids make O(n²) easy to count by hand.
Example: cells = (2n-1)*2n.
Pro Tip: in interviews, state “width 2n, height 2n-1, solid caps, then left/gap/right with mirrored i” before writing a single loop.
Why this framed layout is a strong teaching pattern.
2 * left + gap == width catches off-by-one bugs immediately.
Top/bottom bars are obvious; middle rows share one formula.
A single ternary for i keeps upper and lower halves in sync.
Streaming output needs only counters and a few ints.
Pro Tip: learn the three-loop segment version first; treat print_chars as a polish shortcut afterward.
Small habits that keep framed-diamond code clean.
Compute width and height once — do not mix 2*rows and 2*rows-1 by accident.
Special-case top and bottom before writing the inner formula.
Mentally check 2 * left + gap == width on a middle and a near-tip row.
scanf’s return valueAlways check that scanf returns 1 when reading interactive sizes.
rows = 4The walkthrough table above catches mirror and gap mistakes fast.
Pro Tip: if a line is shorter or longer than the others, you almost certainly used the wrong formula for left or gap.
Mistakes that commonly break the framed hollow diamond.
Using 2*rows-1 as width (or 2*rows as height) skews the whole figure.
→ Width is 2*rows; height is 2*rows-1.
Different coordinate system — i == j style loops will not match this 10-wide picture for rows = 5.
→ Use left / gap / right for this page.
Forgetting 2 * rows - line breaks lower-half symmetry.
→ i = (line <= rows) ? line : (2 * rows - line).
Padding after the right star block makes lines longer than width.
→ Stop after the second left-star run.
Failed scanf leaves rows uninitialized.
→ Check scanf’s return value and require rows >= 1.
Check these inputs before calling the solution done.
Height = 1, width = 2 — only one solid line ** (first == last).
3 lines × 4 columns: bar, * *, bar.
Loop never runs — validate and re-prompt.
rows < 0Treat as invalid; do not print a broken grid.
Width grows as 2n — fine for labs; may wrap on tiny terminals.
Failed scanf leaves rows unset — check its return value.
Try these variations to lock in the pattern.
rows2n vs 2n-1j = 1..widthscanf returns 1 and re-prompt until rows >= 12n, height 2n - 1 for n = rows.2 * left + gap == width — use that as a sanity check.rows > 0 for interactive programs; rows = 1 collapses to a single two-star bar.Quick Takeaway: solid bars on the ends; elsewhere left stars, hollow gap, right stars — with mirrored i and fixed width 2 * rows.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
print_chars helper (Example 3) | O(rows²) | O(rows) temporary per segment |
About 2 * rows - 1 lines; each prints 2 * rows characters — overall Θ(n²).
The hollow diamond inside a square is a fixed-width frame: solid top and bottom bars, and mirrored left / gap / right rows in between. Once the dimensions and the i mapping click, the rest is careful segment printing.
Practice the three examples above, then browse the star-pattern hub to revisit earlier triangles and diamonds.
Width 2n, height 2n-1, solid caps, then left/gap/right with mirrored i — and keep 2*left + gap == width.
i with left and gap formulas2 * left + gap == widthscanf’s return value for interactive demos2*rows and 2*rows-1rows = 1 edge casePrint the hollow diamond inside a square the beginner-friendly way.
2n wide, 2n−1 tall
SizeSolid top & bottom
BorderLeft / gap / right
InnerMap line → i
SymmetryO(n²) time
AnalysisEvery line is exactly 2 * rows characters wide. Inner rows always satisfy 2 * left + gap == 2 * rows — so the frame closes cleanly on both sides.
Review Programs 9 and 10, then explore more C topics from the hub.
12 people found this page helpful