Free Response Question 1
2015 FRQ
class DiverseArray{
public static int arraySum(int[] array){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum = sum + array[i];
}
return sum;
}
public static void main(String[] args){
int[] fram = {1,3,4,5,6};
int x= arraySum(fram);
System.out.print(x);
}
}
DiverseArray.main(null);
Write a static method rowSums that calculates the sums of each of the rows in a given twodimensional array and returns these sums in a one-dimensional array. The method has one parameter, a twodimensional 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}.
class DiverseArray2{
public static int arraySum(int[] array){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum = sum + array[i];
}
return sum;
}
public static int[] rowSums(int[][] array){
int[] sum = new int[array.length];
for(int g = 0; g < array.length; g++){
int sum1 = arraySum(array[g]);
sum[g] = (sum1);
}
return sum;
}
public static void main(String[] args){
int[][] fram = new int[][] {
{1,2,3},
{1,2,3,4},
{3,2,1,0}
};
int[] x = rowSums(fram);
for(int j = 0;j < x.length; j++){
System.out.print(x[j] + ", ");
}
}
}
DiverseArray2.main(null);
class DiverseArray3{
public static int arraySum(int[] array){
int sum = 0;
for(int i = 0; i < array.length; i++){
sum = sum + array[i];
}
return sum;
}
public static int[] rowSums(int[][] array){
int[] sum = new int[array.length];
for(int g = 0; g < array.length; g++){
int sum1 = arraySum(array[g]);
sum[g] = (sum1);
}
return sum;
}
public static boolean isDiverse(int[][] arr2d){
boolean isDiverse = true;
int[] cheese = rowSums(arr2d);
for (int i = 0; i < cheese.length; i++) {
int burger = cheese[i];
for (int j = i + 1; j < cheese.length; j++) {
if (burger == cheese[j]) {
isDiverse = false;
}
}
}
return isDiverse;
}
public static void main(String[] args){
int[][] fram = new int[][] {
{1,2,3},
{1,2,3},
{3,2,1,0}
};
System.out.println("is " + isDiverse(fram));
int[][] fram2 = new int[][] {
{1,2,3},
{1,2,3,4,5},
{5,5,5,5}
};
System.out.println("is " + isDiverse(fram2));
}
}
DiverseArray3.main(null);