CPSC 124, Fall 1996

Quiz Number 6

This is the sixth quiz given in CPSC 124: Introductory Programming, Fall 1996. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers.

Question 1: Explain exactly what is accomplished by the following declarations. What memory is allocated? What is in that memory?

           int[] numbers = { 12, 10, 8, 6, 4, 2 };
           String[] strings = new String[10];

Answer: This creates two arrays. The first array has six elements of type int, which are filled with the six values in the array initializer {12,10,8,6,4,2}. (The size of an array with an initializer is determined by the number of items in the initializer.) The second array can hold 10 items, where each item is either null or is a reference to an object of type String. Initially, all ten spaces in this array are filled with nulls.

(Technically, there is one more value in each array object. An array object has an instance variable that specifies the length of the array. Counting this length, the array numbers actually contains 7 variables, and the array strings contains 11.).

Question 2: What is meant by the "basetype" of an array?

Answer: The base type of an array refers to the type of the items that can be stored in that array.

Question 3: What is the purpose of the following subroutine? Determine what task this subroutine accomplishes, and describe the goal of that task.

           static int test(int[] A) {
              int count = 0;
              for (int i = 0; i < A.length - 1; i++) {
                 if (A[i] == A[i+1])
                    count = count + 1;
              return count;

Answer: This subroutine compares each element in the array with the item that follows it in the array. If the two items are equal, then the counter is incremented. So, the subroutine counts the number of consecutive pairs of components in the array that are equal.

Question 4: Suppose that A has been declared and initialized with the statement

double[] A = new double[20];

And suppose that A has already been filled with 20 values. Write a program segment that will find the average of all the non-zero numbers in the array. (The average is the sum of the numbers, divided by the number of numbers. Note that you will have to count the number of non-zero entries in the array.) Declare any variables that you use.

Answer: (There is one problem with this problem. What happens if all the entries in the array A are zero? In that case, the number of non-zero entries is zero, and the average of non-zero entries is undefined. In my answer, I assign a value of zero to the average in this case, but this is somewhat arbitrary.)

             int nonzeroCt = 0;   // the number of non-zero entries in the array
             double total = 0;    // the total of all the grades in the array
             double average;      // the average of the non-zero entries in the array
             for (int i = 0; i < 20; i++) {
                if (A[i] != 0) {     // process this non-zero entry
                    total += A[i];
             if (nonzeroCt > 0)
                average = total / nonzeroCt;
             else   // (Actually, the average is undefined in this case)
                average = 0;

David Eck