82.2k views
3 votes
For function: int getNumPeaks(const int heights[], int size)

Return the number of peaks in the hike
Sample run
Enter elevations: 1200 3000 3450 2800 2900 1550 1750 1110 1200
Highest points:
First half: 3450
Second half: 1750
Overall: 3450
Average elevation: 2106.67
Peaks: 3
Difficult segments: 2
Elevation change: 5280
In c++

1 Answer

2 votes

Final answer:

The function getNumPeaks takes in an array of heights and returns the number of peaks in the hike.

Step-by-step explanation:

The function getNumPeaks takes in two parameters - an array of heights and the size of the array. It returns the number of peaks in the hike. To find the number of peaks, we can iterate through the array and compare each element with its adjacent elements. If an element is greater than both its adjacent elements, it is considered a peak. We keep track of the count of peaks and return it at the end of the function.

Example:

int getNumPeaks(const int heights[], int size) {
int count = 0;
for(int i = 1; i < size-1; i++) {
if(heights[i] > heights[i-1] && heights[i] > heights[i+1]) {
count++;
}
}
return count;
}

Learn more about getNumPeaks

User Neijwiert
by
8.2k points