CPSC 124, Spring 2006
Answers to Quiz #6


Question 1: Explain briefly what is meant by a partially filled array and how to add a new element to a partially filled array.

Answer: In a "partially filled array," the number of actual data items in the array can be less than the length of the array, and the number can change over time as items are added to and removed from the array. In order to implement this, a separate counter variable of type int is needed to keep track of the number of items in the array. When a new item is added, the value of the counter variable gives the index of the array element where the item is to be stored, and then the counter variable is incremented by one:

         A[counter] = newItem;
         counter++;

Question 2: Write a Java function that tests whether or not a one-dimensional array of integers is already sorted into increasing order. The function should return a value of type boolean. (Hint: The array is sorted if each element in the array is less than or equal to the next element.)

Answer: (Note that when testing pairs of elements A[i] and A[i+1] to see whether they are in the correct order, we must have i+1 < A.length or there will be an array index out of bounds error. This means that i must be less than A.length-1.)

         public static boolean isSorted( int[] A ) {
         
            for (int i = 0; i < A.length - 1; i++) {
               if ( A[i] > A[i+1] )    // Elements i and i+1 are out of order
                  return false;        // so the array is not sorted.
            }
            
            // If we get to this point, each element is <= the following element,
            // so we know that the array is sorted into increasing order.
            
            return true;
         
         }

Question 3: Given a two-dimensional array  int[][] A = new int[50][100]  write a Java code segment that will add up all the numbers in the array.

Answer:

         int sum = 0;   // the sum of all the elements of the array
         
         for (int row = 0; row < 50; row++)
            for (int col = 0; col < 100; col++)
               sum = sum + A[row][col];

Question 4: Suppose that a two-dimensional array of double is created with the statement

double[][] sales = new double[100][365];

This array is used to hold data about sales figures at 100 stores on each of the 365 days of 2005. Suppose that the array has already been filled with data and that the value in sales[s][d] is the amount of sales at store number s on day number d. Write a Java code statement that will print out the total sales for each store over the course of the entire year. The output should consist of lines of the form:  Sales in 2005 for store #17: $178332.31

Answer:

         for (int s = 0; s < 100; s++) {
            
            double total = 0;  // total sales for store number s
            
            for (int d = 0; d < 365; d++)
               total = total + sales[s][d];
               
            System.out.println("Sales in 2005 for store #" + s + ":  $" + total);

         }

David Eck, 24 April 2006