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]