(a) Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.
Complete method arraySum below.
/ * * Returns the sum of the entries in the one-dimensional array arr.
public class Sum {
public static int arraySum (int [ ] arr) {
int sum = 0; // set sum
for (int i = 0; i < arr.length; i++) {
sum += arr[i]; // add the each elements of array into sum
}
return sum;
}
public static void main(String[] args) {
int[] arr = {1, 3, 2, 7, 3};
int sum = Sum.arraySum(arr); // getting the sum for all elements of arr
System.out.println("Sum: " + sum); // print out sum
}
}
Sum.main(null);
Sum: 16
I utilized a for loop to accumulate the elements within the int[] arr. Once the loop completes its iterations, it returns the sum of all the numbers that were added.
(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. The method has one parameter, a two-dimensional array arr2D of int values. The array is in row-major order: arr2D [ r ] [ c ] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.
For example, if mat1 is the array represented by the following table, the call rowSums(mat1) returns the array {16, 32, 28, 20}.
Assume that arraySum works as specified, regardless of what you wrote in part (a). You must use arraySum appropriately to receive full credit.
Complete method rowSums below.
/ * * Returns a one-dimensional array in which the entry at index k is the sum of
the entries of row k of the two-dimensional array arr2D.
/
public class SumOf2Darray {
public static int arraySum (int [ ] arr) { // used in Part A
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static int [ ] rowSums(int [ ] [ ] arr2D) {
int[] sums = new int[arr2D.length]; // create array to return at the end
int sum = 0; // initialize sum as 0
for (int i=0; i < arr2D.length; i++) {
sum = arraySum(arr2D[i]); // put arr2D[i] which will be int[] in arraySum()
sums[i] = sum; // put the sum into the array created at beginning
sum = 0; // reset the sum
}
return sums;
}
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 = SumOf2Darray.rowSums(mat1); // make int[] sums
System.out.print("Sum: ");
for (int i = 0; i < sums.length; i++) {
System.out.print(sums[i] + " "); // print the int[] sums
}
}
}
SumOf2Darray.main(null);
Sum:
16 32 28 20
By using for loop, pull the first row of the 2D array and add to the arraySum function. Since the first row will be one dimensional array, it will work properly. Then, put that sum into sums array and return it.
(c) A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum.
Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse (mat1) returns true and the call isDiverse(mat2) returns false.
Assume that arraySum and rowSums work as specified, regardless of what you wrote in parts (a) and(b). You must use rowSums appropriately to receive full credit. Complete method isDiverse below.
/ * * Returns true if all rows in arr2D have different row sums;
public class check {
public static int arraySum (int [ ] arr) { // answer for part A
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static int [ ] rowSums(int [ ] [ ] arr2D) { // answer for part B
int[] sums = new int[arr2D.length];
int sum = 0;
for (int i=0; i < arr2D.length; i++) {
sum = arraySum(arr2D[i]);
sums[i] = sum;
sum = 0;
}
return sums;
}
public static boolean isDiverse(int[][] arr2D) {
int[] sums = rowSums(arr2D); // get the row sums
for (int i = 0; i < sums.length; i++) { // using for loop
for (int j = i + 1; j < sums.length; j++) {
if (sums[i] == sums[j]) { // compare each elements
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));
int[][] arr2D2 = {
{1, 2, 3},
{3, 2, 1},
{7, 8, 9}
};
System.out.println(isDiverse(arr2D2));
}
}
check.main(null);
true
false
This method takes a 2D array as input and returns true if it’s diverse (no two rows have the same sum), otherwise false. It uses two nested loops to compare each pair of sums in the “sums” array. Inside the loops, it compares each pair of sums. If any two sums are found to be equal, it means there are two rows with the same sum, and the method returns false, indicating that the array is not diverse. If no two sums are equal after comparing all pairs, it means the array is diverse, so the method returns true.
This FRQ wasn’t too tough because I’ve dealt with similar questions before. Part A was easy; I just did that same code last week. For parts B and C, I knew I’d need two loops, one inside the other, to check everything. But using nested loops isn’t great for big problems because it can get slow (it’s O(n^2) complexity). Still, I reckon the college board will give me full marks because there’s not really a better way to do it.
Create a two-dimensional String array with 6 rows and 6 columns, and store each index in the top and first column of the row. Then, receive rows and columns from the user, convert the coordinate values to ‘X’, and output a two-dimensional array.
method name: public void practice( ) { }
public class Extra {
public static void practice() {
Scanner sc = new Scanner(System.in);
String[][] arr = new String[6][6];
System.out.print("enter row : ");
int rowNum = sc.nextInt();
System.out.print("enter col : ");
int colNum = sc.nextInt();
if (rowNum < 0 || rowNum > 4 || colNum < 0 || colNum > 4)
System.out.println("wrong Input.");
arr[rowNum][colNum] = "X";
System.out.println();
System.out.println(" 0 1 2 3 4");
for (int row = 0; row < arr.length - 1; row++) {
System.out.print(row + " ");
for (int col = 0; col < arr[col].length - 1; col++) {
if (arr[row][col] == arr[rowNum][colNum])
arr[row][col] = "X";
else
arr[row][col] = " ";
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
sc.close();
}
}
Extra.practice();
enter row : enter col :
0 1 2 3 4
0
1
2
3
4 X