Java Reference
In-Depth Information
b)
Error: Referencing an array element outside the bounds of the array ( b[10] ).
Correction: Change the <= operator to < .
c)
Error: Array indexing is performed incorrectly.
Correction: Change the statement to a[1][1] = 5; .
Exercises
7.6
Fill in the blanks in each of the following statements:
a)
One-dimensional array p contains four elements. The names of those elements are
,
,
and
.
b)
Naming an array, stating its type and specifying the number of dimensions in the array
is called
the array.
c)
In a two-dimensional array, the first index identifies the
of an element and the
second index identifies the
of an element.
d)
An m -by- n array contains
rows,
columns and
elements.
e)
The name of the element in row 3 and column 5 of array d is
.
7.7
Determine whether each of the following is true or false . If false , explain why.
a)
To refer to a particular location or element within an array, we specify the name of the
array and the value of the particular element.
b)
An array declaration reserves space for the array.
c)
To indicate that 100 locations should be reserved for integer array p , you write the dec-
laration
p[ 100 ];
d)
An application that initializes the elements of a 15-element array to zero must contain
at least one for statement.
e)
An application that totals the elements of a two-dimensional array must contain nested
for statements.
7.8
Write Java statements to accomplish each of the following tasks:
a)
Display the value of element 6 of array f .
b)
Initialize each of the five elements of one-dimensional integer array g to 8 .
c)
Total the 100 elements of floating-point array c .
d)
Copy 11-element array a into the first portion of array b , which contains 34 elements.
e)
Determine and display the smallest and largest values contained in 99-element floating-
point array w .
7.9
Consider a two-by-three integer array t .
a) Write a statement that declares and creates t .
b) How many rows does t have?
c) How many columns does t have?
d) How many elements does t have?
e) Write access expressions for all the elements in row 1 of t .
f) Write access expressions for all the elements in column 2 of t .
g) Write a single statement that sets the element of t in row 0 and column 1 to zero.
h) Write individual statements to initialize each element of t to zero.
i) Write a nested for statement that initializes each element of t to zero.
j) Write a nested for statement that inputs the values for the elements of t from the user.
k) Write a series of statements that determines and displays the smallest value in t .
l) Write a single printf statement that displays the elements of the first row of t .
m) Write a statement that totals the elements of the third column of t . Do not use repetition.
n)
Write a series of statements that displays the contents of t in tabular format. List the
column indices as headings across the top, and list the row indices at the left of each row.
 
Search WWH ::




Custom Search