Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //Write a program, that reads from the console an array of N integers and an integer K, sorts the array
  8. //and using the method Array.BinSearch() finds the largest number in the array which is ≤ K.
  9.  
  10. namespace _04.BinSearchImplementation
  11. {
  12. class BinSearchImplementation
  13. {
  14. //Enter array
  15. static void InputArray(int n, int[] array)
  16. {
  17. for (int i = 0; i < n; i++)
  18. {
  19. array[i] = int.Parse(Console.ReadLine());
  20. }
  21. }
  22.  
  23. //Print Array
  24. static void PrintIntArray(int[] array)
  25. {
  26. foreach(int number in array)
  27. Console.Write("{0}, ", number);
  28. Console.WriteLine();
  29. }
  30.  
  31. static void Main()
  32. {
  33. Console.WriteLine("Please enter N");
  34. int n = int.Parse(Console.ReadLine());
  35. Console.WriteLine("Please enter the array members");
  36. int[] arrayNumbers = new int[n];
  37. InputArray(n, arrayNumbers);
  38. Console.WriteLine("Please enter K");
  39. int k = int.Parse(Console.ReadLine());
  40.  
  41. Array.Sort(arrayNumbers);
  42. Console.WriteLine("This is the sorted array");
  43. PrintIntArray(arrayNumbers);
  44.  
  45. int index = Array.BinarySearch(arrayNumbers, k);
  46. Console.WriteLine(index);
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement