The hollow rhombus pattern extends the solid rhombus concept by introducing boundary checks within nested loops, printing asterisks only along the outer perimeter of the parallelogram
Introduction
Mastering this shape sharpens your ability to pair horizontal space offsets with conditional checks (if-else statements) to isolate interior spaces from exterior borders
Prerequisites: Knowledge of nested
forloops, conditional logic (if-else), standard I/O (printf,scanf), and basic arithmetic. Expected Output:
* *
* *
* *
*****
Deconstructing the Pattern Logic
A hollow rhombus of size n consists of n rows and n columns
Inside the character loop, asterisks are printed only along the four borders
Top and Bottom Edges: On the first row (i = 1) and last row (i = n), every column position from j = 1 to n prints an asterisk (
*). Side Edges: On middle rows (1 < i < n), asterisks are printed only at the first column (j = 1) and last column (j = n), while intermediate positions (1 < j < n) print spaces
.
| Row (i) | Leading Spaces (n−i) | Border Condition (j=1 or j=n or i=1 or i=n) | Character Output |
| 1 (Top) | 4 | All j positions valid | ***** |
| 2 | 3 | j = 1 and j = 5 | * * |
| 3 | 2 | j = 1 and j = 5 | * * |
| 4 | 1 | j = 1 and j = 5 | * * |
| 5 (Bottom) | 0 | All j positions valid | ***** |
Code Implementation
int main() {
int n, i, j, space;
printf("Enter the number of rows: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
for (i = 1; i <= n; i++) {
// Print leading spaces for slant alignment
for (space = 1; space <= n - i; space++) {
printf(" ");
}
// Print boundary asterisks and inner spaces
for (j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
Code Breakdown
Space Offsets: The loop
space <= n - icalculates leading space counts to tilt the shape into a rhombus. Boundary Logic: The condition
if (i == 1 || i == n || j == 1 || j == n)ensures solid horizontal boundaries for top/bottom rows and solid vertical boundaries for side edges. Interior Hollow Space: Any character index not lying on a boundary defaults to printing a blank space
.
Compiling and Execution
Compile and run the program using standard GCC tools
Enter the number of rows: 5
*****
* *
* *
* *
*****
Common Mistakes & Troubleshooting
Missing Top/Bottom Checks: Omitting
i == 1 || i == nleaves only side walls standing, causing top and bottom borders to stay open. Distorted Slant: Using i - 1 instead of n - i for leading spaces slants the rhombus in the opposite direction
. Missing Line Breaks: Forgetting
printf("\n");outputs all characters onto a single horizontal line.
Complexity Analysis
Time Complexity: O(n^2) because the outer loop executes n times containing two inner loops iterating up to n times
. Space Complexity: O(1) auxiliary memory space, using only scalar integer variables
.
Conclusion
Integrating multi-condition boundary checks into fixed-width nested loops allows you to render custom hollow geometric boundaries in terminal applications
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment