PDE - Access & Storage (Lesson)
Array/List Storage and Access
Arrays, lists, and iterations are great topics that you will need to demonstrate in some fashion for the College Board with your code. Let's try some pseudocode here to work with these concepts. In the picture below, the word "Dog" a string is being put in box 0, or box [0] whereas the "Cat" is being put in box 2, box[2]. This storage method allows multiple related items to be stored under the same name and accessed by the location number within the array/list. When we look through the array later, the array variable with the number has the value we put in the box until it is changed.
So what is the array called? Use a name that explains what is inside the array, for example maybe these will all be animals that are pets, so we could call it the petArray[] or pets[]. Note the brackets, not parenthesis that are after the array. This denotes a one dimensional array, so the word Array that was used after petArray[] is only needed if you wish to add it to the name.
As in the above picture, think of putting the values in the various boxes assigned to them, then reverse the order with the assignment below. Use the coding constructs for your pseudocode that corresponds to what you are doing.
For example, using the above boxes
Label your procedure/method. Mine is called pets[].
box[0] = "Dog" would put the word "Dog" in box 1, the first position in the list.
box[1] = "Cat" would put the word "Cat" in box 2, the second position in the list.
Yes, in Alice and most languages, the array starts at position 1, but the computer starts the boxes at box[0] and goes up. So box[1] is the second position in the array, but it is referenced by the number [1] by the computer, not [2].
Notes to remember:
- Positions are 1 relative to humans, thus we start counting at 1. The length of the array is based on actual counting numbers, so the length of an array is the physical number of items counted starting at 1.
- Indices are zero relative to a computer, thus starting at index 0 for access. The last index available to access the array using 0 relative numbers is the length of the array - 1, the length being the actual counting numbers.
Write your pseudocode with shortened code as much as possible, with only pertinent items, not perfect code.
For efficiency, we might pass parameters to the method addWords which inserts the words we want where we want them to go. This also consolidates the number of lines of code to be written as well as assists with less complexity o that the lines of code consolidate.
addWords(word, boxNbr) method called to add to pets[]. // pets array
pets[boxNbr] = word // note the brackets to put the word in the array.
Note that rather than using multiple lines for "Dog" and "Cat", the method handles any word at all giving flexibility to the code to be able to add any words. The parameters, word and boxNbr, hold the information and pass it into the method for use.
In Alice a method is a procedure or a function. We have looked at procedures. In this module we will look at the functions as well.
IMAGE CREATED BY GAVS AND USED ACCORDING TO TERMS OF USE.