10.1 Fill in the blanks in each of the following statements:
- Lists and tables of values can be stored in arrays.
- The elements of an array are related by the fact that they normally have the same type.
- The number used to refer to a particular element of an array is called its subscript.
- The process of putting the elements of an array in order is called sorting the array.
- Determining whether an array contains a certain key value is called searching the array.
- An array that uses two subscripts is referred to as a(n) two-dimensional array.
10.2 State whether each of the following is true or false. If false, explain why.
- An array can store many different types of values. True.
- An array subscript should normally be a floating-point value. False. An array subscript must be an integer or an integer expression.
- 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:
- 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 ];
- Refer to the fourth element of the array.
fractions[ 3 ]
- Refer to array element 4.
fractions[ 4 ]
- Assign the value 1.667 to array element 9.
fractions[ 9 ] = 1.667;
- Assign the value 3.333 to the seventh element of the array.
fractions[ 6 ] = 3.333;
- 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:
- Declare and create the array with three rows and three columns.
var table = new Array( new Array( 3 ), new Array( 3 ), new Array( 3 ) );
- Display the number of elements.
document.write( "total: " + ( table.length * table[ 0 ].length ) );
- 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.
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 <.
var b = new Array( 10 );
for ( var i = 0; i < b.length; ++i )
b[ i ] = 1;
Error: The array subscripting is done incorrectly. Correction: Change the statement to a[ 1 ][ 1 ] = 5;.
var a = [ [ 1, 2 ], [ 3, 4 ] ];
a[ 1 ][ 1 ] = 5;
10.6 Fill in the blanks in each of the following statements:
- JavaScript stores lists of values in arrays.
- The names of the four elements of array p are p[ 0 ], p[ 1 ], p[ 2 ] and p[ 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.
- An m-by-n array contains m rows, n columns and m * n elements.
- The name of the element in row 3 and column 5 of array d is d[ 3 ][ 5 ].
- 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.
- 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 and the element position number, not value of the element, is specified.
- A variable declaration reserves space for an array. False. The new operator reserves space in memory for the array.
- To indicate that 100 locations should be reserved for integer array p, the programmer should write the declaration p[100]; False. The programmer should declare 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. The elements can also be initialized with a comma-separated initializer list.
- A JavaScript program that totals the elements of a two-dimensional array must contain nested for statements. False. Nested repetition can be used (not just a for statement) or each element can be written such as total = array[ 0 ][ 0 ] + array[ 0 ][ 1 ] + ... ;
10.8 Write JavaScript statements to accomplish each of the following tasks:
- Display the value of the seventh element of array f.
document.writeln( f[ 6 ] );
- Initialize each of the five elements of one-dimensional array g to 8.
for ( var x = 0; x < 5; x++ )
g[ x ] = 8;
- Total the elements of array c, which contains 100 numeric elements.
var total = 0;
for ( var x in c )
total += c[ x ];
- 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 ];
- 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 Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650. 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 following ranges (assume that each salesperson's salary is truncated to an integer amount):
- $200-299
- $300-399
- $400-499
- $500-599
- $600-699
- $700-799
- $800-899
- $900-999
- $1000 and over
10.11 Write statements that perform the following operations for a one-dimensional array:
- Set the 10 elements of array counts to zeros.
for ( var x in counts )
counts[ x ] = 0;
- Add 1 to each of the 15 elements of array bonus.
for ( var x in bonus )
++bonus[ x ];
- Display the five values of array bestScores, separated by spaces.
document.writeln( bestScores.join( " " ) );
10.12 Use 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.
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:
order | element |
---|---|
01 | sales[ 0 ][ 0 ] |
02 | sales[ 0 ][ 1 ] |
03 | sales[ 0 ][ 2 ] |
04 | sales[ 0 ][ 3 ] |
05 | sales[ 0 ][ 4 ] |
06 | sales[ 1 ][ 0 ] |
07 | sales[ 1 ][ 1 ] |
08 | sales[ 1 ][ 2 ] |
09 | 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 ] |
10.17 Use a two-dimensional array to solve the following problem: A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product actually sold. Each slip contains
- the salesperson number
- the product number, and
- the total dollar value of the product sold that day.
Thus, each salesperson passes in between zero and five sales slips per day. Assume that the information from all the slips for last month is available. Write a script that will read all this information for last month's sales and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, display the results in an XHTML table format, with each of the columns representing a different product. Cross-total each row to get the total sales of each product for last month. Your tabular printout should include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns.