Question 1

copied below

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

Assume that arraySum works as specified, regardless of what you wrote in part (a). You must use arraySum appropriately to receive full credit.

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

Assume that arraySum and rowSums work as specified, regardless of what you wrote in parts (a) and(b). You must use rowSums appropriately to receive full credit.

Type of FRQ

This FRQ seems to be testing concepts mostly from Array/ArrayLists and 2D Arrays. It includes knowing how to iterate through an array. We also need to understand how 2D arrays are initialized, and how to call specific values from them.

public class DiverseArray {

    //part a:

    // * * Returns the sum of the entries in the one-dimensional array arr.

    public static int arraySum(int[] arr) {
        int n = 0; // sum initialized as 0 
        for (int i : arr) { 
            n = n + i; // sum from each element added to n
        }
        
        return n;
    }

    //part b 

    // * * Returns a one-dimensional array in which the entry at index k is the sum of
    //* the entries of row k of the two-dimensional array arr2D.

    public static int[] rowSums(int[][] arr2D) {

        int[] sumsArr = new int[arr2D.length]; // new array to store sums
    
        for (int i = 0; i < arr2D.length; i++) {
            sumsArr[i] = arraySum(arr2D[i]); // inserting correct sum into correct index; calling arraySum!! 
        }

        return sumsArr; //return array 


    }


    //part c 


    // * * Returns true if all rows in arr2D have different row sums;
    // * false otherwise.

    public static boolean isDiverse(int[][] arr2D) {

        int[] newSumsArr = rowSums(arr2D);

        for (int i = 0; i < newSumsArr.length; i++) { // 2D iteration 
            for (int j = i + 1; j < newSumsArr.length; j++) {
                if (newSumsArr[i] == newSumsArr[j]) {
                    return false; 
                }
            }
        }

        return true;
        
    }



    public static void main(String[] args) {
        int[] inptArr_a = {1, 3, 2, 7, 3};
        int[][] inptArr_b = {
            {1, 3, 2, 7, 3},
            {10, 10, 4, 6, 2},
            {5, 3, 5, 9, 6},
            {7, 6, 4, 2, 1}
        };
        int[][] inptArr_c = {
            {1, 1, 5, 3, 4},
            {12, 7, 6, 1, 9},
            {8, 11, 10, 2, 5},
            {3, 2, 3, 0, 6}
        };
        
        System.out.println("Part A - The sum of the array is: ");
        int A = arraySum(inptArr_a);
        System.out.println(A);

        System.out.println("Part B - The sum of each row is: ");
        int[] B = rowSums(inptArr_b);
        System.out.println(Arrays.toString(B));

        System.out.println("Part C: ");
        System.out.println("mat1 is diverse: " + isDiverse(inptArr_b));
        System.out.println("mat2 is diverse: " + isDiverse(inptArr_c));


    }

}


DiverseArray.main(null);
Part A - The sum of the array is: 
16
Part B - The sum of each row is: 
[16, 32, 28, 20]
Part C: 
mat1 is diverse: true
mat2 is diverse: false

How I solved each problem:

a) This one was pretty simple, I just initialized the sum of the row as 0, and iterated through each element of the row, adding that value to the sum. Then, return the sum so it can be outputted in the main method

b) A new arrayList is initialized to store the sum of each row. The arraySum method is then called for each row (by doing arraySum(arr2D[i]) to find the sum itself). Iteration is used for this step as well. The array is then returned. Note that the array had to be turned into a string to be printed. It was obvious that the array wasn’t printing in this notebook, but that is something I will have to remember doing the actual FRQ, when I will not have access to running code.

c) This part of the problem included nested iteration for comparing different elements in the same list, which I have seem come up multiple times on CS type problems, so I should defintely make sure I know how to do that.

Reflection

One thing to review: make sure I know how to initialize a 2D array. I had to look that up. To create a 2D array, the rows are in curly brackets, seperated by commas. The entire array then also goes into curly brackets.

Was also having some trouble with remembering to add returns to each method, and how to print outputs in main method. However, I figured it out without having to look anything up, which I think means I can figure it out during a test setting as well if I end up struggling.