6 (a) The factorial of an integer number is the product of all the integers from that number down to 1. In general, the factorial of n is n × (n-1) × ... x 2 x 1 For example, the factorial of 5 is 5 × 4 × 3 × 2 × 1 = 120 In this question, n will be referred to as the BaseNumber. A function FindBaseNumber() will: • be called with a positive, non-zero integer value as a parameter • return BaseNumber if the parameter value is the factorial of the BaseNumber • return -1 if the parameter value is not a factorial. For example: Parameter value Value returned 120 5 12 -1 6 3 1 1 FindBaseNumber(12) will return -1 because 12 is not a factorial. You may use the rest of this page for rough working.
- (a)Write pseudocode for the function FindBaseNumber().[7]
- (b)A program is written to allow a user to input a sequence of values to be checked using the function FindBaseNumber(). The user will input one value at a time. The variable used to store the user input has to be of type string because the user will input 'End' to end the program. Valid input will be converted to an integer and passed to FindBaseNumber() and the return value will be output. Complete the table by giving four invalid strings that may be used to test distinct aspects of the required validation. Give the reason for your choice in each case. Input Reason for choice[4]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2022). The mark or mark type is shown in brackets.
- [7] (a) Example by repeated multiplication: Mark as follows (multiplication solution), (Max 7): 1 Function heading and ending including parameter and return type 2 Declaration and initialisation of local Num 3 Any conditional loop 4 Conditional loop until ThisValue found or Try out of range 5 Multiply Try by Num in a loop 6 Compare Try with ThisValue and set termination if the same in a loop 7 Increment Num and repeat in a loop 8 Attempt to Return Num if ThisValue is a factorial or -1 otherwise FUNCTION FindBaseNumber(ThisValue : INTEGER) RETURNS INTEGER DECLARE Num, Try : INTEGER DECLARE Found : BOOLEAN Num <- 0 Found <- FALSE Try <- 1 WHILE Try <= ThisValue AND Found = FALSE Num <- Num + 1 Try <- Try * Num IF Try = ThisValue THEN //BaseNumber found Found <- TRUE ENDIF ENDWHILE IF Found = TRUE THEN RETURN Num ELSE RETURN -1 ENDIF ENDFUNCTION Alternative FOR LOOP solution. Mark as follows: 1 Function heading and ending including parameter and return type 2 Declaration of local Integer value for Num and Try 3 Count-controlled Loop from 1 to ThisValue 4 Multiply Try by Num in a loop 5 Compare Try with ThisValue in a loop 6 ...Immediate return of Num if they are the same in a loop 7 Return -1 if ThisValue not found after loop FUNCTION FindBaseNumber(ThisValue : INTEGER) RETURNS INTEGER DECLARE Num, Try : INTEGER Try <- 1 FOR Num <- 1 TO ThisValue Try <- Try * Num IF Try = ThisValue THEN //BaseNumber found RETURN Num ENDIF NEXT Num RETURN -1 ENDFUNCTION
- [4] (b) One mark per row. Examples of invalid strings: 1 Non-numeric but not "End" // contains space or other non-numeric characters 2 Real number 3 Integer value out of range (i.e. <= 0) 4 Empty string 5 Correct word but wrong case e.g. "end" rather than "End" Input Reason for choice "Aardvark" Non-numeric (and not "End") "27.3" Numeric but not an integer "-3" // "0" A non-positive integer "" An empty string
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 →