Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TestForAlfa
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const int lenArr = 10;
  10.             int[] myArray = new int[lenArr];
  11.             int X, i, ind;
  12.             Random rnd = new Random();
  13.  
  14.             X = rnd.Next(-lenArr, 10);
  15.             Console.WriteLine("X = " + X);
  16.             Console.WriteLine("Initial data:\n");
  17.             for (i = 0; i < lenArr; i++)
  18.             {
  19.                 myArray[i] = rnd.Next(lenArr*2);
  20.                 Console.Write(myArray[i] + " ");
  21.             }
  22.             Array.Sort(myArray);
  23.             Array.Reverse(myArray);
  24.             Console.WriteLine();
  25.             Console.WriteLine("Sorted array:\n");
  26.             for (i = 0; i < myArray.Length; i++)
  27.             {
  28.                 Console.Write(myArray[i] + " ");
  29.             }
  30.             Console.WriteLine();
  31.             if ((ind = bSearch(myArray, X)) != -1)
  32.                 Console.WriteLine("min index = " + ind);
  33.             else
  34.                 Console.WriteLine("There is no element less than X.");
  35.             Console.Read();
  36.  
  37.             int bSearch(int[] arr, int x)
  38.             {
  39.                 int minInd = 0;
  40.                 for (int j = 1; j < arr.Length; j++)
  41.                 {
  42.                     if (arr[j] < x)
  43.                     {
  44.                         minInd = j;
  45.                     }
  46.                 }
  47.                 if (minInd == 0)
  48.                     return -1;
  49.                 return minInd+1;
  50.             }
  51.         }
  52.  
  53.         
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement