Complete the method code below so that the method will sum up the values of the
ID: 3572479 • Letter: C
Question
Complete the method code below so that the method will sum up the values of the odd-number (including row 1) of the array parameter foe as shaded In the table below. The number of rows and Inferred from the array Itself. The last row may or may not be an odd-numbered row. Make sure to your method. Return the sum of those cells from the method. you may only use two loops inside the method. You do not need to write a complete program, but include all relevant declarations you need./** * method Name: sumArray Parameters: in array foo Task: sum odd rows in matrix * Return: the summation of all odd rows */public static int sumArray (int foo [][]) {Explanation / Answer
OddRowSum.java
import java.util.Arrays;
public class OddRowSum {
public static void main(String[] args) {
int foo[][]= {{1,2,3,4,5}, {2,3,4,5,6}, {3,4,5,6,7},{4,5,6,7,8}, {5,6,7,8,9},{5,4,3,2,1} };
System.out.println("Input matrix is ");
for(int i=0; i<foo.length; i++){
System.out.println(Arrays.toString(foo[i]));
}
System.out.println("The sum of odd of rows: "+sumArray(foo));
}
public static int sumArray(int foo[][]){
int sum = 0;
for(int i=0; i<foo.length; i++){
if(i%2!=0){
for(int j=0; j<foo[i].length;j++){
sum = sum + foo[i][j];
}
}
}
return sum;
}
}
Output:
Input matrix is
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
[4, 5, 6, 7, 8]
[5, 6, 7, 8, 9]
[5, 4, 3, 2, 1]
The sum of odd of rows: 65
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.