Part A
All I did for this question was iterate through the array using a for loop, one integer at a time, adding each integer to the sum variable, ultimately getting the sum of the array.
1
2
3
4
5
6
7
8
9
10
11
12
13
class DiverseArray { // class mentioned in FRQ
public static int arraySum(int[] arr) {
int sum = 0; // setting sum equal to 0 so that it only counts for this array
for (int i = 0; i < arr.length; i++) { // iterating through array, on integer at a time
sum += arr[i]; // adding the value of each index to the sum of the array
}
return sum; // returning the array for printing
}
}
int[] arr1 = {1, 3, 2, 7, 3}; // array mentioned in FRQ
System.out.println("Sum: " + DiverseArray.arraySum(arr1)); // printing array
1
Sum: 16
Part B
For this question I reused the arraySum
method from the previous part to help me in finding the individual sums of the rows of the matrix. I then made the rowSums
method by initializing an array that would contain all of the sums and then I iterated through each row of the matrix, running the arraySum
method on each row to get the sums. These sums are stored in the rowSums
array, which when printed by itself prints in hexadecimal, showing the hashcode of the array object because of the way ipynbs work. To fix this, I iterated through each value in the rowSums
array to print the values obtained.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class DiverseArray { // class mentioned in FRQ
public static int arraySum(int[] arr) { // same method as previous part reused
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static int[] rowSums (int[][] arr2D) { // new rowSum method
int [] rowSums = new int[arr2D.length]; // initializing the array for the row sums
for (int j = 0; j < arr2D.length; j++) { // for loop going through each array to find the sum
rowSums[j] = arraySum(arr2D[j]); // placing the resulting sum from the arraySum method into the rowSums array
}
return rowSums; // returning array of values for the rowSums
}
}
int[][] mat1 = { {1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1} }; // example matrix from frq
int [] rowSumsJava = DiverseArray.rowSums(mat1);
System.out.println("Row Sums: " + rowSumsJava); // printing out array, printed bad because of ipynb
for (int k = 0; k < rowSumsJava.length; k++) { // fixed by iterating through each value in the row and printing out
System.out.println("Row " + k + " Sum: " + rowSumsJava[k]);
}
1
2
3
4
5
Row Sums: [I@3f8ce8c
Row 0 Sum: 16
Row 1 Sum: 32
Row 2 Sum: 28
Row 3 Sum: 20
Part C
In this part of the question, I made a method that creates an array of all the sums of each row in the matrix. I then used two for loops to look through the the array, the first looking at one value and the second looking at all other values in the array to compare and see if there are any values that are ever the same. The method returns false if two equal values are found and true if no equal values are found. I printed the data in a similar way to before, because the ipynb kernel prints the data in hexadecimal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class DiverseArray { // class mentioned in FRQ
public static int arraySum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static int[] rowSums (int[][] arr2D) { // same method as previous part reused
int [] rowSums = new int[arr2D.length];
for (int j = 0; j < arr2D.length; j++) {
rowSums[j] = arraySum(arr2D[j]);
}
return rowSums;
}
public static boolean isDiverse(int[][] arr2D) { // new isDiverse method
int[] sums = rowSums(arr2D); // makes array of the row sums from the matrix
for (int k = 0; k < sums.length; k++) { // iterates through each value of the sums array
for (int l = k + 1; l < sums.length; l++) { // looks through each other value to the current value it's on
if (sums[k] == sums[l]) { // compares the two
return false; // when the condition is false
}
}
}
return true; // if no values are equal, return true
}
}
int[][] mat1 = { {1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1} }; // matrix that would return true
int[][] mat2 = { {1, 1, 5, 3, 4}, {12, 7, 6, 1, 9}, {8, 11, 10, 2, 5}, {3, 2, 3, 0, 6} }; // matrix that would return false
// printing out all data as before
int [] rowSumsJava1 = DiverseArray.rowSums(mat1);
for (int m = 0; m < rowSumsJava1.length; m++) {
System.out.println("Row " + m + " Sum: " + rowSumsJava1[m]);
}
System.out.println("Array 1 Diversity: " + DiverseArray.isDiverse(mat1));
int [] rowSumsJava2 = DiverseArray.rowSums(mat2);
for (int n = 0; n < rowSumsJava2.length; n++) {
System.out.println("Row " + n + " Sum: " + rowSumsJava2[n]);
}
System.out.println("Array 2 Diversity: " + DiverseArray.isDiverse(mat2));
1
2
3
4
5
6
7
8
9
10
Row 0 Sum: 16
Row 1 Sum: 32
Row 2 Sum: 28
Row 3 Sum: 20
Array 1 Diversity: true
Row 0 Sum: 14
Row 1 Sum: 35
Row 2 Sum: 36
Row 3 Sum: 14
Array 2 Diversity: false