DSA SheetLesson · no judge

DSA FUNDAMENTALSARRAY BASICS

Print an Array in Reverse Without Changing It

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

Reverse output changes the visit order, not the array

The array [4, 2, 7, 1] stores 4 at index 0, 2 at index 1, 7 at index 2, and 1 at index 3. To print it in reverse order, read the elements from index 3 down to index 0. That produces 1 7 2 4, even though the stored array is still [4, 2, 7, 1].

CPPReading the existing elements from right to left produces reverse-order output.
cout << arr[3] << " ";
cout << arr[2] << " ";
cout << arr[1] << " ";
cout << arr[0];

Printing and rearranging are separate actions. Printing only reads values and sends them to the output. Swapping would change which value is stored at each index, but reverse printing needs no assignment and no swap.

CHECKPOINT 1Not answered

Must [4, 2, 7, 1] be swapped before it can produce 1 7 2 4?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The first reverse index is n - 1, not n

Here n is 4, so the array has four positions numbered 0, 1, 2, and 3. The last valid index is n - 1, which is 3. A reverse loop must therefore start its loop variable at 3.

CPPThe first reverse index is the last valid index.
int n = 4;
int i = n - 1;  // i is 3

Starting at i = n would start at 4. There is no index 4 in this array, so the first access would already be outside the array. In C++, accessing an array outside its valid indices is undefined behaviour, which means the program may print a wrong value, appear to work, or fail.

CHECKPOINT 2Not answered

Fill in the initialization for a reverse loop over n = 4.

for (int i = _____; i >= 0; i--) {
    cout << arr[i] << " ";
}

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

A reverse loop reaches index 0 only when it moves down and checks i >= 0

The loop starts at i = 3 and must move toward smaller indices. The update i-- changes the loop variable from 3 to 2, then 1, then 0. The condition i >= 0 allows index 0 to be visited, so the values are printed as 1 7 2 4.

I BEFORE THE VISITVALUE READOUTPUT SO FAR
311
271 7
121 7 2
041 7 2 4
The loop's index and output after each visit.
CPPStart at the last valid index, move down by one, and stop after index 0.
for (int i = n - 1; i >= 0; i--) {
    cout << arr[i] << " ";
}

After the body runs for i = 0, the update i-- makes i equal to -1. The condition i >= 0 is then false, so the loop stops before trying to access arr[-1]. The direction, stopping condition, and starting index together visit every valid index exactly once.

the array [4, 2, 7, 1] with i moving from index 3 to index 0Four array cells contain 4, 2, 7, and 1 at indices 0, 1, 2, and 3. The loop starts with i = n - 1 = 3, then arrows move left through indices 2, 1, and 0. The values are printed as 1 7 2 4. After index 0, i becomes -1, the condition is false, and no array access occurs.4271i = n - 1 = 3i = -1condition false0123visited values, in order1724
The loop visits every valid index from 3 down to 0, then stops at -1.