Hollow Diamond Inside Square Star Pattern in C++

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n wide, 2n − 1 tall

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:

Output
**********
****  ****
***    ***
**      **
*        *
**      **
***    ***
****  ****
**********
1

Complete C++ Program

Fixed rows = 5 version:

C++
#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

1

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.

Grid
2

Solid top and bottom edges

If line == 1 or line == height, for (j = 1; j <= width; ++j) cout << "*"; draws full horizontal bars.

Frame
3

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.

Mirror
4

Finish each line

cout << "\n" after the full-width pass or the triple inner pass, so every row is exactly width characters wide.

Newline
=

Framed hollow diamond

O(rows²) output, O(1) extra space. Ten-character lines scroll horizontally in the green preview on narrow viewports.

2

Variation — User Input Version

Accept rows with cin:

C++
#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 < 1 after input
  • Rewrite the inner part as one loop over j = 1width with 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) with 2 * rows (line width)—both appear in this pattern
  • Copy-pasting i == j / i + j == rows + 1 snippets with j only running irows; that does not reproduce this 10-wide picture for rows = 5
  • Trailing extra space loops after the right star block (they break alignment)

Key Takeaways

1

Width 2n, height 2n - 1 for n = rows.

2

First and last lines: solid bars of *.

3

Other lines: left stars, gap spaces, left stars with i mirrored.

4

Not the same as Program 9’s diagonal-only loop pattern.

5

Time complexity O(n²).

❓ Frequently Asked Questions

Solid rows cap the top and bottom. On inner rows, two equal star runs sit on the left and right; the space between them is the hollow region. The run length and gap follow left and gap from the formulas above, with i mirrored below the waist row.
The width is 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.
Program 9 is a hollow diamond alone with uniform line width 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.
O(n²) for n = rows: Θ(n) lines, each printing Θ(n) characters.

Explore: All Star Patterns

Browse the full C++ star pattern series.

All Patterns →
Did you know?

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).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful