1 / 18

Arrays and Array Visualization

Arrays and Array Visualization. Alice. What is an array?. An array is a collection of objects or information that is organized in a specific order. The individual components ( elements ) of an array are of the same type (object, number, color, …). Analogy.

williamsonh
Télécharger la présentation

Arrays and Array Visualization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and Array Visualization Alice

  2. What is an array? • An array is a collection of objects or information that is organized in a specific order. • The individual components (elements) of an array are of the same type (object, number, color, …).

  3. Analogy • An analogy of an array is a music CD. • A CD contains a collection of songs, listed in the order recorded. • A CD player allows you to • play any specific song by selecting its number, or • play all the songs in sequence.

  4. Arrays in Alice • In Alice, an array is a data structure used to organize objects or information of the same type (object, number, color, etc.) • Just as you cannot actually "see" the digital bits of the songs recorded on a CD, you cannot actually "see" the digital information in an array. In other words, an array is not visible.

  5. Array Visualization • We will use a built-in 3D model that has been specifically designed to help you "see" how an array works. • The elements of this special array visualization are visual Alice objects

  6. Initial World • As an example, we will create a visualization of an array of bugs. • First, we add five bugs to a new world. • This is not yet an array – just five bug objects!

  7. Initialize array • Now add an arrayVisualization object to the world. • A dialog box pops up to allow you to add each bug to the array.

  8. Completed array • Each bug added to the array is automatically moved to its location in the array. • The locations are numbered 0, 1, 2, 3, 4 (see the digits in the blue squares). • The location number is the index.

  9. elements • An arrayVisualization object has a built-in class-level variable, named elements, that keeps track of which objects are in the array and where each is located. • The order of the objects in the elements variable matches the order of the objects in the array.

  10. Demo: Access • Ch10Lec2Array-V1 • Concepts illustrated in this example • An index is used to order the elements in an array. • To access (get at) an individual element in the array, create an instruction that uses the index to select the element. • An element can be randomly selected from the array. • The maximum value should be the number of elements in the array. • Be sure to select the integer only option for the random number function.

  11. Permutation • Changing the order of elements in an ordered group is a math operation known as creating a permutation. • To demonstrate how to perform a permutation, let's write a swap method that exchanges the positions of two elements in an array.

  12. Storyboard • Two number parameters are needed, indexOne and indexTwo, to specify the indexes of the two objects to be swapped. • A temporary location will be used to hold one object while the other object changes location. swap Parameters: indexOne, indexTwo (of type number) Do in order relocate element at indexOne to a temporary location relocate element at indexOne to indexTwo relocate element from temporary location to indexOne

  13. Temporary location • An objectVisualization object is added to the world to be used as a temporary holding pen.

  14. Demo: swap • Ch10Lec2Array-V2 • Concepts illustrated in this example • In Alice, a let instruction is used to relocate an element in an array. • A temporary holding space, as can be seen in this visualization, is not part of the array.

  15. Walking through an array • In addition to the ability to access an individual element of an array, it is also possible to "walk through" an array, accessing each element of the array in order.

  16. Demo: Index variable • Ch10Lec2Array-V3 • Concepts illustrated in this example • To walk through an array, use the complicated form of the Loop instruction. • An index variable is a counter for the Loop. • When a Loop is used with an array, the Loop's index variable can be used to access successive elements in the array.

  17. Assignment • Read Chapter 10 Section 2, Arrays

  18. Lab • Chapter 10 Lecture 2 Lab

More Related