Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › CONTROL FLOW
For n = 4, the pattern prints four rows, and every row contains the numbers 1 through 4 in a different starting position. Row 1 starts at 2, row 2 starts at 3, and row 3 starts at 4. After 4, the sequence wraps back to 1.
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3Compare the same column in two consecutive rows. The value in row 1, column 0 is 2, while the value in row 0, column 0 is 1. Moving down increases the value by 1. The same is true from row 1 to row 2 and from row 2 to row 3, except that a value of 4 must be followed by 1.
Moving right within a row also increases the value by 1. In row 0, the values move from 1 to 2 to 3 to 4. In row 1, they move from 2 to 3 to 4 and then wrap to 1. The pattern is therefore controlled by position, not by a counter that remembers what the previous cell printed.
Label the rows and columns from 0 to 3. At any position, first add the row and column indices. For example, row 0, column 0 gives 0, and row 0, column 2 gives 2. Row 1, column 2 gives 3. The sum increases by 1 whenever you move one cell right or one cell down.
| ROW \ COLUMN | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 3 |
| 1 | 1 | 2 | 3 | 4 |
| 2 | 2 | 3 | 4 | 5 |
| 3 | 3 | 4 | 5 | 6 |
The sums do not yet have the required range, because they can reach 6. Taking the remainder after division by 4 changes the repeating sums into values from 0 through 3. A sum of 4 has remainder 0, a sum of 5 has remainder 1, and a sum of 6 has remainder 2. Adding 1 then changes 0-3 into the printed values 1-4.
What value is printed at row 2, column 3 when n = 4?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The value at every position can be calculated with one expression: ((row + column) % n) + 1. It uses the current row and column directly, so no separate counter needs to be increased, reset after printing 4, or repaired when a new row begins.
int value = ((row + column) % n) + 1;
cout << value << " ";At row 0, column 2, the expression is ((0 + 2) % 4) + 1, which gives 3. This is a non-wrapping position. At row 1, column 3, it is ((1 + 3) % 4) + 1. The sum is 4, the remainder is 0, and the printed value is 1. The wrap happens automatically at the exact point where the sequence reaches 4.
A mutable-counter version would need to notice that 4 was just printed and change the counter back to 1. It would also need to reset or adjust the starting value for each row. Those repairs are extra state, and extra state creates places for rows to start incorrectly. The expression has no memory of earlier cells, so the same rule works at every position.
Complete the expression in print(___) for the fixed 4 by 4 output. Use the current row and column variables.
print(___)Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The expression chooses a value, but the loop bounds choose how many values and rows exist. With n = 4, the outer loop must use row < n, so row takes the values 0, 1, 2, and 3. The inner loop must use column < n, so each row receives exactly four values.
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
int value = ((row + column) % n) + 1;
cout << value << " ";
}
cout << "\n";
}for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) {
int value = ((row + column) % n) + 1;
System.out.print(value + " ");
}
System.out.println();
}For n = 4, row < n creates four passes of the outer loop, and column < n creates four passes of the inner loop during each outer pass. The output therefore has four rows with four values each. Changing the inner bound to column < row would create rows of different widths, while changing either bound to include n would create an extra pass with an index of 4.