Home 2015 FRQ Question 1
Post
Cancel

2015 FRQ Question 1

2015 Collegeboard FRQ

Question 1

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class q1{
    public static int arraySum (int[] arr){
        int sum = 0;
        for (int n : arr){
            sum += n;
        }
        return sum;
    }

    //test
    public static void main(String[] args){
        int[] arr = {1, 1, 2, 3, 5};
        System.out.print(q1.arraySum(arr));
    }
}
q1.main(null);
1
12

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class q1{
    public static int arraySum (int[] arr){
        int sum = 0;
        for (int n : arr){
            sum += n;
        }
        return sum;
    }
    
    public static int [] rowSums(int[][] arr2D){
        int[] sums = new int[arr2D.length];
        for (int i = 0; i < arr2D.length; i++) { 
            sums[i] = arraySum(arr2D[i]);
        }
        return sums;
    }

    // test
    public static void main(String[] args){
        int[][] mat1 = {
            {1,2,4},
            {5,8,1},
            {3,7,9}
        };

        int[] matSum = q1.rowSums(mat1);
        for (int r : matSum){
            System.out.print(r + " ");
        }
    }
}   
q1.main(null);

1
7 14 19 

c.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class q1 {
    public static int arraySum (int[] arr){
        int sum = 0;
        for (int n : arr){
            sum += n;
        }
        return sum;
    }

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

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

    //test
    public static void main(String[] args){
        int[][] mat1 = {
            {1,2,5},
            {5,8,1},
            {3,7,9}
        }; // true 8 14 19

        int[][] mat2 = {
            {1,2,0},
            {1,1,1},
            {3,7,1}
        }; // false 3 3 11

        int[][] mat3 = {
            {1,1,5},
            {4,2,3},
            {3,2,2}
        }; // false 7 9 7

        System.out.println("Mat1: "+ isDiverse(mat1));
        System.out.println("Mat2: "+ isDiverse(mat2));
        System.out.println("Mat3: "+ isDiverse(mat3));
    }
}
q1.main(null);
1
2
3
Mat1: true
Mat2: false
Mat3: false

Blog

The specific type of FRQ 1 is 2D Array and Array/Array List. In this FRQ we use static methods to manipluate both 1D and 2D arrays In part a, we created the arraySum method to sum 1D array elements. This method would be used through the rest of the problem in parts b and c. In part b, we specifically expanded on part a by summing rows in a 2D array with method rowSums. I used for loops and the previous arraySum to do this. Part c used both parts a and b, along with nested for loops, to check the uniqueness of the sums for each of the rows to determine array diversity.

This post is licensed under CC BY 4.0 by the author.