Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Write a program, that reads from the console an array of N integers and an integer K,
- //sorts the array and using the method Array.BinSearch() finds the largest number in the array which is ≤ K.
- class BinarySearchMethod
- {
- static void Main()
- {
- //Prepare the array
- Console.Write("Enter Array's length: ");
- int n = int.Parse(Console.ReadLine());
- int[] array = new int[n];
- for (int col = 0; col < array.Length; col++)
- {
- Console.Write("initilize array {0} left: ",array.Length - col);
- array[col] = int.Parse(Console.ReadLine());
- }
- Array.Sort(array);
- Console.Write("Enter K:");
- int k = int.Parse(Console.ReadLine());
- int indexK = Array.BinarySearch(array, k);
- Console.WriteLine("Position :" + indexK);
- if (indexK < array[0])
- {
- Console.WriteLine("Best number: " + array[n-1]);
- }
- if (indexK >= 0)
- {
- int count = 0;
- while (count < array.Length)
- {
- if (array[count] == indexK)
- {
- Console.WriteLine("The best number: " + array[count+1]);
- }
- count++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement