gashink_t

обьединение массивов

Apr 24th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <malloc.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int* create_rand(int* A, int N, int N2); // создание массива А
  6. int* create_rand2(int* B, int N2); // создание массива В
  7. void insertSort(int* A, int N);// сортировка вставкой
  8. void print(int* A, int N);
  9. void unification(int* A, int* B, int N, int N2);// обьеденение в один массив
  10. int main()
  11. {
  12.     int n = 0,n2=0;
  13.     int* a = NULL;
  14.     int* b = NULL;
  15.     cout << "Enter the size of the array A: ";
  16.     cin >> n;
  17.     cout << "Enter the size of the array B: ";
  18.     cin >> n2;
  19.     cout << "\nEnter your array A: ";
  20.     a = create_rand(a, n, n2);
  21.     print(a, n);
  22.     insertSort(a, n);
  23.     cout << "Your sorted array: ";
  24.     print(a, n);
  25.     cout << "\nEnter your array B: ";
  26.     b = create_rand2(b, n2);
  27.     print(b, n2);
  28.     insertSort(b, n2);
  29.     cout << "Your sorted array: ";
  30.     print(b, n2);
  31.     unification(a, b, n, n2);
  32.     cout << "\nYour unification array: ";
  33.     print(a, n+n2);
  34.     return 0;
  35. }
  36.  
  37. int* create_rand(int* A, int N, int N2)
  38. {
  39.     A = (int*)malloc((N + N2) * sizeof(int));
  40.     for (int i = 0; i < N; ++i)
  41.         A[i] = rand() % 100;
  42.     return A;
  43. }
  44.  
  45. int* create_rand2(int* B, int N2)
  46. {
  47.     B = (int*)malloc(N2 * sizeof(int));
  48.     for (int i = 0; i < N2; ++i)
  49.         B[i] = rand() % 100;
  50.     return B;
  51. }
  52.  
  53. void print(int* A, int N)
  54. {
  55.     for (int i = 0; i < N; i++)
  56.         cout << A[i] << " ";
  57.     cout << endl;
  58. }
  59.  
  60. void unification(int* A, int* B, int N, int N2)
  61. {
  62.     int i, k=0;
  63.     for (i = N; i < N + N2; i++) {
  64.         A[i] = B[k];
  65.         k++;
  66.     }
  67.     insertSort(A, N + N2);
  68. }
  69.  
  70. void insertSort(int* A, int N)
  71. {
  72.     int tmp;
  73.     for (int i = 1, j; i < N; ++i) // цикл проходов, i - номер прохода
  74.     {
  75.         tmp = A[i];
  76.         for (j = i - 1; j >= 0 && A[j] > tmp; --j) // поиск места элемента в готовой последовательности
  77.             A[j + 1] = A[j];    // сдвигаем элемент направо, пока не дошли
  78.         A[j + 1] = tmp; // место найдено, вставить элемент    
  79.     }
  80. }
Add Comment
Please, Sign In to add comment