Chapter 11: Arrays CIS 275—Web Application Development for Business I.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
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]
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
 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.
Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
Arrays Chapter 6.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
 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.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C 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,
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
© 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.
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.
 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.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing a Line of Text in a Web Page 7.3 Another JavaScript.
Adapted from  2004 Prentice Hall, Inc. All rights reserved. 5 th and 4 th ed: Chapters 6,7,8 SY306 Web and Databases for Cyber Operations SlideSet #6:
11/30: Sorting and Searching Arrays Look at PassArray.java Sorting arrays: the bubble sort method Searching arrays: the linear search Searching arrays:
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
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.
5 th and 4 th ed: Chapters 9, 10, 11 SY306 Web and Databases for Cyber Operations SlideSet #7: JavaScript Functions and Arrays.
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)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
 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.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Chapter 11 - JavaScript: Arrays
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 17 – JavaScript/Jscript: Arrays
JavaScript: Functions.
JavaScript: Arrays.
Chapter 11 - JavaScript: Arrays
Chapter 12 – JavaScript/Jscript: Arrays
JavaScript Arrays.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
JavaScript: Arrays.
10 JavaScript: Arrays.
Presentation transcript:

Chapter 11: Arrays CIS 275—Web Application Development for Business I

2 Array Object An Array object is a contiguous series of storage locations (_________) in memory. First, create the Array object. Then _____ the Array: var famname = new Array(3) famname[0] = "Jan Egil" famname[1] = "Tove" famname[2] = "Hege“ Use a ____ loop to write an Array (note the use of the length property of the Array object): for (i=0; i < famname.length; i++) { document.write(famname[i] + " ") }

3 Creating, Initializing, Summing Creating Arrays var n1 = new Array( 5 ); // allocate 5-element Array var n2 = new Array(); // allocate empty Array Three ways to initialize Arrays with lists var colors = new Array( “cyan”, “magenta”, “yellow”, “black” ); var integers1 = [ 2, 4, 6, 8 ]; var integers2 = [ 2,,, 8 ]; Summing elements in an Array for ( var i = 0; i < theArray.length; i++) total1 += theArray[ i ]; for ( var element in theArray) total2 += theArray[ element ];

4 Random Image Generator Random Image Generator <!-- var pictures = [ "CPE", "EPT", "GPP", "GUI", "PERF", "PORT", "SEO" ]; document.write ( "<img src = \"" + pictures[ Math.floor( Math.random() * 7 ) ] + ".gif\" width = \"105\" height = \"100\" />" ); // --> Click Refresh (or Reload) to run the script again

5 Passing Data to Functions Example var a = [ 1, 2, 3, 4, 5 ]; Pass-by-value In JavaScript, numbers and ________ values are passed to functions by value modifyElement( a[ 3 ] ); Pass-by-reference In JavaScript, references to ______ are passed to functions A reference is a location in memory modifyArray( a );

6 function start(){ var a = [ 1, 2, 3, 4, 5 ]; outputArray( "The values of the original array are: ", a ); modifyArray( a ); // array a passed by reference outputArray( "The values of the modified array are: ", a ); document.writeln( " Effects of passing array " + "element by value " + "a[3] before modifyElement: " + a[ 3 ] ); modifyElement( a[ 3 ] ); document.writeln( " a[3] after modifyElement: " + a[ 3 ] ); } function outputArray( header, theArray ){ document.writeln(header + theArray.join( " " ) + " " ); } function modifyArray( theArray ){ for ( var j in theArray ) theArray[ j ] *= 2; } function modifyElement( e ){ e *= 2; document.writeln( " value in modifyElement: " + e ); } Fig. 11.8, p. 353

7 Sorting Arrays The Array class a built-in method called _______ for sorting arrays. anArray.sort(); // uses string comparisons for sort An optional argument of sort is the name of a function called a ___________ function that compares its two arguments. anArray.sort(compareIntegers); function compareIntegers( integer1, integer2 ) { return parseInt( integer1) – parseInt ( integer2 ); }

8 sort.html Fig. 11.9, p. 355 Sorting an Array with Array Method sort function start() { var a=[10,1,9,2,8,3,7,4,6,5]; document.writeln( " Sorting an Array " ); outputArray( "Data items in original order: ", a ); a.sort( compareIntegers ); outputArray( "Data items in ascending order: ", a ); } function outputArray(header, theArray) { document.writeln( " " + header + theArray.join( " " ) + " " ); } function compareIntegers(value1, value2) { return parseInt(value1) - parseInt(value2); }

9 Searching an Array You search an array for a ______ value. The following contains the logic for a ______ search. var searchKey = “Johnson”; var element = linearSearch(anArray, searchKey); function linearSearch(theArray, key) { for ( var n = 0; n < theArray.length; ++n) if ( theArray[n] == key ) return n; return -1; } See p. 360 for the more complicated binary search (will not be covered on exam).

Multidimensional Arrays An array with two subscripts can be thought of as a table with rows and _______ (a two-dimensional array). In JavaScript, a two-dim array is a one-dim array whose elements are _________ arrays (rows). Example: var b = [ [ 1, 2], [ 3, 4 ] ]; The 1st row of Array b contains 1 in the 1st column, 2 in the 2nd column The 2nd row of Array b contains 3 in the 1st column, 4 in the 2nd column Declaring a two-dim array var b = new Array(2); b[0]=new Array(5); b[1]=new Array(3);

11 Manipulating Multi-Dim Arrays Set all elements in the 3 rd row of array a to zero: for( var col = 0; col < a[2].length; ++col) a[2][col] = 0; The same thing using for______: for( var col in a[2]) a[2][col] = 0; Determining the total of all elements in array a: var total=0; for(var row=0; row < a.length; ++row) for(var col=0; col < a[row].length; ++col) total += a[row][col]; See Fig , p. 367