Use loops to process arrays

Arrays – Using Loops to Process Arrays (Cambridge IGCSE 0478)

1. Quick Overview of the Whole IGCSE 0478 Syllabus (2026‑2028)

TopicKey Sub‑points (exam‑required)
1. Computer Systems CPU, RAM, ROM, motherboard, input/output devices, storage, binary ↔ decimal ↔ hexadecimal, two’s‑complement, basic logic gates.
2. Data Representation & Transmission Bits & bytes, ASCII, Unicode, image/audio/video encoding, data compression, packet structure, error‑detecting codes (parity, checksum).
3. Hardware & Software Operating system functions, firmware, drivers, high‑level vs low‑level languages, compiler vs interpreter, IDEs, debugging tools.
4. Internet & Cyber‑Security IP, MAC, DNS, HTTP/HTTPS, cookies, firewalls, encryption basics, common threats (malware, phishing, DDoS), safe computing.
5. Emerging Technologies AI & machine learning basics, cloud computing, IoT, robotics, ethical considerations.
6. Boolean Logic & Digital Circuits Truth tables, Boolean algebra, logic gate symbols, simplification, combinational vs sequential circuits.
7. Algorithms & Flowcharts Algorithm design cycle (Analyse‑Design‑Code‑Test), flowchart symbols, pseudocode conventions, trace tables.
8. Programming – Arrays & Loops Array declaration, bounds, 1‑D & 2‑D processing, FOR/WHILE/DO‑WHILE loops, nested loops, common array algorithms, validation.
9. Databases Data tables, primary keys, simple SQL SELECT/INSERT/UPDATE, relational concepts.
10. Evaluation (AO3) Testing, debugging, efficiency, safety (bounds checking), suitability of solutions.

2. Assessment Objectives (AO) for the Array & Loop Topic

AOWhat the exam looks for in this topic
AO1 – Knowledge & UnderstandingDefine an array, state its bounds, describe why loops are used, list the three loop types.
AO2 – Application of KnowledgeWrite correct Cambridge‑style pseudocode (including line numbers, indentation, comments) to declare arrays, traverse them, and implement common algorithms.
AO3 – EvaluationTest algorithms with trace tables, discuss off‑by‑one errors, show how to check array bounds and handle invalid input, comment on efficiency and readability.

3. Pseudocode Conventions Required by Cambridge

  • Every line begins with a line number (1, 2, 3 …).
    1. DECLARE Scores : ARRAY[0:9] OF INTEGER
  • Indentation is used to show block structure (typically 4 spaces).
    2. FOR i ← 0 TO 9 DO
  • Comments are written after // and are ignored by the examiner when marking logic.
  • Keywords are written in UPPER‑CASE: FOR, IF, THEN, END IF, NEXT, OUTPUT.
  • All loop bounds are inclusive – use TO upper, never <= upper in the pseudocode.

4. What Is an Array?

  • An array is a collection of elements of the same data type stored in contiguous memory locations.
  • Each element is accessed by an index. The index range is fixed when the array is declared: lower … upper.
  • Cambridge allows the lower bound to be 0 or 1 (or any other integer), but the same lower bound must be used in every loop that accesses the array.

5. Declaring Arrays (Cambridge‑specified Pseudocode)

Array typeDeclaration syntaxExample
One‑dimensional (1‑D) DECLARE name : ARRAY[lower:upper] OF type DECLARE Scores : ARRAY[0:9] OF INTEGER // 10 scores
Two‑dimensional (2‑D) DECLARE name : ARRAY[low1:up1, low2:up2] OF type DECLARE Matrix : ARRAY[1:3, 1:3] OF INTEGER // 3×3 matrix

6. Loop Structures Used in the Syllabus

  • FOR … NEXT – known number of iterations; ideal for traversing arrays.
  • WHILE … END WHILE – repeats while a condition is true; useful when the number of iterations is not known in advance.
  • DO … WHILE … END DO – guarantees at least one execution before the condition is tested.

7. Traversing a One‑Dimensional Array with a FOR Loop

General pattern (Cambridge style, with line numbers and indentation):

1. FOR i ← lower TO upper DO
2.     // process Array[i]
3. NEXT i

For DECLARE Scores : ARRAY[0:9] OF INTEGER the loop becomes:

