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.

public static int arraySum(int[] arr){
    int total = 0;
    for(int number : arr){
        total += number; // for loop iterates through every number in array and adds them all up.
    }
    return total;
}
// tester code
int[] numbers = {1,3,2,7,3};   
int sum = arraySum(numbers);  
System.out.println(sum);
16

This part was fairly simple, as it just required basic recursion. I used a for each loop (also known as an enhanced for loop) to iterate through each number in any given array.

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 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 static int [] rowSums(int[][] arr2D){
    int[] rowSum = new int[arr2D.length];
    int i = 0;
    for(int[] arr:arr2D){
        rowSum[i] = arraySum(arr);
        i++;
    }
    return rowSum;
}
int [][] arrayThing = {
    {1,3,2,7,3},{10,10,4,6,2},{5,3,5,9,6},{7,6,4,2,1}
    };
System.out.println(Arrays.toString(rowSums(arrayThing)));
[16, 32, 28, 20]

This part expanded upon the last part, now being expanded to work with 2D Arrays. This one took me a bit longer, but if you think about it, it’s pretty much the same thing as the last problem, as instead of iterating through each individual number, you’re just iterating through the different arrays while using “arraySum()” as defined in part A in order to get the various sums. Then all I had to do is put the values into an array, which just needed me to create a new array, rowSum, and set the given index to the sum of the corresponding array.

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.

public static boolean isDiverse(int[][] arr2D){
    int[] sumRows = rowSums(arrayThing);
    for(int i = 0; i < sumRows.length; i++){
        for(int j = i+1; j < sumRows.length; j++){
            if (sumRows[i] == sumRows[j]){
                return false;
            }
        }
    }
    return true;
}
int [][] arrayThing = {
    {1,3,2,7,3},{10,10,4,6,2},{5,3,5,9,6},{7,6,4,2,1}
    }; // should return true, as all arrays have unique sequence of numbers.
System.out.println("Testing True Condition:");
System.out.println("------------------------");
System.out.println(isDiverse(arrayThing));

Testing True Condition:
------------------------
true

This part took me the longest, as it took me a while to figure out that I needed a nested for loop in order to check not only the given index, but the index next to it as well, in order to check whether the arrays are unique in their sequencing. I definitely need to work on my nested loop knowledge, as this took me a very long time, and I needed to review previous material in order to get this.