A-level Computing/AQA/Paper 1/Fundamentals of data structures/Arrays



Arrays
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 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. This can also be done in a single line of code, initialising and populating the array in one step.

Array indicies
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ː 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

Declare an array listing 5 animals in a zoo (aardvark, bear, cuckoo, deer, elephant) in alphabetical order:

Write code to output the first and last animal

Someone has accidentally eaten the cuckoo, let the user add a new third animal and print them all out:

Insert new third animal: Crocodile 1: Aardvark 2: Bear 3: Crocodile 4: Deer 5: Elephant

Two dimensional arrays
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:

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! We are modelling the following board using the two dimensional board variable:

Declare an array to make a small checkers board of type char, 3 squares by 3 squares

create a chequered pattern using  for black and   for white

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  this board (HINT: you need loops), that takes checkBoard as a parameter

Declare a chessBoard (8*8 squares), programmatically colour it in with  for black and. You might want to look for a pattern in the colour assignments for the checker board above and make friends with the MOD function. You might also go a little loopy trying to answer this question

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):
 * Write code to output the name
 * Insert  on row 2 (the third row)
 * Overwite  with