A program includes the following assignment statement: Result ← STR_TO_NUM(x) / STR_TO_NUM(y) When the program evaluates the expression in the statement, it performs a calculation. Variable Result is of type real and variables x and y are of type string. Two checks are required before the calculation is performed: 1. The two strings represent valid numeric values. 2. The numeric value of string y is not zero.
- (a)Identify the type of error that could occur if these checks are not carried out and state a cause of this error. Type Cause[2]
- (b)The designer considers implementing the checks and calculation as a module (a procedure or a function). One reason for this is that the same checks and calculations are performed at several places in the program. Give another reason why this is a suitable approach and state what is avoided by this approach. Reason Avoided[2]
- (c)The module to perform the checks and calculation will be implemented as a function. The function will need to return both a real and a Boolean value. To achieve this a record type is defined in pseudocode as follows: TYPE Result DECLARE Done : BOOLEAN DECLARE Value : REAL ENDTYPE The function Evaluate() will: • take two parameters of type string representing the two numeric values • return a variable of type Result with the Done field set to FALSE if either of the following applies: o at least one of the strings does not represent a valid numeric value o the numeric value of the string representing value y is zero • otherwise return a variable of type Result with the Done field set to TRUE and the Value field assigned the result of the formula (based on the numeric value of the two parameters). Write pseudocode for the function Evaluate().[6]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2024). The mark or mark type is shown in brackets.
- [1] (a) Type: Run-time error
- [1] (a) Cause: A divide by zero operation is attempted // Attempt to convert a non-numeric string to a number
- [1] (b) Reason: This part of the algorithm performs a specific task // if the check or calculation is changed it is changed only once
- [1] (b) Reason: A (similar) subroutine is already available // Library routine is already available
- [1] (b) Reason: The program is simplified / easier to understand / easier to design / code / test / debug / maintain
- [1] (b) Avoided: Unnecessary code duplication
- [1] (b) Avoided: Errors caused by differences where several copies of the check and calculation exist
- [1] (c) Function heading, parameters, ending
- [1] (c) Correct return type of Result
- [1] (c) Local declaration of type Result
- [1] (c) Attempt at using IS_NUM() to check both parameters/strings
- [1] (c) Attempt at using of STR_TO_NUM() to convert both parameters/strings
- [1] (c) Check if the divisor, is non-zero
- [1] (c) and if so correct calculation and assignment to Value field
- [1] (c) Return variable of type Result having assigned both fields
- FUNCTION Evaluate(NumStr1, NumStr2 : STRING) RETURNS Result
- DECLARE RetVal : Result
- DECLARE Num1, Num2 : REAL
- RetVal.Done ← FALSE
- IF IS_NUM(NumStr1) = TRUE AND IS_NUM(NumStr2) = TRUE THEN
- Num1 ← STR_TO_NUM(NumStr1)
- Num2 ← STR_TO_NUM(NumStr2)
- IF Num2 <> 0 THEN
- RetVal.Value ← (Num1 / Num2)
- RetVal.Done ← TRUE
- ENDIF
- ENDIF
- RETURN RetVal
- ENDFUNCTION
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 →