Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
For n = 4, the triangular pattern has four rows. Row 1 prints one value, row 2 prints two values, row 3 prints three values, and row 4 prints four values. The row width changes, but the numbers form one uninterrupted sequence from 1 through 10.
1
2 3
4 5 6
7 8 9 10The first row consumes only 1. The second row consumes 2 and 3, so the third row must begin with 4. After the third row consumes 4, 5, and 6, the fourth row must begin with 7 and contain four values: 7, 8, 9, and 10. A row boundary changes where the next output line begins, not which number comes next.
Which output is the correct fourth row for the n = 4 triangle?
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. On each outer-loop pass, the current row number becomes the inner loop's bound. That gives the inner loop one pass for row 1, two passes for row 2, three passes for row 3, and four passes for row 4.
int number = 1;
for (int row = 1; row <= 4; row++) {
for (int column = 1; column <= row; column++) {
cout << number << " ";
number++;
}
cout << endl;
}When row is 1, column takes one value, so the inner loop prints one number. When row is 2, column takes two values, so it prints two numbers. The same rule gives three positions for row 3 and four positions for row 4. The bound column <= row controls how many positions are printed; it does not decide which number appears in those positions.
| ROW | INNER-LOOP PASSES | VALUES PRINTED |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 2, 3 |
| 3 | 3 | 4, 5, 6 |
| 4 | 4 | 7, 8, 9, 10 |
The counter starts at 1 before the outer loop begins. Each printed position uses its current value, then increments it once. When a row ends, the newline moves output to the next line, but number still holds the next value because its initialization happened outside the outer loop.
int number = 1;
for (int row = 1; row <= 4; row++) {
for (int column = 1; column <= row; column++) {
cout << number << " ";
number++;
}
cout << endl;
}If you initialize number inside the outer loop, every new row assigns 1 to it again. The inner loop still prints the correct number of positions, but the sequence restarts on every row. The output becomes 1, then 1 2, then 1 2 3, then 1 2 3 4. The variable may be visible to the inner loop, yet its initialization location still determines whether its value continues across rows.
Move the counter initialization so the rows share one continuing counter.
for (int row = 1; row <= 4; row++) {
int number = 1;
for (int column = 1; column <= row; column++) {
cout << number << " ";
number++;
}
cout << endl;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
You can verify the pattern by recording number after each row finishes. Row 1 prints one value, so the counter changes from 1 to 2. Row 2 prints two values, so it changes from 2 to 4. Row 3 prints three values, so it changes from 4 to 7. Row 4 prints four values, so it changes from 7 to 11.
| COMPLETED ROW | VALUES PRINTED IN THAT ROW | COUNTER AFTERWARD |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2, 3 | 4 |
| 3 | 4, 5, 6 | 7 |
| 4 | 7, 8, 9, 10 | 11 |
The counter increases once for each printed position, and the rows contain 1 + 2 + 3 + 4 = 10 positions. Starting at 1 and advancing ten times leaves the counter at 11. Since every increment happens after one distinct printed value, no number is repeated or skipped, and the ten positions contain exactly 1 through 10.