Write a method get Totals which accepts as parameters a reference to a two-dimen
ID: 3689848 • Letter: W
Question
Write a method get Totals which accepts as parameters a reference to a two-dimensional array of integers called table, a reference to a one-dimensional array of integers called rowsums and a reference to a one-dimensional array of integers called colsums. The method should compute the correct values for each element of rowSums and each element of colsums based on the data in table Assume that table refers to a rectangular array (i. e . all rows have the same number of columns). Assume the length of row Sums match the number of rows of table.Explanation / Answer
public class TwoD {
public static void main(String[] args) {
int [][] table = {{ 1, 1, 3, 0,},
{ 3, 2, 1, 1},
{ 1, 1, 1, 3},
{ 2, 2, 2, 4}};
int[] colSum =new int[table[0].length];
int[] rowSum =new int[table[0].length];
getTotals(table,rowSum,colSum);
}
public static void getTotals(int[][] table, int[] rowSum, int[] colSum) {
int sum= 0;
int rowSize = table.length;
int columnSize = table[0].length;
System.out.println("rows=" + rowSize + "cols=" + columnSize);
for (int i = 0; i < table.length; i++) {
sum = 0;
for (int j = 0; j < table[0].length; j++) {
sum += table[i][j];
colSum[j] += table[i][j];
}
System.out.println("Print the sum of rows = " + sum);
}
for(int k=0;k<colSum.length;k++){
System.out.println("Print the sum of columns =" + colSum[k]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.