DSA FUNDAMENTALS › CONTROL FLOW
For n = 5, the outer loop creates five rows. The row number also gives the row length: row 1 has 1 position, row 2 has 2 positions, row 3 has 3 positions, row 4 has 4 positions, and row 5 has 5 positions. The inner loop visits those positions, so the output has a triangular shape.
| ROW | NUMBER OF POSITIONS | VALUES PLACED IN THOSE POSITIONS |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 2 3 |
| 3 | 3 | 4 5 6 |
| 4 | 4 | 7 8 9 10 |
| 5 | 5 | 11 12 13 14 15 |
The shape and the values are separate decisions. The row number controls how many times the inner loop runs. A value is placed in each position, but the value does not have to equal the row number. For this triangle, the positions are filled from left to right and continue from one row into the next.
row 1: 1
row 2: 2 3
row 3: 4 5 6
row 4: 7 8 9 10
row 5: 11 12 13 14 15How many numbers does row 4 of the n = 5 triangle print?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The values must continue through every line break, so the counter named next cannot be restarted for each row. It begins at 1 and advances once for every position. Row 1 prints 1, then row 2 prints 2 and 3. When row 2 ends, next is ready to print 4, so row 3 begins with 4 rather than starting again from the row number 3.
| ROW | VALUES PRINTED | VALUE OF NEXT AFTER THE ROW |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 3 | 4 |
| 3 | 4 5 6 | 7 |
| 4 | 7 8 9 10 | 11 |
| 5 | 11 12 13 14 15 | 16 |
A line break changes where the next value appears, not which value comes next. After 1, the next value is 2 even though the new row has started. After 3, the next value is 4 even though row 3 has just begun. Treating the row number as the next value would produce the right shape but the wrong sequence.
Declare next before both loops so the outer loop does not create a fresh counter for each row. Inside the inner loop, print next first, then increment it. The first position therefore prints 1 before next changes to 2. The final position prints 15 before next changes to 16.
int next = 1;
for (int row = 1; row <= 5; row++) {
for (int column = 1; column <= row; column++) {
cout << next << " ";
next++;
}
cout << endl;
}Incrementing first reverses that order. The first pass changes next from 1 to 2 and then prints 2, so every printed value is one too large. There are 15 positions, which means the last printed value becomes 16 instead of 15. The counter still advances 15 times, but its starting value was skipped before the first print.
Move next++ to the correct place so the output begins at 1 and ends at 15.
int next = 1;
for (int row = 1; row <= 5; row++) {
for (int column = 1; column <= row; column++) {
next++;
cout << next << " ";
}
cout << endl;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The five row lengths add up to 1 + 2 + 3 + 4 + 5 = 15 positions. Starting at 1 and increasing by 1 after each position therefore prints exactly the values 1 through 15, with no gap and no duplicate. After the last value, 15, has been printed, the increment changes next to 16.
The line break belongs after the inner loop, not inside it as a replacement for the counter update. The inner loop first completes every position in one row, then the outer loop moves to the next row and the line break is printed. This keeps the five values of row 5 on one line while next continues to carry the sequence.
positions: 1 + 2 + 3 + 4 + 5 = 15
printed: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
next: 1 16