Some of you may have wished at one point that there was a way to conveniently keep track of a series of inputs that you are providing to your system. Luckily, an array is an excellent way to solve this predicament, and it has the added bonus of being easy to use!
In essence, an array is a set or group of values of a particular data type, such as an integer. However, rather than having to declare each and every integer by name, we only have to declare the array itself; the compiler will assign a location number to the elements of the array based on where they are located within the array. To initialize an array (i.e., declaring an array with starting values), we use the following format:
For example, if we wanted to declare an array of integers that is five values long with 2, 4, and 6 as the first three integers, we would type:
Any part of the array that is not assigned values when the array is declared, such as our last two values in the set of five, are assigned a value of zero by the compiler. As I mentioned earlier, each value within the array is assigned a location number with the first value in the array receiving a “0”, the second value receiving a “1”, and so on until all values within the array have been assigned a sequential location number. We can access individual values within the array to utilize or change them by using their location number. So, if we wanted to increase the third integer of myFirstArray by 10 and then delay the system for the new value of the third integer we would type:
While the single dimension arrays that we have discussed are incredibly useful, it would be a shame if we did not also talk about multi-dimensional arrays.
At their core, multi-dimensional arrays are groups of single dimension arrays, much like single dimension array is a group of values. With this in mind, you can think of a two-dimensional array as multiple single dimensional arrays (of the same length) aligned so that you have a 2D plane of values. A three-dimensional array will (not surprisingly) add a third dimension to create a cube of values (or, in a sense, a plane of arrays).
As we continue to add extra dimensions, we start getting into some interesting perspectives. A 4D array could be thought of as a line of cubes and 6D array could be thought of as a cube of cubes. And so on.
You will rarely ever need an array that is larger than three dimensions, since you start to rapidly eat up the memory you have available to you as dimensions are added. Luckily, this can be remedied by making switches in your choice of microcontroller–like from the chipKIT Uno32 to the chipKIT uC32, which has notably more memory.
Naturally, as we start adding more dimensions to create our groups of arrays, the way that we initialize the arrays will change slightly. Keeping in line with our “group of arrays” theme, we can initialize a 2D array with three single-dimension arrays of four elements each by typing:
Similarly, we can also initialize a three-dimensional array by adding the “number of layers” as the first bracketed element in the declaration of the array, with the “number of rows” of single dimension arrays as the second bracketed element and the last bracketed element as number of values in each single dimension array. A visually condensed version of a 3D array could be be initialized by:
But what can we do with arrays? One of the more entertaining things (in my opinion) that you can do with an array is to use it as a recording device. A two-dimensional array could be set up to hold two single-dimension arrays: one array to record if a button was being pressed or not and a second array to record the length of time that the button was held in each state, with an incrementer to be able to store the values in the appropriate number location within the arrays. Another function could also be created to access and “play” each of those stored values within the two dimensional array. You can check out the YouTube video of a recording device that does just that.
Check back every day to see what other cool things Digilent is up to!