Study the algorithm: DECLARE Chars : ARRAY[1:4] OF CHAR DECLARE I, J : INTEGER DECLARE Key : CHAR I ← 2 Chars[1] ← 'D' Chars[2] ← 'T' Chars[3] ← 'H' Chars[4] ← 'R' WHILE I <= 4 Key ← Chars[I] J ← I - 1 WHILE J >= 0 AND Chars[J] > Key Chars[J + 1] ← Chars[J] J ← J - 1 ENDWHILE Chars[J + 1] ← Key I ← I + 1 ENDWHILE
- (a)The outer loop structure used in the algorithm is not the most appropriate one to use. State the type of loop structure that would be the most appropriate to use and justify why it is the most appropriate. Loop structure Justification[2]
- (b)Complete the trace table by dry running the algorithm. The first row has been completed. | I | Key | J | Chars[J] | Chars[1] | Chars[2] | Chars[3] | Chars[4] | |---|-----|---|----------|----------|----------|----------|----------| | 2 | | | | 'D' | 'T' | 'H' | 'R' |[9]
Mark scheme
Marking points from the official mark scheme (May/June 2025). The mark or mark type is shown in brackets.
- [1] Count-controlled
- [1] The number of iterations required is known
- [1] For I=3, Key becomes 'H', J becomes 2, Chars[J] (Chars[2]) is 'T'
- [1] Chars[2] is updated to 'H'
- [1] For I=3, J becomes 1, Chars[J] (Chars[1]) is 'D'
- [1] For I=4, Key becomes 'R', J becomes 3, Chars[J] (Chars[3]) is 'T'
- [1] For I=4, J becomes 2, Chars[J] (Chars[2]) is 'H'
- [1] Final Chars array values: ['D', 'H', 'R', 'T']
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →