Question 1

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

(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 class Test {
    public static int arraySum(int[] array) {
        int sum = 0;
        for (int i=0; i<array.length; i++){//iterates through array and returns sum of all numbers
            sum+=array[i];
        }
        return sum;
    }
    
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println("The sum of the array is: " + arraySum(array));
    }
}
Test.main(null);
The sum of the array is: 15

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

public class Test {
    public static int arraySum(int[] array) {
        int sum = 0;
        for (int i=0; i<array.length; i++){//iterates through array and returns sum of all numbers
            sum+=array[i];
        }
        return sum;
    }

    public static int[] rowSums(int[][] arr2D){
        int[] sum = new int[arr2D[0].length];
        for (int i=0; i<arr2D.length; i++) {//iterates through each row of the 2d array and calls the arraySum method to sum the row, then adds the sum to a new array
            int rowSum = arraySum(arr2D[i]);
            sum[i] = rowSum;
        }
        return sum;
    }

    public static void main(String[] args) {
        int[][] Array1 = {
            {1, 2, 3},
            {4, 5, 6},
            {1, 2, 3}
        };
        int[] rowsums = rowSums(Array1);
        for (int i=0; i<rowsums.length; i++) {
            System.out.println("The sum of row number " + (i+1) + " is " + rowsums[i]);
        }
    }
}
Test.main(null);
The sum of row number 1 is 6
The sum of row number 2 is 15
The sum of row number 3 is 6

(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 Test {
    public static int arraySum(int[] array) {
        int sum = 0;
        for (int i=0; i<array.length; i++){//iterates through array and returns sum of all numbers
            sum+=array[i];
        }
        return sum;
    }

    public static int[] rowSums(int[][] arr2D){
        int[] sum = new int[arr2D[0].length];
        for (int i=0; i<arr2D.length; i++) {//iterates through each row of the 2d array and calls the arraySum method to sum the row, then adds the sum to a new array
            int rowSum = arraySum(arr2D[i]);
            sum[i] = rowSum;
        }
        return sum;
    }

    public static boolean isDiverse(int[][] arr2D) {
        int[] newArray = rowSums(arr2D);
        boolean diverse = true;
        for (int i=0; i<newArray.length; i++) {
            for (int j=0; j<newArray.length; j++) {
                if(i!=j && newArray[i]==newArray[j]) {// checks if any 2 sums in the array of rowSums are the same
                    diverse = false;
                }
            }
        }
        return diverse;
    }

    public static void main(String[] args) {
        int[][] Array1 = {
            {1, 2, 3},
            {4, 5, 6},
            {1, 2, 3}
        };
        int[][] Array2 = {
            {1,2,3},
            {12,9,6},
            {4,5,6}
        };
        System.out.println("This array is diverse: " + isDiverse(Array1));
        System.out.println("This array is diverse: " + isDiverse(Array2));
    }
}
Test.main(null);
This array is diverse: false
This array is diverse: true
  • FRQ Type: 2D Array
  • How main algorithm shows FRQ type:

This FRQ involves using and manipulating 2D arrays, and even 1D arrays. In the FRQ, we must use for loops (and sometimes even nested for loops) to iterate through the arrays to add the items together and determine other types of information about the arrays and 2D arrays. In particular, part c, in which we have to make a method to determine if a 2D array is diverse or not, deals with both iteration through the 2d array, and adding items inside each item of the 2d array. Manipulating 2D Arrays is an important skill in this class and beyond.