ACSA Exam Prep
← All free-response questions
Question 4 · 2D Array · 6 points

Elevation Map

A hiking app models terrain as a grid of elevations in meters.

public class ElevationMap
{
    /** Elevations; rectangular, with at least one row and column */
    private int[][] heights;

    /** Replaces every elevation below seaLevel with seaLevel,
     *  then returns the number of peaks in the updated map.
     */
    public int floodAndCountPeaks(int seaLevel)
    { /* to be implemented */ }

    // There may be instance variables, constructors,
    // and methods that are not shown.
}

Your class

6 points

Write floodAndCountPeaks. First, set every element of heights that is less than seaLevel to seaLevel. Then count the peaks: a cell is a peak if it is strictly greater than each of its neighbors directly above, below, left, and right. Neighbors that would be off the edge of the grid are ignored.

Before and after floodAndCountPeaks(2):

BeforeAfter
1 5 2
3 1 4
0 2 1
2 5 2
3 2 4
2 2 2

After flooding, the peaks are 5 (row 0, column 1), 3 (row 1, column 0), and 4 (row 1, column 2). The method returns 3.

Tab inserts spaces. Drafts save as you type.
Self-score
0 / 6