Shape Rule
Mirrored diagonals
Row i prints i on the main diagonal and again on the mirrored diagonal — spaces fill every other column.

Program 53 prints a mirror diagonal number pattern: each row shows the row number on the main diagonal (left) and on a mirrored diagonal (right), forming a symmetric V-shape — a natural step after Program 52’s palindromic pyramid. This tutorial covers two inner loops with i == j and i == k conditions, a live preview, worked C++ examples, edge cases, and complexity.
Mirrored diagonals
Row i prints i on the main diagonal and again on the mirrored diagonal — spaces fill every other column.
i = 1..rows
for (i = 1; i <= rows; i++) picks the current row index.
j = 1..rows
(i == j ? cout << j : cout << " ") — print the digit only on the main diagonal.
k = rows-1..1
(i == k ? cout << k : cout << " ") — mirrored diagonal; skipping the center column avoids duplication.
rows = 3..9
Pick row count and draw the V-shaped mirror diagonal pattern in the browser.
Complexity
Each row prints about 2n-1 characters — total work grows as O(n²).
A mirror diagonal number pattern prints row i with the digit i on the main diagonal and again on a mirrored diagonal — spaces fill the gaps to form a V-shape. With rows = 5, you get 1 1, 2 2, 3 3, 4 4, 5.
In C++, use an outer loop for rows, then two inner loops: left half with i == j, right mirrored half with i == k, printing spaces elsewhere before cout << "\n".
It bridges Program 52’s palindromic rows to conditional diagonal placement — combining nested loops with i == j logic.
i == j prints the row digit on the main diagonal.
i == k mirrors the digit on the opposite diagonal.
Program 52 uses m++/m-- for palindromic rows; Program 53 uses spacing and conditions.
Follow Program 52; continue to Program 54 next.
In short: outer i = 1..rows, left loop j = 1..rows with i == j, right loop k = rows-1..1 with i == k, else space, then cout << "\n".
Given row count rows = 5, print a mirror diagonal number pattern — row i shows digit i on both diagonals with spaces between.
// rows = 5
//1 1
// 2 2
// 3 3
// 4 4
// 5 | Item | Type | Description |
|---|---|---|
rows | int | How many V-shaped rows to print. |
i (outer) | int | Current row index — runs from 1 to rows. |
j (left) | int | Scans columns 1..rows; prints digit when i == j. |
k (right) | int | Scans columns rows-1..1; prints digit when i == k. |
| Cell output | string | Digit when condition matches; otherwise a space. |
| Row width | int | About 2n-1 characters per row. |
for i from 1 to rows:
for j from 1 to rows:
print digit if i == j else space
for k from rows - 1 down to 1:
print digit if i == k else space
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Left i == j, right i == k with spaces elsewhere | Learning and interviews |
| Ternary operator | i == j ? j : " " | Compact one-liners |
| User-input rows | cin >> rows | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Full X pattern | i == j || i + j == rows + 1 in one loop | Extension after mastering V-shape |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Left half | for (j = 1; j <= rows; j++) (i == j ? cout << j : cout << " "); |
| Right half | for (k = rows - 1; k >= 1; k--) (i == k ? cout << k : cout << " "); |
| End row | cout << "\n"; |
| Skip center duplicate | Right loop starts at rows - 1, not rows |
| Program 52 contrast | Program 52 uses palindromic m++/m--; Program 53 uses diagonal conditions |
Same V-shape — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
cin >> rowsRead row count from console
rows = 3Quick dry-run on paper
i == jMain diagonal digit placement
i == kMirrored diagonal digit placement
Reach for this pattern when teaching conditional diagonal placement, mirrored halves, and spacing in console output.
Natural follow-up after Program 52’s palindromic pyramid — introduces i == j diagonal conditions.
Each row places digits only where indices match — good bridge to matrix and grid problems.
Each row scans about 2n-1 positions — classic nested-loop O(n²) complexity.
Program 54 mirrors this V-shape downward to form a full diamond — compare the two next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in diagonal conditions, mirrored halves, and O(n²) thinking.
Choose row count between 3 and 9 and draw the mirror diagonal number pattern in the browser.
Three complete C++ programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Print five rows of the mirror diagonal V-shape with conditional digit placement on both diagonals.
rows = 5Hard-coded row count — print digit when i == j or i == k, otherwise print a space.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++)
(i == j ? cout << j : cout << " ");
for (k = rows - 1; k >= 1; k--)
(i == k ? cout << k : cout << " ");
cout << "\n";
}
return 0;
} When i = 3, the left loop prints spaces until j = 3, then the right loop prints spaces until k = 3 — output 3 3. When i = 5, only the center column gets a digit because both diagonals meet at the bottom tip.
Read row count with cin and validation.
Read rows with cin >> rows (check cin.fail()) and validate the result.
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
if (cin.fail() || rows <= 0) {
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++)
(i == j ? cout << j : cout << " ");
for (k = rows - 1; k >= 1; k--)
(i == k ? cout << k : cout << " ");
cout << "\n";
}
return 0;
} Same diagonal two-loop core as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace left and right diagonal conditions before scaling to 5 rows.
#include <iostream>
using namespace std;
int main() {
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows; j++)
(i == j ? cout << j : cout << " ");
for (k = rows - 1; k >= 1; k--)
(i == k ? cout << k : cout << " ");
cout << "\n";
}
return 0;
} With only three rows you can trace every i == j and i == k check on paper before running the full rows = 5 demo.
int rows = 5; controls how many V-shaped lines print.
for (j = 1; j <= rows; j++) — print digit when i == j, else space.
for (k = rows - 1; k >= 1; k--) — print digit when i == k, else space.
cout << "\n" after both inner loops finish the current line.
Each row prints about 2n-1 characters — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s left diagonal position, right diagonal position, and full line output.
i | Left (j) | Right (k) | Row output |
|---|---|---|---|
1 | j = 1 | k = 1 | 1 1 |
2 | j = 2 | k = 2 | 2 2 |
3 | j = 3 | k = 3 | 3 3 |
4 | j = 4 | k = 4 | 4 4 |
5 | j = 5 | (none — center tip) | 5 |
Row 5 prints only one digit because the right loop starts at rows - 1, avoiding a duplicate center column.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Each row scans a fixed-width grid with conditional digit placement.
Example: trace row i = 3 in the walkthrough table.
Each row mirrors digits on two diagonals — good bridge to matrix indexing.
Example: row 5 ends with a single center digit 5 at the V tip.
Practice (i == j ? cout << j : cout << " ") vs cout << "\n" with two inner loops per row.
Example: put cout << "\n" inside the inner loop by mistake.
Each row prints about 2n-1 characters — links loops to grid traversal.
Example: 10 rows scan about 19 characters on the widest line.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: 5 rows scan about 9 characters per line on average.
Pair the pattern with cin.fail() checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C++ courses.
Wrong conditions show up immediately as misaligned diagonals.
Each row uses real diagonal logic — not abstract loop drill.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 on paper — watch both loops print 3 at column 3 with spaces elsewhere.
Small habits that keep number-pattern code clean.
Scan all columns in the left half — print digit only when i == j.
Avoid crashes when the user types letters instead of a number.
Only call cout << "\n" after the inner loop finishes the row.
Start the right loop at rows - 1 to skip duplicating the center column.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put cout << "\n" inside the inner loop.
Mistakes that commonly break mirror diagonal number patterns.
Each character lands on its own line — you get a column, not a V-shape.
→ Use (i == j ? cout << j : cout << " ") and (i == k ? cout << k : cout << " "); cout << "\n" only after both inner loops.
Starting the right loop at k = rows duplicates the center digit on the bottom row.
→ Use for (k = rows - 1; k >= 1; k--) — skip the center column.
Digits appear everywhere instead of on the diagonals only.
→ Print the digit when i == j (or i == k), not when they differ.
All numbers print on one long line without row breaks.
→ Add cout << "\n" after both inner loops complete.
Letters or empty input leave rows unread when cin.fail() is not checked.
→ Check cin.fail() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the right loop does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Five rows ending with a single center 5 — good for dry-runs.
Unchecked cin leaves rows uninitialized — check the return value.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
m++/m-- rowsi == j diagonal conditionsi == j || i + j == rows + 1 in one column loopj = 1..rows with i == j. Right: k = rows-1..1 with i == k. Else print a space.cout stays on the line; cout << "\n" advances — call it only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single 1.2n-1 characters — total work is O(n²) for n rows.Quick Takeaway: outer i = 1..rows, left i == j, right i == k, else space, then cout << "\n".
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Characters per row | About 2n-1 | No storage beyond loop counters |
The mirror diagonal number pattern is a natural follow-up to Program 52: conditional digit placement on mirrored diagonals with spaces elsewhere. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 54 to mirror this V-shape into a full diamond.
Row i prints digit i on both diagonals — left with i == j, right with i == k.
for (j = 1; j <= rows; j++) (i == j ? cout << j : cout << " ");for (k = rows - 1; k >= 1; k--) (i == k ? cout << k : cout << " ");rows - 1 to skip center duplicationcout << "\n" after both inner loopscin >> rows for user input and check cin.fail()k = rows — duplicates the center digiti != j — fills the whole row with numberscout << "\n" inside either inner looprows = 3 dry-run before coding rows = 5Print the V-shape the beginner-friendly way.
Digit on both diagonals per row
Definitioni == j
Codei == k
Codek = rows - 1..1
LogicO(n²) time
AnalysisEach row prints the row number on the main diagonal (left) and on a mirrored diagonal (right) using i == j and i == k. Row 3 shows 3 on both sides — about 2n-1 characters per row, so O(n²) total.
Mirror this V-shape downward to form a full diamond in the next tutorial.
12 people found this page helpful