Array Traversal Loop Bounds
The countPeaks method below is intended to return the number of local minimums in an integer array. A local minimum is an element that is less than both of its adjacent elements. The first and last elements of the array are not considered local minimums. For example, for the array {3, 1, 7, 4, 2, 12, 3, 5}, the method should return 3.
public static int countPeaks(int[] data)
{
int numPeaks = 0;
for ( /* missing loop header */ ) {
if (data[p - 1] > data[p] && data[p] < data[p + 1])
{
numPeaks++;
}
}
return numPeaks;
}
Which of the following can replace /* missing loop header */ so that the method works as intended?
A
int p = 0; p < data.length - 1; p++
B
int p = 1; p < data.length - 1; p++
C
int p = 0; p < data.length; p++
D
int p = data.length - 1; p > 0; p–
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE