Cambridge AS Computer Science

Functions

In pseudocode, functions and procedures are used to divide a program into smaller, manageable parts. They help improve clarity, reduce repetition, and make algorithms easier to understand and maintain. A procedure is a block of code that performs a specific task and may accept input values but does not return a result, while a function performs a task and returns a single value to the part of the program that called it. Both functions and procedures are essential in Cambridge A Level Computer Science, as they demonstrate structured programming and are commonly used in exam-style problem solving


Question #1: Write pseudocode to validate a given password. For a password to be valid it must contain:

Write a function ValidatePassword that will check if the given password follows the above rules. This function takes a parameter Pass as a parameter and returns TRUE if it is valid and FALSE otherwise.

FUNCTION ValidatePassword(Pass: STRING) RETURNS BOOLEAN DECLARE Result: BOOLEAN DECLARE Lcase: INTEGER DECLARE Ucase: INTEGER DECLARE len: INTEGRER DECLARE Letter: CHARACTER Lcase ← 0 Ucase ← 0 Digits ← 0 Len ← LENGTH(Pass) FOR x ← 1 TO Len Letter ← MID(Pass,x,1) IF Letter >= 'A' Letter <= Z THEN Ucase ← Ucase + 1 ELSE IF Letter >= 'a' AND Letter <= 'z' THEN Lcase ← Lcase + 1 ELSE IF Letter >= '0' AND Letter <= '9' THEN Digits ← Digits + 1 ENDIF NEXT x IF len >= 8 AND Lcase >= 2 AND Ucase >= 2 AND Digits >= 2 THEN Result ← True ELSE Result ← False ENDIF RETURN Result ENDFUNCTION
Description
STACK PROGRAM
Write pseudocode for implementing a stack using array. Use the following procedures for stack operations: Push() – Accepts an integer value to be pushed into the stack. Pop() – to remove an item from the stack and Display() – to display the content of the stack. Display a menu to choose the stack operations like, Push, Pop, Display and Exit.
DECLARE MyStack: ARRAY[1,5] OF INTEGER DECLARE Choice: INTEGER DECLARE Max: INTEGER DECLARE Top: INTEGER DECLARE Bottom: INTEGER // Procedure to insert an item to the stack PROCEDURE PUSH(val : INTEGER) IF Top = StackFull THEN OUTPUT "Sorry, Stack Full" ELSE Top ← Top + 1 MyStack[Top] ← Val ENDIF ENDPROCEDURE // Procedure to display the content of the stack PROCEDURE DISPLAY() IF Top = 0 THEN OUTPUT "Stack is Empty" ELSE ind ← Top WHILE ind > 0 OUTPUT MyStack[ind] ind ← ind - 1 ENDIF ENDPROCEDURE // Procedure for removing an item from the stack PROCEDURE POP() IF Top < 1 THEN OUTPUT "Stack is Empty!" ELSE OUTPUT MyStack[Top],"has been removed from stack" Top ← Top - 1 IF Top = 0 THEN Bottom ← 0 ENDIF ENDIF ENDPROCEDURE // Stack main program StackFull ← 5 Top ← 0 Bottom ← 0 REPEAT OUTPUT "1. PUSH" OUTPUT "2. POP" OUTPUT "3. DISPLAY" OUTPUT "4. EXIT" OUTPUT "Enter your choice" INPUT choice CASE Choice OF 1: OUTPUT "Enter a number to push" INPUT num PUSH(num) 2: POP() 3: DISPLAY() ENDCASE UNTIL Choice = 4 ------------- END ---------------

Get In Touch

123 Street, New York, USA

info@ictmadesimple.com

+012 345 67890

Newsletter

Follow Us

© www.ictmadesimple.com. All Rights Reserved.