CollegeBoard FRQ
This shows an FRQ for Collegeboard focused on iteration and arrays
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.
In part (a), the instructions are to Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array.
public static int arraySum (int[] arr1) {
int sum = 0;
for (int i = 0; i<arr1.length; i++) {
sum += arr1[i];
}
return sum;
}
Here we can test the method with the example array which is [1, 3, 2, 7, 3], which should have a sum of 16.
int[] arrayTest = {1, 3, 2, 7, 3};
System.out.println(arraySum(arrayTest));
Part (b) asks to 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.
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length]; //elements initialize to 0
for (int i = 0; i < arr2D.length; i++) {
for (int j = 0; j < arr2D[i].length; j++) {
sums[i] += arr2D[i][j];
}
}
return sums;
}
Now we can test with the example:
1 3 2 7 3
10 10 4 6 2
5 3 5 9 6
7 6 4 2 1
The expected output here is {16, 32, 28, 20}.
int[][] arrayTest2D = {
{1, 3, 2, 7, 3},
{10, 10, 4, 6, 2},
{5, 3, 5, 9, 6},
{7, 6, 4, 2, 1}
};
int[] sums = rowSums(arrayTest2D);
for (int i = 0; i<sums.length; i++) {
System.out.println(sums[i]);
}
Part (c) asks us to 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
public static boolean isDiverse (int[][] arr2D) {
int[] sums = rowSums(arr2D);
for (int i = 0; i < sums.length; i++) {
for (int j = i + 1; j < sums.length; j++) {
if (sums[i] == sums[j]) return false; // returns false if two elements are the same
}
}
//if end is reached all elements have passed uniqueness test
return true;
}
We can test with the two provided examples which are diverse and not diverse.
This example is diverse:
1 3 2 7 3
10 10 4 6 2
5 3 5 9 6
7 6 4 2 1
This example is not diverse:
1 1 5 3 4
12 7 6 1 9
8 11 10 2 5
3 2 3 0 6
int[][] arrayTestDiverse = {
{1, 3, 2, 7, 3},
{10, 10, 4, 6, 2},
{5, 3, 5, 9, 6},
{7, 6, 4, 2, 1}
};
int[][] arrayTestNotDiverse = {
{1, 1, 5, 3, 4},
{12, 7, 6, 1, 9},
{8, 11, 10, 2, 5},
{3, 2, 3, 0, 6}
};
System.out.println(isDiverse(arrayTestDiverse));
System.out.println(isDiverse(arrayTestNotDiverse));