50.9k views
0 votes
Consider the following code segment, where num is a properly declared and initialized integer variable. The following code segment is intended to set foundRow and foundCol to the row and column indexes of an array element containing num. The code segment does not work as intended.

int[][] arr = {{10, 11, 12, 13},

{22, 24, 26, 28},

{15, 16, 17, 18},

{40, 41, 42, 43}};

int foundRow = -1;

int foundCol = -1;

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

{

for (int k = 1; k < arr[0].length; k++)

{

if (arr[j][k] == num)

{

foundRow = j;

foundCol = k;

}

}

}

Which of the following values for num can be used as a test case to show that the code segment does not work as intended?

User Sam Firke
by
5.2k points

1 Answer

4 votes

Answer:

10 or 22 or 15 or 40

Step-by-step explanation:

In the second for loop[for (int k = 1; k < arr[0].length; k++)], k starts from 1 instead of zero. That means that it doesn't read the first value of every row when finding num.

So if num was any of the above values, the program wouldn't work. I had the same question, and the answer for mine was 15.

User Ben Diamant
by
5.3k points