CH9

February 14, 2008 at 5:11 pm (Uncategorized)

12.1

1)array is a data structure. It helps user easy to manipulate millions of items and elements.

2) items in the array are called elements. They all must be in same type. The type can be any primitive or reference type. For example, there can be array of scores, name or student objects. Each array have to have same length.

3) Mary is using an array of doubles to store an employee’s wage amounts for each day of the week (Monday through Friday). Draw a picture of this array with sample items and references to each one.

12.2

1) Assume that the array a contains the five integers 34, 23, 67, 89, and 12. Write the values of the following expressions:a. a [1] a array’s 2nd element, so 23. b. a[a.length - 1] 4c. a[2] + a[3]67 + 89 = 156 2. What happens when a program attempts to access an item at an index that is less than 0 or greater than or equal to the array’s length? it’’s going to be out of bound, causing error.

9.3

1.) int i=0 for (i==0, i=0; i-) System.out.print(a[i]). 3.) int index; for (int i=0; i<a. length; i++){if(a[i]<0){index=i; break;} else{index=a.length;}System.out.print (index); 4a)It will change all the negative values of a to positive value by taking the absolute values of them.

9.4

1.)double[] abc=new double[15] string[] abcd=new sting[20].

2)It is another way to initialize the array.it assign the exact values to array’s element.

3-a)int[] aaa={100,90,75,60,88}

b.) double[] bbb={0.12,0.05,0.15}

c) string [] ccc={david, chen}.

4. When declaring an array variable.

9.5.

1.)The programmer will try to solve the problem by tracking the array’s logical size with a separate integer variable.
2.)When the array is not full, it must replace the array;s physical length with its logical size in the loop.9.6

1.A parallel array is situations in which it is convenient to declare.

2.One array should contain the exponent and the other array should contain 2 raised to that power.

3. String [] name = new String[50]int [] age = new int [50]int [] security = new int [50]String searchPerson;int correspondingAge = -1, correspondingSecurity = -1searchName = …for (int i = 0, i<name.length; i ++) if (searchName.equal (name[i]))correspondingAge = age [i]correpondingSecurity = security [i]break;}4.

9.7

1.An array in which each data item is accessed by specifying a pair of indices.

2.If an application require the variable tale reference an array of four elements. Each of these elements in turn reference and array of five integers. And the application will need to use two dimensional arrays.

Permalink Leave a Comment