Second Summer 2002

Arrays (Chapter 11): The warm-ups.

Questions:

1. For each of the following sets of values, write code that fills an array `a` with the values:
`1 2 3 4 5 6 7 8 9 10`
```int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = (i + 1); ```
`0 2 4 6 8 10 12 14 16 18`
```int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = 2 * i; ```
`1 4 9 16 25 36 49 64 81 100`
```int[] a = new int[10];
for (int i = 0; i < a.length; i++)
a[i] = i * i;
```
`0 0 0 0 0 0 0 0 0 0`
`int[] a = new int[10];`
`1 4 9 16 9 7 4 9 11`
`int[] a = { 1, 4, 9, 16, 9, 7, 4, 9, 11}; `
Use a loop when appropriate.

2. Write a loop that fills an array `a` with ten random numbers between 1 and 100. Write code for two nested loops that fill `a` with ten different random numbers between 1 and 100.
```for (int i = 0; i < a.length; i++)
a[i] = 1 + gen.nextInt(100);

for (int i = 0; i < a.length; i++) {
int number = 1 + gen.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1; // ...why?
else           a[i] = number;
} ```

3. Write Java code for a loop that simultaneously computes the maximum and the minimum of an array.
```int min = a[0];
int max = a[0];
for (int i = 0; i < a.length; i++) {
if (min > a[i]) min = a[i];
if (max < a[i]) max = a[i];
} ```

4. What is wrong with the following loop?
```int[] v = new int[10];
for (int i = 1; i <= 10; i++) v[i] = i * i;```
`v[10]` will produce an array out of bounds exception.

Explain two ways of fixing the error.

Change the limits to `0` and `9`

`for (int i = 0; i <= 9; i++) v[i] = (i + 1) * (i + 1); `
or change the body of the for loop to index `v` with a value of `i - 1`
`for (int i = 1; i <= 10; i++) v[i - 1] = i * i; `

5. What is an array index? What are the bounds of an array?

An integer-valued expression that indicates position in a sequence (starting from `0`). The bounds are `0` and the length of the array minus one.

6. Give an example of

1. A useful method that has an array of integers as a parameter that is not modified

Computing and reporting the average, finding min, max, counting, searching, some versions of `append`, `insert` and `remove` (those that work with full arrays, not with companion variables).

2. A useful method that has an array of integers as a parameter that is modified

Sorting an array. Also those versions of `remove` and `insert` that work with a companion variable.

3. A useful method that has an array of integers as a return value

Generating an array of random values. Those versions of `insert` and `remove` that work with full arrays.

Just describe each method. Don't implement the methods.

7. A method that has an array as a parameter can change the array in two ways. It can change the contents of individual array elements, or it can rearrange the elements. Describe two useful methods with `Product[]` parameters that change an array of products in each of the two ways just described.

Think of an array as being a book shelf. The books are the actual products that the shelf is storing. One can do these two things:

1. sort them by price

2. lower their prices by 5% (change the label on the back cover).

8. What are parallel arrays? Why are parallel arrays indications of poor programming? How can they be avoided?

Parallel arrays are arrays of features such that the collection of all values that appear in the locations with the same index (`i`) in the arrays all refer to one and the same entity.

Avoid them by defining an entity class (with those features) and creating an array of such entities. Helps keep the features in sync.

9. Design a class `Catalog` that stores a collection of products. What public methods should you support? What advantages and disadvantages does a `Catalog` class have over a `Product[]` array?

The products will be available by name. The advantage would be that the names could be meaningful, but you'd have to either know it or implement a method that works as a table of contents. Another disadvantage is that you can't add a new product at run time, you'd have to change the source code and recompile. In this respect a `Hashtable` would work much better.

Arrays will give us more uniformity and the ability to scan all the elements through a loop, sort the products in the catalog, add some more at run time if we want to, the disadvantage is that the index may be somewhat meaningless.

10. Suppose `v` is a sorted vector of products. Describe how a new product can be inserted in its proper position so that the resulting vector stays sorted.

Start from the beginning and search for an element that has the same value. You will be going either up or down. Insert where you find the value or when you know that you want be able to find the value (the value is below or above the one that you're looking, depending on how the vector is sorted -- ascending or descending).

11. How do you perform the following tasks with arrays in Java?

1. Test that two arrays contain the same elements in the same order.

The arrays should have the same length. Compare the elements one by one and stop with `false` if there is a mismatch. Otherwise the answer is: `true`

2. Copy one array to another.

Arrays should be the same length. Use a loop to scan all elements in one array and set the elements with the same index in the other array have the values you find in the first.

3. Fill an array with zeroes, overwriting all elements in it.

There's more than one way of answering this question: you could essentially get a new array of the same size (filled with zero by default) and change the reference to it. If you're doing this in a method it won't work so you need to change the elements to 0 one by one.

4. Remove all elements from an array.

If you have the actual reference just point it to a new array of length 0. Otherwise return such an array and ask the caller to do that (agree on a protocol).

12. True or false?

1. All elements of an array are of the same type.

Yes.

2. Array subscripts must be integers.

Yes, of type `int`.

3. Arrays cannot contain strings as elements.

They can.

4. Arrays cannot use strings as subscripts.

They cannot.

5. Parallel arrays must have equal length.

They should.

6. Two-dimensional arrays always have the same numbers of rows and columns.

They may not.

7. Two parallel arrays can be replaced by a two-dimensional array.

Sometimes, in certain circumstances.

8. Elements of different columns in a two-dimensional array can have different types.

No. How could you declare that.

9. Elements in a vector can have different types.

That's why the `Vector` treats them all as `Object`s.

13. True or false?

1. A method cannot return a two-dimensional array.

It can.

2. A method can change the length of an array parameter.

Not directly, not really.

3. A method cannot change the dimensions of a two-dimensional array parameter.

Indeed.

4. A method cannot change the length of a vector that is passed as a parameter.

It can (just add elements to it)

5. A method can only reorder the elements of an array parameter, not change the elements.

`m[i].deposit(100);`

Last updated: Jun 16, 2002 by Adrian German for A201