1. FOR i ← 0 TO 9 DO
2.     // e.g. total ← total + Scores[i]
3. NEXT i

8. Common Array Algorithms (All with Full Pseudocode)

8.1 Calculating the Sum and Average (single pass)

1. DECLARE Scores : ARRAY[0:9] OF INTEGER
2. DECLARE total, average ← 0
3. FOR i ← 0 TO 9 DO
4.     total ← total + Scores[i]
5. NEXT i
6. average ← total / 10
7. OUTPUT "Sum = ", total
8. OUTPUT "Average = ", average

8.2 Finding the Maximum Value

1. DECLARE Scores : ARRAY[0:9] OF INTEGER
2. DECLARE max ← Scores[0]
3. FOR i ← 1 TO 9 DO
4.     IF Scores[i] > max THEN
5.         max ← Scores[i]
6.     END IF
7. NEXT i
8. OUTPUT "Maximum = ", max

8.3 Finding the Minimum Value and Its Frequency (single pass)

1. DECLARE Data : ARRAY[1:10] OF INTEGER
2. DECLARE min, count, value ← 0
3. FOR i ← 1 TO 10 DO
4.     INPUT value
5.     Data[i] ← value
6.     IF i = 1 THEN
7.         min ← value
8.         count ← 1
9.     ELSIF value = min THEN
10.        count ← count + 1
11.    ELSIF value < min THEN
12.        min ← value
13.        count ← 1
14.    END IF
15. NEXT i
16. OUTPUT "Smallest value = ", min
17. OUTPUT "Occurs ", count, " times"

8.4 Counting Occurrences of a Specific Value (two‑pass version)

1. DECLARE Scores : ARRAY[0:9] OF INTEGER
2. DECLARE target, count ← 0
3. INPUT target
4. FOR i ← 0 TO 9 DO
5.     IF Scores[i] = target THEN
6.         count ← count + 1
7.     END IF
8. NEXT i
9. OUTPUT "Target occurs ", count, " times"

8.5 Reversing a One‑Dimensional Array In‑Place

1. DECLARE Scores : ARRAY[0:9] OF INTEGER
2. DECLARE temp ← 0
3. FOR i ← 0 TO (10 DIV 2) - 1 DO
4.     temp ← Scores[i]
5.     Scores[i] ← Scores[9 - i]
6.     Scores[9 - i] ← temp
7. NEXT i

8.6 Initialising (or Clearing) an Array

1. DECLARE Names : ARRAY[1:30] OF STRING
2. FOR i ← 1 TO 30 DO
3.     Names[i] ← ""          // empty string – clears the element
4. NEXT i

8.7 Processing a Two‑Dimensional Array (Nested FOR Loops)

1. DECLARE Matrix : ARRAY[1:3, 1:3] OF INTEGER
2. // ----- Input all elements -----
3. FOR row ← 1 TO 3 DO
4.     FOR col ← 1 TO 3 DO
5.         INPUT Matrix[row, col]
6.     NEXT col
7. NEXT row
8. // ----- Output the matrix -----
9. FOR row ← 1 TO 3 DO
10.    FOR col ← 1 TO 3 DO
11.        OUTPUT Matrix[row, col]   // space between values
12.    NEXT col
13.    OUTPUT NEWLINE
14. NEXT row

9. Array Bounds Checking & Validation (AO3)

  • Why it matters: Accessing an index outside lower … upper is a logical error that can cause wrong results or runtime failures.
  • Typical safety pattern:
1. DECLARE Scores : ARRAY[0:9] OF INTEGER
2. DECLARE i, value ← 0
3. FOR i ← 0 TO 9 DO
4.     INPUT value
5.     IF value < 0 OR value > 100 THEN
6.         OUTPUT "Invalid – enter 0‑100"
7.         i ← i - 1          // repeat this iteration
8.     ELSE
9.         Scores[i] ← value
10.    END IF
11. NEXT i
  • Line 7 demonstrates how to “stay” on the same index when input fails.
  • When processing an existing array, always test the index before using it, e.g.:
    1. IF index < lower OR index > upper THEN
    2.     OUTPUT "Index out of range"
    3. ELSE
    4.     // safe to use Array[index]
    5. END IF
            

10. Exam‑Focus Box – How This Topic Is Marked

