Home 2015 FRQ 1
Post
Cancel

2015 FRQ 1

    • You will write three static methods, all of which are in a single enclosing class, named DiverseArray.
    • 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
    • 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//create an array of integers
int[] arr1 = {1, 3, 2, 7, 3};

public static int arraySum (int [ ] arr){
    //calculate sum of array
    int sum = 0;
    for (int i : arr) {
        sum += i;
    }

    //return sum of array
    return sum;
}

arraySum(arr1)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//initial 2D array for my own testing
int[][] mat1 = { { 1, 3, 2, 8, 3 }, { 10, 10, 4, 6, 2 }, { 5, 3, 5, 9, 6 }, { 7, 6, 4, 2, 1 }};

public static int[] rowSums (int [][] arr2D) {
    //create initial array 
    int [] arrSums = new int[arr2D.length];
    
    //calculate sum of each row in 2d array
    int rowNum = 0;
    for (int[] row : arr2D) {
        //call part a function to calculate sum of each row (1D arrays)
        arrSums[rowNum] = arraySum(row);
        //System.out.println(Arrays.toString(arrSums));
        rowNum++;
    }

    //return new array of row sums
    return arrSums;
}

int[] rowSumsResult = rowSums(mat1);
System.out.println("Row Sums: " + Arrays.toString(rowSumsResult));
1
Row Sums: [17, 32, 28, 20]

(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
public static boolean isDiverse(int [][] arr2D) {
    //call function in part b
    int[] rowSumsResult = rowSums(mat1);
    
    //iterate through for each row sum
    for (int i = 0; i < rowSumsResult.length; i++) {
        //iterate through array for each sum but to compare this time. starts at the second item in the list
        for (int j = i + 1 ; j < rowSumsResult.length; j++) {
             if (rowSumsResult[i] == rowSumsResult[j]) {
                return false;
             }
        }
    }
    return true;
}

isDiverse(mat1);

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

Tri 2 N@TM

2015 FRQ 2