Swapping two values with a temporary variable

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Swapping two values with a temporary variable

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which set of lines correctly swaps Marks[Index] and Marks[Index + 1]?
A Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
B Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index]
2. Why do we store a value in Temp before swapping two array elements?
A So the first value is not lost when it is overwritten
B So the array becomes sorted automatically
3. Marks[3] is 40 and Marks[4] is 70. You run Marks[3] ← Marks[4] and then Marks[4] ← Marks[3]. What are the values now?
A Both are 70
B Marks[3] is 70 and Marks[4] is 40
4. Which line should come first in a swap of Marks[Index] and Marks[Index + 1]?
A Marks[Index + 1] ← Temp
B Temp ← Marks[Index]
5. What does the line Marks[Index + 1] ← Temp do at the end of a swap?
A Saves the value of Marks[Index + 1] into Temp
B Puts the saved original value of Marks[Index] into Marks[Index + 1]

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Temp ← Marks[Index] ACopies the saved value from Temp into the next element, finishing the swap
2Marks[Index + 1] ← Temp BTemp is assigned after Marks[Index] has already been overwritten, so Temp saves the wrong value and the original is lost.
3The middle step of a swap copies the next element into the current one, which is safe because the old value is already in Temp CMarks[Index] ← Marks[Index + 1]
4A complete swap of two neighbouring array elements uses three assignments in the correct order DSaves the value in Marks[Index] in Temp so it is not lost when that element is overwritten
5Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index] ETemp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
6Marks[Index] ← Marks[Index + 1] Temp ← Marks[Index] Marks[Index + 1] ← Temp FThere is no temporary variable. After the first line both elements hold the same value, because the original Marks[Index] was overwritten and lost.

Definitions

Temp ← Marks[Index]

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
____ ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
2.
Temp ← Marks[_____] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
3.
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← ____
4.
Temp _ Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
5.
Temp ← Marks[Index] Marks[Index] ← Marks[_________] Marks[Index + 1] ← Temp

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Temp ← Marks[Index+1]
B Marks[Index] ← Marks[Index+1]
C Marks[Index+1] ← Temp
Question 2
A Marks[1] ← 40
B Marks[2] ← 75
C Temp ← Marks[1]
D Marks[2] ← Marks[1]
E Marks[2] ← Temp
Question 3
A Temp ← Marks[Index]
B Marks[Index] ← Marks[Index+1]
C Marks[Index+1] ← Marks[Index]
Question 4
A DECLARE Temp : INTEGER
B Temp ← Marks[3]
C Marks[3] = Marks[4]
D Marks[4] ← Temp
Question 5
A IF Marks[1] > Marks[2] THEN
B Marks[1] ← Temp
C Marks[1] ← Marks[2]
D Marks[2] ← Temp
E ENDIF

Answer Key

Swapping two values with a temporary variable
Section A: Quick Questions
Q1
A — Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Q2
A — So the first value is not lost when it is overwritten
Q3
A — Both are 70
Q4
B — Temp ← Marks[Index]
Q5
B — Puts the saved original value of Marks[Index] into Marks[Index + 1]
Section B: Match the Pairs
Q1
Temp ← Marks[Index] → Saves the value in Marks[Index] in Temp so it is not lost when that element is overwritten
Q2
Marks[Index + 1] ← Temp → Copies the saved value from Temp into the next element, finishing the swap
Q3
The middle step of a swap copies the next element into the current one, which is safe because the old value is already in Temp → Marks[Index] ← Marks[Index + 1]
Q4
A complete swap of two neighbouring array elements uses three assignments in the correct order → Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Q5
Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Marks[Index] → There is no temporary variable. After the first line both elements hold the same value, because the original Marks[Index] was overwritten and lost.
Q6
Marks[Index] ← Marks[Index + 1] Temp ← Marks[Index] Marks[Index + 1] ← Temp → Temp is assigned after Marks[Index] has already been overwritten, so Temp saves the wrong value and the original is lost.
Section C: Definitions
Q1
Temp ← Marks[Index] — Saves the value of Marks[Index] in Temp so it is not lost. Marks[Index] can then be overwritten, and Temp is copied into Marks[Index + 1] to finish the swap.
Section D: Fill in the Blanks
Q1
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Q2
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Q3
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Q4
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Q5
Temp ← Marks[Index] Marks[Index] ← Marks[Index + 1] Marks[Index + 1] ← Temp
Section E: Spot the Error
Q1
A — Temp must save Marks[Index], the value that is about to be overwritten. Saving Marks[Index+1] means the original Marks[Index] is lost. It should be Temp ← Marks[Index].
Q2
D — The second element is overwritten with Marks[1] when it should be Marks[1] that is overwritten with Marks[2]. Both elements would end up as 40. It should be Marks[1] ← Marks[2].
Q3
C — Marks[Index] has already been overwritten on line 2, so copying it again gives the same value twice. The original value is in Temp. It should be Marks[Index+1] ← Temp.
Q4
C — The = sign is used for comparison, not for assignment. Cambridge pseudocode uses ← to store a value. It should be Marks[3] ← Marks[4].
Q5
B — Temp has not been given a value yet, so this overwrites Marks[1] and loses it. The first step must be to save Marks[1] in Temp. It should be Temp ← Marks[1].