AOTypical Marks & What Examiners Look For
AO1 (Knowledge) Define array, state lower/upper bounds, list the three loop types, explain why loops are used (2‑3 marks).
AO2 (Application) Write correct pseudocode with line numbers, proper indentation, and comments. Include declaration, loop, and at least one array algorithm (4‑6 marks).
AO3 (Evaluation) Provide a trace table, discuss off‑by‑one errors, show a bounds‑checking routine, comment on efficiency (e.g., single‑pass vs two‑pass) (3‑4 marks).

11. Common Pitfalls & How to Avoid Them

  • Off‑by‑one errors: Remember the last valid index is the upper bound you declared. Use FOR i ← lower TO upper (inclusive).
  • Wrong loop condition syntax: Do **not** write i <= upper in Cambridge pseudocode; use the TO keyword.
  • Uninitialised accumulators: Set total ← 0, count ← 0, max/min ← first element before the loop.
  • Modifying array size inside a loop: Bounds are fixed at declaration – never change them.
  • Inconsistent lower bound: The same lower bound must appear in the declaration **and** in every loop that accesses the array.
  • Missing bounds check when reading user input: Always validate before storing; repeat the iteration if the check fails (see Section 9).

12. Step‑by‑Step Trace Table Example (Sum of 10 Scores)

StepiScores[i]total (after addition)
Initialise0
107878
2185163
10992825
After loop825 (average = 82.5)

13. Development Cycle (Topic 7 – Algorithm Design) Applied to Arrays

  1. Analyse – Identify inputs (e.g., 10 scores), required outputs (sum, average), and constraints (0‑100).
  2. Design – Choose a FOR loop, decide on accumulator initialisation, decide whether a single‑pass or two‑pass solution is more efficient.
  3. Code – Write numbered, indented pseudocode following Cambridge conventions.
  4. Test – Create at least two test data sets (including boundary values), run a trace table, and verify the output.

14. Exam Practice Question

Question: Write pseudocode that reads 10 integer values into an array called Data, then outputs the smallest value and how many times it occurs.

Full Development Cycle Answer

  1. Analyse
    • Input: 10 integers (range –1000 to 1000).
    • Output: minimum value and its frequency.
    • Processing: store values, keep track of current minimum and count.
  2. Design – single‑pass algorithm (updates min and count as each value is read).
  3. Code (Cambridge style, numbered)
    1. DECLARE Data : ARRAY[1:10] OF INTEGER
    2. DECLARE min, count, value ← 0
    3. FOR i ← 1 TO 10 DO
    4.     INPUT value
    5.     Data[i] ← value
    6.     IF i = 1 THEN
    7.         min ← value
    8.         count ← 1
    9.     ELSIF value = min THEN
    10.        count ← count + 1
    11.    ELSIF value < min THEN
    12.        min ← value
    13.        count ← 1
    14.    END IF
    15. NEXT i
    16. OUTPUT "Smallest value = ", min
    17. OUTPUT "Occurs ", count, " times"
            
  4. Test (two data sets)
    Test #Input valuesExpected minExpected count
    15 3 8 3 9 2 2 7 2 423
    210 10 10 10 10 10 10 10 10 101010

15. Quick Reference Cheat‑Sheet (Arrays & Loops)

  • Declaration (1‑D): DECLARE name : ARRAY[lower:upper] OF TYPE
  • Declaration (2‑D): DECLARE name : ARRAY[low1:up1, low2:up2] OF TYPE
  • FOR loop (Cambridge): FOR i ← lower TO upper DO … NEXT i
  • Common patterns
    • Sum – initialise accumulator to 0.
    • Maximum/Minimum – initialise with first element.
    • Count – initialise to 0.
    • Initialise/clear – loop and assign a default value.
  • Validation tip: check each input before storing; repeat the iteration if the check fails (see Section 9).
  • Nested loops for 2‑D arrays: outer loop for rows, inner loop for columns; both use FOR … TO ….

16. Suggested Diagram for Revision

Horizontal array of boxes labelled 0 … n‑1 with an arrow labelled “i” moving left‑to‑right. The arrow highlights the current element accessed by a FOR loop.

Create an account or Login to take a Quiz

47 views
0 improvement suggestions

Log in to suggest improvements to this note.