Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
For scores = [4, 2, 7, 1], n is 4, so the valid indices are 0, 1, 2, and 3. A loop that starts at 0 and continues while i < n visits exactly those four positions. Index 4 is not part of scores, because the array has four elements and its last index is n - 1, which is 3.
for (int i = 0; i < n; i++) {
// use scores[i]
}| I | DOES I < N HOLD? | RESULT |
|---|---|---|
| 0 | Yes | The loop body runs |
| 1 | Yes | The loop body runs |
| 2 | Yes | The loop body runs |
| 3 | Yes | The loop body runs |
| 4 | No | The loop stops |
Choose the exact sequence of indices visited by for (int i = 0; i < n; i++) 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 expression scores[i] uses the current value of i to select one element. The index changes as the loop runs, while scores[i] names the element selected at that moment. With scores = [4, 2, 7, 1], the same expression produces a different value on each iteration.
| I | EXPRESSION | SELECTED VALUE |
|---|---|---|
| 0 | scores[0] | 4 |
| 1 | scores[1] | 2 |
| 2 | scores[2] | 7 |
| 3 | scores[3] | 1 |
for (int i = 0; i < n; i++) {
cout << scores[i] << " ";
}The variable i does not hold the element value. When i is 2, i is still 2, but scores[i] means scores[2], whose value is 7. This distinction lets one loop use the same expression to read each position in the array.
Reading scores[i] gives you the value stored at the selected position. If you copy that value into another variable, the two names do not become connected. Changing the separate variable changes only its own value, not the element that supplied the original value.
int current = scores[i];
current += 3;To change the array, the left side of the assignment must be scores[i]. The right side first reads the selected value, adds 3, and then the assignment stores the result back in that same position.
scores[i] = scores[i] + 3;Replace the ineffective update to current with the array-element assignment that adds 3 to scores[i].
int current = scores[i];
current += 3;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
When the loop assigns scores[i] = scores[i] + 3, it changes one position and leaves the other positions as they are for that moment. Starting with scores = [4, 2, 7, 1], the first iteration updates index 0 from 4 to 7. The next iteration reads the still-current array and updates index 1 from 2 to 5.
for (int i = 0; i < n; i++) {
scores[i] = scores[i] + 3;
}| ITERATION | UPDATED INDEX | ARRAY STATE |
|---|---|---|
| Before the loop | None | [4, 2, 7, 1] |
| i = 0 | 0 | [7, 2, 7, 1] |
| i = 1 | 1 | [7, 5, 7, 1] |
| i = 2 | 2 | [7, 5, 10, 1] |
| i = 3 | 3 | [7, 5, 10, 4] |
After i = 2, the first three positions have their increased values and index 3 is still 1. The final iteration updates that last position to 4. When you read scores after the loop, the stored array is [7, 5, 10, 4], because every valid index was visited and every assignment wrote back through scores[i].