A mirror diagonal diamond extends Program 53’s V-shape: print the top half (i = 1..rows), then mirror it downward (i = rows-1..1) so digits climb to the tip and descend again — a full diamond of 2n - 1 lines.
Unlike Program 53 (top V only), this shape adds a second outer loop so the pattern closes into a diamond.
Approach
How to Solve It
Reuse the same row builder twice: top i = 1..rows, bottom i = rows-1..1.
Method
Idea
Best for
Two outer loops
Top V-half, then mirror without repeating the tip
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
for i from 1 to rows: // top half
print left (i == j) and right (i == k) halves
print newline
for i from (rows - 1) down to 1: // bottom half
print left (i == j) and right (i == k) halves
print newline
// left: j from 1 to rows
// right: k from (rows - 1) down to 1
Cheat sheet
Goal
Pattern
Top half
for (i = 1; i <= rows; i++)
Bottom half
for (i = rows - 1; i >= 1; i--)
Left diagonal
(i == j ? cout << j : cout << " ")
Right diagonal
(i == k ? cout << k : cout << " ")
Skip tip twice
Bottom starts at rows - 1
End the row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / cout << " "
Stays on the same line
Each cell (digit or space)
cout << "\n"
Ends the current line
After both inner loops
Print cells without a newline, then end the row once. Spaces keep both diagonals aligned on every line of the diamond.
Try it
Live Preview
Change the peak row count and the full diamond updates instantly.
Use peak rows from 2 to 9. Total lines = 2 × peak - 1. Tap a chip or type a value — the preview redraws as you go.
Live resultpeak 5 · 9 lines
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
Trace
Worked Walkthrough
How the two outer loops build a diamond for rows = 5.
Pass
i values
What appears
Top
1, 2, 3, 4, 5
V opens → tip at 5
Bottom
4, 3, 2, 1
mirror down (skips tip)
Sample line
i = 3
3 3
Starting the bottom loop at rows - 1 avoids printing the tip twice. Each line still uses the same left/right diagonal checks as Program 53.
Code
C++ Programs
Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded peak — top V-half, then bottom mirror for a complete 9-line diamond.
C++
#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";
}
for (i = rows - 1; i >= 1; 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;
}
Output
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1
How It Works
1. Top half opens the V.i runs from 1 to 5 — same row builder as Program 53.
2. Bottom half mirrors out.i runs from 4 down to 1 so the tip is not printed twice.
3. Same cell rule everywhere. Left i == j, right i == k, spaces elsewhere, then "\n".
Example 2 — User Input Rows
Read the peak count at runtime — both halves scale automatically.
C++
#include <iostream>
using namespace std;
int main()
{
int rows, i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
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";
}
for (i = rows - 1; i >= 1; 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;
}
Output (when user enters 4)
Enter the number of rows: 4
1 1
2 2
3 3
4
3 3
2 2
1 1
How It Works
1. Same two-pass structure. Only the source of rows changes from a literal to cin.
3. Validate in real apps. Prefer checking cin failure and requiring rows >= 1 (tip below).
Safer input tip
if (!(cin >> rows) || rows < 1)
{
cout << "Please enter a positive integer.\n";
return 1;
}
Example 3 — Compact rows = 3
Smaller peak for quick tracing — five lines total.
C++
#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";
}
for (i = rows - 1; i >= 1; 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;
}
Output
1 1
2 2
3
2 2
1 1
How It Works
1. Five lines total. Top prints three rows; bottom adds two more (i = 2..1).
2. Same formula. Nothing changes except rows — proving the pattern scales.
3. Tip stays unique. The peak 3 appears once in the middle.
Edge Cases & Pitfalls
Check these before calling the solution done.
Bottom start
Start the bottom loop at rows - 1
Starting at rows duplicates the tip and breaks the diamond.
Missing bottom
Forgetting the second outer loop
That is Program 53 (V only). Add the bottom pass for the full diamond.
Right start
Right inner loop starts at rows - 1
Same as Program 53 — skips the center column so the tip is a single digit.
Bad cin
Validate input
Check cin >> rows and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Full diamond (Examples 1–3)
O(n²)
O(1)
There are 2n - 1 lines and each scans about 2n - 1 characters, so work is quadratic in n. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print digit when i == j or i == k; else space — same as Program 53.
Two passes: top i = 1..rows, bottom i = rows-1..1.
Break the row: call cout << "\n" only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: print Program 53’s V for i = 1..rows, then repeat for i = rows-1..1 to finish the diamond.
Frequently Asked Questions
The first loop prints the top V-half from i = 1 to rows. The second prints the bottom half from rows-1 down to 1, mirroring the shape.
Starting at rows would print the middle peak row twice. rows-1 skips the tip already printed by the top half.
Program 53 prints only the top V-half. Program 54 adds a second outer loop to mirror the same row logic downward.
The left loop uses i == j for the main diagonal. The right loop uses i == k for the mirrored diagonal, with spaces elsewhere.
Printing a digit or space stays on the same line. Printing a newline ends the current row. Cells use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because the diamond has about 2n-1 lines and each line scans about 2n-1 positions.
Change rows or read it from user input with cin — see Example 2.
After cin >> rows, check failure and require rows >= 1 before the loops.
🤔
Did you know?
Program 53’s V becomes a full diamond by adding a second outer loop from rows - 1 down to 1. Total lines = 2n - 1 with about 2n - 1 characters each — O(n²) overall.