DSA SheetLesson · no judge

DSA FUNDAMENTALSCONTROL FLOW

Floyd's Triangle Keeps One Continuous Counter

Reading · 5 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 26

Row r prints exactly r numbers

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.

ROWNUMBER OF POSITIONSVALUES PLACED IN THOSE POSITIONS
111
222 3
334 5 6
447 8 9 10
5511 12 13 14 15
The row number controls the number of positions.

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.

TEXTFive rows with lengths 1 through 5.
row 1: 1
row 2: 2 3
row 3: 4 5 6
row 4: 7 8 9 10
row 5: 11 12 13 14 15
CHECKPOINT 1Not answered

How 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.

One counter must survive every row boundary

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.

ROWVALUES PRINTEDVALUE OF NEXT AFTER THE ROW
112
22 34
34 5 67
47 8 9 1011
511 12 13 14 1516
The counter's path across the five rows.

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.

The n = 5 Floyd's triangle with one counter crossing row boundariesFive left-aligned rows contain 1; 2 3; 4 5 6; 7 8 9 10; and 11 12 13 14 15. A continuous path joins every consecutive pair from 1 to 15, including pairs that cross a line break. The path is labeled next and indicates an increase of 1 after each printed value.123456789101112131415row 1row 2row 3row 4row 5nextafter each printed number: next ← next + 1
A line break changes the row, not the next number.

Printing before incrementing preserves the 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.

CPPThe persistent counter is printed before it is incremented.
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.

CHECKPOINT 2Not answered

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 final counter value verifies the whole triangle

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.

TEXTFifteen positions produce fifteen consecutive values, then next becomes 16.
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
Previous · Triangular Number Patterns 2Next part · Quiz