Guest User

Zadacha1

a guest
Jan 11th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include<iostream.h>
  2.  
  3. int A[2][20];
  4. int B[40];
  5.  
  6. void CreateAndPrintA()
  7. {
  8.     srand(time(NULL));
  9.  
  10.     for (int i = 0; i < 2; i++)
  11.     {
  12.         for (int j = 0; j < 20; j++)
  13.         {
  14.             A[i][j] = rand() % 100;
  15.             cout << A[i][j] << " ";
  16.         }
  17.  
  18.         cout << endl;
  19.     }
  20. }
  21.  
  22. void BubbleSort()
  23. {
  24.     for (int i = 0; i < 2; i++)
  25.     {
  26.         for (int j = 0; j < 19; j++)
  27.         {
  28.             for (int k = 0; k < 19; k++)
  29.             {
  30.                 if (A[i][k] > A[i][k + 1])
  31.                 {
  32.                     int initialValue = A[i][k];
  33.                     A[i][k] = A[i][k + 1];
  34.                     A[i][k + 1] = initialValue;
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. void CreateAndPrintB()
  42. {
  43.     int index = 0;
  44.  
  45.     for (int i = 0; i < 2; i++)
  46.     {
  47.         for (int j = 0; j < 20; j++)
  48.         {
  49.             B[index] = A[i][j];
  50.             cout << B[index] << " ";
  51.             index++;
  52.         }
  53.     }
  54.  
  55.     cout << endl;
  56. }
  57.  
  58. void PrqkaSelekciq()
  59. {
  60.     for (int i = 0; i < 39; i++)
  61.     {
  62.         int min = i;
  63.  
  64.         for (int j = i + 1; j < 40; j++)
  65.         {
  66.             if (B[min] > B[j])
  67.             {
  68.                 min = j;
  69.             }
  70.         }
  71.  
  72.         int initialValue = B[min];
  73.         B[min] = B[i];
  74.         B[i] = initialValue;
  75.     }
  76. }
  77.  
  78. bool BinarySearch(int n)
  79. {
  80.     int left = 0;
  81.     int mid = 0;
  82.     int right = 39;
  83.  
  84.     while (left <= right)
  85.     {
  86.         mid = (left + right) / 2;
  87.  
  88.         if (B[mid] == n)
  89.         {
  90.             return true;
  91.         }
  92.         else
  93.         {
  94.             if (B[mid] > n)
  95.             {
  96.                 right = mid - 1;
  97.             }
  98.             else
  99.             {
  100.                 left = mid + 1;
  101.             }
  102.         }
  103.     }
  104.  
  105.     return false;
  106. }
  107.  
  108.  
  109.  
  110. int main() {
  111.     CreateAndPrintA();
  112.     BubbleSort();
  113.     cout << "-------------------------------" << endl;
  114.  
  115.     for (int i = 0; i < 2; i++)
  116.     {
  117.         for (int j = 0; j < 20; j++)
  118.         {
  119.             cout << A[i][j] << " ";
  120.         }
  121.  
  122.         cout << endl;
  123.     }
  124.    
  125.     cout << "-------------------------------" << endl;
  126.  
  127.     CreateAndPrintB();
  128.     PrqkaSelekciq();
  129.     cout << "-------------------------------" << endl;
  130.    
  131.     for (int i = 0; i < 40; i++)
  132.     {
  133.         cout << B[i] << " ";
  134.     }
  135.  
  136.     int n;
  137.     cout <<endl<<"n = ";
  138.     cin >> n;
  139.  
  140.     if (BinarySearch(n))
  141.     {
  142.         cout << "The number was found" << endl;
  143.     }
  144.     else
  145.     {
  146.         cout << "The number was not found" << endl;
  147.     }
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment