The hollow diamond pattern is a classic logic-building exercise in C programmingif-else statements) within nested loops
Introduction
Constructing a hollow diamond involves dividing the pattern into two symmetrical phases—an upper growing pyramid and a lower shrinking pyramid
Prerequisites: Proficiency in nested
forloops, conditional logic (if-else), standard input/output (printf,scanf), and arithmetic operations. Expected Output:
* *
* *
* *
* *
* *
* *
* *
*
Deconstructing the Pattern Logic
For an upper-half height of $n$, the total height of the shape is 2n - 1 rows
Phase 1: Upper Hollow Pyramid (i = 1 to n)
Leading Spaces: Decreases each row (n - i)
. Boundary Condition: Print
*only at the first (j = 1) and last (j = 2i - 1) column positions. Fill all intermediate positions (1 < j < 2i - 1) with empty spaces .
Phase 2: Lower Inverted Hollow Pyramid (i = 1 to n - 1)
Leading Spaces: Increases each row (i)
. Boundary Condition: Print
*only at the first (j = 1) and last (j = 2(n - i) - 1) column positions. Fill intermediate positions with spaces .
| Row Index | Phase | Leading Spaces | Star Indices (j) | Inner Spaces |
| 1 | Upper | 4 | 1 | 0 |
| 2 | Upper | 3 | 1, 3 | 1 |
| 3 | Upper | 2 | 1, 5 | 3 |
| 4 | Upper | 1 | 1, 7 | 5 |
| 5 (Center) | Upper | 0 | 1, 9 | 7 |
| 1 (Row 6) | Lower | 1 | 1, 7 | 5 |
| 2 (Row 7) | Lower | 2 | 1, 5 | 3 |
| 3 (Row 8) | Lower | 3 | 1, 3 | 1 |
| 4 (Row 9) | Lower | 4 | 1 | 0 |
Code Implementation
int main() {
int n, i, j, space;
printf("Enter the number of rows for upper half: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
// Phase 1: Upper Hollow Pyramid (Rows 1 to n)
for (i = 1; i <= n; i++) {
// Print leading spaces
for (space = 1; space <= n - i; space++) {
printf(" ");
}
// Print boundary asterisks and inner spaces
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
// Phase 2: Lower Inverted Hollow Pyramid (Rows 1 to n - 1)
for (i = 1; i <= n - 1; i++) {
// Print leading spaces
for (space = 1; space <= i; space++) {
printf(" ");
}
// Print boundary asterisks and inner spaces
for (j = 1; j <= (2 * (n - i) - 1); j++) {
if (j == 1 || j == (2 * (n - i) - 1)) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Code Breakdown
Boundary Checks: The condition
j == 1 || j == (2 * i - 1)restricts character printing solely to the edges of the shape, creating the hollow interior. Phase Split: Executing Phase 2 up to n - 1 prevents duplicating the central apex line
. Row Transition:
printf("\n");ensures proper line breaks after completing each horizontal scan line.
Compiling and Execution
Compile and execute using GCC
Enter the number of rows for upper half: 5
*
* *
* *
* *
* *
* *
* *
* *
*
Common Mistakes & Troubleshooting
Filling the Interior: Omitting conditional statements within the character loop results in a standard solid diamond rather than a hollow outline
. Off-by-One Boundary Errors: Checking
j == iinstead ofj == (2 * i - 1)distorts the right boundary line.
Complexity Analysis
Time Complexity: O(n^2) due to nested space allocation and character loops across 2n - 1 total iterations
. Space Complexity: O(1) auxiliary memory since only scalar integer counters are used
.
Conclusion
Combining boundary conditions with multi-phase nested loops provides a foundational technique for rendering custom graphical boundaries in terminal applications
For all Pattern Programs list click here:
…till the next post, bye-bye & take care