CISC 110 Day 5 Arrays and Random Numbers
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 2
Data Structures are ways to organize groups of data elements. We have worked with individual variables and with properties of objects. 3
Data Structures Examples of Single Variables age name height cute 4 “Daniel” 38. 5 true 4
Data Structures A String is actually an object “Daniel” characters 6 length name 5
Common Ways to Structure Data 1. List (in Action. Script: one-dimensional arrays) Example Lists: * Shopping List * Marks List * List of Student Names Physical Structure: 6
Common Ways to Structure Data 2. Table (in Action. Script: 2 -dimensional arrays) Example Tables: * Periodic Table of Elements * Representation of a Chess Board Physical Structure: 7
Common Ways to Structure Data 3. Record (in Action. Script: objects) Example Records: * Student Record (will have name, address, phone, …) * Movie. Clip Record (will have length, width, rotation, alpha, …) No Particular Physical Structure 8
Common Ways to Structure Data 4. Tree ( in A. S. : objects + references to objects) Example Trees : * Company Management Hierarchy * Family Tree Physical Structure: 9
Lists: One-dimensional Arrays 0 1 names 2 3 A list is a sequence of items, in which each item has a position (i. e. , an index) 4 5 6 7 “Daniel” “Aislinn” “Zhixing” “Anna” “Ya-Hui” “Praveena” “Sumeira” “Meghan” 10
1 D Array Object Declaration Name of array Type Constructor method var salaries: Array = new Array( ); salaries [ 0 ] salaries [ 1 ] salaries [ 2 ] salaries Note: The array is variable-sized. It starts with 0 elements. salaries [ 3 ] salaries [ 4 ] … 11
Array Assignment Statements salaries [ 0 ] salaries 7 salaries [ 1 ] salaries [ 2 ] salaries[0] = 7; Name of array Index Value of element salaries [ 3 ] salaries [ 4 ] … 12
Example Array Statements base var base: int = 30000; salaries[ 2 ] = 45000; salaries [ 0 ] salaries [ 1 ] salaries[ 5 ] = base; salaries [ 2 ] salaries[ 6 ] = base + 6000; salaries [ 3 ] 49000 salaries [ 4 ] salaries[ 2 ] = salaries[ 2 ] + 4000; salaries [ 5 ] trace( salaries [ 2 ] ); salaries [ 6 ] Output: 49000 30000 36000 … 13
Array Constructor Shortcuts var num. List : Array = new Array( 33, 2, 91, 12, 8 ); OR var num. List : Array = [ 33, 2, 91, 12, 8 ]; num. List[0] 33 num. List[1] 2 num. List[2] 91 num. List[3] 12 num. List[4] 8 14
Tracing an Array num. List[0] 33 num. List[1] 2 num. List[2] 91 num. List[3] 12 num. List[4] 8 trace(num. List. to. String( )); Output: 33, 2, 91, 12, 8 15
Array Objects have a Length Property num. List[0] 33 num. List[1] 2 num. List[2] 91 num. List[3] 12 num. List[4] 8 5 num. List. length trace(num. List. length); Output: 5 16
Adding Elements to an Array num. List[0] 33 num. List[0] 20 num. List[1] 2 num. List[1] 33 num. List[2] 91 num. List[2] 2 num. List[3] 12 num. List[3] 91 num. List[4] 8 num. List[4] 12 num. List[5] 8 num. List[6] 11 num. List. push(11); num. List. unshift(20); 17
Adding Elements to an Array num. List[0] 20 num. List[1] 33 num. List[2] 2 num. List[3] 91 num. List[3] 44 num. List[4] 12 num. List[4] 91 num. List[5] 8 num. List[5] 12 num. List[6] 11 num. List[6] 8 num. List[7] 11 num. List. splice(3, 0, 44); 18
Removing Elements from an Array var n 1: int = num. List. pop( ); var n 2: int = num. List. shift( ); // Now n 1 = 11 // and n 2 = 20 // and these elements are // removed from num. List[0] 33 num. List[1] 2 num. List[2] 44 num. List[3] 91 num. List[4] 12 num. List[5] 8 19
Returning Elements from an Array var list: Array = num. List. slice(3, 5); // Now list = 91, 12, 8 // but num. List is not changed num. List[0] 33 num. List[1] 2 num. List[2] 44 num. List[3] 91 num. List[4] 12 num. List[5] 8 20
Random Numbers The random( ) method in the Math class returns a random value between 0 and. 99999999 Example: var rand: Number = Math. random( ); trace( "rand = " + rand ); 21
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
- Slides: 22