Exercise 8.6
- The following fragment determines the largest number in list, an
array of double values. Rewrite the fragment to make it clearer.
double largest = list [0] ;
int i = 1;
while (i < list.length)
if (list[i++] > largest)
largest = list[i-1];
- Suppose that you are required to read a set of values and determine
the given quantity. For which ones (if any) would you need to use an
array?
| (a) the largest value | (b) the median |
| (c) the mean | (d) the range |
- Identify and correct the error in each declaration.
double a = new double [10] j
-
int[] b = new int[];
-
char[] [] c = char [30] [];
float [] [] d = new float [] [10] ;
-
int[] e = new {3,5,2,9,1};
- A programmer, using a square two-dimensional array of int values
called table, wanted to sum the elements along the main diagonal
(the diagonal whose elements are table[0][0], table[1][1], and
so on). To do this the programmer wrote
int total = 0;
for (int i = 0; i < list.length; i++)
for (int j = 0; j < list.length; j++)
total += table[i] [j];
- What does the fragment actually do?
- Write a fragment that does set total to the sum of the elements
of the main diagonal.
- Write a fragment that sets total to the sum of the elements of
the other diagonal of table.
| |