Fundamentals of data structures: Arrays
Arrays
[edit | edit source]An array is an indexed set of related elements. When we want to store several pieces of data of the same type, instead of using many variables we can instead use an array. You can think of an array as a list of data items or elements each with a number or index to enable you to refer to each item. Note that the index of the array starts at 0
Many similar variables | One Array | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
name0=Alice |
|
In most languages, an array must be declared. This sets up how many elements there are in the array and what data type the elements are. This will reserve space in memory for the array before it has been filled with data. Python uses lists in place of arrays. Lists are more flexible and do not require initialisation but it can still be useful to perform this step in order to make your intentions and your code easier to understand.
VB.NET | Python |
---|---|
Dim names(0 To 4) As String
names(0) = "Alice"
names(1) = "Bob"
names(2) = "Claire"
names(3) = "Dan"
names(4) = "Eve"
|
names = ["" for x in range(5)]
names[0] = "Alice"
names[1] = "Bob"
names[2] = "Claire"
names[3] = "Dan"
names[4] = "Eve"
|
This can also be done in a single line of code, initialising and populating the array in one step.
VB.NET | Python |
---|---|
Dim names() As String = {"Alice","Bob","Claire","Dan","Eve"}
|
names = ["Alice","Bob","Claire","Dan","Eve"]
|
Array indicies
[edit | edit source]We can access a single element of an array by using the index. This is written in parentheses or brackets after the array name. To access Eve from our names array we would useː
VB.NET | Python |
---|---|
console.WriteLine(names(4))
|
print(names[4])
|
Some programming languages start numbering arrays from 1, others from 0. In our examples above, it does not matter which number relates to which person and so 0 is used as a starting point but if we were storing months, many people might prefer January to be stored in months[1] rather than months[0]. XKCD on Array indicies
Exercise: One-Dimensional Arrays Declare an array listing 5 animals in a zoo (aardvark, bear, cuckoo, deer, elephant) in alphabetical order: Answer:
Write code to output the first and last animal Answer:
Someone has accidentally eaten the cuckoo, let the user add a new third animal and print them all out:
Code Output
Insert new third animal: Crocodile Answer:
|
Two dimensional arrays
[edit | edit source]An array is an indexed set of elements. If each element is itself an array then we have a two dimensional (2d) array.
If we think of an array as a list, we can think of a 2d array as a grid or matrix.
Most major programming languages allow you to use two-dimensional arrays. They work in much the same way as a one-dimensional array but allow you to specify a column index and a row index.
We can create the two-dimensional array shown above and assign values by doing the following:
VB.NET | Python |
---|---|
Dim grid(4,4) As String
grid(0,3) = "A"
grid(3,2) = "B"
grid(1,4) = "C"
Console.Writeline("The content of 3,2 is:" & grid(3,2))
|
grid = [["" for x in range(4)] for x in range(4)]
grid[0][3] = "A"
grid[3][2] = "B"
grid[1][4] = "C"
print("The content of 3,2 is:",grid[3][2])
|
Example: Two-Dimensional Arrays Two-dimensional arrays are very useful and a good place to get started is to create your own version of the game Battleships with a 4 cell by 4 cell grid. See if you can win, break it or better still, improve it!
|
Exercise: Two-Dimensional Arrays
Declare an array to make a small checkers board of type char, 3 squares by 3 squares
Answer:
create a chequered pattern using
b for black and w for whiteAnswer:
A much smarter way is to use a loop, this will allow for you to quickly create an board of any size you wish. There is a question coming up that will want you to build this! Note that in python, we are forced to start the index at 0. Also python uses the same datatype for characters and strings.
Write a sub routine to
display this board (HINT: you need loops), that takes checkBoard as a parameterAnswer:
Declare a chessBoard (8*8 squares), programmatically colour it in with Answer:
If you've done this you might want to get the program to print some massive boards, whatever floats your boat. Using the following two-dimensional array, grid(4,4):
Answer:
|