Write values into, and read values from, an array using iteration

Programming – Arrays and Iteration

Learning Objective (AO1)

Write values into, and read values from, an array using iteration (count‑controlled FOR loops and pre‑condition WHILE loops). Include both one‑dimensional and two‑dimensional arrays, demonstrate correct handling of array bounds and show how to validate user‑supplied indices.

Cambridge IGCSE 0478 Syllabus Links

Syllabus RequirementHow the note satisfies it
Topic 8.2 – Arrays (declare, write, read, 1‑D & 2‑D, lower‑bound convention) Declarations for 1‑D and 2‑D arrays, explicit lower‑bound tables, write/read examples with FOR and WHILE loops, and index‑validation.
Topic 8.1 – Programming concepts (variables, constants, data types, I/O, sequence, selection, iteration, operators) Quick‑reference box, keyword‑to‑syllabus mapping, and a simple sequence example.
Iteration structures (count‑controlled, pre‑condition, post‑condition) Side‑by‑side comparison table, examples of FOR, WHILE and REPEAT … UNTIL.
Array bounds & validation (AO2) Two validation snippets – one simple check, one repeat‑until‑valid – each labelled “AO2”.
Algorithm design & trace tables (AO2 & AO3) Step‑by‑step procedure, full trace table for a student‑marks program, and guidance on dry‑run.

Quick Reference Box (Keywords & Operators)

Keywords (uppercase)
DECLARE … AS INTEGER | REAL | STRING
CONSTANT … ← …
INPUT …OUTPUT …
FOR i ← lower TO upper DO … END FOR
WHILE condition DO … END WHILE
REPEAT … UNTIL condition
IF … THEN … ELSE … END IF

Operators
Assignment:  Arithmetic: +, –, *, /, MOD Relational: =, ≠, <, ≤, >, ≥ Logical: AND, OR, NOT

Keyword → Syllabus Mapping (Topic 8.1)

  • DECLARE – variables & data types
  • CONSTANT – constants
  • INPUT / OUTPUT – I/O
  • IF … THEN … ELSE – selection
  • FOR, WHILE, REPEAT … UNTIL – iteration (count‑controlled, pre‑condition, post‑condition)
  • Arithmetic & relational operators – operators

Sequence Example (straight‑line code)

DECLARE a ← 5
DECLARE b ← 12
DECLARE sum ← a + b
OUTPUT "The sum is ", sum

This demonstrates the “sequence” part of Topic 8.1 – code executed from top to bottom without any control structures.

Lower‑Bound Conventions

ConventionFirst valid indexLoop limits for an array of size SIZE
0‑based (default in many languages) 0 FOR i ← 0 TO SIZE‑1
1‑based (allowed by Cambridge) 1 FOR i ← 1 TO SIZE
Common‑Mistake: Mixing conventions in the same program (e.g. declaring a 0‑based array but looping FOR i ← 1 TO SIZE) leads to off‑by‑one errors and runtime “index out of bounds” messages.

Iteration Structures – Comparison

StructureSyntax (pseudocode)When to useAssessment Objective
Count‑controlled FOR i ← lower TO upper DO … END FOR Number of repetitions known before the loop starts (e.g. processing every element of a fixed‑size array). AO1 – write/read arrays; AO2 – recognise correct bounds.
Pre‑condition WHILE condition DO … END WHILE Loop should continue only while a condition remains true; condition tested before each iteration (e.g. sentinel‑controlled input). AO1 – use of WHILE; AO2 – correct loop termination.
Post‑condition REPEAT … UNTIL condition At least one execution is required; condition tested after the body (e.g. menu that must be shown at least once). AO1 – use of REPEAT … UNTIL; AO2 – proper termination condition.

1‑Dimensional Arrays

  • Declaration – size must be a constant known at compile time.
CONSTANT SIZE ← 10
DECLARE numbers[SIZE] AS INTEGER

Writing values with a count‑controlled FOR loop

FOR i ← 0 TO SIZE‑1 DO
    numbers[i] ← (i + 1) * 3      // store the first SIZE multiples of 3
END FOR

Reading values with a count‑controlled FOR loop

FOR i ← 0 TO SIZE‑1 DO
    OUTPUT numbers[i]
END FOR

Pre‑condition loop example – reading a fixed number of marks

