Skip to content

Instantly share code, notes, and snippets.

@igorLisovitskiy
Created March 1, 2019 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorLisovitskiy/03531032d522b880e5fe422f1e77cb3b to your computer and use it in GitHub Desktop.
Save igorLisovitskiy/03531032d522b880e5fe422f1e77cb3b to your computer and use it in GitHub Desktop.
public static void Task4()
{
/*
4) Пользователь вводит с клавиатуры размерность одномерного массива n и заполняет его элементами. Найти и вывести на экран два наибольших элемента этого массива. (Значения элементов массива могут повторятся).
Примеры:
В массиве [1, 7, 6, 4, 9] два максимальных элемента — это числа 9 и 7.
В массиве [5, 1, 2, 9, 9] два максимальных элемента — это числа 9 и 9.
*/
Console.WriteLine("Enter the length of array");
int arrLength = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[arrLength];
Console.WriteLine($"Enter {arrLength} numbers");
int index = arrLength - 1;
while (index >= 0)
{
arr[index] = Convert.ToInt32(Console.ReadLine());
index--;
}
int firstMax = arr[0];
int secondMax = arr[1];
for (int i = 2; i < arr.Length; i++)
{
if (arr[i] > firstMax)
{
secondMax = firstMax;
firstMax = arr[i];
}
else if (arr[i] > secondMax)
{
secondMax = arr[i];
}
}
Console.WriteLine($"Max values are: {firstMax}, {secondMax}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment