Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active April 29, 2025 13:34
Show Gist options
  • Save Strelok78/a8d2e79da0b9469907ed0443d56a6060 to your computer and use it in GitHub Desktop.
Save Strelok78/a8d2e79da0b9469907ed0443d56a6060 to your computer and use it in GitHub Desktop.
A one-dimensional array of integers of 30 elements is given. Finds all local maxima and outputs them. The program works with an array of any size.
using System.Collections;
using System.Diagnostics;
using System.Text;
namespace iJuniorPractice;
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[30];
Random random = new Random();
int maxRandomValue = 100;
int lastIndex;
Console.WriteLine("numbers:");
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = random.Next(maxRandomValue + 1);
Console.Write(numbers[i] + " ");
}
lastIndex = numbers.Length - 1;
Console.WriteLine("\nLocal max values: ");
if (numbers[0] > numbers[1])
{
Console.Write(numbers[0] + " ");
}
for (int i = 1; i < numbers.Length - 1; i++)
{
if (numbers[i] > numbers[i + 1] && numbers[i] > numbers[i - 1])
{
Console.Write(numbers[i] + " ");
}
}
if (numbers[lastIndex] > numbers[lastIndex - 1])
{
Console.WriteLine(numbers[lastIndex]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment