The hourglass star pattern is an iconic multi-phase geometry problem in C programming that tests your ability to mirror logic by connecting an inverted full pyramid seamlessly to a standard full pyramid
Introduction
Mastering the hourglass pattern refines your mastery over multi-loop coordination, dynamic bounds calculation, and vertical symmetry
Prerequisites: Proficiency in nested
forloops, standard input/output (printf,scanf), and integer arithmetic. Expected Output:
*******
*****
***
*
***
*****
*******
*********
Deconstructing the Pattern Logic
For a half-height of n, the total height of the hourglass is 2n - 1 rows
Phase 1: Upper Inverted Pyramid (i = 1 to n)
Leading Spaces: Increases each row from 0 to n - 1 (i - 1 spaces)
. Asterisks: Decreases in odd increments given by 2(n - i) + 1.
Phase 2: Lower Full Pyramid (i = 2 to n)
Leading Spaces: Decreases each row from n - 2 down to 0 (n - i spaces)
. Asterisks: Increases in odd increments given by 2i - 1.
| Row Index | Phase | Leading Spaces | Asterisks |
| 1 | Upper | 0 | 9 |
| 2 | Upper | 1 | 7 |
| 3 | Upper | 2 | 5 |
| 4 | Upper | 3 | 3 |
| 5 (Center) | Upper | 4 | 1 |
| 2 (Row 6) | Lower | 3 | 3 |
| 3 (Row 7) | Lower | 2 | 5 |
| 4 (Row 8) | Lower | 1 | 7 |
| 5 (Row 9) | Lower | 0 | 9 |
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 Inverted Pyramid (Rows 1 to n)
for (i = 1; i <= n; i++) {
// Print leading spaces
for (space = 1; space < i; space++) {
printf(" ");
}
// Print asterisks
for (j = 1; j <= (2 * (n - i) + 1); j++) {
printf("*");
}
printf("\n");
}
// Phase 2: Lower Full Pyramid (Rows 2 to n)
for (i = 2; i <= n; i++) {
// Print leading spaces
for (space = 1; space <= n - i; space++) {
printf(" ");
}
// Print asterisks
for (j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Code Breakdown
Upper Loop Phase: Iterates through rows 1 to n, reducing asterisks from 2n - 1 down to 1 to form the contracting top half
. Lower Loop Phase: Starts at i = 2 to prevent duplicating the single central asterisk vertex, expanding back out to 2n - 1 asterisks
. Line Break Sequencing:
printf("\n");executes after each inner character loop completes, ensuring precise row alignment.
Compiling and Execution
Compile and run the program using GCC
Enter the number of rows for upper half: 5
*********
*******
*****
***
*
***
*****
*******
*********
Common Mistakes & Troubleshooting
Duplicate Single Star Vertex: Starting Phase 2 at i = 1 prints two consecutive rows with a single asterisk (
*), distorting the central focal point. Off-by-One Space Shift: Using
space <= iinstead ofspace < iin Phase 1 adds an unnecessary extra leading space on the first row. Incorrect Odd Count Formulas: Mixing up upper (2(n - i) + 1) and lower (2i - 1) star formulas breaks the symmetry
.
Complexity Analysis
Time Complexity: O(n^2) because two sequential nested loop blocks execute outer iterations up to $n$ times with inner loops proportional to n.
Space Complexity: O(1) auxiliary memory space, using only scalar integer counters
.
Conclusion
Symmetrically coupling inverted and upright nested loops enables you to construct complex hourglass geometries cleanly
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment