Now that you have mastered the basics of grid patterns, it is time to introduce a variable limit to your inner loop. This creates the classic Half Pyramid pattern, a fundamental shape that teaches you how to make your code dynamic based on the row number.
What You Will Learn
This tutorial will guide you through creating an alphabet triangle (or half pyramid). You will learn how to use the row index to control the inner loop's limit, allowing the number of characters printed to increase with each row.
Prerequisites:
Understanding of nested
forloops.Knowledge of how to print sequences using
'A' + jlogic.
Final Output:
If we create a triangle with 5 rows, the output will look like this:
AB
ABC
ABCD
ABCDE
Deconstructing the Pattern: The Logic
1. The Visual Representation
Each row determines how many characters are printed:
Row 0: 'A' (1 character)
Row 1: 'A' 'B' (2 characters)
Row 2: 'A' 'B' 'C' (3 characters)
Row 3: 'A' 'B' 'C' 'D' (4 characters)
Row 4: 'A' 'B' 'C' 'D' 'E' (5 characters)
2. Problem Statement
The objective is to create a pattern where the number of columns in each row is dependent on the row's index. The inner loop must adjust its boundary dynamically based on the current iteration of the outer loop.
3. Pattern Analysis & Logic
Outer Loop: Manages the rows, running from $i = 0$ to $n-1$.
Inner Loop: Manages the columns. To create the triangular shape, the inner loop runs from $j = 0$ to $i$.
Character Logic: We continue to print
'A' + jto ensure the sequence resets correctly in every row.
The Code Implementation
#include <stdio.h>
int main() {
int n = 5; // Total number of rows
// Outer loop for rows
for (int i = 0; i < n; i++) {
// Inner loop runs up to the current row index (i)
for (int j = 0; j <= i; j++) {
printf("%c", 'A' + j);
}
// Move to the next line after finishing the row
printf("\n");
}
return 0;
}
Explanation:
for (int i = 0; i < n; i++): The outer loop defines the height of the triangle.for (int j = 0; j <= i; j++): This is the key logic. By setting the limit toi, we ensure the first row prints 1 letter, the second prints 2, and so on.printf("%c", 'A' + j): Prints the character sequence corresponding to the column index.
Sample Output and Analysis
User Input:
The code is set for
n = 5.
Program Output:
A
AB
ABC
ABCD
ABCDE
Output Analysis:
Because the inner loop condition j <= i is tied to the outer loop variable, the inner loop executes exactly one extra time for every row, successfully building the triangle shape.
Common Mistakes and Troubleshooting
Incorrect Loop Condition: Using
j < ninstead ofj <= iwill result in a square grid rather than a triangle.Resetting the Sequence: If you wanted to continue the alphabet (A-O) instead of resetting to 'A' in each row, you would need to change the inner logic to use a persistent counter variable.
Complexity Analysis
Time Complexity: O(n^2), as the number of operations follows the sum of the first n integers.
Space Complexity: O(1), as the space requirements do not change with input size.
Conclusion
By making your inner loop boundary dependent on the outer loop index, you have moved from static grids to dynamic, shape-based patterns. This is the foundation for almost all triangle and pyramid variations in C.
For all Pattern Programs list click here:
…till the next post, bye-bye & take care
No comments:
Post a Comment