Instructions
-
(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.
-
(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}.
-
(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.
Reflection
- After Completing this frq it tested my understanding of the array parts in java which a key component I need to work on part a came easy to me but I needed some help on structure of syntax and formatting which is wheere I struggled. Part B was what I taught in trimester 1 on 2d arrays and traversing and accesing new elements and storing so there wasnt much trouble there. However I struggled on getting the Diverse array to work on part C becuase I was a bit confused by the instructions which left me with a lot of error testing to do. Overall advanced concepts within arrays is what I need to practice more on.
Part A
public class DiverseArray {
public static int arraySum(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
public static void main(String[] args) {
int[] arr1 = {1, 3, 2, 7, 3};
System.out.println("Sum of arr1: " + arraySum(arr1));
}
}
DiverseArray.main(null);
Sum of arr1: 16
Part B
public class DiverseArray {
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length]; // Create
for (int i = 0; i < arr2D.length; i++) { // Iterate
int rowSum = 0;
for (int j = 0; j < arr2D[i].length; j++) { // Iterate through each column in the current row
rowSum += arr2D[i][j]; // adds the net
}
sums[i] = rowSum;
}
return sums; // Return array of row sums
}
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[] rowSumsArray = rowSums(mat1);
// Print row sums
System.out.print("Row sums: {");
for (int i = 0; i < rowSumsArray.length; i++) {
System.out.print(rowSumsArray[i]);
if (i < rowSumsArray.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
}
DiverseArray.main(null)
Row sums: {16, 32, 28, 20}
Part C
import java.util.HashSet;
public class DiverseArray {
public static int arraySum(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
public static int[] rowSums(int[][] arr2D) {
int[] sums = new int[arr2D.length];
for (int i = 0; i < arr2D.length; i++) {
sums[i] = arraySum(arr2D[i]);
}
return sums;
}
public static boolean isDiverse(int[][] arr2D) {
HashSet<Integer> sumsSet = new HashSet<>();
int[] sums = rowSums(arr2D);
for (int sum : sums) {
if (sumsSet.contains(sum)) {
return false; // If a duplicate check
}
sumsSet.add(sum);
}
return true; // If all sums are unique, return true
}
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(isDiverse(mat1));
System.out.println(isDiverse(mat2));
}
}
DiverseArray.main(null)
true
false