Question 2
AQA Computer Science June 2017 Paper 1AS7 marks
On the specification
- 2.1
- 3.4 Theory of computation3.5 Fundamentals of data representation
- 2.2
- 3.1 Fundamentals of programming3.5 Fundamentals of data representation
Question text
2 The algorithm represented using pseudo-code in Figure 3 describes a method to convert two hexadecimal numbers into decimal. The subroutine ToDecimal used in Figure 3 is shown in Figure 4 and the built-in subroutine ASCII is explained in Table 2. Figure 3 FOR Count ← 1 TO 2 INPUT HexString Number ← 0 FOR EACH HexDigit IN HexString Value ← ToDecimal(HexDigit) Number ← Number * 16 + Value ENDFOR OUTPUT Number ENDFOR The FOR EACH command steps through each character in a string working from left to right. Figure 4 SUBROUTINE ToDecimal(HexDigit) IF HexDigit = "A" THEN Value ← 10 ELSEIF HexDigit = "B" THEN Value ← 11 ELSEIF HexDigit = "C" THEN Value ← 12 ELSEIF HexDigit = "D" THEN Value ← 13 ELSEIF HexDigit = "E" THEN Value ← 14 ELSEIF HexDigit = "F" THEN Value ← 15 ELSEIF HexDigit IN ["0", "1", ..., "9"] THEN Value ← ASCII(HexDigit) – 48 ELSE Value ← –1 ENDIF RETURN Value ENDSUBROUTINE Table 2 Subroutine used in Figure 4 Description ASCII(Char) Returns the ASCII code of the character passed as a parameter. Example: ASCII("1") returns 49 2.1 Complete Table 3 by hand-tracing the algorithm in Figure 3. Use "A2" and "1G" as input strings. You may not need to use all the rows in Table 3. Table 3 Count HexString Number HexDigit Value Output Copy the contents of all the unshaded cells in Table 3 into your Electronic Answer Document. [5 marks] 2.2 Explain how the algorithm in Figure 3 has attempted to deal with the conversion of "1G" into decimal and why this method is not fully effective. [2 marks]