#include <iostream>
using namespace std;
int main()
{
int array_length;
cout << "--------------Program to check for PEAK ELEMENTS in an Array.--------------\n" << endl;
cout << "Enter the array length: ";
cin >> array_length;
int nums[array_length];
cout << "\n";
cout << "Input array elements: " << endl;
for(int i = 0; i < array_length; i++)
{
cin >> nums[i];
}
cout << "\n";
cout << "Indices are: " << endl;
for(int index = 0; index < array_length; index++)
{
if(index == 0 && nums[index] > nums[index + 1])
{
cout << index << ", ";
}
else if(index == array_length - 1 && nums[index - 1] < nums[index])
{
cout << index << ", ";
}
else if(nums[index - 1] < nums[index] && nums[index] > nums[index + 1])
{
cout << index << ", ";
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter