Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
The pyramid for n = 4 has four rows, and each row is made from two visible parts: leading spaces and consecutive stars. The spaces move the first star to the right, while the stars fill the row around the same center column. You print the spaces as characters, even though they look blank; you do not print the unused columns after the stars.
*
***
*****
*******| ROW | LEADING SPACES | STARS |
|---|---|---|
| 1 | 3 | 1 |
| 2 | 2 | 3 |
| 3 | 1 | 5 |
| 4 | 0 | 7 |
The star region grows around one center column instead of moving only to the right. Row 1 has its star in the center, row 2 adds one star on each side, and the same happens again in rows 3 and 4. That is why every star count is odd, and why the leading prefix gets shorter as the pyramid gets wider.
Use a 1-based row number r. For n = 4, the number of leading spaces is n - r, so the first row has 4 - 1 = 3 spaces and the last row has 4 - 4 = 0 spaces. The number of stars is 2 * r - 1, so the rows contain 1, 3, 5, and 7 stars.
| ROW R | N - R SPACES | 2 * R - 1 STARS |
|---|---|---|
| 1 | 4 - 1 = 3 | 2 * 1 - 1 = 1 |
| 2 | 4 - 2 = 2 | 2 * 2 - 1 = 3 |
| 3 | 4 - 3 = 1 | 2 * 3 - 1 = 5 |
| 4 | 4 - 4 = 0 | 2 * 4 - 1 = 7 |
Increasing r by one removes one space from the left and adds two stars to the star region. Those two stars are one new position on the left and one new position on the right of the center. Using r stars would not describe this growth: it would give row 3 only three stars, leaving two positions that should extend equally away from the center empty.
For row r = 3 when n = 4, how many leading spaces and stars should you print?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The outer loop visits rows 1 through 4. Inside each row, the first inner loop prints n - r spaces, the second inner loop prints 2 * r - 1 stars, and one newline comes after both inner loops finish. This order keeps the spaces before the stars and keeps the whole row on one line.
int n = 4;
for (int r = 1; r <= n; r++) {
for (int space = 0; space < n - r; space++) {
cout << ' ';
}
for (int star = 0; star < 2 * r - 1; star++) {
cout << '*';
}
cout << '\n';
}Reversing the inner loops prints stars before spaces, so the star region is pushed to the wrong side. Putting the newline inside either inner loop breaks one row into several lines: spaces or stars that belong together no longer share a line. The newline belongs after the space loop and the star loop, exactly once per outer-loop pass.
Complete the star-loop bound so it prints the correct number of stars for row r.
for (int star = 0; star < _____; star++) {
cout << '*';
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The final row has no leading spaces, so its width is entirely the star count: 2 * 4 - 1 = 7. The four rows therefore have total widths 4, 5, 6, and 7 characters when their leading spaces are included, while their visible star widths are 1, 3, 5, and 7. These values give you a direct check for the loop bounds.
The star loop starts at 0 and uses star < 2 * r - 1. It runs exactly 2 * r - 1 times. If you use star <= 2 * r - 1, the loop runs one extra time, so the final row contains 8 stars instead of the required 7. If you use r as the count, the final row contains only 4 stars, and the pyramid never reaches its required bottom width.