A hollow diamond inside a square frame sits in a fixed grid: width 2 * rows, height 2 * rows - 1. Solid star bars close the top and bottom; every other line is left stars, a hollow gap, then right stars.
Unlike Program 9 (standalone hollow diamond) or Program 10 (filled diamond), every line here is exactly 2 * rows characters. Middle rows use a mirrored index i so the gap opens to the waist and closes again.
Approach
How to Solve It
Two ways to emit the same shape — start with nested loops, then optionally shorten with a print_chars helper.
Method
Idea
Best for
Nested loops
Bar vs left / gap / right via printf
Learning, interviews, exams
print_chars helper
Reuse one loop for each segment
Cleaner demos once formulas click
Pseudocode
Pseudocode
height = 2 * rows - 1
width = 2 * rows
for line from 1 to height:
if line is 1 or height:
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
print newline
Cheat sheet
Goal
Pattern
Grid size
height = 2 * rows - 1, width = 2 * rows
Solid bars
line == 1 || line == height → print width stars
Mirror index
i = (line <= rows) ? line : (2 * rows - line)
Side stars
left = rows - i + 1
Hollow gap
gap = 2 * (i - 1)
Width check
2 * left + gap == width
End the row
printf("\n");
Printing Stars vs Starting a New Line
API
Effect
Use for
printf("*") / printf(" ")
Stays on the same line
Each star or space
printf("\n")
Ends the current line
After the bar or left/gap/right
Try it
Live Preview
Change the size and the framed hollow diamond updates instantly — including height and width.
Whole numbers from 1 to 12. Width is 2 * rows; height is 2 * rows - 1.
1. One helper for runs.print_chars(ch, n) prints n copies of ch with putchar.
2. Same formulas. Bars still use width; inner rows still use left and gap.
3. Cleaner main loop. The outer loop only chooses which segments to call — useful once you can explain the math out loud.
Learn the nested-loop version first (Examples 1–2) so both bounds stay visible in an interview; treat the helper as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Wrong width
Use 2 * rows
Program 9 uses width 2 * rows - 1. Mixing that here breaks the solid bars and side closure.
No mirror
Only half a diamond
Forget i = 2 * rows - line on the lower half and the gap never closes again.
Swapped formulas
Broken sides
Keep left = rows - i + 1 and gap = 2 * (i - 1). Swapping them misaligns the hollow diamond.
Newline early
Broken rows
Call printf("\n") only after the full bar or all three segments — never inside the character loops.
rows = 1
Two identical bars
Height is 1, width is 2 — a single line of ** (top and bottom are the same line).
rows ≤ 0
Empty or invalid
Validate and re-prompt for interactive programs.
Bad scanf
Check the return
If scanf fails, rows is uninitialized — always test != 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
print_chars helper (Example 3)
O(rows²)
O(1)
About 2n - 1 lines, each up to 2n characters — still quadratic in n = rows.
Remember
Key Takeaways
Grid: width 2 * rows, height 2 * rows - 1.
Bars: first and last lines are solid runs of width stars.
Inner rows:left stars, gap spaces, left stars — with mirrored i.
Check:2 * left + gap == width; time is O(n²).
One line: solid bars on the ends; elsewhere print left, gap, left with a mirrored i.
Frequently Asked Questions
Use height 2*rows-1 and width 2*rows. Print a full bar of stars on the first and last lines. For every other line, map line to i with symmetry, then print (rows-i+1) stars, a gap of 2*(i-1) spaces, and the same number of stars again.
Width is 2*rows and height is 2*rows-1 so the top and bottom are full horizontal bars while the sides close on the leftmost and rightmost columns of the inner rows.
Program 9 prints a hollow diamond alone with constant width 2*rows-1 and diagonal j/k tests. Program 11 adds solid top and bottom bars of length 2*rows and builds each inner line from left stars, gap, and right stars.
left = rows - i + 1 is how many stars sit on each side. gap = 2 * (i - 1) is the hollow space between them. Together they always equal the line width.
If line <= rows, i = line. Otherwise i = 2 * rows - line. That mirrors distance from the nearest end so the gap is widest at the waist.
printf("*") (or printf(" ")) stays on the same line. printf("\n") ends the current line. Characters use printf without a newline; the row break comes after the bar or the three segments.
O(n²) for n = rows. There are about 2n lines and each prints about 2n characters.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input, and require rows >= 1. Unchecked scanf leaves rows uninitialized on failure.
🤔
Did you know?
Every line is exactly 2 * rows characters wide. Inner rows always satisfy 2 * left + gap == 2 * rows — so the frame closes cleanly on both sides.