Part a
(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.
public static int arraySum(int [] arr){
int sum = 0;
for (int num: arr) {
sum += num;
}
return sum;
}
public class partA {
public static void main(String[] args) {
int[] mat1 = {1,3,2,7,3};
int sum = arraySum(mat1);
System.out.println(sum);
}
}
partA.main(null);
16
Part b
(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.
public static int[] rowSums(int[][] mat1) {
int[] values = new int[mat1.length];
for (int k = 0; k < values.length; k++) {
values[k] = arraySum(mat1[k]);
}
return values;
}
public class partB {
public static void main(String[] args) {
int[][] mat1 = {
{1, 3, 2, 7, 3},
{10, 10, 4, 6, 2},
{5, 3, 5, 9, 6},
{7, 6, 4, 2, 1}
};
int[] sums = rowSums(mat1);
for(int i = 0; i < sums.length; i++){
System.out.println("Row " + i + " sum: " + sums[i]);
}
}
}
partB.main(null)
Row 0 sum: 16
Row 1 sum: 32
Row 2 sum: 28
Row 3 sum: 20
Part 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.
public static boolean isDiverse(int[][] arr2D) {
int[] values = rowSums(arr2D);
for(int k = 0; k < values.length; k++){
for(int i = k + 1; i < values.length; i++)
if(values[k] == values[i]) {
return false;
}
}
return true;
}
public class partC {
public static void main(String[][] args) {
int[][] mat1 = {
{1, 3, 2, 7, 3},
{10, 10, 4, 6, 2},
{5, 3, 5, 9, 6},
{7, 6, 4, 2, 1}
};
int[][] mat2 = {
{1, 1, 5, 3, 4},
{12, 7, 6, 1, 9},
{8, 11, 10, 2, 5},
{3, 2, 3, 0, 6}
};
System.out.println("mat1 is " + isDiverse(mat1));
System.out.println("mat2 is " + isDiverse(mat2));
}
}
partC.main(null)
mat1 is true
mat2 is false
Summary of FRQ 1
This is a ArrayList question that asks you to manipulate 2D Arrays and 1D Arrays and asks you to find the sum of each row within the 2D Array. This is specifically asking about Diverse Array which is seen in part c. The key algorithms in part a asks us to calculate the sum of that specific row. The key algorithms in part b asks us to calculate the sums of each row in the whole array list. The key algorithms in part c asks us to see if the arrays are . diverse or not. These all match the FRQ type of Arrays because it includes different methods on how to manipulate arrays which is what the focus of this FRQ is about.
Stats: Time: 23 minutes
Date Completed: February 24th, 2023
CollegeBoard Scoring: 9/9
Personal Ranking: 1
Scoring
This question was worth 9 points
part a:
- +1 for Accesses all elements of
arr
- +1 for Initializes, computes, and returns sum of elements
part b:
- +1 for Constructs correctly-sized 1D array of ints
- +1 for Accesses all words in
arr2D
- +1 for Computes sum of row in
arr2D
usingarraySum
and assigns to element in 1D Array - +1 for Returns 1D Array where kth element is computed sum of corresponding row in 2D Array for all rows
part c:
- +1 for Computes and uses array of row sums from arr2D using
rowSums
- +1 for Compare all and only pairs of row sums for equality
- +1 for Returns
true
if all compared row sums are different andfalse
otherwise
question specific penalties:
- -0 for uses
getLength/getSize
for array size - -0 for Destruction of persistent data
(arr or arr2D)