CPSC 124, Fall 1998

Quiz Number 7

This is the seventh and final quiz given in CPSC 124: Introductory Programming, Fall 1998. 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. In some cases, I give more detailed answers here than would be necessary to get full credit.

Question 1: What does it mean to sort an array?

Answer: To sort an array means to rearrange the items in the array so that they are in increasing or decreasing order (according to some criterion).

Question 2: Write a subroutine search42. It should have a parameter named A of type int[], and it should return a value of type boolean. The subroutine should search for the number 42 in the array. If 42 occurs in the array A, the subroutine should return the answer true. If not, it should return false.

Answer: The routine can look at each space in the array. If it finds a 42, it returns true immediately. If it looks at every space in the array and does not find 42, it returns false:

                static boolean search42(int[] A) {
                   for (int i = 0; i < A.length; i++) {
                      if (A[i] == 42)
                         return true; // We've found 42; return true.
                   // If we reach this point, we've looked at every 
                   // item in the array without finding 42, so return
                   // false as the answer.
                   return false;

Question 3: Suppose that data is a two-dimensional array of double that has been created with the statement

               double[][]  data  =  new  double[5][7];

Write a code segment that computes the sum of all the numbers in data.

Answer: Use nested for loops to add all the numbers in the array together in a variable, sum:

                 double sum = 0.0;
                 for (int row = 0; row < 5; row++) {
                    for (int column = 0; column < 7; column++)
                       sum = sum + data[row][column];
                 // At this point, the variable sum contains the
                 // sum of all the numbers in the array.

Question 4: Suppose that temperature measurements were made on each day of 1997 in each of 100 cities. The measurements have been stored in an array

                int[][]  temps  =  new  int[100][365];

where temps[c][d] holds the measurement for city number c on the dth day of the year. Write a code segment that will print out the average temperature, over the course of the whole year, for each city. The average temperature for a city can be obtained by adding up all 365 measurements for that city and dividing the answer by 365.0.

Answer: A pseudocode outline of the answer is

                For each city {
                   Add up all the temperatures for that city
                   Divide the total by 365 and print the answer

Adding up all the temperatures for a given city itself requires a for loop, so the code segment looks like this:

                for (int city = 0; city < 0; city++) {
                    int total = 0;  // total of temperatures for this city
                    for (int day = 0; day < 365; day++)
                       total = total + temps[city][day];
                    double avg = total / 365.0;  // average temp for this city
                    System.out.println("Average temp for city number " 
                             + city + " is " + avg);

David Eck, 11 November 1998