Laid out (arrayed) out before you...
We're at the last building block - data structures!
But, there is no way to cover every kind of data structure because it is possible for anyone (even you) to make a new one at any time. So instead, we will start with the Array.
Just about every programming language includes a built in type that corresponds to an array and the defining characteristic of an array is that each element stored in it can be referenced by an index number that counts up from index [0] and goes up through index [n-1] where 'n' is the number of elements in the array. Usually, there is an easy way to know how many elements there are in an array with some sensible name like 'size', 'length', or 'len'.
That is a bunch of words, so let's see a simple example of an array called 'example' with ten elements in it. Here is the list of all of the elements along with their indexes (indices?):
But, there is no way to cover every kind of data structure because it is possible for anyone (even you) to make a new one at any time. So instead, we will start with the Array.
Just about every programming language includes a built in type that corresponds to an array and the defining characteristic of an array is that each element stored in it can be referenced by an index number that counts up from index [0] and goes up through index [n-1] where 'n' is the number of elements in the array. Usually, there is an easy way to know how many elements there are in an array with some sensible name like 'size', 'length', or 'len'.
That is a bunch of words, so let's see a simple example of an array called 'example' with ten elements in it. Here is the list of all of the elements along with their indexes (indices?):
- example[0]
- example[1]
- example[2]
- example[3]
- example[4]
- example[5]
- example[6]
- example[7]
- example[8]
- example[9]
Depending on the particular programming language, you may be able to store mixed types of data in different array elements (for example a string in element 0 and an integer in element 1). This is not necessarily a good idea because a program spinning through each of the elements could end up trying to do something to an element that is not applicable to the (strange) type of data that it finds. You may find times when (if the programming language allows it) you want to mix data in an array. In that case --you-- have to make sure that your program will be able to handle the mixed data types.
Why might you want to use an array? Well, any time you have a list of things that you want to keep track of! For example: a list of temperature measurements, a list of guest names for your wedding, or chapters in a book.
Arrays are especially handy data structures to work with for-loops. What if we wanted to find the average temperature for our array of temperatures:
- Create an array called temp - we will assume that it is somehow filled with temperature values of the high temperatures for two weeks
- Create a variable to store the total of temperatures. Call it total_temp and set it to zero
- Create a variable called index to specify which element in the array you want to access
- For index equal zero (the first index) to size - 1
- Add the temperature in temp[index] to total_temp
- Divide the value of total_temp by the size of the temp array
The nice thing about arrays is that something like the above pseudo-code would work regardless of how big the array is. We 'said' that there were high temperatures for two weeks...What if there were daily temperatures for two years?
If we did not have an array to group all of this similar data into - we would have to have separate individual variables to store each one. And, to figure out the average we would have to know in advance exactly how many there were and what the variable names for each of them was!
Remember, I am not trying to give you a deep understanding of how to program in any particular language. What we are doing is going through to see how to think about how programs work.
Once you have some familiarity with programming concepts, you can apply them to any language you come in contact with.
If you have gone through all of the posts up to now, then you should now have some familiarity with the four building blocks of programming:
- Data
- Control Structures
- Loops
- Data Structures
Next time, we'll start looking at some more of the standard data structures that you will likely come across. We'll start with one called a Stack which is similar to a stack of papers. You can add to a stack and the first piece of paper that you pull off will be the last one you added.
Comments
Post a Comment