Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
The butterfly for n = 4 looks irregular only when you compare whole rows. Each row has the same three parts: a left star run, a middle gap, and a right star run. The star runs always have equal widths, while the gap shrinks until the middle row and then grows again.
row 1: * * left = 1, gap = 6, right = 1
row 2: ** ** left = 2, gap = 4, right = 2
row 3: *** *** left = 3, gap = 2, right = 3
row 4: ******** left = 4, gap = 0, right = 4
row 5: *** *** left = 3, gap = 2, right = 3
row 6: ** ** left = 2, gap = 4, right = 2
row 7: * * left = 1, gap = 6, right = 1The two star runs and the middle gap always occupy eight columns. In the widest row, the gap has width zero, so the two four-star runs touch and appear as eight consecutive stars. Nothing special has to be added for that row. A zero-width loop simply prints no spaces.
In the row with three stars on each side, what are the widths of the left star run, middle gap, and right star run?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For the upper half, let i run from 1 through 4. The left star run uses i stars, and the right star run uses the same i stars. The middle gap uses 2 * (4 - i) spaces. As i increases by one, each side gains one star and the gap loses two spaces.
i = 1: 1 star, 6 spaces, 1 star -> * *
i = 2: 2 stars, 4 spaces, 2 stars -> ** **
i = 3: 3 stars, 2 spaces, 3 stars -> *** ***
i = 4: 4 stars, 0 spaces, 4 stars -> ********The expression 2 * (4 - i) is the part that preserves the fixed width. At i = 1, it gives six spaces. At i = 4, it gives zero spaces. The star count and gap count therefore change together instead of requiring a separate instruction for each visible row.
After the widest row, the same row-building rule works again if i decreases from 3 down to 1. That produces the upper rows in reverse order: three stars on each side, then two, then one. The lower loop starts at 3 because i = 4 already printed the widest row.
i = 3: 3 stars, 2 spaces, 3 stars -> *** ***
i = 2: 2 stars, 4 spaces, 2 stars -> ** **
i = 1: 1 star, 6 spaces, 1 star -> * *Starting the lower loop at 4 would print the row of eight stars a second time. Starting at 2 would skip the row with three stars on each side. The boundary value is part of the pattern logic: the middle value belongs to the upper half, while the lower half begins one step below it.
Fix the starting value in the lower loop so the widest row is not printed twice.
for (int i = 4; i >= 1; i--) {Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Each row can be printed with three inner loops. The first prints i stars for the left star run, the second prints 2 * (4 - i) spaces for the middle gap, and the third prints i stars for the right star run. A newline finishes the row. The increasing and decreasing outer loops call this same row-building logic, so no branch checks whether the current row is the first, middle, or last.
int n = 4;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) cout << '*';
for (int j = 1; j <= 2 * (n - i); j++) cout << ' ';
for (int j = 1; j <= i; j++) cout << '*';
cout << '\n';
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) cout << '*';
for (int j = 1; j <= 2 * (n - i); j++) cout << ' ';
for (int j = 1; j <= i; j++) cout << '*';
cout << '\n';
}The outer loops decide only how i changes. The first moves upward from 1 to n, and the second moves downward from n - 1 to 1. The inner loops do not know which half they are in. They only apply the rule for the current i, which is why the pattern stays correct without hardcoded rows.