Hollow Diamond Inside Square Star Pattern in C++

What You'll Learn
You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap).
The width is 2 * rows; the number of lines is 2 * rows - 1. This layout differs from the standalone hollow diamond (Program 9).
⭐ Pattern Output
When you run the program with rows = 5:
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********Complete C++ Program
Fixed rows = 5 version:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int height = 2 * rows - 1;
int width = 2 * rows;
int line, j;
for (line = 1; line <= height; ++line) {
if (line == 1 || line == height) {
for (j = 1; j <= width; ++j) cout << "*";
} 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) cout << "*";
for (j = 1; j <= gap; ++j) cout << " ";
for (j = 1; j <= left; ++j) cout << "*";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Grid size
height = 2 * rows - 1 lines, width = 2 * rows columns (10×9 when rows = 5). line walks each output row; j drives inner cout loops.
Solid top and bottom edges
If line == 1 or line == height, for (j = 1; j <= width; ++j) cout << "*"; draws full horizontal bars.
Inner rows: left, gap, right
Else branch: i = (line <= rows) ? line : (2 * rows - line); left = rows - i + 1; gap = 2 * (i - 1). Three loops: left stars, gap spaces, left stars — hollow interior, mirrored halves.
Finish each line
cout << "\n" after the full-width pass or the triple inner pass, so every row is exactly width characters wide.
Framed hollow diamond
O(rows²) output, O(1) extra space. Ten-character lines scroll horizontally in the green preview on narrow viewports.
Variation — User Input Version
Accept rows with cin:
#include <iostream>
using namespace std;
int main() {
int rows;
int height, width;
int line, j;
cout << "Enter the number of rows: ";
cin >> 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) cout << "*";
} 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) cout << "*";
for (j = 1; j <= gap; ++j) cout << " ";
for (j = 1; j <= left; ++j) cout << "*";
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Reject
rows < 1after input - Rewrite the inner part as one loop over
j = 1…widthwith boolean tests (border OR left block OR right block) - Compare side-by-side with Program 9 output for the same
rows - Swap
*for digits on the border only
Avoid
- Mixing up
2 * rows - 1(line count) with2 * rows(line width)—both appear in this pattern - Copy-pasting
i == j/i + j == rows + 1snippets withjonly runningi…rows; that does not reproduce this 10-wide picture forrows = 5 - Trailing extra space loops after the right star block (they break alignment)
Key Takeaways
Width 2n, height 2n - 1 for n = rows.
First and last lines: solid bars of *.
Other lines: left stars, gap spaces, left stars with i mirrored.
Not the same as Program 9’s diagonal-only loop pattern.
Time complexity O(n²).
❓ Frequently Asked Questions
left and gap from the formulas above, with i mirrored below the waist row.2 * rows, so 10 when rows = 5. The height is 2 * rows - 1 (9 lines), so the outer shape uses a wide horizontal bar and matching side columns on the inner rows.2 * rows - 1. This program adds full-width top and bottom rows of length 2 * rows and builds each inner line from left block, gap, and right block.n = rows: Θ(n) lines, each printing Θ(n) characters.Explore: All Star Patterns
Browse the full C++ star pattern series.
If you remove the box border conditions and print only the diagonal stars, the top half matches an inverted V-shaped hollow pattern (Program 7 style).
9 people found this page helpful
