Ask Question
7 January, 13:38

Consider the following method, sumRows, which is intended to traverse all the rows in the two-dimensional (2D) integer array num and print the sum of all the elements in each row.

public static void sumRows (int[][] num)

{

for (int[] r : num)

{

int sum = 0;

for (int j = 0; j < num. length; j++)

{

sum + = r[j];

}

System. out. print (sum + " ");

}

}

For example, if num contains {{3, 5}, {6, 8}}, then sumRows (num) should print "8 14 ".

The method does not always work as intended. For which of the following two-dimensional array input values does sumRows NOT work as intended?

A. {{10, - 18}, {48, 17}}

B. {{-5, 2, 0}, {4, 11, 0}}

C. {{4, 1, 7}, {-10, - 11, - 12}}

D. {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

E. {{0, 1}, {2, 3}}

+4
Answers (1)
  1. 7 January, 15:45
    0
    Option C: {{4, 1, 7}, {-10, - 11, - 12}}

    Explanation:

    There is a logical error in the inner loop.

    for (int j = 0; j < num. length; j++)

    It shouldn't be "j < num. length" as the iteration of the inner loop will be based on the length of the row number of the two dimensional array. The inner loop is expected to traverse through the every column of the same row in the two dimensional array and sum up the number. To fix this error, we can change it to

    for (int j = 0; j < r. length; j++)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the following method, sumRows, which is intended to traverse all the rows in the two-dimensional (2D) integer array num and print ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers