An hourglass design is a symmetrical pattern resembling an hourglass shape. It comprises rows of asterisks (*) arranged in a particular sequence of decreasing and increasing order. For example, an hourglass pattern with a height of 5 rows appears as follows:
*********
*******
*****
***
*
***
*****
*******
*********
In this scenario, the quantity of asterisks decreases as we progress downwards in the initial section and increases in the latter section. The number of spaces preceding the asterisks enlarges in the first half and diminishes in the second half to maintain symmetry.
Steps to Implement the Hourglass Pattern:
To create an hourglass pattern in C , we require:
- Understand how the stars and spaces change in the number of rows.
- Use nested loops: One for rows, and others to print spaces and stars.
- Divide the pattern into two parts: The upper half and the lower half.
Algorithm:
- Input: Accept the height of the hourglass (number of rows in the upper half).
- Upper Half:
- For each row: Print leading spaces. Print stars in decreasing order.
- Print leading spaces.
- Print stars in decreasing order.
- Lower Half:
- For each row: Print leading spaces. Print stars in increasing order.
- Print leading spaces.
- Print stars in increasing order.
- Output: Display the complete hourglass pattern.
Example:
Let's consider an illustration to display the hourglass pattern in the C programming language.
#include <stdio.h>
void printHourglass(int n)
{
int i, j;
// Upper half of the hourglass
for (i = 0; i < n; i++)
{
// Print leading spaces
for (j = 0; j < i; j++)
{
printf(" ");
}
// Print stars
for (j = 0; j < (2 * (n - i) - 1); j++) {
printf("*");
}
printf("\n");
}
// Lower half of the hourglass
for (i = n - 2; i >= 0; i--) {
// Print leading spaces
for (j = 0; j < i; j++) {
printf(" ");
}
// Print stars
for (j = 0; j < (2 * (n - i) - 1); j++) {
printf("*");
}
printf("\n");
}
}
int main()
{
int n;
// Input the number of rows for the upper half
printf("Enter the number of rows for the hourglass pattern: ");
scanf("%d", &n);
// Print the hourglass pattern
printHourglass(n);
return 0;
}
Sample Input and Output:
Input:
Enter the number of rows for the hourglass pattern: 5
Output:
*********
*******
*****
***
*
***
*****
*******
*********
Input:
Enter the number of rows for the hourglass pattern: 3
Output:
*****
***
*
***
*****
Explanation of the Code:
- Function Definition:
- The printHourglass function takes an integer n as input, which represents the height of the upper half of the hourglass.
- Upper Half Loop:
- First Inner Loop: Before printing the stars, print enough spaces to take up the right-hand side, for (j = 0; j < i; j++)
- The more i increases, the more the number of spaces increases.
- The second innermost loop (for (j = 0; j < (2 * (n - i) - 1); j++)) also prints stars: the number is decreasing with the row.
- Lower Half Loop:
- The outer loop encloses each row of the lower half starting from the second last row of the upper half. for (i = n-2; i >= 0; i--).
- The first inner loop prints spaces just as the upper half does.
- The second inner loop prints stars in ascending order.
- Main Function:
- The main function accepts user input for the number of rows (n) and calls the printHourglass function to generate the pattern.
- Dynamic Input: The program dynamically adjusts the pattern based on the input n. This makes it versatile for various sizes of hourglass patterns.
- Symmetry: The upper and lower halves of the hourglass are symmetric, which makes the logic reusable.
- Nested Loops: Understanding nested loops is crucial to implementing such patterns. The number of iterations depends on the row index.
- Off-by-One Errors: Ensure the loops run for the correct number of iterations, especially for spaces and stars.
- Input validation: Adding validation checks to verify that the inputs are valid-for example, nonnegative integers-will make our program more robust.
- Formatting: Pay attention to spaces and newlines for symmetry in the pattern.
Key Points to Note:
Common Pitfalls:
Variations of the Hourglass Pattern
- Adjust the script to display asterisks solely along the edges while leaving spaces within.
Example:
Replace the asterisks in the Number Hourglass pattern with numerical values that match the respective row index.
Example:
123456789
1234567
12345
123
1
123
12345
1234567
123456789
Conclusion:
In summary, generating hourglass designs in C serves as a beneficial practice that challenges one's logical reasoning and improves their coding abilities. By breaking down the problem into smaller tasks, mastering loops, and refining formatting techniques, crafting complex patterns becomes more manageable. This code can be a foundation for exploring unique iterations and engaging in further experimentation.