Question 34 2005 · May-June · Paper 2 · 38 marks
Reveal official mark-scheme answer
The answer for this question is in the official mark scheme: open the mark scheme.
Show text version of question
Question 34
END PROCEDURE Validate
The function Len(aString) returns the number of characters in aString. For example, if aString = "Computer", Len(aString) = 8.
Question 4
When the procedure is called with
Validate("58")
the lines
1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 21, 22, 24, 25, 26,12, 26, 27,
are executed and the output is
Valid string.
The example test above can be described as shown in Table 2.1.
Path Test Data Expected Output
1, 2, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 21, 22, 58 Valid string. 24, 25, 26,12, 26, 27, 28, 29, 30, 32, 33, 34
Table 2.1
Using a table similar to Table 2.1, write down the lines that are executed and the output when the procedure is called with
(i)
Validate(".376");
(ii)
Validate("4.9");
(iii)
Validate("6.2.7").
Question 5
Task 3 [20 marks]
This is a design and implementation task
You are to design and create a very simple binary calculator using a high-level language of your choice. A binary calculator can only use the digits 0 and 1. This calculator will allow the user to enter two unsigned binary numbers and will then allow the user to add, subtract, multiply or divide these numbers. In the case of division, the result should be truncated to a whole number. This is equivalent to integer division. The result is to be displayed in binary.
To do this you are advised to follow these steps.
Part (a)
Design an interface that will allow the user to input two unsigned binary numbers and will
display them in two boxes. The user should then be able to choose addition, subtraction, multiplication or division. There should be a box to show the result of the calculation. The user should also be able to clear all the boxes. (No processing is required at this stage.)
Part (b)
Create a function, called BinaryToDecimal, that will accept a binary number as a parameter
and will return its decimal equivalent. For example,
BinaryToDecimal(101101) will return the decimal value 45.
You may use the following algorithm to do this.
Input the binary number as a string Total = 0 For each binary digit in the string, starting on the left, Total = Total * 2 + value of digit Return Total
You should clearly annotate your code and use meaningful names for variables and other objects such as buttons and text boxes (if you use them). You should include a copy of your code as evidence.
Part (c)
Create a function, called DecimalToBinary, that will accept a decimal number as a parameter
and will return its binary equivalent. For example,
DecimalToBinary (47) will return the binary value 101111.
You may use the following algorithm to do this.
Input the decimal number called Number Binary = "" 'Empty string Repeat Remainder = remainder after Number is divided by 2 Number = Whole part of Number divided by 2 Binary = String value of Remainder & Binary Until Number = 0
& means concatenation. For example
"1010" & "1" = "10101"
You should clearly annotate your code and use meaningful names for variables and other objects such as buttons. You should include a copy of your code as evidence.
Question 6
Part (d)
Create code for each of the operations of addition, subtraction, multiplication and division.
You are advised to change the binary numbers input by the user to decimal, do the operation and then convert the result back to binary before displaying it. You should include a copy of your code as evidence.
Part (e)
Create a set of test data that shows that the operations of addition, subtraction, multiplication
and division work and provide evidence that you have used this data. Screen dumps are acceptable but must show the data entered and the result of the operation.
Question 7
Question 8
Every reasonable effort has been made to trace all copyright holders. The publishers would be pleased to hear from anyone whose rights we have unwittingly infringed.
University of Cambridge International Examinations is part of the University of Cambridge Local Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.
Why this question appears here
91% assigned-topic match · Programming › Constructs
Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.
Permanent source page: Educate A Change · From 9691_s05_qp_2.pdf
