Text of Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012
Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012
Arrays The purpose of this tutorial is to demonstrate how to use arrays in Alice worlds. An array is an ordered collection of objects stored by an index. Alice has two types of arrays: visual and nonvisual. Visual arrays put the items on the array index number to show their location in the array. This is useful for objects that need to stand in a line. In nonvisual arrays, however, the items can be put anywhere in the Alice world and still function as an array or they can be used for an array of numbers, strings, objects, etc.
Arrays vs. Lists Arrays are similar to lists, but elements in arrays are ordered and elements in a list are unordered. When traversing an array, one uses a loop (complicated version) to step through indexing particular items. Not all elements need to be processed. One could access every other element. When traversing a list, the order does not matter and the user wants to handle every element. Use For all in order or For all together to process the elements in a list.
Standards CSTA Standard 5.3.B- Computer Science Concepts and Practices (CT): Students will be able to 6. Compare and contrast simple data structures and their uses (e.g., arrays and lists).
Getting Started You want to add in different people to put into this world. You can find them under the People tag or use the students under the High School tag. I used 8 people in my world to form the array as well as the coach to describe whats going on (9 people total). Now were ready to build the array!
Visual Arrays To use a visual array, go to the Visualizations folder in the Alice Local Gallery and add an ArrayVisualization to your world.
Visual Arrays After adding the ArrayVisualization, a menu will pop up for you to add elements to the array. Select new item to add objects into the array and choose the items that you would like to add at the specified index. Make sure to add the entire ___. You should add 8 people and have one person left over.
Visual Arrays And here is your visual array! Click and drag the ArrayVisualization object to turn it and move it around to get the entire array to fit the screen and facing the camera. Note that the people in the array will move with it. Dont click on the people and move them or this will cause problems accessing the array later on. Click Undo if you accidentally move a person.
Using the Array Now we will go over several uses for the visual array including modifying each element of the array, every other element of the array, choosing random elements in the array, accessing specified indices of the array, and swapping elements in the array (Note that for arrays with n objects, the first element is at index 0 and the last element has the position n-1)!
Using the Array Create a new world method by clicking on world in the object tree on the left and then under worlds details click on the methods tab and then create new method. Name this method eachElement.
Loops Loops are very important for iterating through the objects of an array. In this method, drag in a Loop from the bottom of the screen into the Do Nothing. Have the loop go from 0 to the number of elements in the array, in this case we want to choose 8. When the menu comes up, go down to other and enter 8 into the calculator.
Each Element To get each element to do something in order, click on any element of the array in the object tree and go to the methods tab. The will be your default person that you will use for all of the characters methods that you want the elements of the array to perform. Have your object move up meter then move back down.
Loops If you click on show complicated version, you will see more information about how loops work. The index variable is used to traverse the array and its value will increase from 0 to 7 each time you go through the loop.
Each Element Next, click on arrayVisualization in the object tree and go to the properties panel. Under properties, drag the elements property over your default person that you used before to get the instructions and select ith item from array expressions index to access the object at position index in the array.
Each Element And that concludes how to modify each element in an array in Alice! Make sure to test this method by changing the Events panel at top right corner select When the world starts, do world.eachElement to run this when the world starts.
Each Element (Reverse) Now, we want to go through the elements of the array backwards. Unfortunately, the complicated loop will not let you increment by negative numbers, so we will have to use the formula 7 index to process the correct position in the array. Note that as index increases, 7 index will decrease. index7 index (current position of the array) 07 16 25 34 43 52 61 70
Each Element (Reverse) To go through the array in reverse, you only need to change one thing. When you select the ith item from array, select the highest index of the array, in this case, 7. Then, click on the down-arrow next to the number and select math 7 expressions index.
Each Element (Reverse) If you create a method called reverse to try this out, the two lines inside of the loop should look like this (Have the object move up and down again): Now, add a line at the beginning of the method to have the coach say, Now in reverse, under the coachs methods before the loop starts and you are done with the reverse method. Test this method out by changing When the world starts, do world.reverse in the Events editor.
Every Other Element Next, we will discover how to change every other element in an array. Start by creating a new world method called everyOtherElement, and adding a loop into the method from 0 to 8. Make sure to hit the show complicated version button of the loop, and your loop should be a longer structure like this:
Every Other Element There are a couple of ways to modify the elements of an array at a particular interval. The simplest way is to change the incrementing by portion of the loop to 2 or whatever interval you want. Add the methods that you would like done in the loop and only every other element will act out the method. We chose to have them turn left and then right 1 revolution each direction. Remember, when asked for the ith item from array after dragging the elements property into the method, go to expressions index.
Every Other Element On your own, try creating a method that has every third element in the array do the action of your choice. Name this method everyThirdElement. Dont forget to test your new method to see if it works by changing the event When the world starts, do
Every Other Element Different Another way to modify every other element in an array is to check if the index is even or odd, or is divisible by some number. This is better if you want every object to do an action, then every other object to do a different action. Create a new world method called everyOtherElementDiff.
Every Other Element Different To do this, you first need to drag an If/Else block inside of your loop from the bottom of the window and select true for now.
Every Other Element Different Next, go to the worlds functions tab and select a==b. Let a be any arbitrary number (I chose 0.25 but this value will be replaced) and let b = 0. Remember that you may need to select other and type in 0.
Every Other Element Different Scroll down in the functions tab, select IEEERemainder of a/b and drag it over the arbitrary value that you used for a before. When asked for the value of a, go to expressions index OR just drag the index variable from the loop, and let b = 2. This will check to see if the remainder of the index divided by 2 returns a remainder of zero or not, determining if the index is odd or even.
Every Other Element Different Now, have the objects at the even indices turn backward then forward revolution, and the objects at odd indices just turn left one revolution. (Remember to use the default person, then replace them with the elements property of the array). And that concludes modifying every other element in an array! Have your extra person say Every other element different. and test this method by changing the event when the world starts.
Random Elements Now, well go over how to choose random elements from an array. First, create a new world method called randomElements and add a loop from 0 to the number of random positions that you want in this method (I chose 5). You will also want to create a new number variable and call it random that will be used to calculate the random indices to be modified. You should see your variable show up to the left.
Random Elements Now drag the random variable into the loop and when prompted, set the value of rando