Question 1:
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
Part 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.
Part 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 t 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. For example, if mat1 is the array represented by the following table, the call rowSums(mat1) returns the array {16, 32, 28, 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.
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.
Question Type:
- 2D arrays
Overall Objective:
Create a DiverseArray class that has all of the following:
- A static method
arraySum
that can calculate the sum of the entries and returns the values - A static method
rowSums
that calculates the sum of each row in a 2D array and then returns the sums as a 1D array - A static method
isDiverse
that determines if a 2D array is diverse (meaning it has different sums for each row) - Tester data that shows both of the possible outcomes
public class DiverseArray {
public static int arraySum(int[] arr){ // A (Create a static method `arraySum` that can calculate the sum of the entries and returns the values)
int sum = 0;
for (int num : arr ){
sum += num;
}
System.out.println(sum);
return sum;
}
public static int[] rowSums(int[][] arr2D) { // B (Create a static method `rowSums` that calculates the sum of each row in a 2D array and then returns the sums as a 1D array)
int[] rowSum = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++){
rowSum[i] = arraySum(arr2D[i]); // recalling arraySum to print sums by row
}
return rowSum;
}
public static boolean isDiverse(int[][] arr2D) { // C (Create a static method `isDiverse` that determines if a 2D array is diverse (meaning it has different sums for each row)
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]){ // iterates through each of the sums calculated and compares them with each other to determine if they are the same or different
return false;
}
}
}
return true;
}
public static void main(String[] args) { // Data for testing
int[][] arr1 = { // Diverse array (true)
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int[][] arr2 = { // Not a diverse array (false)
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{1, 2, 3, 4}
};
System.out.println("If the array is diverse: \n");
System.out.println(isDiverse(arr1) + "\n");
System.out.println("If the array isn't diverse: \n");
System.out.println(isDiverse(arr2));
}
}
DiverseArray.main(null);
If the array is diverse:
10
26
42
58
true
If the array isn't diverse:
10
26
42
10
false
Reflection:
- This frq was honestly pretty easy to do and in my opinion the easiest to understand, it was pretty straightforward and you just had to create a function that added up the sums of the array and then use that function in another function that takes all the rows and prints all the sums and then finally another function that iterates through it all one last time and makes sure that the array is diverse or not