Home 2015 CSA FRQ Q1
Post
Cancel

2015 CSA FRQ Q1

Question 1

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Notes:

  • Assume that the classes listed in the Java Quick Reference have been imported where appropriate.

  • Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied.

  • In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.

This question involves reasoning about one-dimensional and two-dimensional arrays of integers. You will write three static methods, all of which are in a single enclosing class, named DiverseArray (not shown). The first method returns the sum of the values of a one-dimensional array; the second method returns an array that represents the sums of the rows of a two-dimensional array; and the third method analyzes row sums.

(a) Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.

A table is shown with two columns, arr 1 on the left and Value returned by arraySum open parentheses arr 1 close parentheses. In the left column an array is shown. It is a horizontal row of 5 boxes with the numbers 0 through 4 above the boxes from left to right. The boxes read 1, 3, 2, 7, and 3. In the right column is the value 16.

Complete method arraySum below.

/ * * Returns the sum of the entries in the one-dimensional array arr.

  • /

public static int arraySum (int [ ] arr)

public static int arraySum (int[] arr) {
    int sum = 0;
    for (int i = 0; i < arr.length; i++) {
        sum += arr[i];
    }
    return sum;
}

/* explanation:
 * 
 * Pretty simple, here we just iterate through the array and add each element to the sum, returning the final sum at the end.
 * 
 */

int[] array = new int[]{1, 3, 2, 7, 3};
arraySum(array); // should return 16
1
16

(b) Write a static method rowSums that calculates the sums of each of the rows in a given two-dimensional array and returns these sums in a one-dimensional array. The method has one parameter, a two-dimensional array arr2D of int values. The array is in row-major order: arr2D [ r ] [ c ] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.

For example, if mat1 is the array represented by the following table, the call rowSums(mat1) returns the array {16, 32, 28, 20}.

A table titled mat1 is shown with five columns and four rows. From the upper left the columns are labeled 0 through 4 and the rows 0 through 3. The first row reads 1, 3, 2, 7, 3. The second row reads 10, 10, 4, 6, 2. The third row reads 5, 3, 5, 9, 6. The fourth row reads 7, 6, 4, 2, 1.

A box with text at the top that reads, Methods written in this question. A 3-line code segment reads as follows. Line 1: public static int array Sum, open parenthesis, int, open square bracket, close square bracket, arr, close parenthesis. Line 2: public static int, open square bracket, close square bracket, row Sums, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, close parenthesis. Line 3: public static boolean is Diverse, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, open parenthesis.

Assume that arraySum works as specified, regardless of what you wrote in part (a). You must use arraySum appropriately to receive full credit.

Complete method rowSums below.

/ * * Returns a one-dimensional array in which the entry at index k is the sum of

  • the entries of row k of the two-dimensional array arr2D.

  • /

public static int [ ] rowSums(int [ ] [ ] arr2D)

public static int [ ] rowSums(int [ ] [ ] arr2D) {
    int[] res = new int[arr2D.length];
    for (int i = 0; i < arr2D.length; i++) {
        res[i] = arraySum(arr2D[i]);
    }
    return res;
}

/* explanation:
 * 
 * Going through each element of the 2D array, we call the arraySum method on each row and store sum the results array,
 * giving us the resultant sums we are looking for.
 * 
 */

// TESTING SECTIONS REMOVED FOR LIQUID SYNTAX COMPLIANCE, OUTPUT STILL SHOWN

1
2
3
4
16
32
28
20

(c) A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum.

A table titled mat1 is shown with five columns and four rows. From the upper left the columns are labeled 0 through 4 and the rows 0 through 3. The first row reads 1, 3, 2, 7, 3. The second row reads 10, 10, 4, 6, 2. The third row reads 5, 3, 5, 9, 6. The fourth row reads 7, 6, 4, 2, 1. To the right of the table is a column with the title Row sums, with an entry lined up with each row of the table. The entries in the column are 16, 32, 28, and 20. Below the first table, table titled mat1 is shown with five columns and four rows. From the upper left the columns are labeled 0 through 4 and the rows 0 through 3. The first row reads 1, 1, 5, 3, 4. The second row reads 12, 7, 6, 1, 9. The third row reads 8, 11, 10, 2, 5 The fourth row reads 3, 2, 3, 0, 6. To the right of the table is a column with the title Row sums, with an entry lined up with each row of the table. The entries in the column are 14, 35, 36, and 14. Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse (mat1) returns true and the call isDiverse(mat2) returns false.

A box with text at the top that reads, Methods written in this question. A 3-line code segment reads as follows. Line 1: public static int array Sum, open parenthesis, int, open square bracket, close square bracket, arr, close parenthesis. Line 2: public static int, open square bracket, close square bracket, row Sums, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, close parenthesis. Line 3: public static boolean is Diverse, open parenthesis, int, open square bracket, close square bracket, open square bracket, close square bracket, arr 2 D, open parenthesis.

Assume that arraySum and rowSums work as specified, regardless of what you wrote in parts (a) and(b). You must use rowSums appropriately to receive full credit. Complete method isDiverse below.

/ * * Returns true if all rows in arr2D have different row sums;

  • false otherwise.

  • /public static boolean isDiverse(int [ ] [ ] arr2D)

public static boolean isDiverse(int [ ] [ ] arr2D) {
    int[] res = rowSums(arr2D);
    for (int i = 0; i < res.length; i++) {
        for (int j = i + 1; j < res.length; j++) {
            if (res[i] == res[j]) {
                return false;
            }
        }
    }
    return true;
}

/* explanation:
 * 
 * We can get all the resultant sums from our previous method, and from there, figure out if there are any duplicates.
 * 
 */

// TESTING SECTIONS REMOVED FOR LIQUID SYNTAX COMPLIANCE, OUTPUT STILL SHOWN
1
2
true
false
This post is licensed under CC BY 4.0 by the author.