a) 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 sum
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 class FRQ1{
public static int arraySum(int[] arr1){
int total = 0;
for(int i : arr1){
total += i;
}
return total;
}
public static void main(String[] args){
// testing arraySum
int[] arr1 = {1,3,2,7,3};
System.out.print("Sum of arr1: ");
System.out.print(FRQ1.arraySum(arr1));
}
}
FRQ1.main(null);
Sum of arr1: 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
. 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}.
public class FRQ1{
public static int arraySum(int[] arr1){
int total = 0;
for(int i : arr1){
total += i;
}
return total;
}
public static int[] rowSums(int[][] arr2D){
int[] rows = new int[arr2D.length];
for(int i = 0; i < arr2D.length; i++){
rows[i] = arraySum(arr2D[i]);
}
return rows;
}
public static void main(String[] args){
// testing rowSums
int[][] mat1 = {
{1,3,2,7,3},
{10,10,4,6,2},
{5,3,5,9,6},
{7,6,4,2,1}
};
int[] mat1Sums = FRQ1.rowSums(mat1);
for(int matRows : mat1Sums){
System.out.print(matRows + " ");
}
}
}
FRQ1.main(null);
16 32 28 20
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
.
public class FRQ1{
public static int arraySum(int[] arr1){
int total = 0;
for(int i : arr1){
total += i;
}
return total;
}
public static int[] remove(int[] array, int index){
int[] narray = new int[array.length-1];
for(int i = 0, k=0; i<array.length; i++){
if(i == index){
continue;
}
narray[k++] = array[i];
}
return narray;
}
public static int[] rowSums(int[][] arr2D){
int[] rows = new int[arr2D.length];
for(int i = 0; i < arr2D.length; i++){
rows[i] = arraySum(arr2D[i]);
}
return rows;
}
public static boolean isDiverse(int[][] arr2D){
int[] sums = rowSums(arr2D);
for(int i=0; i < sums.length; i++){
int[] nar = FRQ1.remove(sums, i);
for (int j=0; j<nar.length; j++){
if(sums[i] == nar[j]){
return false;
}
}
}
return true;
}
public static void main(String[] args){
// testing rowSums
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 Diverse? " + isDiverse(mat1));
System.out.println("Mat1 Diverse? " + isDiverse(mat2));
}
}
FRQ1.main(null);
Mat1 Diverse? true
Mat1 Diverse? false