BobTheFarmer / Colin-Blog3

MIT License
0 stars 2 forks source link

FRQ 1 #19

Open BobTheFarmer opened 6 months ago

BobTheFarmer commented 6 months ago

This broke the notebook so I had to put it here. I showed Rachit the working ipynb file but it breaks the blog when pushed.

Question 1

Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.

Notes:

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.

Complete method arraySum below.

public static int arraySum(int[] arr) {[^17^][17][^18^][18]
    int sum = 0;
    for (int num : arr) {
        sum += num;
    }
    return sum;
}
public class DiverseArray1 {
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int num : arr) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] testArray = {1, 2, 3, 4, 5};
        System.out.println(arraySum(testArray)); // It should print 15
    }
}

DiverseArray1.main(new String[]{});

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

Complete method rowSums below.

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 class DiverseArray2 {
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i : arr) {
            sum += i;
        }
        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[][] arr2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int[] sums = rowSums(arr2D);
        for (int sum : sums) {
            System.out.println(sum);
        }
    }
}

DiverseArray2.main(new String[]{});

(c) Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse.

Complete method isDiverse below.

public static boolean isDiverse(int[][] arr2D) {
    int[] rowSums = rowSums(arr2D);
    for (int i = 0; i < rowSums.length - 1; i++) {
        for (int j = i + 1; j < rowSums.length; j++) {
            if (rowSums[i] == rowSums[j]) {
                return false;
            }
        }
    }
    return true;
}
public class DiverseArray {
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i : arr) {
            sum += i;
        }
        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[] rowSums = rowSums(arr2D);
        for (int i = 0; i < rowSums.length - 1; i++) {
            for (int j = i + 1; j < rowSums.length; j++) {
                if (rowSums[i] == rowSums[j]) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[][] arr2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(isDiverse(arr2D));
    }
}

DiverseArray.main(new String[]{});