Finding Closest Value in a 2D Array
Consider the following findClosest method, which is intended to return the element in a 2D array mat that is closest in value to a given number val.
/** @return the element of 2-dimensional array mat whose value is closest to val */
public double findClosest(double[][] mat, double val)
{
double answer = mat[0][0];
double minDiff = Math.abs(answer - val);
for (double[] row : mat)
{
for (double num : row)
{
if ( /* missing code */ )
{
answer = num;
minDiff = Math.abs(num - val);
}
}
}
return answer;
}
Which of the following expressions can replace /* missing code */ so that the method works as intended?
A
val - row[num] < minDiff
B
Math.abs(num - val) < minDiff
C
Math.abs(row[num] - val) < minDiff
D
val - num < 0.0
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | angelajxi11 | 0 | 1 | 0m 00s | -10 |
| #3 | psak12 | 0 | 2 | 0m 00s | -20 |
| #4 | y.seong2027 | 1 | 1 | 8m 07s | -387 |
Items per page:
10
1 – 4 of 4
APFIVE