Understand the use of arrays

Programming – Understanding the Use of Arrays (IGCSE 0478)

0. Context – Why Arrays Matter in the Whole Syllabus

  • Arrays are the primary way to store a collection of values of the same type, which is essential when:
    • reading or writing multiple records in a file (see Topic 8.3 – File handling),
    • storing a table of data for a simple database (see Topic 9 – Databases),
    • performing calculations in algorithms that use variables, selection and iteration (see Topic 8.1 – Variables, Selection, Iteration),
    • implementing basic AI or data‑processing ideas such as searching and sorting (see Topic 10 – Emerging technologies).
  • Understanding arrays also helps you to apply Boolean logic (e.g., “found AND average > 70”) and to draw correct flow‑charts (see Topic 8.2 – Flow‑chart conventions).
  • All the code you write in the exam must respect the limits on nesting (max 3 levels) and on the number of parameters (max 2) – these limits apply to any sub‑program that uses arrays.

1. Formal Definition (Syllabus wording)

An array is a fixed‑size, homogeneous data structure that stores a sequence of items of the same data type in contiguous memory locations. Each item is called an element and is identified by an index. The index range is defined by a lower‑bound and an upper‑bound that are declared explicitly.

2. Key Characteristics (Syllabus 8.2)

Characteristic What the syllabus expects Exam‑relevant note
Fixed size The number of elements is set when the array is declared; it cannot be altered at run‑time. Declare the size before any input or processing.
Homogeneous All elements must be of the same data type (INTEGER, REAL, CHARACTER, etc.). Mixing types causes a compilation error.
Explicit lower‑bound Both 0‑based and 1‑based indexing are allowed, but the lower‑bound must be stated in the declaration. e.g. DECLARE scores[1:5] or DECLARE scores[0:4].
Random (direct) access Any element can be read or written directly using its index. No need to scan previous elements.
Maximum three levels of nesting Loops or conditionals may be nested, but not more than three deep. Exceeding this limit loses marks.
Maximum two parameters per sub‑program A procedure or function can have at most two arguments. Design your array‑handling routines accordingly.

3. Declaration & Initialisation – Exact Pseudocode Syntax

Language / Pseudocode Declaration (lower‑bound → upper‑bound) Initialisation (if any)
Cambridge Pseudocode (exam style) DECLARE name[lower:upper] OF Optional – elements are undefined until assigned.
Python (list used as array) myArray = [] # lower‑bound is 0 by convention myArray = [3, 7, 2, 9]
Java int[] myArray; // size fixed when created myArray = new int[]{3, 7, 2, 9};
C++ int myArray[4]; int myArray[4] = {3, 7, 2, 9};

4. Accessing Elements

  • Read: value ← myArray[i] – retrieves the element at index i.
  • Write: myArray[i] ← newValue – stores newValue at index i.
  • For an array of size n:
    • upper‑bound = lower‑bound + n – 1
    • Attempting to use an index outside this range causes an array‑bounds error.

5. Iterating Through an Array

5.1 General algorithm (pseudocode)

DECLARE i, lower, upper, total AS INTEGER
total ← 0
FOR i ← lower TO upper
    total ← total + myArray[i]
END FOR

5.2 Java example (1‑D)

int[] scores = {78, 85, 92, 67, 80};
int total = 0;
for (int i = 0; i < scores.length; i++) {
    total += scores[i];
}
double average = total / (double)scores.length;   // real‑division

5.3 2‑D array (matrix addition) – Pseudocode

DECLARE A[0:2][0:2], B[0:2][0:2], C[0:2][0:2]   # 3×3 matrices
/* initialise A and B – omitted for brevity */
FOR i ← 0 TO 2
    FOR j ← 0 TO 2
        C[i][j] ← A[i][j] + B[i][j]
    END FOR
END FOR

6. Allowed Array Operations (exam‑style syntax)

Operation Pseudocode syntax Typical use in the exam
Declare DECLARE name[lower:upper] OF First line of any program that uses an array.
Read (input) INPUT name[i] Collect a series of values from the candidate.
Write (output) OUTPUT name[i] Display a single element or a computed result.
Assign name[i] ← expression Store a calculated value.
Loop (iteration) FOR i ← lower TO upper … END FOR Process every element; may be nested (max 3 levels).
Search (linear) IF name[i] = target THEN … END IF Locate a specific value; often combined with EXIT FOR.
File handling (read/write whole array) OPEN file FOR INPUT
FOR i ← lower TO upper
 READ file → name[i]
END FOR
CLOSE file
Topic 8.3 – reading a list of marks from a file and writing results back.

7. Common Array Operations Required by the Syllabus

  • Summation – accumulate values in a total variable.
  • Average – divide the sum by the number of elements; use real division (sum / 5.0).
  • Maximum / Minimum – compare each element with a stored extreme value.
  • Linear search – scan until the required value is found; use EXIT FOR to stop early.
  • Reverse – swap symmetric elements from the ends toward the centre.
  • Selection sort (simple) – optional but useful for practice.

