top of page
Writer's pictureMiss S

Arrays

An array is a set of elements of the same data type. This means that it could be a set of integers, chars, strings or even floats. Arrays are made up of multiple elements, which are always ordered, and the amount of elements is finite (finite here means that the number of elements have to be declared before the array is used and this number of elements can't change). Arrays can have multiple dimensions ranging from 1- and 2-dimensional all the way to n-dimensional.


In other words, arrays allow us to store multiple related values, of the same data type, under one identifier (the array name) and this can be for a range of dimensions. The naming of arrays follows the same conventions as the naming of variables, so bear that in mind when you are creating your first arrays.


Arrays are easy to recognise by their square brackets ( [] ), and this is used to show both the overall array contents and when we are accessing elements of arrays. Here is an example of declaring an array in pseudocode, and assigning values to it.


myArray = [1,5,12,33,72,90,2]
x = myArray[2]             #assigns 12 to the variable x

As you can see, in pseudocode, it is really simple to demonstrate an array and how we access elements from it, but there is another thing that you can spot in the above example. Something that is SUPER important to remember. That is, that arrays start counting from 0. So, the first element in an array is 0, not 1. This is something that is really important to remember in programming and pseudocode, because it can cause a lot of errors down the line. Now, I will point out that there are some cases where this isn't the case (MATLAB, R, COBOL and more), but for the programming languages that you are expected to know at this level, arrays start at 0.


Now, as I mentioned above, arrays can have two or more dimensions, and arrays of two dimensions can be imagined like a spreadsheet, with rows and columns.


When you declare an array of two dimensions, think of it as an array containing lots of mini arrays. Then, when you want to access an element, you use the array's name, the row position and column position (like in a graph) as so: arrayname[row, column].


So, for an array of different types of tree comprising of three rows and two columns:

treeArray = [['oak','ash'],['beech','cedar'],['pine','elm']

#treeArray would look like:
#      |  0    |   1   |
#    -------------------
#    |0|  oak  |  ash  |
#    -------------------
#    |1| beech | cedar |
#    -------------------
#    |2| pine  |  elm  |

x = treeArray[0,1]            #assigns ash to the variable x

Arrays can have more than two dimensions, and this can be up to any number. For an n dimensional array, it works the same way as 1- and 2-dimensional arrays (being an ordered, finite set of elements of the same data type), but it can contain up to n dimensions.


For example, for a 3-dimensional array, an element would be reffered to as exampleArray[74,9,2] with the first element being exampleArray[0,0,0].


16 views0 comments

Recent Posts

See All

Records

A brief introduction to the record data structure.

Σχόλια


bottom of page