CS101 Introduction To Computing: (Web Development Lecture 9)
CS101 Introduction To Computing: (Web Development Lecture 9)
Lecture 26 Arrays
(Web Development Lecture 9)
1
During the last lecture we had a
discussion on Flow Control & Loops
• We discussed the concept of flow control using
the “if” and “switch” structures
3
Compound Statements
• At times, we need to put multiple statements at
places where JavaScript expects only one
x=1;
Initial count
while ( x < 6000 ) {Condition Operation
document.write ( x ) ;
x=x+1;
} for ( x = 1 ; x < 6000 ; x = x + 1 ) {
document.write ( x ) ;
}
5
for --?-- while
• When the exact number of iterations is
known, use the ‘for’ loop
6
‘for’ loops become especially useful
when used in conjunction with arrays
7
Today’s Topic:
Arrays
8
Array?
9
Array
12
Array
fruit[ 0 ]
Square
Identifier Index
bracket
13
Let’s now take look at one of the
advantages of using arrays
14
var student1, student2, student3, student4 ;
student1 = “Waseem” ;
student2 = “Waqar” ;
student3 = “Saqlain” ;
student4 = “Daanish” ;
document.write( student1 ) ;
document.write( student2 ) ;
document.write( student3 ) ;
document.write( student4 ) ; 15
student = new Array( 4 ) ; //array declaration
student[ 0 ] = “Waseem” ;
Can you see
student[ 1 ] = “Waqar” ; the advantage
student[ 2 ] = “Saqlain” ; of using arrays
student[ 3 ] = “Daanish” ; along with the
‘for’ loop?
for ( x = 0 ; x < 4 ; x = x + 1 ) {
document.write( student[ x ] ) ;
}
16
Arrays in JavaScript
• In JavaScript, arrays are implemented in the
form of the ‘Array’ object
18
The ‘new’
This is the operator creates Pair of
identifier of the an instance paren-
new instance theses
20
‘Instances’ of an Object
21
All instances
of an object are
objects themselves!
22
‘Property’ Values of the Instances May Differ
23
student = new Array( 4 )
24
Array Identifiers
The naming rules for Array identifiers are
the same as were discussed for variable
identifiers
25
Assigning Values to Array Elements
a[ 1 ] = 5 ; //the second element
name[ 5 ] = “bhola” ;
number = 5 ;
name[ number ] = name[ 5 ] ;
for ( x = 0 ; x < 10 ; x = x + 1 ) {
y[ x ] = x * x ;
} 26
Remember: just like C, C++ and Java,
the first element of an array has an
index number equal to zero
27
JavaScript Arrays are Heterogeneous
Unlike many other popular languages,
a JavaScript Array can hold elements
of multiple data types, simultaneously
a = new Array( 9 ) ;
b = new Array( 13 ) ;
b[ 0 ] = 23.7 ;
b[ 1 ] = “Bhola Continental Hotel” ;
b[ 2 ] = a ; 28
The ‘length’ Property of Arrays
‘d’ is an ‘length’ is a
instance of the property of
‘Array’ object the object ‘d’
d = new Array ( 5 ) ;
document.write( d.length ) ;
29
The ‘length’ Property of Arrays
What is
x = new Array ( 10 ) ;
advantage
for ( x = 0 ; x < 10 ; x = x + 1 ) { of using
‘x.length’
y[ x ] = x * x ; here
} instead of
x = new Array ( 10 ) ; using the
literal ‘10’?
for ( x = 0 ; x < x.length ; x = x + 1 ) {
y[ x ] = x * x ;
} 30
Array Methods: sort( )
Sorts the elements in alphabetical order
32
Array Methods: reverse( )
Reverses the order of the elements
x = new Array ( 4 ) ; Saqlain
x[ 0 ] = “Waseem” ; Shoaib
x[ 1 ] = “Waqar” ; Waqar
x[ 2 ] = “Saqlain” ; Waseem
x[ 3 ] = “Shoaib” ; Is this the
x.reverse( ) ; required
x.sort( ) ; result?
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>”) ;
} 33
Array Methods: reverse( )
Reverses the order of the elements
x = new Array ( 4 ) ; Waseem
x[ 0 ] = “Waseem” ; Waqar
x[ 1 ] = “Waqar” ; Shoaib
x[ 2 ] = “Saqlain” ; Saqlain
x[ 3 ] = “Shoaib” ;
x.sort( ) ;
x.reverse( ) ;
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>”) ;
} 34
Let’s Now Do a More Substantial Example
36
37
38
Pseudo Code
1. Declare the array that will be used for
storing the words
42
Pseudo Code
1. Declare the array that will be used for
storing the words
46
Pseudo Code
1. Declare the array that will be used for
storing the words
48
Assignment #9
Build a Web page that implements the Bubble
Sort algorithm discussed during the 17th lecture
(Algorithms II)
50
Next (the 10 ) Web Dev Lecture:
th