Chapter 10 - Answers
Question 10.1
- Arrays
- Index
- Sorting
- Two-Dimensional
Question 10.2
- True
- False. An array index must be an integer or an integer expression.
- False. In individual primative-data-type elements are passed by value.
If a reference to an array is passed, then modifcations 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.
Question 10.3
- var fractions = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
- fractions [ 3 ]
- fractions [ 4 ]
- fractions [ 9 ] = 1.667;
- fractions [ 6 ] = 3.333;
- var total = 0
for (var x in fractions )
{
total += fractions [ x ];
}
Question 10.4
- var table = new Array ( new Array (3), new Array(3), new Array (3));
- var totalElements = table.length * table[0].length;
- for (var x in table)
{
for (var y in table[x] )
{
table[x][y] = x + y;
}
}
Question 10.5
- Error: Referencing an array element outside the bounds of the array (b[10]).
Correction: Change the <= to <
- Error: the array indexing is done incorrectly.
Correction: Change the statement to a [1][1] = 5;
Question 10.6
- 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 rows, n columns and m*n elements.
- e) The name the element in row 3 and column 5 of array d is d[ 3 ][ 5 ].
Question 10.7
- To refer to a particular location or element in an array, we specify the name of the array and the value of the element.
- False - you must specify the name of the array as well as the index location for a specific element.
- A variable declaration reserves space for an array.
- False - Space is not reserved until the new operator is used to initiate the array.
- To indicate that 100 locations should be reserved for integer array p, the programmer should write the declaration p[ 100 ];
- False - var p = new Array[ 100 ].
- A JavaScript program that initializes the elements of a 15-element array to zero must contain at least one for statement.
- False - Arrays can be initialized when they are declared
- A JavaScript program that totals the elements of a two-dimensional array must contain nested for statements.
- True
Question 10.8
- Display the value of the seventh element of array f.
document.write( f[ 6 ] );
- Initialize each of the five elements of one-dimensional array g to 8.
for ( var i = 0; i < 5; i++ );
g[ i ] = 8;
- Total the elements of array c, which contains 100 numeric elements.
for ( var i = 0; i < 100; i++ );
total = total + parseInt( c[ i ] );
- Copy 11-element array a into the first portion of array b, which contains 34 elements.
for ( var i = 0; i < a.length; i++ );
b[ i ] = a[ i ];
- Determine and print the smallest and largest values contained in 99-element floating-point array w.
var largeNumber = a[ 0 ];
var smallNumber = a[ 0 ];
for ( var i = 1; i < 99; i++ )
{
if( a[ i ] > largeNumber )
largeNumber = a[ i ];
if( a[ i ] > smallNumber )
smallNumber = a[ i ];
}
document.writeln( largeNumber + " " + smallNumber );
Question 10.9
- Write a statement that declares and creates array t.
var t = new Array ( new Array( 3 ), new Array( 3 ) ) ; -
- How many rows does t have? 2
- How many columns does t have? 3
- How many elements does t have? 6
- Write the names of all the elements in the second row of t. t[ 1 ][ 0 ], t[ 1 ][ 1 ], and t[ 1 ][ 2 ]
- Write the names of all the elements in the third column of t. t[ 0 ][ 2 ] and t[ 1 ][ 2 ]
- Write a single statement that sets the elements of t in row 1 and column 2 to zero. t[ 0 ][ 1 ] = 0 ;
- Write a series of statements that initializes each element of t to zero. Do not use a repetition structure. t[ 0 ][ 0 ] = 0 ; t[ 0 ][ 1 ] = 0 ; t[ 0 ][ 2 ] = 0 ; t[ 1 ][ 0 ] = 0 ; t[ 1 ][ 1 ] = 0 ; t[ 1 ][ 2 ] = 0 ;
- Write a nested for statement that initializes each element of t to zero. for ( var row in t ) for ( var col in t[ row ] ) t[ row ][ col ] = 0 ;
- Write a series of statements that determines and prints the smallest value in array t. t.sort( compareNumbers ); document.writeln( "The smallest number in the array is " + t[ 0 ][ 0 ] ; function compareNumbers( num1, num2 ) { return parseInt( num 1 ) - parseFloat( num 2 ) ; }
- Write a nonrepetition statement that displays the elements of the first row of t. for ( var col in t[ 0 ] ) document.writeln( t [ 0 ][ col ] ) ;
- 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.
document.writeln( "<table border = \"1\"" ) ; document.writeln( " <thead><th> t </th>" ) : for ( var col in t ) document.writeln( " <th> " + col + "< /th> " ) ; document.writeln( "</thead><tr>" ) ; for ( var row in t ) document.writeln( "<td><b>" + row + "</b></td>" ) ; for ( var col in t[row] ) document.writeln( "<td>" + t[ row ][ col ] + "</td>" ) ; document.writeln( "</tr>" ) ;< >
Question 10.10
Question 10.10