Question 1

Type: Array/2D Array

Blog This is the frq that I incorporated into our project.

Part A

Calculate Array Sum - Iterate through an array and sum its elements

Question 1A

    // adds everything in statsArray to calculate "power"
    public static int arraySum(int[][] statsArray) {
      int sum = 0;    // sum initializer
  
      // Nested loops to iterate over each element of the 2D array
      for (int[] row : statsArray) {
          for (int num : row) {
              sum += num;
              System.out.print(num + "\t");  // debug
          }
      }
  
      return sum;
  }
// Testing
int[] array = {1, 2, 3};
System.out.print("Original Array: ");
System.out.println(Arrays.toString(array));

System.out.print("Sum: ");
System.out.print(arraySum(array)); // should return 6
Original Array: [1, 2, 3]
Sum: 6

Part B

Check Array Diversity - Use the array sum to determine if arrays are diverse by comparing their sums.

Question 1B

public static int[] rowSums(int[][] statsArray)
{
   int[] sums = new int[statsArray.length];

   for(int i = 0; i < sums.length; i++) {
      sums[i] = arraySum(statsArray[i]);
   }

   return sums;
}
public static int[] rowSums(int[][] statsArray)
{
   int[] sums = new int[statsArray.length];

   for(int i = 0; i < sums.length; i++) {
      sums[i] = arraySum(statsArray[i]);
   }

   return sums;
}

public static int arraySum(int[] array) {
   int sum = 0;
   
   for (int num : array) {
       sum += num;
   }
   
   return sum;
}

int[][] statsArray = { {100, 200}, {100, 200}};

System.out.println("Total Healh: ");
System.out.print(statsArray[0][1]); // Output: 2

System.out.println("\n\nSum");
System.out.print(Arrays.toString(rowSums(statsArray))); // Output: [6, 15, 24]

Total Healh: 
200

Sum
[300, 300]

Part C

Diverse Arrays in 2D Array - Check each row of a 2D array for diversity using the methods from parts (a) and (b).

Question 1C

public static boolean isDiverse(int[][] arr2D)
 {
    int[] sums = rowSums(arr2D);

    for(int i = 0; i < sums.length; i++)
       for(int j = i+1; j < sums.length; j++)
          if(sums[i] == sums[j])
             return false;

    return true;
 }

Has no usuability in our project but included for frq completion