Advanced Algorithms and Recursion: Question 9

Syllabus 20.1

Structured A2 8 marks

A programmer solves the same simple task, deciding whether a number is even or odd, three times, using three different programming paradigms.

Snippet 1, a simplified low-level (assembly-style) instruction sequence, operating on a register R1:

LOAD R1, Num
AND R1, R1, #1
CMP R1, #0
JUMPIFEQUAL IsEven

Snippet 2, written in pseudocode:

DECLARE Num : INTEGER
IF (Num MOD 2) = 0 THEN
    OUTPUT "Even"
ELSE
    OUTPUT "Odd"
ENDIF

Snippet 3, written in a declarative, logic-based style:

even(X) :- 0 is X mod 2.
odd(X) :- 1 is X mod 2.

(a) Identify which programming paradigm (low-level, imperative, or declarative) each of the three snippets represents. [3]

(b) For Snippet 1, explain one feature of the code that identifies it as low-level programming, referring to what the instructions directly manipulate. [2]

(c) For Snippet 3, explain how declarative programming differs fundamentally from imperative programming, in terms of what the programmer specifies to the computer. [3]

Show worked solution Hide worked solution

Worked solution

Part (a): Identifying each paradigm

  • Snippet 1 is low-level. It works directly with a named register (R1) and uses machine-level instructions (LOAD, AND, CMP, JUMPIFEQUAL) instead of named variables and structured statements.
  • Snippet 2 is imperative. It uses a named variable (Num), a high-level operator (MOD), and an explicit IF ... THEN ... ELSE ... ENDIF structure that the computer executes step by step, in the order written.
  • Snippet 3 is declarative. It states a logical rule. even(X) holds true whenever 0 is X mod 2, rather than a sequence of instructions to carry out.

[3 marks]: [1] for each correctly identified snippet.

Part (b): What makes Snippet 1 low-level

Snippet 1 is low-level programming because it directly manipulates the hardware rather than working through named variables and high-level control structures:

  • LOAD R1, Num moves a value directly into a specific, named register, rather than into a variable managed automatically by a high-level language.
  • AND R1, R1, #1 performs an explicit bitwise operation to isolate the lowest bit of R1. The programmer must know that testing this bit is how “even or odd” is determined at the level of individual bits, rather than being able to use a built-in operator such as MOD.
  • CMP R1, #0 and JUMPIFEQUAL IsEven explicitly control the flow of execution by comparing a register’s contents and conditionally jumping to a labelled instruction, rather than using a structured IF ... THEN ... ELSE block.

[2 marks]: [1] for identifying that the code manipulates a register directly, [1] for identifying a specific low-level instruction (the bitwise AND, or the CMP/JUMPIFEQUAL pair) and explaining what it does at the hardware level.

Part (c): Declarative versus imperative programming

Imperative programming (Snippet 2) requires the programmer to write out how the task is to be carried out: an explicit, ordered sequence of instructions. Evaluate Num MOD 2, compare the result to 0, then choose one of two branches to output. That the computer follows exactly as written, one statement after another.

Declarative programming (Snippet 3) instead requires the programmer to state what is true, as a fact or rule, without specifying the steps needed to use it. even(X) :- 0 is X mod 2 simply declares that X is even exactly when 0 is X mod 2 is true. No sequence of instructions is given for how to test this. It is left to the underlying system to work out how to evaluate the rule whenever it is asked whether a particular value is even.

This is the fundamental difference between the two paradigms: imperative code specifies the method of solving the problem, while declarative code specifies only the relationship that must hold, leaving the method of evaluating it to the language’s own execution mechanism.

[3 marks]: [1] for correctly explaining that imperative programming specifies how to solve the task step by step, [1] for correctly explaining that declarative programming states what relationship is true instead, [1] for a valid, clearly linked comparison referring to both snippets.

Final answers

  • (a) Snippet 1: low-level. Snippet 2: imperative. Snippet 3: declarative.
  • (b) Snippet 1 directly manipulates a named register and uses machine-level instructions (bitwise AND, CMP/JUMPIFEQUAL) instead of named variables and high-level control structures.
  • (c) Imperative code specifies how to solve the task, as an explicit step-by-step procedure; declarative code states what relationship is true, leaving how to evaluate it to the system.