logo

Cambridge IGCSE Computer Science revision notes

Arrays Revision Notes

Cambridge IGCSE revision notes for Arrays.

At a glance

revision notes

Resource type

Topic

Arrays

CambridgeIGCSEComputer ScienceProgramming

Complete revision guide 1 of 3

Creating and Using One-Dimensional and Two-Dimensional Arrays

This note explains how to create and use one-dimensional (1D) and two-dimensional (2D) arrays in programming. It covers array declaration, initialization, element access, and practical examples to illustrate their structure and usage.

Syllabus source

Section 3, Topic 8.2, page 29, objective 8.2.1

Open source document ↗

What you should be able to do

  • ✓Understand the structure and purpose of 1D and 2D arrays.
  • ✓Learn how to declare and initialize 1D and 2D arrays.
  • ✓Access and modify elements in arrays using indices.
  • ✓Apply arrays to store and manage collections of related data efficiently.
1

Introduction to Arrays

Arrays are data structures that store multiple values under a single variable name, allowing efficient management of related data. Instead of creating separate variables for each value, an array groups these values in an ordered collection.

A one-dimensional (1D) array is like a list or sequence of elements arranged in a single row. Each element can be accessed using a single index representing its position.

A two-dimensional (2D) array extends this concept into a grid or table format, with rows and columns. Each element is accessed using two indices: one for the row and one for the column.

  • 1D arrays store elements in a linear sequence.
  • 2D arrays store elements in rows and columns, forming a matrix-like structure.
2

Declaring and Initializing One-Dimensional Arrays

To create a 1D array, you specify the type of elements it will hold and the number of elements it can store. For example, to declare an array of five integers:

In many programming languages, this might look like: int numbers[5];

Initialization assigns values to the array elements, either at declaration or later. For example: numbers = [2, 4, 6, 8, 10]; or assigning individually: numbers[0] = 2; numbers[1] = 4; etc. The index typically starts at 0, so numbers[0] refers to the first element.

  • Declare with a fixed size to allocate memory.
  • Initialize with values at declaration or by assignment.
  • Indices usually start at 0, so the first element is at index 0.
3

Declaring and Initializing Two-Dimensional Arrays

A 2D array requires specifying the number of rows and columns. For example, a 3x4 array of integers can be declared as: int matrix[3][4];

This creates a table with 3 rows and 4 columns. Elements are accessed by two indices: matrix[row][column].

Initialization can be done row-wise, for example: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]; where matrix[0][0] is 1 and matrix[2][3] is 12.

  • Specify rows and columns when declaring 2D arrays.
  • Access elements using two indices: row and column.
  • Initialize with nested lists or loops assigning values row by row.
4

Accessing and Modifying Array Elements

To retrieve or change a value in an array, use its index or indices. For a 1D array, access the element at position i with array[i]. For example, to get the third element: value = numbers[2];

For a 2D array, access the element at row r and column c with array[r][c]. For example, to access the element in the second row, fourth column: value = matrix[1][3];

Modifying elements follows the same indexing pattern: array[i] = newValue; or array[r][c] = newValue;

  • Use indices to access or modify elements.
  • Remember that indices start at 0 unless otherwise specified.
  • For 2D arrays, provide both row and column indices.
5

Practical Uses and Examples

1D arrays are useful for storing lists such as student names, scores, or sensor readings.

2D arrays are ideal for representing tables, such as a timetable, a chessboard, or pixel data in images.

Example: Storing daily temperatures for a week (1D array): temperatures = [22, 24, 19, 23, 25, 20, 21]; Access the temperature on day 4 with temperatures[3]. Example: Storing a 3x3 tic-tac-toe board (2D array): board = [['X', 'O', 'X'], [' ', 'X', ' '], ['O', ' ', 'O']]; Access the centre cell with board[1][1].

  • 1D arrays store sequential data like lists.
  • 2D arrays represent grids or tables.
  • Use indices to efficiently retrieve or update data.

At-a-glance summary

Comparison table

StepDetailExampleExam Use
Declare arraySpecify type and sizeint scores[10];Show correct syntax for array creation
Initialize arrayAssign values at declaration or laterscores = [5,7,8,6,9];Demonstrate initialization methods
Access elementUse index to get valuevalue = scores[2];Explain zero-based indexing
Modify elementAssign new value at indexscores[2] = 10;Show element update
2D array declarationSpecify rows and columnsint matrix[3][4];Understand multi-dimensional arrays
Access 2D elementUse row and column indicesvalue = matrix[1][3];Apply nested indexing

Active recall

Quick check

1. How do you access the first element in a 1D array named 'data'?

Use data[0] because array indices start at zero.

