Figure 8 shows an algorithm in pseudo-code. The line numbers are for reference only.
Trace Figure 8 with b1 set to '0010' and b2 set to '0111', filling in columns b1, b2, new and i.
Our own questions on this topic, marked straight away, so you see what to revise. Free now.
Show model answer for 08.1Hide model answer for 08.1
b1 b2 new i
'0010' '0111' '' 0
'0' 1
'01' 2
'010' 3
'0101'How the marks are earned
- Both input strings, '0010' under b1 and '0111' under b2, are written and nothing else appears in those two columns.
- i begins at 0.
- After the first row, i goes 1, 2, 3 on the next three rows and nothing more.
- The first value of new is '0', or an empty string followed by '0'.
- Below the first entry, new reads '01', then '010', then '0101', with nothing extra.
- Different rows may be used if the order within each column is clear.
- Writing the same value again on the next row of a column is not penalised.
- Missing quotes round the strings are ignored.
- If the table has any error, the maximum is 4 marks.
Why it scores
The strings are constant, and new starts empty. At position 0 both digits are 0, so a 0 is added. At position 1 only b2 has a 1, so a 1 is added. At position 2 both digits are 1, so a 0 is added. At position 3 only b2 has a 1, so a 1 is added, giving '0101' as the last value of new.
Revise Trace TablesExplain why line 4 of Figure 8 stops at LEN(b1) - 1, not LEN(b1).
See how you would score on Iteration (Loops)Show model answer for 08.2Hide model answer for 08.2
Positions in a string are numbered from 0, so a string of 4 characters has positions 0 to 3. A loop going up to LEN(b1) would use position 4, which is outside the string and would cause an error.
How the marks are earned
- So the algorithm does not try to reach a character that is not in the string.
- Or so it does not use an index that is out of range.
- Or because positions in a string are numbered from 0, not 1.
- Saying it stops the program crashing, or prevents a run-time error, is also accepted.