Finding Closest Value in a 2D Array
Which of the following could be used to replace /* missing code */ so that findClosest will work as intended?
Consider the following method, which is intended to return the element of a 2-dimensional array that is closest in value to a specified 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 could be used to replace /* missing code */ so that findClosest will work 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 |
APFIVE