Using Arrays (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Becci Peters

Expertise

Computer Science

Using Arrays

  • Arrays are used to store and manage multiple values of the same data type efficiently
  • They can be used for tasks such as storing student scores, calculating averages, and managing inventory data

Using variables in arrays

  • Variables can be used as indexes in arrays to access and modify elements by repeatedly checking every element
  • This is useful when iterating through the array using loops or performing calculations based on user input

What is the first index value?

  • The first index can be either 0 or 1, depending on the programming language and personal preference
  • Most programming languages, such as Python, Java, and Visual Basic, use 0 as the first index

Exam Tip

  • In pseudocode, the first index can be either 0 or 1, so it is crucial to read the question to find out

Pseudocode example:

DECLARE scores[5] OF INTEGER

FOR i FROM 0 TO 4

INPUT scores[i]

ENDFOR

Python example:

scores = [0] * 5

for i in range(5):

scores[i] = int(input())

Java example:

int[] scores = new int[5];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
    scores[i] = scanner.nextInt();
}

Visual Basic example:

Dim scores(5) As Integer
For i As Integer = 0 To 4

   console.Writeline("Enter score: ")
    scores(i) = Console.ReadLine())
Next

Worked example

Declare an array to store customer names

[1]

CustomerNames[1:30] [1]

Declare the arrays to store each customer's date of birth, telephone number, email address and balance

[2]

  • CustomerDOB[1:30]
  • CustomerTelNo[1:30]
  • CustomerEmail[1:30] [1]
  • CustomerBalance[1:30] [1]

Using Iteration

  • Loops can be used to write values into arrays, iterating through each element and assigning a value
  • Iteration can also be used to read values from arrays, traversing through each element and performing operations or outputting the value
  • Nested iteration is useful for working with multi-dimensional arrays, such as 2D arrays, where you need to iterate through rows and columns

Pseudocode example:

DECLARE scores[3][3] OF INTEGER
FOR i FROM 0 TO 2
   FOR j FROM 0 TO 2
      INPUT scores[i][j]
   ENDFOR
ENDFOR

FOR i FROM 0 TO 2
   FOR j FROM 0 TO 2
      OUTPUT scores[i][j]
   ENDFOR
ENDFOR

Python example:

scores = [[0] * 3 for _ in range(3)]
for i in range(3):
    for j in range(3):
        scores[i][j] = int(input())

for i in range(3):
    for j in range(3):
        print(scores[i][j])

Java example:

int[][] scores = new int[3][3];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        scores[i][j] = scanner.nextInt();
    }
}

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.print(scores[i][j] + " ");
    }
    System.out.println();
}

Visual Basic example:

Dim scores(2, 2) As Integer
For i As Integer = 0 To 2
    For j As Integer = 0 To 2

        Console.Writeline("Enter score: ")
        scores(i, j) = Console.ReadLine())
    Next
Next

For i As Integer = 0 To 2
    For j As Integer = 0 To 2
        Console.Write(scores(i, j) & " ")
    Next
    Console.WriteLine()
Next

Exam Tip

  • Keep track of your loop variables to avoid confusion and errors - it's best to give these clear names (row and column) rather than i and j

Worked example

Write the pseudocode to output the contents of the arrays Balance[ ] and Email [ ] along with suitable messages

[3]

  Count ← 0
REPEAT
   PRINT "Customer: ", Count, " Balance: ", Balance[Count], " Email: ",Email[Count]
[3]
        Count ← Count + 1  
  UNTIL Count = 30 [1]
 

You could also answer with a WHILE or FOR loop

 

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.