22
CISC 110 Day 5 Arrays and Random Numbers

CISC 110 Day 5

Embed Size (px)

DESCRIPTION

CISC 110 Day 5. Arrays and Random Numbers. Outline. Data Structures Lists: One-dimensional arrays Array Declarations and Statements Initializing Arrays Size of Arrays length attribute Adding and Removing Elements Random Numbers. Data Structures. - PowerPoint PPT Presentation

Citation preview

Page 1: CISC 110 Day 5

CISC 110Day 5

Arrays and Random Numbers

Page 2: CISC 110 Day 5

2

Outline

• Data Structures• Lists: One-dimensional arrays• Array Declarations and Statements• Initializing Arrays• Size of Arrays

– length attribute• Adding and Removing Elements• Random Numbers

Page 3: CISC 110 Day 5

3

Data Structures

Data Structures are ways to organize groups ofdata elements.

We have worked with individual variables and withproperties of objects.

Page 4: CISC 110 Day 5

4

Data Structures

Examples of Single Variables

4 “Daniel” true38.5

age name height cute

Page 5: CISC 110 Day 5

5

Data Structures

A String is actually an object

“Daniel”

6 characters

lengthname

Page 6: CISC 110 Day 5

6

Common Ways to Structure Data

1. List (in ActionScript: one-dimensional arrays)

Example Lists: * Shopping List * Marks List * List of Student Names

Physical Structure:

Page 7: CISC 110 Day 5

7

Common Ways to Structure Data

2. Table (in ActionScript: 2-dimensional arrays)

Example Tables: * Periodic Table of Elements* Representation of a Chess Board

Physical Structure:

Page 8: CISC 110 Day 5

8

Common Ways to Structure Data

3. Record (in ActionScript: objects)

Example Records:

* Student Record

(will have name, address, phone, …)

* MovieClip Record

(will have length, width, rotation, alpha, …)

No Particular Physical Structure

Page 9: CISC 110 Day 5

9

Common Ways to Structure Data

4. Tree ( in A.S.: objects + references to objects)

Example Trees :

* Company Management Hierarchy

* Family Tree

Physical Structure:

Page 10: CISC 110 Day 5

10

Lists: One-dimensional Arrays

A list is a sequence of items, in which each item has a position (i.e., an index)

“Daniel”

“Aislinn”

“Zhixing”

“Anna”

“Ya-Hui”

“Praveena”

“Sumeira”

“Meghan”

0

1

2

3

4

5

6

7

names

Page 11: CISC 110 Day 5

11

1D Array Object Declaration

var salaries: Array = new Array( );

salaries [ 0 ]

salaries [ 1 ]

salaries [ 2 ]

salaries [ 3 ]

salaries [ 4 ]

salaries

TypeName of array Constructor method

Note: The array is variable-sized. It starts with 0 elements.

Page 12: CISC 110 Day 5

12

Array Assignment Statements

salaries[0] = 7;

7

salaries [ 0 ]

salaries [ 1 ]

salaries [ 2 ]

salaries [ 3 ]

salaries [ 4 ]

salaries

Name of array

Index Value of element

Page 13: CISC 110 Day 5

13

Example Array Statements

var base: int = 30000;

salaries[ 2 ] = 45000;

salaries[ 5 ] = base;

salaries[ 6 ] = base + 6000;

salaries[ 2 ] = salaries[ 2 ] + 4000;

trace( salaries [ 2 ] );

Output: 49000

49000

30000

36000

salaries [ 0 ]

salaries [ 1 ]

salaries [ 2 ]

salaries [ 3 ]

salaries [ 4 ]

salaries [ 5 ]

salaries [ 6 ]

30000base

Page 14: CISC 110 Day 5

14

Array Constructor Shortcuts

var numList : Array = new Array( 33, 2, 91, 12, 8 );

33

2

91

12

8

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

var numList : Array = [ 33, 2, 91, 12, 8 ];

OR

Page 15: CISC 110 Day 5

15

Tracing an Array

trace(numList.toString( ));

33

2

91

12

8

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

Output: 33, 2, 91, 12, 8

Page 16: CISC 110 Day 5

16

Array Objects have a Length Property

trace(numList.length);

33

2

91

12

8

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

numList5

numList.length

Output: 5

Page 17: CISC 110 Day 5

17

Adding Elements to an Array

numList.push(11);

numList.unshift(20);

20

33

2

91

12

8

11

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

numList[5]

numList[6]

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

33

2

91

12

8

Page 18: CISC 110 Day 5

18

Adding Elements to an Array

numList.splice(3, 0, 44);

20

33

2

44

91

12

8

11

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

numList[5]

numList[6]

numList[7]

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

numList[5]

numList[6]

20

33

2

91

12

8

11

Page 19: CISC 110 Day 5

19

Removing Elements from an Array

var n1: int = numList.pop( );var n2: int = numList.shift( );

// Now n1 = 11// and n2 = 20// and these elements are // removed from numList

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

numList[5]

33

2

44

91

12

8

Page 20: CISC 110 Day 5

20

Returning Elements from an Array

var list: Array = numList.slice(3,5);

// Now list = 91, 12, 8// but numList is not changed

numList[0]

numList[1]

numList[2]

numList[3]

numList[4]

numList[5]

33

2

44

91

12

8

Page 21: CISC 110 Day 5

Random Numbers

The random( ) method in the Math class returns a

random value between 0 and .9999999999999999

Example:

var rand: Number = Math.random( );

trace( "rand = " + rand );

21

Page 22: CISC 110 Day 5

Random Numbers

To obtain a random integer between 0 and 20,

multiply the result by 20 and then round the

result, using the round method, also in the Math

class.

Example:

var rand: Number = 20 * Math.random( );

rand = Math.round( rand );

trace( "rand = " + rand );22