Question 1

(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.

Reflection

This FRQ was pretty easy to me as I was able to breeze through as this was more mased on initizalising classes and working with arrays, which is one of my strengths in this class. I have worked with arrays and taught multiple lesson on arrays in both CSA trimesters.

public class ArrayUtils {

    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, 2, 3, 4, 5};
        int sum = arraySum(arr1);
        System.out.println("Sum of the array elements: " + sum);
    }
}

ArrayUtils.main(null)
Sum of the array elements: 15

Explanation:

When you call arraySum(arr1), passing the array arr1, it will return the sum of all elements in that array.

(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 ArrayUtils {

    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 void main(String[] args) {
        int[][] mat1 = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
        };
        int[] rowSumsArray = rowSums(mat1);
        System.out.print("Row sums: ");
        for (int sum : rowSumsArray) {
            System.out.print(sum + " ");
        }
        System.out.println();
    }
}

ArrayUtils.main(null)

Row sums: 10 26 42 58 

Explanation:

rowSums method takes a 2D array arr2D as input and returns a 1D array containing the sums of each row. Inside rowSums, we iterate through each row of the 2D array and calculate the sum of elements in that row using the arraySum method (which we defined earlier). The sums are stored in the sums array, which is returned at the end. In the main method, we create an example 2D array mat1, call rowSums with it, and print out the resulting array.

(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.

public class ArrayUtils {

    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) {
        int[] sums = rowSums(arr2D);
        for (int i = 0; i < sums.length - 1; i++) {
            for (int j = i + 1; j < sums.length; j++) {
                if (sums[i] == sums[j]) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[][] mat1 = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 16}
        };

        int[][] mat2 = {
            {1, 2, 15, 16},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {3, 4, 14, 13}
        };

        System.out.println("Is mat1 diverse? " + isDiverse(mat1)); 
        System.out.println("Is mat2 diverse? " + isDiverse(mat2)); 
    }
}

ArrayUtils.main(null)

Is mat1 diverse? true
Is mat2 diverse? false

Explanation:

In the isDiverse method, we first calculate the row sums using the rowSums method. Then, we compare each pair of sums. If we find any two sums that are equal, we return false indicating that the array is not diverse. If we finish the comparisons without finding any equal sums, we return true, indicating that the array is diverse. In the main method, we test the isDiverse method with two example arrays mat1 and mat2, printing out whether they are diverse or not.