Last active
April 29, 2025 13:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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