Chapter 10

10.1

Fill in the blanks in each of the following statements:

a) Lists and tables of values can be stored in arrays.

b) The elements of an array are related by the fact that they normally have the same type.

c) The number used to refer to a particular element of an array is called its subscript.

d) The process of putting the elements of an array in order is called sorting the array.

e) Determining whether an array contains a certain key value is called searching the array.

f) An array that uses two subscripts is reffered to as a(n) multidimensional array.

10.2

State whether each of the following is true or false. If false, explain why.

a) An array can store many different types of values. True.

b) An array subscript should normally be a floating-point value. False. An array subscript must be an integer of an integer expression.

c) An individual array element that is passed to a function and modified in it will contain the modified value when the called function completes execution. False. Individual primitive-data-type elements are passed by value. If a reference to an array is passed, then modifications to the elements of the array are reflected in the original element of the array. Also, an individual element of an object type passed to a function is passed by reference, and changes to the object will be reflected in the original array element.

10.3

Write JavaScript statements (regarding array functions) to accomplish each of the following tasks:

a) Declare an array with 10 elements, and initialize the elements of the array to 0. var fractions = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];

b) Refer to the fourth element of the array. fractions[3]

c) Refer to array element 4. fractions[4]

d) Assign the value 1.667 to array element 9. fractions[9] = 1.667;

e) Assign the value 3.333 to the seventh element of the array. fractions[6] = 3.333;

f) Sum all the elements of the array, using a for...in statement. Define variable x as a control variable for the loop. var total = 0;

for ( var x in fractions )

total += fractions[x];

10.4

Write JavaScript statements (regarding array table) to accomplish each of the following tasks:

a) Declare and create the array with three rows and three columns. var table = new Array( new Array( 3 ), new Array( 3 ),

new Array( 3 ) );

b) Display the number of elements. document.write( "total:" + ( talbe.length * table[0].length ));

c) Use a for..in statement to initialize each element of the array to the sum of its subscripts. Assume that the variables x and y are declared as control variables. for ( var y in table[x] )

table[x][y] = x + y;

10.5

Find the error(s) in each of the following program segments, and correct them.

a) var b = new Array( 10 );

for ( var 1 = 0; i <= b.length; i++ )

b[ i ] = 1; Change the <= to <. References an area outside the bounds of the array.

b) var a = [ [ 1, 2 ], [ 3, 4 ] ];

a[ 1, 1 ] = 5; Change the statement to a [ 1 ] [ 1 ];

10.6

Fill in the blanks in each of the following statements:

a) JavaScript stores lists of values in Arrays.

b) The names of the four elements of array p are p[0], p[1], p[2], and p[3].

c) In a two-dimensional array, the first subscript identifies the row of an element, and the second subscript identifies the column of an element.

d) An m-by-n array contains m-1 rows, n-1 columns and m-1*n-1 elements.

e) The name the eleent in row 3 and column 5 of array d is d[ 2 ] [ 4 ].

f) The name of the element in the thrid row and fifth column of array d is d[ 2 ] [ 4 ].

10.7

State whether each of the following is true or false. If false, explain why.

a) To refer to a particular location of element in an array, we specify the name of the array and the value of the element. False. Refer to the position number, not element.

b) A variable declaration reserves space for an array. False. Instantiating the array object with the "new" operator creates the space.

c) To indicate that 100 locations should be reserved for integer array p, the programmer should write the declaration p[ 100 ]; False. It would be done as var p = new Array(99);

d) A JavaScript program that initializes the elements of a 15-element array to zero must contain at least one for statement. True.

e) A JavaScript program that totals the elements of a two-dimensional array must contain nested for statements. True.

10.8

Write JavaScript statements to accomplish each of the following tasks:

a) Display the value of the seventh element of array f. document.writeln( f [ 6 ] );

b) Initialize eack of the five elements of one-dimendional array g to 8. var g = [ 8, 8, 8, 8, 8 ];

c) Total the elements of array c, which contains 100 numeric elements. for ( var element in c ) total += c [ element ];

d) Copy 11-element array a into the first portion of array b, which contains 34 elements. for ( var element in a ) b = a [ element ];

e) Determine and print the smallest and largest values contained in 99-element floating-point array w. w.sort( compareIntegers );

function compareIntegers( value1, value2 )

{ return parseInt( value1 ) - parseInt( value2 ); }

document.writeln( w[0] + " " + w[98] );

10.9

Consider a two-by-three array t that will store integers.

a) Write a statement that declares and creates array t. var t = new Array [ [ 1, 2, 3 ], [ 3, 4, 5] ];

b) How many rows does t have? two

c) How many columns does t have? three

d) How many elements does t have? six

e) Write the names of all the elements in the second row of t. t[ 1 ][ 0 ], t[ 1 ][ 1 ], t[ 1 ][ 2 ]

f) Write the names of all the elements in the third column of t. t[ 0 ][ 2 ], t[ 1 ][ 2 ]

g) Write a single statement that sets the elements of t in row 1 and column 2 to zero. t[ 0 ][ 1 ] = 0;

j) Write a series of statements that determines and prints the smallest value in array t.

t.sort( compareIntegers );

function compareIntegers( value1, value2 )

{ return parseInt( value1 ) - parseInt( value2 ); }

document.writeln( t[ 0 ][ 0 ]);

k) Write a statement that displays the elements of the first row of t. document.writeln( t[ 0 ][ 0 ] + " " + t[ 0 ][ 1 ] + " " + t[ 0 ][ 2 ] );

l) Write a statement that totals the elements of the fourth column of t.

var total = 0;

for ( var row in t )

for ( var col in t[ row ] )

total += t[ row ][ 3 ];

m) Write a series of statements that prints the array t in neat, tabular format. List the column subscripts as headings across the top, and list the row subscripts at the left of each row.

for ( var i in t )

{

for ( var j in t[ i ] )

document.write( j + 1 + " " );

document.write( "<br />" );

document.write( j + 1 + " " );

for ( var j in t[ i ] )

document.write( t[ i ][ j ] + " " );

document.write( "<br />" );

}



10.11

Write statements that perform the following operations for a one-dimensional array:

a) Set the 10 elements of array counts to zeros.

var counts = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];

b) Add 1 to each of the 15 elements of array bonus.

var bonus = new Array[ 15 ];

for ( var element in bonus )

bonus[ element ] = bonus[ element ] + 1;

c) Display the five values of array bestScores, separated by spaces.

var bestScores = new Array[ 5 ];

for ( var element in bestScores )

document.write( "bestScores[ element ] + " " );



10.13

Label the elements of three-by-five two-dimensional array sales to indicate the order in which they are set to zero by the following program segment:

for ( var row in sales )

for ( var col in sales[row] )

sales[row][col] = 0;

1: sales[ 0 ][ 0 ];

2: sales[ 0 ][ 1 ];

3: sales[ 0 ][ 2 ];

4: sales[ 0 ][ 3 ];

5: sales[ 0 ][ 4 ];

6: sales[ 1 ][ 0 ];

7: sales[ 1 ][ 1 ];

8: sales[ 1 ][ 2 ];

9: sales[ 1 ][ 3 ];

10: sales[ 1 ][ 4 ];

11: sales[ 2 ][ 0 ];

12: sales[ 2 ][ 1 ];

13: sales[ 2 ][ 2 ];

14: sales[ 2 ][ 3 ];

15: sales[ 2 ][ 4 ];

News:

Working on Chapter 15