Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick revision guide and must not be used by students during a NEA where internet access is not allowed. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Arrays

Strings, reals, booleans and integers are all primitive data types: they only store one value at a time.

An array lets you store more than one value of the same primitive data type in the same variable.

A variable can store one primitive data type or it can contain an array.

An array stores data in the order that you specify in your code. The position of a value inside an array is called the index.

Top tips:

  • Arrays can only store one data type (e.g. all strings or all integers)
    Python doesn't have arrays but it does have lists, which are more powerful and let you store a mixture of different data types.
  • Both VB.NET and Python start counting the index of an array from 0.
    This means what we think of as being the 1st item in an array has an index of 0.

 

Create array of strings
   

An array let your store more than one piece of data into the same variable as an ordered list.

This example stores three strings into the array called names.

SET names TO ["Percy", "James", "Gordon"]
Dim names As String() names = {"Percy", "James", "Gordon"}
names = ["Percy", "James", "Gordon"]
array names[3] names[0] = "Percy" names[1] = "James" names[2] = "Gordon"
names[3] set names[0] = "Percy" set names[1] = "James" set names[2] = "Gordon"
Create array of numbers
   

An array lets you store more than one piece of data into the same variable as an ordered list.

This example stores three numbers into the array called ages.

SET ages to [14, 15, 14]
Dim ages As Integer() ages = {14, 15, 14}
ages = [14, 15, 14]
array ages[3] ages[0] = 14 ages[1] = 15 ages[2] = 14
ages[3] set ages[0] = 14 set ages[1] = 15 set ages[2] = 14
Access data in an array
   

An array lets you store more than one piece of data into the same variable as an ordered list.

Data in an array can be accessed using an index. This is a number which defines the position in the list, which starts counting from 0

This example displays the first string in an array called names

SET names TO ["Percy", "James", "Gordon"] SEND names[0] TO DISPLAY
Dim names As String() names = {"Percy", "James", "Gordon"} MsgBox(names(0))
names = ["Percy", "James", "Gordon"] print(names[0])
array names[3] names[0] = "Percy" names[1] = "James" names[2] = "Gordon" print(names[0])
names[3] set names[0] = "Percy" set names[1] = "James" set names[2] = "Gordon" output names[0]
Create a 2D array
   

If an array is like a list of data, a 2d array is like a grid of data where the data is stored in one variable but organised into rows and columns like a spreadsheet.

This example stores details of 3 different fruit for a supermarket.

It will then display the colour of the first fruit.

The first index will select the row then the second index selects the column

SET fruit TO [["Plum", "Purple"], ["Apple", "Red"], ["Banana", "Yellow"]] SEND fruit[0,1] TO DISPLAY
Dim fruit As String(,) fruit = {{"Plum", "Purple"}, {"Apple", "Red"}, {"Banana", "Yellow"}} MsgBox(fruit(0,1))
fruit = [["Plumb", "Purple"], ["Apple", "Red"], ["Banana", "Yellow"]] print(fruit[0][1])
array fruit[3][2] fruit[0][0] = "Plumb" fruit[0][1] = "Purple" fruit[1][0] = "Apple" fruit[1][1] = "Red" fruit[2][0] = "Banana" fruit[2][1] = "Yellow" print(fruit[0][1])
{ not specified in eduqas pseudocode reference }

Loading...