Programming Basics (Pseudocode Tracing)
Array indices start at 1. What does array a contain after the following program runs?
integer array: a ← {5, 1, 4, 2, 3}
integer: t ← a[1]
for (i from 1 to 4 step 1)
a[i] ← a[i + 1]
endfor
a[5] ← t
- A{1, 4, 2, 3, 5}
- B{1, 4, 2, 3, 3}
- C{3, 5, 1, 4, 2}
Show answer and explanation
Answer: A. {1, 4, 2, 3, 5}
After saving the first value 5 in t, a[i] ← a[i + 1] moves each element one place forward. Because i runs upward, each value moves before it is overwritten, giving {1, 4, 2, 3, 3}. Finally a[5] ← t puts 5 at the end, giving {1, 4, 2, 3, 5}. The program rotates the whole array one place to the left.
- A. Correct. The first value 5 moves to the end and the rest move forward.
- B. This is the content without the final a[5] ← t.
- C. This is a rotation one place to the right. This program moves elements forward.
Continue: 9 more Programming Basics (Pseudocode Tracing) questions