 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.

Slides:



Advertisements
Similar presentations
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Advertisements

1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
C++ for Engineers and Scientists Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 6: More JavaScript CIS 275—Web Application Development for Business I.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
INE1020: Introduction to Internet Engineering 3: Web Page Authoring1 Lecture 6: Introduction to Javascript II r Javascript Objects Array String Date Math.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 11: Arrays CIS 275—Web Application Development for Business I.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
JavaScript: Functions.
Chapter 11 - JavaScript: Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
Object Oriented Programming in java
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
JavaScript: Arrays.
10 JavaScript: Arrays.
Presentation transcript:

 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays 11.4 Examples Using Arrays 11.5Random Image Generator Using Arrays 11.6 References and Reference Parameters 11.7 Passing Arrays to Functions 11.8 Sorting Arrays 11.9 Searching Arrays: Linear Search and Binary Search Multidimensional Arrays 11.11Building an Online Quiz Web Resources

 2004 Prentice Hall, Inc. All rights reserved. 2 Objectives In this tutorial, you will learn: –To introduce the array data structure. –To understand the use of arrays to store, sort and search lists and tables of values. –To understand how to declare an array, initialize an array and refer to individual elements of an array. –To be able to pass arrays to functions. –To be able to search and sort an array. –To be able to declare and manipulate multi-dimensional arrays.

 2004 Prentice Hall, Inc. All rights reserved Introduction Arrays –Data structures of related items Also called Collections –Dynamic

 2004 Prentice Hall, Inc. All rights reserved Arrays Arrays in JavaScript –Each element referenced by a number Start at “zeroth element” Subscript or index –Accessing a specific element Name of array Brackets Number of element –Arrays know their length length property

 2004 Prentice Hall, Inc. All rights reserved Arrays c[ 6 ] Name of array c[ 0 ] c[ 1 ] c[ 2 ] c[ 3 ] c[ 11 ] c[ 10 ] c[ 9 ] c[ 8 ] c[ 7 ] c[ 5 ] c[ 4 ] Position number (index or subscript) of the element within array c Fig. 11.1A 12-element array.

 2004 Prentice Hall, Inc. All rights reserved Arrays

 2004 Prentice Hall, Inc. All rights reserved Declaring and Allocating Arrays Arrays in memory –Objects –Operator new Allocates memory for objects Dynamic memory allocation operator var c; c = new Array( 12 );

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Arrays grow dynamically –Allocate more space as items are added Must initialize array elements –Default value is undefined –for loops convenient –Referring to uninitialized elements or elements outside array bounds is an error

 2004 Prentice Hall, Inc. All rights reserved. Outline 9 InitArray.html (1 of 3) Array n1 has five elements.The for loop initializes the elements in n1 to their subscript numbers (0 to 4). Array n2 is an empty array.

 2004 Prentice Hall, Inc. All rights reserved. Outline 10 InitArray.html (1 of 3) The for loop adds five elements to Array n2 and initialize each element to its subscript number (0 to 4). Each function displays the contents of its respective Array in an XHTML table. The first time function ouputArray is called, variable header gets the value of “Array n1 contains” and variable theArray gets the value of n1. The second time function ouputArray is called, variable header gets the value of “Array n2 contains” and variable theArray gets the value of n2.

 2004 Prentice Hall, Inc. All rights reserved. Outline 11 InitArray.html (1 of 3)

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Fig Initializing the elements of an array.

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Possible to declare and initialize in one step –Specify list of values Initializer list var n = [ 10, 20, 30, 40, 50 ]; var n = new Array( 10, 20, 30, 40, 50 ); –Also possible to only initialize some values Leave uninitialized elements blank Uninitialized elements default to “undefined” var n = [ 10, 20,, 40, 50 ];

 2004 Prentice Hall, Inc. All rights reserved. Outline 14 InitArray2.html (1 of 2) Array integers1 is initialized using an initializer list.Two values are not supplied for integers2, which will be displayed as undefined.

 2004 Prentice Hall, Inc. All rights reserved. Outline 15 InitArray2.html (2 of 2)

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Fig Initializing the elements of an array.

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays for … in statement –Perform an action for each element in an array –Iterates over array elements Assigns each element to specified variable one at a time –Ignores non-existent elements

 2004 Prentice Hall, Inc. All rights reserved. Outline 18 SumArray.html (1 of 2) The for loop sums the values contained in the 10- element integer array called theArray.

 2004 Prentice Hall, Inc. All rights reserved. Outline 19 SumArray.html (2 of 2) Variable element is assigned a subscript in the range of 0 up to, but not including, theArray.length.

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Fig Calculating the sum of the elements of an array.

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Arrays can provide shorter and cleaner substitute for switch statements –Each element represents one case

 2004 Prentice Hall, Inc. All rights reserved. Outline 22 RollDie.html (1 of 2) Referencing Array frequency replaces the switch statement used in Chapter 10’s example.

 2004 Prentice Hall, Inc. All rights reserved. Outline 23 RollDie.html (2 of 2)

 2004 Prentice Hall, Inc. All rights reserved Examples Using Arrays Fig Dice-rolling program using arrays instead of a switch.

 2004 Prentice Hall, Inc. All rights reserved Random Image Generator Using Arrays Cleaner approach than previous version –Specify any file name rather than integers 1-7 –Result of Math.random call is index into array of image file names

 2004 Prentice Hall, Inc. All rights reserved. Outline 26 RandomPicture2.html (1 of 2)

 2004 Prentice Hall, Inc. All rights reserved. Outline 27 RandomPicture2.html (2 of 2)

 2004 Prentice Hall, Inc. All rights reserved Random Image Generator Using Arrays Fig Random image generation using arrays.

 2004 Prentice Hall, Inc. All rights reserved References and Reference Parameters Two ways to pass parameters –Pass-by-value Pass copy of original value Default for numbers and booleans Original variable is unchanged –Pass-by-reference How objects are passed, like arrays Pass location in memory of value Allows direct access to original value Improves performance

 2004 Prentice Hall, Inc. All rights reserved Passing Arrays to Functions Name of array is argument –Not necessary to also pass size of array Arrays know their size –Passed by reference Individual elements are passed by value if numbers or booleans Array.join –Creates string containing all array elements –Specify separator

 2004 Prentice Hall, Inc. All rights reserved. Outline 31 PassArray.html (1 of 3) The first call to function outputArray displays the contents of the Array a before it is modified. Function modifyArray multiplies each element by 2.

 2004 Prentice Hall, Inc. All rights reserved. Outline 32 PassArray.html (2 of 3) Again, function outputArray is called to show that the contents of Array a have been modified. Method join takes as its argument a string containing a separator that should be used to separate the elements of the array in the string that is returned. Function modifyElement multiplies the contents of a[ 3 ] by 2. The value of a[ 3 ] is output to show its contents before it is modified.

 2004 Prentice Hall, Inc. All rights reserved. Outline 33 PassArray.html (3 of 3) Multiply each element in theArray by 2.

 2004 Prentice Hall, Inc. All rights reserved Passing Arrays to Functions Fig Passing arrays and individual array elements to functions.

 2004 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting –Important computing task Array.sort –Defaults to string comparison –Optional comparator function Return negative if first argument less than second Return zero if arguments equal Return positive if first argument greater than second

 2004 Prentice Hall, Inc. All rights reserved. Outline 36 Sort.html (1 of 2) Method sort takes as its optional argument the name of a function that compares two arguments and returns a value of –1, 0 or 1.

 2004 Prentice Hall, Inc. All rights reserved. Outline 37 Sort.html (2 of 2) Function compareIntegers calculates the difference between the integer values of its arguments.

 2004 Prentice Hall, Inc. All rights reserved Sorting Arrays Fig Sorting an array with sort.

 2004 Prentice Hall, Inc. All rights reserved Searching Arrays: Linear Search and Binary Search Searching –Look for matching key value Linear search –Iterate through each element until match found –Inefficient Worst case scenario, must test entire array Binary search –Requires sorted data –Cuts search range in half each iteration –Efficient Only look at small fraction of elements

 2004 Prentice Hall, Inc. All rights reserved. Outline 40 LinearSearch.html (1 of 3) Array a is initiated with 100 elements. Array a is populated with the even integers 0 to 198.

 2004 Prentice Hall, Inc. All rights reserved. Outline 41 LinearSearch.html (2 of 3) Get value of search key from the input field in the XHTML form. Calling function linearSearch and passing it the Array a and the value of variable searchKey as an integer.

 2004 Prentice Hall, Inc. All rights reserved. Outline 42 LinearSearch.html (3 of 3) Variable theArray gets the value of Array a and variable key gets the value of variable searchKey. Function linearSearch compares each each element with a search key.

 2004 Prentice Hall, Inc. All rights reserved Searching Arrays: Linear Search and Binary Search Fig Linear search of an array.

 2004 Prentice Hall, Inc. All rights reserved. Outline 44 BinarySearch.html (1 of 5) Array a is initialized with 15 elements.

 2004 Prentice Hall, Inc. All rights reserved. Outline 45 BinarySearch.html (2 of 5) Function binarySearch receives two arguments: the Array a and the search key, searchKey.

 2004 Prentice Hall, Inc. All rights reserved. Outline 46 BinarySearch.html (3 of 5) If the key matches the middle element of a subarray, the subscript of the current element is returned. If key is less than the middle element, the high subscript is set to middle – 1. If key is greater then the middle elements, the high subscript is set to middle + 1.

 2004 Prentice Hall, Inc. All rights reserved. Outline 47 BinarySearch.html (4 of 5) Function buildOutput creates the markup that displays the results of the search.

 2004 Prentice Hall, Inc. All rights reserved. Outline 48 BinarySearch.html (5 of 5)

 2004 Prentice Hall, Inc. All rights reserved Searching Arrays: Linear Search and Binary Search Fig Binary search of an array.

 2004 Prentice Hall, Inc. All rights reserved Multidimensional Arrays Two-dimensional arrays analogous to tables –Rows and columns Specify row first, then column –Two subscripts

 2004 Prentice Hall, Inc. All rights reserved Multidimensional Arrays a[ 1 ][ 0 ]a[ 1 ][ 1 ]a[ 1 ][ 2 ]a[ 1 ][ 3 ] Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 Row subscript (or index) Array name Column subscript (or index) a[ 0 ][ 0 ]a[ 0 ][ 1 ]a[ 0 ][ 2 ]a[ 0 ][ 3 ] a[ 2 ][ 0 ]a[ 2 ][ 1 ]a[ 2 ][ 2 ]a[ 2 ][ 3 ] Fig Two-dimensional array with three rows and four columns.

 2004 Prentice Hall, Inc. All rights reserved Multidimensional Arrays Declaring and initializing multidimensional arrays –Group by row in square brackets –Treated as arrays of arrays –Creating array b with one row of two elements and a second row of three elements: var b = [ [ 1, 2 ], [ 3, 4, 5 ] ];

 2004 Prentice Hall, Inc. All rights reserved Multidimensional Arrays Also possible to use new operator –Create array b with two rows, first with five columns and second with three: var b; b = new Array( 2 ); b[ 0 ] = new Array( 5 ); b[ 1 ] = new Array( 3 );

 2004 Prentice Hall, Inc. All rights reserved. Outline 54 InitArray3.html (1 of 2) Array array1 provides six initializers in two rows. Array array2 provides six initializers in three rows. Function outputArray displays each array’s elements in a Web page.

 2004 Prentice Hall, Inc. All rights reserved. Outline 55 InitArray3.html (2 of 2) Referencing the multidimensional array theArray.

 2004 Prentice Hall, Inc. All rights reserved Multidimensional Arrays Fig Initializing multidimensional arrays.

 2004 Prentice Hall, Inc. All rights reserved Building an Online Quiz Radio buttons –Represented as an array Name of radio buttons is name of array One element per button –checked property is true when selected XHTML Forms –Contain controls, including radio buttons –action property specifies what happens when submitted Can call JavaScript code

 2004 Prentice Hall, Inc. All rights reserved. Outline 58 Quiz.html (1 of 2) Determining the value of property checked.

 2004 Prentice Hall, Inc. All rights reserved. Outline 59 Quiz.html (2 of 2) Call the checkAnswers function when the form is submitted.

 2004 Prentice Hall, Inc. All rights reserved Building an Online Quiz Fig Online quiz graded with JavaScript.