Created
March 1, 2019 21:50
-
-
Save igorLisovitskiy/03531032d522b880e5fe422f1e77cb3b to your computer and use it in GitHub Desktop.
This file contains 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
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