Objectives: Students should be able to —
- 1 Write, amend and correct errors in flowcharts.
- 2 Write, amend and correct errors in programs.
- 3 Write, amend and correct errors in pseudocode.
- 4 Describe the stages of designing and constructing an algorithm.
- 5 Use 1D and 2D arrays, nested loops, CASE and REPEAT-UNTIL in algorithms.
- 6 Use procedures (SUB) and functions with parameters and return values.
- 7 Apply pre-defined functions: LEN(X), LENGTH(X), ROUND(X, n), UPPER(X), LOWER(X), SUBSTRING(X, start, len).
- 8 Use meaningful names and comments to make code readable.
- 9 Use test data (normal, abnormal, boundary) and trace tables to validate algorithms.
Stages of Designing and Constructing an Algorithm
Method: The ten sequential stages of designing and constructing an algorithm are —
- Analyse the problem and make sure that you understood it properly.
- Determine what would be the — Inputs, Processes and Outputs.
- Break down the problem into sub-problems if it is complex.
- Write down all the steps needed to solve the problem sequentially from start to end.
- Determine what variables and constants need to be declared and initialised to set up the program.
- Determine the sequential statements and compound statements (like selection and looping) need to be used.
- Construct your algorithm using either flowchart or pseudocode designing tools.
- Making sure that it can be easily read and understood by others. Use meaningful names for variables and constants.
- Use several sets of test data (normal, abnormal and boundary) and trace tables to find any errors in your algorithm.
- Debug the errors if found, and test your algorithm until it works perfectly to produce the desired output.
Algorithms using Arrays, Nested Loops and CASE
(a) Pseudocode algorithm — uses a nested FOR loop (outer: 4 subjects; inner: 600 students), CASE OF to switch subject name and a REPEAT-UNTIL range-check validation:
(b) Explain how you would test your algorithm:
For the algorithm to be tested by dry running, I would reduce the number of students to 5 and the number of subjects to 2.
Method: This reduces the total iterations from 4 × 600 = 2400 down to 2 × 5 = 10, making it practical to fill in a trace table by hand. Test data should include:
- Normal data — marks within range (e.g., 55, 78, 92).
- Boundary data — marks at 0 and 100 (the limits of the range check).
- Abnormal data — marks outside range (e.g., -5, 105) to verify the REPEAT-UNTIL rejects them.
(a) Pseudocode algorithm to declare arrays and variables, and input the names and marks using nested FOR loops:
(b) Grading program — uses CONSTANT thresholds, nested FOR loop for totals, ROUND() pre-defined function and nested IF for grade selection:
The grade boundaries are —
| Average mark | Grade awarded |
|---|---|
| Average >= 70 | Distinction |
| Average >= 55 AND < 70 | Merit |
| Average >= 40 AND < 55 | Pass |
| Average < 40 | Fail |
Password Algorithms — Validation, Procedures and Functions
Method: The algorithm uses a REPEAT-UNTIL loop controlled by a flag variable PassCheck and an attempt counter. It performs a length check using LEN() and a double-entry verification by comparing Password with Password2.
Method: The main program uses a REPEAT-UNTIL loop that stops when Option = 4. It CALLs the DashBoard SUB procedure to display the menu, then uses CASE OF to dispatch each option. Option 2 and 3 CALL the PassChecker FUNCTION with parameters and use its return value.
Method: The main loop REPEATs until PassValidation() returns "Valid password.". The function uses LENGTH(), UPPER(), LOWER() and SUBSTRING() pre-defined functions, and four counters (NoSpace, NoUpCase, NoLowCase, NoDigit) to verify each rule.
Game Algorithm — Noughts & Crosses with 2D Array
Method: The 3×3 grid is stored in Game[1:3, 1:3]. The main FOR Count = 1 TO 9 loop allows up to 9 moves. Each move validates the chosen cell is empty and the symbol is X/O and is different from the previous one. The SUB DisplayBoard procedure prints the grid and the FUNCTION Result checks all four winning lines (rows, columns, two diagonals) using EXIT FOR to short-circuit.
Part 1 — Main program (declare & initialise array, accept moves, call procedure/function):
Part 2 — SUB DisplayBoard procedure (prints the 3×3 grid):
Part 3 — FUNCTION Result (checks rows, columns and both diagonals for a winning line):
Revision: Statements and Key Computing Terms
| Statement | Key Term |
|---|---|
| A step-by-step set of instructions used to solve a problem. | Algorithm |
| A diagrammatic representation of an algorithm using shapes (oval, rectangle, diamond, parallelogram). | Flowchart |
| A text-based, half-formal representation of an algorithm using keywords like INPUT, OUTPUT, FOR, WHILE. | Pseudocode |
| Instructions executed one after another, in order. | Sequence |
| A construct that chooses between paths based on a condition (IF/THEN/ELSE, CASE OF). | Selection |
| A construct that repeats a block of code (FOR/NEXT, WHILE/ENDWHILE, REPEAT/UNTIL). | Iteration (Looping) |
| A named storage location whose value can change during program execution. | Variable |
| A named storage location whose value is fixed throughout the program (e.g., CONSTANT Pass = 40). | Constant |
| A collection of variables of the same type accessed using a single identifier and an index. | Array |
| An array with one index, storing a single list of values (e.g., StudentName[1:100]). | 1D Array |
| An array with two indexes, storing rows and columns of values (e.g., Game[1:3, 1:3]). | 2D Array |
| A keyword used to define the size and data type of an array or variable. | DECLARE |
| The operator ← used to store a value in a variable (e.g., Attempt ← 0). | Assignment operator |
| A loop that runs a fixed number of times using a counter (FOR ... NEXT). | FOR-NEXT loop |
| A loop that tests the condition at the start and may not execute at all (WHILE ... ENDWHILE). | WHILE loop |
| A loop that tests the condition at the end and always executes at least once (REPEAT ... UNTIL). | REPEAT-UNTIL loop |
| A multi-way selection construct that runs one branch out of many (CASE OF ... ENDCASE). | CASE statement |
| A sub-routine that performs a task but does not return a value, called with CALL. | Procedure (SUB) |
| A sub-routine that performs a task and returns a single value using RETURN. | Function |
| A value passed into a procedure or function when it is called. | Parameter |
| A variable used to control how many times a loop runs (e.g., Count, NoStd). | Counter variable |
| A variable used to keep a running total (e.g., StdTotal = StdTotal + Mark). | Accumulator |
| A Boolean variable used to remember the state of a check (e.g., PassCheck ← TRUE). | Flag / Tag variable |
| A statement that jumps out of a loop before its natural end (e.g., EXIT FOR). | EXIT statement |
| A pre-defined function that returns the number of characters in a string. | LEN(X) / LENGTH(X) |
| A pre-defined function that returns a substring of length len starting at position start. | SUBSTRING(X, start, len) |
| A pre-defined function that returns the string converted to upper-case / lower-case. | UPPER(X) / LOWER(X) |
| A pre-defined function that rounds a number to n decimal places. | ROUND(X, n) |
| A validation check that ensures input is within a given range (e.g., Mark >= 0 AND Mark <= 100). | Range check |
| A validation check that ensures a string has the correct number of characters. | Length check |
| A verification technique where the user types the same data twice and the two inputs are compared. | Double-entry verification |
| Test data that should be accepted by the program (typical values). | Normal data |
| Test data that should be rejected by the program (wrong type or out of range). | Abnormal data |
| Test data at the edges of acceptability (the lowest and highest valid values). | Boundary / Extreme data |
| A technique of running an algorithm by hand, recording the values of all variables at each step. | Dry run / Trace table |
| Text in a program that is ignored by the computer but explains the code to humans (e.g., // ...). | Comment |
| The process of finding and fixing errors in an algorithm or program. | Debugging |