a) Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array
public static int arraySum(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
(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
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;
}
(c) Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse
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;
}
Explanation of Each Method
Part (a) - arraySum Method
- Purpose: This method calculates and returns the sum of the elements in a one-dimensional array.
- Algorithm: It uses a simple for loop to iterate through each element of the array and adds it to the sum variable.
Example:
- Input: [1, 3, 2, 7, 3]
- Output: 16
Part (b) - rowSums Method
- Purpose: This method calculates the sums of each row in a two-dimensional array and returns these sums in a one-dimensional array.
- Algorithm: It utilizes the arraySum method from part (a) to calculate the sum of each row.
Example:
- Input: [[1, 3, 2, 7, 3], [10, 10, 4, 6, 2], [5, 3, 5, 9, 6], [7, 6, 4, 2, 1]]
- Output: [16, 32, 28, 20]
Part (c) - isDiverse Method
- Purpose: This method checks if a two-dimensional array is diverse, meaning no two rows have the same sum.
- Algorithm: It first calculates the row sums using the rowSums method and then checks for any duplicate sums using nested loops.
Example:
- Input (Diverse): [[1, 3, 2, 7, 3], [10, 10, 4, 6, 2], [5, 3, 5, 9, 6], [7, 6, 4, 2, 1]]
- Output: True
- Input (Not Diverse): [[1, 1, 5, 3, 4], [12, 7, 6, 1, 9], [8, 11, 10, 2, 5], [3, 2, 3, 0, 6]]
- Output: False
Blog: Key Algorithms and Matching FRQ Types
Array/ArrayList:
arraySum and rowSums both deal with arrays, one-dimensional and two-dimensional respectively. The isDiverse method works with arrays of arrays, showcasing handling of 2D arrays.
2D Array:
The rowSums and isDiverse methods both focus on operations with 2D arrays, calculating row sums and checking for diversity. In this FRQ set, we’ve explored Java methods and control structures, used arrays extensively, and dealt with the organization of methods within a class. These exercises offer valuable practice in handling arrays and creating effective algorithms, aligning well with the specified FRQ types.
Putting all of the Methods together with User Input
import java.util.Scanner;
public class DiverseArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Taking input for the 2D array
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int cols = scanner.nextInt();
int[][] arr2D = new int[rows][cols];
System.out.println("Enter the elements of the 2D array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter element at position (" + i + ", " + j + "): ");
arr2D[i][j] = scanner.nextInt();
}
}
// Displaying the input grid
System.out.println("\nInput Grid:");
displayGrid(arr2D);
// Calculating row sums
int[] sums = rowSums(arr2D);
// Checking if the array is diverse
boolean diverse = isDiverse(arr2D);
// Displaying results
System.out.println("\nRow Sums:");
for (int i = 0; i < rows; i++) {
System.out.print("Sum of row " + i + ": " + sums[i] + "\n");
}
System.out.println("\nIs the array diverse? " + diverse);
scanner.close();
}
// Method to calculate sum of elements in a 1D array
public static int arraySum(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
// Method to calculate sums of rows in a 2D array
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;
}
// Method to check if a 2D array is diverse
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;
}
// Method to display the 2D array as a grid
public static void displayGrid(int[][] arr2D) {
for (int[] row : arr2D) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}
DiverseArray.main(null)
Enter the number of rows: Enter the number of columns: Enter the elements of the 2D array:
Enter element at position (0, 0): Enter element at position (0, 1): Enter element at position (0, 2): Enter element at position (1, 0): Enter element at position (1, 1): Enter element at position (1, 2): Enter element at position (2, 0): Enter element at position (2, 1): Enter element at position (2, 2): Enter element at position (3, 0): Enter element at position (3, 1): Enter element at position (3, 2):
Input Grid:
1 2 3
4 5 6
12 11 9
8 3 2
Row Sums:
Sum of row 0: 6
Sum of row 1: 15
Sum of row 2: 32
Sum of row 3: 13
Is the array diverse? true
Explanation of Code
- The main method now utilizes a Scanner object to take input from the user for the 2D array.
- It prompts the user to enter the number of rows and columns, then takes in the elements of the 2D array.
- The rowSums and isDiverse methods remain the same, performing the calculations on the input array.
- After taking input, it calculates the row sums and checks if the array is diverse.
- Finally, it displays the row sums and whether the array is diverse based on the user input.