2. What indices would you use to access the element in the third row, second column of a 2D array 'grid'?

Use grid[2][1], since rows and columns are zero-indexed.

3. How do you change the value of the fifth element in a 1D array 'scores' to 100?

Assign scores[4] = 100; to update the fifth element.

4. What is the difference between a 1D and a 2D array?

A 1D array is a single list of elements; a 2D array is a table with rows and columns, accessed by two indices.

Complete revision guide 2 of 3

Understanding the Function of Arrays and the Use of Variables as Indexes

This note explains how arrays function as data structures for storing multiple values, the role of indexes in accessing these values, and how variables can dynamically serve as indexes to manipulate array elements effectively.

Syllabus source

Section 3, Topic 8.2, page 29, objective 8.2.2

Open source document ↗

What you should be able to do

  • ✓Explain what an array is and why it is used in programming.
  • ✓Describe how indexes identify positions of elements within an array.
  • ✓Understand that array indexing usually starts at zero.
  • ✓Demonstrate how variables can be used as indexes to access or modify array elements dynamically.
1

What is an Array?

An array is a structured way to store multiple values under a single variable name. Instead of creating separate variables for each value, an array groups them together in a sequence. This makes it easier to manage and process collections of related data, such as a list of student scores or daily temperatures.

Each value stored in an array is called an element. Arrays can hold elements of the same data type, such as integers, characters, or strings, depending on the programming language.

For example, an array named 'scores' might store five test results: 78, 85, 90, 72, and 88. Instead of five separate variables, all these values are stored in 'scores'.

  • Arrays store multiple values in one variable.
  • Elements in an array are related and usually of the same type.
  • Arrays simplify handling large sets of data.
2

Understanding Indexes in Arrays

Each element in an array is identified by its position, called an index. Indexes allow programs to access or modify specific elements within the array.

Most programming languages start array indexing at 0, meaning the first element is at index 0, the second at index 1, and so on. This zero-based indexing is important to remember when working with arrays.

For example, in the 'scores' array, 'scores[0]' refers to the first test score (78), and 'scores[3]' refers to the fourth test score (72).

  • Indexes specify the position of elements in an array.
  • Indexing usually starts at 0, not 1.
  • Accessing an element requires its index, e.g., arrayName[index].
3

Using Variables as Indexes

Variables can be used as indexes to access array elements dynamically. This means the position accessed can change during program execution depending on the variable's value.

For instance, if a variable 'i' holds the value 2, then 'scores[i]' accesses the third element of the array (since indexing starts at 0). Changing 'i' to 4 would access the fifth element.

This technique is essential in loops where the index variable iterates through the array to process all elements one by one.

  • Variables can hold index values to access array elements.
  • Using variables as indexes enables dynamic and flexible data access.
  • Commonly used in loops to process arrays efficiently.
4

Practical Example: Accessing Array Elements with Variables

Consider the array: temperatures = [22, 24, 19, 23, 25]. To print each temperature, a variable 'i' can be used as an index in a loop:

Start with i = 0, print temperatures[i] → 22, then increment i.

Repeat until i reaches the last index (4). This accesses all elements sequentially without hardcoding each index.

  • Example array: temperatures = [22, 24, 19, 23, 25].
  • Use variable i as index to access elements: temperatures[i].
  • Loop from i = 0 to i = 4 to process all elements.
5

Key Points to Remember

Arrays group multiple values under one name, making data management easier.

Indexes identify the position of each element; zero-based indexing is standard.

Variables used as indexes allow flexible and dynamic access to array elements, especially in loops or conditional statements.

  • Arrays store multiple related values.
  • Indexes start at 0 and access elements by position.
  • Variables as indexes enable dynamic element access.

At-a-glance summary

Comparison table

StepDetailExampleExam Use
Define arrayDeclare a variable to hold multiple valuesscores = [78,85,90]Show understanding of array purpose
Access elementUse index to get valuescores[1] returns 85Explain zero-based indexing
Use variable as indexIndex is stored in a variablei = 2; scores[i] returns 90Describe dynamic access
Iterate arrayLoop through indexesfor i in range(3): print(scores[i])Demonstrate processing all elements

Active recall

Quick check

1. What does an array store?

Multiple values grouped under a single variable name.

2. If an array has elements [10, 20, 30], what is the index of the value 20?

1, because indexing starts at 0.

3. How can a variable be used with an array?

A variable can hold an index value to access or modify a specific element dynamically.

4. What is the first index in most programming languages?

0, meaning the first element is at index 0.

Complete revision guide 3 of 3

Inputting and Retrieving Values in Arrays Using Iteration, Including Nested Iteration

