CIST 1400-Introduction to Computer Programming EXAM #4, 22 POINTS, SECTIONS 6.8,
ID: 3694352 • Letter: C
Question
CIST 1400-Introduction to Computer Programming EXAM #4, 22 POINTS, SECTIONS 6.8, 6.15 AND 7.2-7.6 OF DEITE NAME: EXAM # 7. (4 points) Complete the method code below so that the method will sum up the values of the odd-numbere (including row 1) of the array parameter foo as shaded in the table below. The number of rows and colum inferred from the array itself. The last row may or may not be an odd-numbered row. Make sure to accous 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 Row 0 Row 1 Row 2 Row 3 Col o Co 1 Col 2 Col 3 Col 4 Col 5 876 97 75 76 69886 1234 875 89 96 Last col 9876 767698 54 765 2354 64 765 57 467 23 23 456 1231344 456 67 Last row 123 3887 78783773 method Name sumArray Parameters Task int array foo : sum odd rows in matrix : the summation of all odd rows Return public static int sumArray ( int foo )Explanation / Answer
public static int sumArray ( int foo[][] )
{
// total stores the sum of all odd rows in array
int total = 0;
// foo.length finds the number of rows in 2D array
// foo[r].length finds the number of columns in 2D array
for (int r=0; r<foo.length; r++) {
for (int c=0; c<foo[r].length; c++) {
// check if row is odd
if( r%2 != 0)
total = total + foo[r][c];
}
}
// return the total obtained by running the two for loops
return total;
}