Reflection and Solutions of the FRQ
public static int arraySum(int[] arr) {
int total = 0; // initialize total to 0
for(int num : arr) // loop through each number in the array
total += num; // add the number to the total
return total; // return the total
}
// Proof it works
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arraySum(arr));
15
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length]; // create an array to store row sums
for (int i = 0; i < arr2D.length; i++) { // iterate through each row
int rowSum = 0;
for (int j = 0; j < arr2D[i].length; j++) { // iterate through each element in the row
rowSum += arr2D[i][j]; // add the element to the row sum
}
sums[i] = rowSum; // store the sum of the current row in the sums array
}
return sums; // return the array of row sums
}
// Proof it works
int[][] arr2D = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[] sums = rowSums(arr2D);
System.out.println(Arrays.toString(sums));
[6, 15, 24]
public static boolean isDiverse(int [ ] [ ] arr2D) {
int[] sums = rowSums(arr2D); // get the row sums
for (int i = 0; i < sums.length; i++) { // iterate through each row sum
for (int j = i + 1; j < sums.length; j++) { // iterate through each subsequent row sum
if (sums[i] == sums[j]) { // if any two row sums are equal
return false; // the array is not diverse
}
}
}
return true; // if no two row sums are equal, the array is diverse
}
// Proof it works
int[][] arr2D = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(isDiverse(arr2D));
int[][] arr2D2 = {
{1, 2, 3},
{3, 2, 1},
{7, 8, 9}
};
System.out.println(isDiverse(arr2D2));
true
false