A star-and-zero X pattern fills a rectangle with 0s, then overwrites * on the main diagonal, the anti-diagonal, and the center column — forming an X with a vertical stem.
Remember
Rule: * if i==j or j==mid or i==cols+1-j; else 0
*000*000*
0*00*00*0
00*0*0*00
000***000 ← 4 × 9 grid
Unlike Program 44 (centered digit diamond), this shape is a flat grid decided by per-cell diagonal and center checks.
Approach
How to Solve It
Visit every cell; print * when any of three edge conditions match, otherwise print 0.
Method
Idea
Best for
X + center
Diagonals and middle column print *
Learning, interviews, demos
Diagonals only
Drop j == mid for a pure X
Contrast once the full shape clicks
Pseudocode
Pseudocode
mid = (cols + 1) / 2
for i from 1 to rows:
for j from 1 to cols:
if i == j or j == mid or i == cols + 1 - j:
print "*" (no newline)
else:
print "0" (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk rows / cols
for (i = 1; i <= rows; i++) / for (j = 1; j <= cols; j++)
Center column
mid = (cols + 1) / 2;
Main diagonal
i == j
Anti-diagonal
i == (cols + 1) - j
Print cell
cout << "*" or cout << "0"
End the row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << "*" / cout << "0"
Stays on the same line
Each cell
cout << "\n"
Ends the current line
After the inner loop
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 rows and columns (prefer an odd column count so the center line is clear) and the X pattern updates instantly.
Rows 2–9, cols 3–15 (odd cols look best). Tap a chip or type values — the preview redraws as you go.
Live result4×9 · 36 cells
*000*000*
0*00*00*0
00*0*0*00
000***000
Trace
Worked Walkthrough — row i = 2, cols = 9
Trace each column on row 2 with mid = 5 — which cells hit a diagonal or the center.
Three complete programs: fixed 4×9, parameterized dimensions, and diagonals-only. Use View Output to reveal sample results.
Example 1 — Fixed rows = 4, cols = 9
Hard-coded grid dimensions — ideal for first demos and screenshots.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 9; j++)
{
if (i == j || j == 5 || i == 10 - j)
cout << "*";
else
cout << "0";
}
cout << "\n";
}
return 0;
}
Output
*000*000*
0*00*00*0
00*0*0*00
000***000
How It Works
1. Nested loops visit every cell. Outer i is the row; inner j is the column on a 4×9 grid.
2. Three ways to earn a star.i == j (main diagonal), j == 5 (center), or i == 10 - j (anti-diagonal).
3. Everything else is zero. The else branch fills the background; "\n" ends each row after the inner loop.
Example 2 — Variable Rows and Columns
Compute mid and use (cols + 1) - j so the pattern scales when you change dimensions.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 4, cols = 9;
int mid = (cols + 1) / 2;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == j || j == mid || i == (cols + 1) - j)
cout << "*";
else
cout << "0";
}
cout << "\n";
}
return 0;
}
Output (rows = 4, cols = 9)
*000*000*
0*00*00*0
00*0*0*00
000***000
How It Works
1. Derive mid.(cols + 1) / 2 is 5 when cols is 9 — the center column.
2. Same three checks. Only literals are replaced: mid for 5, (cols + 1) - j for 10 - j.
3. Resize freely. Prefer an odd cols so the vertical stem sits on a single middle column.
Example 3 — Diagonals Only
Remove j == mid from the condition — only the two diagonals print stars.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 9; j++)
{
if (i == j || i == 10 - j)
cout << "*";
else
cout << "0";
}
cout << "\n";
}
return 0;
}
Output
*0000000*
0*00000*0
00*000*00
000*0*000
How It Works
1. Only diagonals remain.i == j and i == 10 - j — no vertical stem.
2. Row 4 changes shape. Example 1 prints 000***000; here it becomes 000*0*000.
3. One condition, big visual change. Compare side by side with Example 1 to see what the center column adds.
Edge Cases & Pitfalls
Check these before calling the solution done.
10 - j
Hard-coded anti-diagonal
Using 10 - j only works for cols = 9. Prefer (cols + 1) - j whenever size can change.
even cols
Blurry center
Even widths have no single middle column. Use an odd cols so mid is unambiguous.
0-based
Off-by-one diagonals
These formulas assume 1-based i and j. Zero-based loops need different diagonal equations.
\n early
Column of cells
If cout << "\n" is inside the inner loop, each cell lands on its own line. End the row only after the inner loop.
rows > cols
Diagonal clips
When rows > cols, i == j never fires for lower rows. Keep rows <= cols for a full main diagonal in the visible grid.
no if
All zeros
Without the three-way check you only print 0. The conditions are what draw the X.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested cell checks (Examples 1–3)
O(rows × cols)
O(1)
Every cell is visited once. Star positions are a thin subset, but the loops still scan the full rectangle. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print * when i == j, j == mid, or i == cols + 1 - j; else 0.
Scale safely: use mid and (cols + 1) - j instead of hard-coded 5 and 10.
Break the row: call cout << "\n" only after the inner loop.
Complexity:O(rows × cols) time; O(1) extra space.
One line: for each cell, print * on a diagonal or the middle column, otherwise 0, then end the line after each row.
Frequently Asked Questions
An X-style pattern using * on the two diagonals and the center column, filling remaining positions with 0 on a 4x9 grid.
The width is 9 columns (j = 1..9). The middle column is 5, so checking j == 5 prints a vertical center line.
The left-to-right diagonal uses i == j. The right-to-left diagonal uses i == cols + 1 - j (10 - j when cols is 9).
Program 44 prints a centered number diamond with ascending digits. Program 45 prints a fixed grid with * and 0 using diagonal and center conditions.
Yes — drop the j == mid check to get a pure X of diagonals only — see Example 3.
Printing a cell 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 the inner loop.
O(rows × cols) because each cell is visited once in the nested loops.
For 1-based indexing, row i meets column j on the anti-diagonal when i + j equals cols + 1.
🤔
Did you know?
Print * when i == j, j == mid, or i == cols + 1 - j; otherwise print 0. A rows × cols grid visits every cell once — total prints = rows × cols.