This website is preserved for historical and scholarly reference and is no longer actively maintained.

Assignment #1 (50 pts. total)

Due Monday, October 5 by the beginning of class.

Individual programming assignments will be accepted up to three calendar days after the program was due. There will be penalty points deducted as follows.


Assignment 1: Problem 2 on page 424 of Arnow/Weiss.
(1) The prototype for the insertElementAt for arrays should be:
public static int [] insertElementAt ( int [] a, int index, int value )

(2) Immediately after creating an array of ints, call a method (which you have written) to fill all elements of the int array with zeros.

(3) Immediately after expanding an array, immediately call a method (which you have written, and which may be the same method used above) to fill all additional elements of the int array with zeros.

(4) Assume that the last non-zero entry in an array is the last valid element of the array. For example, suppose the declaration
int [ ] a = new int [5];

is followed by

a = insertElementAt (a, 0, 5 );
a = insertElementAt (a, 1, 6 );
a = insertElementAt (a, 3, 10 );

The elements of the array would be: 5, 6, 0, 10, 0. The first four integers would be considered valid elements. Thus, a call to

a = insertElementAt (a, 2, 8 );

would yield valid elements: 5, 6, 8, 0, 10. That is, the insertion works as you would expect it to work. However, any call to insertElementAt will now require resizing of the array. For example,

a = insertElementAt (a, 4, 13);

requires a new array to be created, all the elements of the former array copied to the new array, fill to the right with zeros, and then perform the insert.

The result of the create new array, copy and fill will be: 5, 6, 8, 0, 10, 0, 0, 0, 0, 0, when the increment in the size of the array is 5 elements. After the insertion, the elements will be: 5, 6, 8, 0, 13, 10, 0, 0, 0, 0. The valid elements of the array are: 5, 6, 8, 0, 13. The last four zeros are virtual (not really there).

(5) The test driver you write should have at least three arrays. Operations on these arrays should be interleaved.

Return to Eleanor Hare's home page.

Return to Department of Computer Science Home page.