Chapter 10

10.1 Fill in the blanks in each of the following statements:

  1. Lists and tables of values can be stored in arrays.
  2. The elements of an array are related by the fact that they normally have the same type.
  3. The number used to refer to a particular element of an array is called its subscript.
  4. The process of putting the elements of an array in order is called sorting the array.
  5. Determining whether an array contains a certain key value is called searching the array.
  6. An array that uses two subscripts is referred to as a two-dimensional array.

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

  1. An array can store many different types of values. True.
  2. An array subscript should normally be a floating-point value. False. An array subscript must be an integer or an integer expression.
  3. 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 fractions) to accomplish each of the following tasks:

  1. 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 ];
  2. Refer to the fourth element of the array.
    fractions[ 3 ]
  3. Refer to array element 4.
    fractions[ 4 ]
  4. Assign the value 1.667 to array element 9.
    fractions[ 9 ] = 1.667;
  5. Assign the value 3.333 to the seventh element of the array.
    fractions[ 6 ] = 3.333;
  6. Sum all the elements of the array, using 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:

  1. Declare and create the array with three rows and three columns.
    var table = new Array( new Array( 3 ), new Array( 3 ), new Array( 3 ) );
  2. Display the number of elements.
    document.write( "total: " + ( table.length * table[ 0 ].length ) );
  3. 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 x in table )
       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.

  1. Error: Referencing an array element outside the bounds of the array (b[ 10 ]). [Note: This error is actually a logic error, not a syntax error.] Correction: Change the <= operator to <.
  2. Error: The array subscripting is done incorrectly. Correction: Change the statement to a[ 1 ][ 1 ] = 5;.

10.6 Fill in the blanks in each of the following statements:

  1. JavaScript stores lists of values in arrays.
  2. The names of the four elements of array p are p[ 0 ], p[ 1 ], p[ 2 ] and p[ 3 ].
  3. In a two-dimensional array, the first subscript identifies the row of an element, and the second subscript identifies the column of an element.
  4. An m-by-n array contains m rows, n columns and m * n elements.
  5. The name of the element in row 3 and column 5 of array d is d[ 3 ][ 5 ].
  6. The name of the element in the third 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.

  1. 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. The name of the array should be specified instead of the value.
  2. A variable declaration reserves space for an array.
    False. The new operator reserves space an array.
  3. To indicate that 100 locations should be reserved for integer array p, the programmer should write the declaration p[100];
    False. The declaration should be p = new Array( 100 );
  4. A JavaScript program that initializes the elements of a 15-element array to zero must contain at least one for statement.
    True.
  5. A JavaScript program that totals the elements of a two-dimensional array must contain nested for statements.
    True.

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

  1. Display the value of the seventh element of array f.
    document.writeln( f[ 6 ] );
  2. Initialize each of the five elements of one-dimensional array g to 8.
    for ( var x = 0; x < 5; x++ )
       g[ x ] = 8;
  3. Total the elements of array c, which contains 100 numeric elements.
    var total = 0;
    for ( var x in c )
       total += c[ x ];
  4. Copy 11-element array a into the first portion of array b, which contains 34 elements.
    for ( var x in a )
    b[ x ] = a[ x ];
  5. Determine and print the smallest and largest values contained in 99-element floating-point array w.
    function sortNumber( x, y )
    {
    return x - y;
    }

    w.sort( sortNumber );

    document.write( "Smallest value: " + w[ 0 ] + "<br />" );
    document.write( "Largest value: " + w[ 98 ] + "<br />" );

10.10 Write a script (using an array of counters) that obtains the gross sales for each employee through an XHTML form and determines how many of the salespeople earned salaries in each of the given ranges.

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

  1. Set the 10 elements of array counts to zeros.
    for ( var x in counts )
       counts[ x ] = 0;
  2. Add 1 to each of the 15 elements of array bonus.
    for ( var x in bonus )
       ++bonus[ x ];
  3. Display the five values of array bestScores, separated by spaces.
    document.writeln( bestScores.join( " " ) );

10.12Use a one-dimensional array to solve the following problem: Read in 20 numbers, each of which is between 10 and 100. As each number is read, print it only if it is not a duplicate of a number that has already been read. Provide for the "worst case," in which all 20 numbers are different. Use the smallest possible array to solve this problem.

Home