CONSTANT CLASS_SIZE ← 5
DECLARE marks[CLASS_SIZE] AS INTEGER
DECLARE i ← 0

WHILE i < CLASS_SIZE DO
    OUTPUT "Enter mark for student ", i+1, ": "
    INPUT marks[i]
    i ← i + 1
END WHILE

Post‑condition loop example – sentinel input (optional)

DECLARE total ← 0
DECLARE count ← 0
DECLARE n ← -1

REPEAT
    OUTPUT "Enter a positive number (0 to stop): "
    INPUT n
    total ← total + n
    count ← count + 1
UNTIL n = 0

AO2 – Simple index validation (single test)

DECLARE index ← 7
IF index ≥ 0 AND index < SIZE THEN
    OUTPUT "Element = ", numbers[index]
ELSE
    OUTPUT "Error: index out of bounds"
END IF

AO2 – Robust validation for a user‑supplied index (repeat until valid)

DECLARE idx ← -1
REPEAT
    OUTPUT "Enter an index between 0 and ", SIZE-1, ": "
    INPUT idx
    IF idx < 0 OR idx ≥ SIZE THEN
        OUTPUT "Invalid index – try again."
    END IF
UNTIL idx ≥ 0 AND idx < SIZE

OUTPUT "Element at index ", idx, " = ", numbers[idx]

2‑Dimensional Arrays

  • Declared as a matrix of rows × columns. Both dimensions must be constant.
CONSTANT ROWS ← 3
CONSTANT COLS ← 4
DECLARE matrix[ROWS][COLS] AS INTEGER

Writing values with nested FOR loops

FOR r ← 0 TO ROWS‑1 DO
    FOR c ← 0 TO COLS‑1 DO
        matrix[r][c] ← (r+1) * (c+1)      // simple multiplication table
    END FOR
END FOR

Reading values with nested FOR loops

FOR r ← 0 TO ROWS‑1 DO
    FOR c ← 0 TO COLS‑1 DO
        OUTPUT "matrix[", r, "][", c, "] = ", matrix[r][c]
    END FOR
END FOR

Step‑by‑Step Procedure (Algorithm Design)

StepAction
1Declare the array (or matrix) with a constant size.
2Choose a lower‑bound convention (0‑based or 1‑based) and set the loop counter to that lower bound.
3Write values: inside the loop assign array[i] ← value (or matrix[r][c] ← value).
4If a second loop is needed, reset the counter (or use a new one) before the read phase.
5Read values: inside the loop output array[i] (or matrix[r][c]).
6When an index may come from the user, include an AO2 validation step before any access.

Common Pitfalls (AO2)

  • Accessing an index outside the declared bounds (e.g. array[SIZE] when the highest valid index is SIZE‑1).
  • Mixing lower‑bound conventions – loop limits must match the chosen base (0 or 1).
  • Forgetting to reset the loop counter before a second loop, resulting in zero iterations.
  • Omitting validation when the index is supplied by the user; this can cause runtime errors.

Example: Storing and Displaying Student Marks (1‑D Array)

Class size = 5. The program asks for each mark, stores it, then prints the marks.

CONSTANT CLASS_SIZE ← 5
DECLARE marks[CLASS_SIZE] AS INTEGER
DECLARE i ← 0

// ---- Write phase (FOR loop) ----
FOR i ← 0 TO CLASS_SIZE‑1 DO
    OUTPUT "Enter mark for student ", i+1, ": "
    INPUT marks[i]
END FOR

// ---- Read phase (FOR loop) ----
OUTPUT "Marks entered:"
FOR i ← 0 TO CLASS_SIZE‑1 DO
    OUTPUT "Student ", i+1, ": ", marks[i]
END FOR

Trace Table – Write Phase

Iteration (i)Input valuemarks[i] after assignment
07878
18585
29292
36767
47373

Students should produce a similar table for the read phase, showing the values displayed on each iteration.

Suggested Diagram

Flowchart showing initialise counter → write loop → reset counter → read loop. For 2‑D arrays, the write and read sections contain nested loops.
Flowchart: initialise counter → write loop (assign) → reset counter → read loop (output). For a 2‑D array, the write and read sections contain nested loops.

Create an account or Login to take a Quiz

44 views
0 improvement suggestions

Log in to suggest improvements to this note.