8. Array Bounds, Overflow & Memory Model

  • Bounds checking: Accessing an index i where i < lower‑bound or i > upper‑bound is a run‑time error (array‑bounds overflow). Always use the declared bounds in loop limits.
  • Memory storage:
    • Arrays are stored in primary memory (RAM) while the program runs.
    • If the array is written to a file, the data is transferred to secondary storage (disk) – see Topic 8.3.
  • Relation to databases: A single‑field table can be represented as a one‑dimensional array; a table with rows and columns maps naturally to a two‑dimensional array.

9. File‑Handling Example (Reading & Writing an Array)

/*--- Read 10 integer marks from a file called "marks.txt" ---*/
DECLARE marks[1:10] OF INTEGER
OPEN "marks.txt" FOR INPUT
FOR i ← 1 TO 10
    READ "marks.txt" → marks[i]
END FOR
CLOSE "marks.txt"

/*--- Process the marks (e.g., find the highest) ---*/
DECLARE max ← marks[1]
FOR i ← 2 TO 10
    IF marks[i] > max THEN
        SET max ← marks[i]
    END IF
END FOR

/*--- Write the result to a file called "result.txt" ---*/
OPEN "result.txt" FOR OUTPUT
WRITE "result.txt" ← "Highest mark: " & max
CLOSE "result.txt"

Notice the explicit lower‑bound 1, the use of OPEN … FOR INPUT/OUTPUT, and the loop limits that match the array bounds.

10. Example Exam‑Style Problem (All Required Elements)

Task: Write a program that reads **five** integer test scores into an array, then outputs the highest score, the average (to one decimal place), and tells whether a given mark m is present in the array.

10.1 Algorithm (Cambridge pseudocode – correct indentation)

DECLARE scores[1:5] OF INTEGER
DECLARE i, max, sum, m AS INTEGER
DECLARE average AS REAL
DECLARE found AS BOOLEAN ← FALSE

/*--- Input phase ---*/
FOR i ← 1 TO 5
    INPUT scores[i]
END FOR

/*--- Find maximum and sum ---*/
SET max ← scores[1]
SET sum ← 0
FOR i ← 1 TO 5
    IF scores[i] > max THEN
        SET max ← scores[i]
    END IF
    SET sum ← sum + scores[i]
END FOR

/*--- Average (real division) ---*/
SET average ← sum / 5.0

/*--- Search for a specific mark ---*/
INPUT "Enter mark to search for: " → m
FOR i ← 1 TO 5
    IF scores[i] = m THEN
        SET found ← TRUE
        EXIT FOR
    END IF
END FOR

/*--- Output results ---*/
OUTPUT "Highest score: " & max
OUTPUT "Average score: " & average
IF found THEN
    OUTPUT "Mark " & m & " is in the list."
ELSE
    OUTPUT "Mark " & m & " is NOT in the list."
END IF

10.2 Key points highlighted

  • Explicit lower‑bound 1 in the declaration.
  • Use of the assignment operator and the Boolean variable found.
  • Four‑space indent for each block; two‑space before THEN and ELSE.
  • Real‑division achieved by the literal 5.0.
  • Linear search demonstrates the required “search” technique.
  • No more than three levels of nesting and only two parameters (none in this example) – complies with syllabus limits.

11. Limits on Nesting and Parameters (Reminder)

12. Typical Errors to Avoid

  • Using an index outside the declared range (lower‑bound … upper‑bound).
  • Confusing the array size with the highest valid index.
  • Failing to initialise an array before reading its elements (especially in Java/C++ where default values may be unexpected).
  • Off‑by‑one errors in loop bounds (i <= n instead of i < n for 0‑based arrays).
  • Performing integer division when a real result is required (e.g., sum / 5 yields an INTEGER).
  • Forgetting to close a file after reading or writing an array.

13. Exam Tips

  1. State the array size and lower‑bound explicitly before any use.
  2. Write loop bounds exactly as lower‑bound TO upper‑bound; never mix size and bound.
  3. When an average is required, ensure real division (use a decimal literal or multiply by 1.0).
  4. Show a brief example of the array’s contents if it helps the examiner visualise your algorithm – this can earn extra clarity marks.
  5. Keep nesting to a maximum of three levels and limit sub‑program parameters to two.
  6. Remember the distinction between primary (RAM) and secondary (disk) storage when you include file handling.

14. Summary

Arrays are a core data structure in the IGCSE Computer Science syllabus. To obtain full marks you must be able to:

  • Declare 1‑D and 2‑D arrays with an explicit lower‑bound using the exact pseudocode syntax.
  • Access, read, and write elements correctly, respecting array bounds.
  • Iterate through an array with properly bounded FOR loops (max 3 nesting levels).
  • Implement the required operations: summation, averaging (real division), finding extremes, linear search, reversing, and simple selection sort.
  • Use arrays in file‑handling contexts and recognise their role in simple databases.
  • Observe the syllabus limits on nesting and parameters, and avoid common off‑by‑one and type‑mismatch errors.
Suggested diagram: a linear 1‑D array (indexes 0‑4) and a 2‑D 3×3 matrix (indexes [0:2][0:2]) with example values.

Create an account or Login to take a Quiz

38 views
0 improvement suggestions

Log in to suggest improvements to this note.