Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
For n = 4, the output has four rows. Row 1 contains one value, row 2 contains two values, row 3 contains three values, and row 4 contains four values. The outer loop selects the row, and the inner loop visits the positions in that row.
row 1: 1 position
row 2: 2 positions
row 3: 3 positions
row 4: 4 positionsA position has both a row and a column. On row 3, the columns are 1, 2, and 3, so the row number and column number describe where a value is placed. The printed value is separate: in the target output, those three positions contain 4, 5, and 6.
1
2 3
4 5 6
7 8 9 10Which values belong on row 3 of the n = 4 output?
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 can use row values from 1 through 4. For each row r, the inner loop starts its column at 1 and continues while the column is at most r. That loop condition gives row 1 one iteration, row 2 two iterations, row 3 three iterations, and row 4 four iterations.
for (int row = 1; row <= 4; row++) {
for (int column = 1; column <= row; column++) {
// One print operation for this column
}
}| ROW | COLUMNS VISITED | INNER-LOOP ITERATIONS |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1, 2 | 2 |
| 3 | 1, 2, 3 | 3 |
| 4 | 1, 2, 3, 4 | 4 |
At this point, the column tells you which position is being visited, but it does not need to be the number you print. The inner loop supplies the shape of the triangle. A separate value counter supplies the continuous sequence.
Initialize value before the outer loop so one counter serves all four rows. Print value during every inner-loop iteration, then increment it immediately after the print. When a row ends, value keeps its current number instead of starting over.
int value = 1;
for (int row = 1; row <= 4; row++) {
for (int column = 1; column <= row; column++) {
cout << value << " ";
value++;
}
cout << endl;
}If you initialize value inside the outer loop, each new row sets it back to 1. The row widths would still be correct, because column still controls the number of print operations, but the values would restart on every row.
for (int row = 1; row <= 4; row++) {
int value = 1;
for (int column = 1; column <= row; column++) {
cout << value << " ";
value++;
}
cout << endl;
}Move the initialization so value survives the row breaks. Type the corrected placement as a short line and its location.
for (int row = 1; row <= 4; row++) {
int value = 1;
for (int column = 1; column <= row; column++) {
cout << value << " ";
value++;
}
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
DIAGRAM — NOT DRAWN YET
A four-row triangular grid is shown. Row 1 has column 1 containing value 1. Row 2 has columns 1 and 2 containing values 2 and 3. Row 3 has columns 1, 2, and 3 containing values 4, 5, and 6. Row 4 has columns 1, 2, 3, and 4 containing values 7, 8, 9, and 10. Markers show that the column returns to 1 at every new row, while one arrow carries the printed values continuously from 1 to 10.
The four rows perform 1 + 2 + 3 + 4 = 10 inner-loop iterations. Each iteration prints exactly one value, so the output must contain ten printed numbers. The counter starts at 1, prints each current value, and increments after printing, which means the tenth iteration prints 10 and changes value to 11.
row 1: 1 print total = 1
row 2: 2 prints total = 3
row 3: 3 prints total = 6
row 4: 4 prints total = 10
after printing 10: value = 11This count checks more than the shape. If the column were printed, its reset to 1 would cause repeated values at row breaks. If value were reset inside the outer loop, each row would begin at 1. Keeping the column for position and value for numbering gives ten prints with no skipped, repeated, or extra number.