This note explains how to input and retrieve values from arrays by iterating through their indexes. It covers the importance of understanding that array indexing can start at zero or one depending on the programming language, and introduces nested iteration for working with multi-dimensional arrays.

Syllabus source

Section 3, Topic 8.2, page 29, objective 8.2.3

Open source document ↗

What you should be able to do

  • ✓Understand how to input values into an array using iteration.
  • ✓Retrieve values from an array by iterating through its elements.
  • ✓Recognise that array indexing may start at 0 or 1 depending on the language.
  • ✓Apply nested iteration to access elements in multi-dimensional arrays.
1

Understanding Arrays and Indexing

An array is a collection of elements stored under a single variable name, where each element can be accessed using an index. The index identifies the position of an element within the array.

Different programming languages use different starting points for array indexes. For example, Python and C start indexing at 0, meaning the first element is at index 0. Some languages like Visual Basic start indexing at 1. It is essential to know the indexing convention of the language you are using to avoid off-by-one errors.

Example: In a zero-indexed array of length 5, the valid indexes are 0, 1, 2, 3, and 4.

  • Arrays store multiple values under one variable name.
  • Indexing starts at 0 or 1 depending on the language.
  • Incorrect indexing leads to errors such as accessing invalid elements.
2

Inputting Values into an Array Using Iteration

To input values into an array, a loop is used to iterate through each index and assign a value. This process automates data entry and avoids repetitive code.

For example, to input 5 numbers into an array, a loop runs from the first index to the last, prompting the user or assigning values at each step.

Example in pseudocode (assuming zero-based indexing): FOR i FROM 0 TO 4 array[i] = input("Enter value:") END FOR

  • Use a loop to assign values to each array element.
  • Loop bounds depend on array size and indexing convention.
  • Iteration reduces code repetition and errors.
3

Retrieving Values from an Array Using Iteration

Retrieving or accessing values stored in an array is done by iterating through the array indexes and reading each element.

This is useful for processing or displaying all elements in the array.

Example in pseudocode: FOR i FROM 0 TO 4 PRINT array[i] END FOR

  • Iterate through array indexes to access elements.
  • Useful for processing or displaying all values.
  • Loop bounds must match array size and indexing.
4

Nested Iteration for Multi-Dimensional Arrays

Nested iteration involves using a loop inside another loop. This is necessary when working with multi-dimensional arrays, such as two-dimensional arrays (arrays of arrays).

For example, a 2D array representing a grid with rows and columns requires two loops: one for rows and one for columns.

Example in pseudocode for a 3x3 2D array: FOR row FROM 0 TO 2 FOR col FROM 0 TO 2 PRINT array[row][col] END FOR END FOR

  • Nested loops allow access to elements in multi-dimensional arrays.
  • Outer loop iterates over one dimension (e.g., rows).
  • Inner loop iterates over the other dimension (e.g., columns).
5

Practical Considerations and Common Indexing Issues

Always confirm the indexing convention of your programming language before writing loops to avoid off-by-one errors or index out-of-range errors.

When using nested iteration, ensure that the loop bounds correspond correctly to each dimension's size.

Example: For a 2D array with 4 rows and 5 columns, outer loop runs from 0 to 3, inner loop from 0 to 4 in zero-based indexing.

  • Check indexing start (0 or 1) before coding loops.
  • Set loop bounds carefully to avoid runtime errors.
  • Nested loops must match array dimensions exactly.

At-a-glance summary

Comparison table

StepDetailExampleExam Use
Input valuesUse a loop to assign values to each indexFOR i FROM 0 TO 4: array[i] = input()Show how iteration inputs values into an array
Retrieve valuesUse a loop to access and process each elementFOR i FROM 0 TO 4: PRINT array[i]Calculate or display array contents using iteration
Indexing startIndex may start at 0 or 1 depending on languagePython arrays start at 0; Visual Basic arrays start at 1Explain indexing to avoid off-by-one errors
Nested iterationUse loops inside loops for multi-dimensional arraysFOR row FROM 0 TO 2: FOR col FROM 0 TO 2: PRINT array[row][col]Show nested loops to access 2D array elements

Active recall

Quick check

1. If an array has 10 elements and indexing starts at 0, what is the index of the last element?

9, because indexing starts at 0 so the last element is at index 10 - 1 = 9.

2. How would you input values into a one-dimensional array of size 5?

Use a loop from the first index (0 or 1) to the last index, assigning a value at each iteration.

3. What is nested iteration and when is it used?

Nested iteration is a loop inside another loop, used to access elements in multi-dimensional arrays.

4. Why is it important to know the starting index of an array?

Because incorrect assumptions about the starting index can cause errors like accessing invalid elements or missing the first element.

Related topics

Study nearby topics next

Revision hubs

Keep studying from the right hub