A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for...

10
Alice Arrays: Intro A primitive data type is one built into the language They hold a single value, like integers, Booleans, and doubles An array is a data structure A variable represents multiple values Components of an array are called elements Elements are numbered starting with zero Individual elements can be accessed by an integer index Size indicates how many elements are in the array In an array, all elements must be the same data type The value stored in an array variable (memory) is a pointer (reference) to values it represents Arrays are useful in situations where you need to store many values of the same type that are related in some way E.g., grades, days in months, team scores Easier than using a variable for each individual value Easier processing because can use a loop to process each element of an array 1

Transcript of A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for...

Page 1: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Intro

• A primitive data type is one built into the language

– They hold a single value, like integers, Booleans, and doubles

• An array is a data structure

– A variable represents multiple values

– Components of an array are called elements

– Elements are numbered starting with zero

– Individual elements can be accessed by an integer index

– Size indicates how many elements are in the array

– In an array, all elements must be the same data type

– The value stored in an array variable (memory) is a pointer (reference) tovalues it represents

• Arrays are useful in situations where you need to store many values of the sametype that are related in some way

– E.g., grades, days in months, team scores

– Easier than using a variable for each individual value

– Easier processing because can use a loop to process each element of an array

1

Page 2: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Creating and Using Arrays

– To create an array, use the same steps used to create any other kind ofvariable, but check the array box

– Give it a name and type as usual

– To add elements to the array, select Custom Array

∗ You then choose values to add to the array

∗ The first value goes in slot (position) zero, the second in slot 2, etc.

∗ The size/length of the array is determined by how many elements youadd

2

Page 3: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Creating and Using Arrays (2)

– The actual code for the resulting declaration is

∗ The square brackets indicate an array

∗ The braces indicate the initial values to be stored in the array

3

Page 4: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Creating and Using Arrays (3)

– Creation works the same for other data types:

4

Page 5: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Programming with Arrays - Alice 3’s Special Control Structures

• A common activity with arrays is processing each element in the array, oneafter the other

– This is done with a loop

– Alice3 provides a special loop for doing this: for each in array

– To use this control structure, you need to

1. Specify the data type of the array elements

2. Name the LCV

3. Provide the array to be iterated thru

– The syntax for this statement is

for (Type LCV: array_name) {

statements

}

– The semantics: Execute statements for element 0, then for element 1, etc.

– NOTE: Use the LCV variable name in statements to have the code affectthe array elements

5

Page 6: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Programming with Arrays - Alice 3’s Special Control Structures(2)

• On the other hand, you might want to process all elements in the array at thesame time

– Alice3 provides a special control structure for doing this: for each in arraytogether

– To use this control structure, you specify the same things as for for each inarray

– The syntax for this statement is

ThreadUtilities.eachInTogether ()(Type = LCV) -> {

statements

}, array_name)

– The semantics: Execute statements for elements 0 thru n all at once, likedo together

6

Page 7: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Programming with Arrays - ”Regular” Processing

• Array operators/properties

– The subscript operator: [ ]

∗ Syntax: array name[integer]

∗ Semantics: Refers to the element in array name at slot integer

· Note: Each element in an array is a variable in its own right

∗ The text states there are two versions: read and write

· Which is meant depends on which side of an assignment the referenceis on

· v = a[2] is a read version: read the value in slot 2 of array a

· a[2] = 5 is a write version: write the value 5 into slot 2 of array a

– The length property

∗ Syntax: array name.length

∗ Semantics: Refers to the number of elements in array name

∗ This is useful for creating your own loop structures for processing arrays

• A for loop that does the same thing as for each in array

7

Page 8: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Arrays: Programming with Arrays - ”Regular” Processing (2)

• Parallel arrays

– Sometimes you have different kinds of data that need to be processed intandem

– For example, names (Strings) and grades (doubles)

– These can’t be stored in the same array because an array only holds a singledata type

– In this case, use two arrays: a String array for the names, a double arrayfor the grades

8

Page 9: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Random Number Generation

• Sometimes you need to generate random values in a program

– For example, you want to simulate rolling dice for a game, for studyingprobability, etc.

– For a six-sided die, you’d want to generate a number between one and sixin an unpredictable (usually) pattern

• A random number generator (more properly a pseudo random number genera-tor) is a function that does just this

• To generate random values in Alice:

1. Create a variable in the usual manner

– Select a data type (integer, double, or boolean)

– Give it a name

– Assign an initial value

2. Once the variable has been created, it can be assigned a random value

– Click on the arrow next to the assigned value

– One of the choices will be Random

– Clicking on Random will give one or more options for generating therandom value

∗ Integer

(a) Random number x where 0 ≤ x < N

(b) Random numbers x where A ≤ x < B

(c) Random numbers x where A ≤ x ≤ B

9

Page 10: A They hold a single value, like integers, Booleans, and ...{ Alice3 provides a special loop for doing this: for each in array { To use this control structure, you need to 1. Specify

Alice Random Number Generation (2)

∗ Double

(a) Random number x where 0.0 ≤ x < 1.0

(b) Random numbers x where A ≤ x ≤ B

∗ Boolean

· This provides no options - it generates either true or false

10