Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //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.
- namespace _04.BinSearchImplementation
- {
- class BinSearchImplementation
- {
- //Enter array
- static void InputArray(int n, int[] array)
- {
- for (int i = 0; i < n; i++)
- {
- array[i] = int.Parse(Console.ReadLine());
- }
- }
- //Print Array
- static void PrintIntArray(int[] array)
- {
- foreach(int number in array)
- Console.Write("{0}, ", number);
- Console.WriteLine();
- }
- static void Main()
- {
- Console.WriteLine("Please enter N");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("Please enter the array members");
- int[] arrayNumbers = new int[n];
- InputArray(n, arrayNumbers);
- Console.WriteLine("Please enter K");
- int k = int.Parse(Console.ReadLine());
- Array.Sort(arrayNumbers);
- Console.WriteLine("This is the sorted array");
- PrintIntArray(arrayNumbers);
- int index = Array.BinarySearch(arrayNumbers, k);
- Console.WriteLine(index);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement