The Trail
problem from the 2010 AP Computer Science Exam is typical of free response problems that test arrays. The problem requires you to find the minimum and maximum in an part of an array and access 2 elements of an array for each loop run.
Trail
is #3 from the 2010 AP Computer Science Free Response.
https://secure-media.collegeboard.org/apc/ap10_frq_computer_science_a.pdf
Part (a) isLevelTrailSegment
method
public boolean isLevelTrailSegment(int start, int end)
{
int minElevation = markers[start];
int maxElevation = markers[start];
for (int i = start + 1; i <= end; i++)
{
if (markers[i] < minElevation)
minElevation = markers[i];
if (markers[i] > maxElevation)
maxElevation = markers[i];
}
return maxElevation - minElevation <= 10;
}
This is a find the min/max (both in this case) problem with the constraint that only the values at indexes from start
up to and including end
are considered. The algorithm is the same as any other find the max/min problem.
Part (b) isDifficult
method
public boolean isDifficult()
{
int significantChanges = 0;
for (int i = 1; i < markers.length; i++)
if (Math.abs(markers[i - 1] - markers[i]) >= 30)
significantChanges++;
return significantChanges >= 3;
}
2010 AP CS Exam Free Response Solutions
Help & comments
Get help from AP CS Tutor Brandon Horn