FRQ QUESTION 1

public static int arraySum(int[] arr) {
    int sum = 0;
    for (int num : arr) {
        sum += num;
    }
    return sum; 
}
public class partA {
    public static void main(String[] args) {
        int[] mat1 = {1,3,2,7,3};
        
        int sum = arraySum(mat1);
        System.out.println(sum);
    }
}
partA.main(null);

image

  • The code defines a method arraySum that takes an integer array as input and calculates the sum of its elements. Then, in the partA class’s main method, an array mat1 is created and its sum is calculated using the arraySum method. The result is printed to the console.
public static int[] rowSums(int[][] arr2D) {
    int numRows = arr2D.length;
    int[] sums = new int[numRows];

    for (int i = 0; i < numRows; i++) {
        sums[i] = arraySum(arr2D[i]);
    }

    return sums;
}
public class partB {
    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[] sums = rowSums(mat1);

        for(int i = 0; i < sums.length; i++){
            System.out.println("Row " + i + " sum: " + sums[i]);
        }
    }
}
partB.main(null)

image

  • The code defines a method rowSums that takes a 2D array of integers (arr2D) and calculates the sum of each row using the previously defined arraySum method. Then, in the partB class’s main method, a 2D array mat1 is created, and the sums of each row are calculated using the rowSums method. The results are printed in the console.
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 class partC {
    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("mat1 is " + isDiverse(mat1));
        System.out.println("mat2 is " + isDiverse(mat2));
    }
}
partC.main(null)

image

  • The code defines a method isDiverse that takes a 2D array of integers (arr2D) and checks if the row sums are diverse, meaning that no two rows have the same sum. In the partC class’s main method, two 2D arrays (mat1 and mat2) are created, and the isDiverse method is called to determine if the row sums are diverse. The results are printed.

  • This question was related to arraylists and was intresting to figure out, asking us to calculate the sums of each row in the whole array list. The key algorithms in part c is about seeing if the arrays are diverse or not.