Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // 2 thu vien nay de anh rand ra 1 so ngau nhien
- #include <time.h>
- #include <Windows.h>
- #define MAX 100
- void random_array(int arr[], int &n)
- {
- // rand() tra ve 1 so nguyen tu 0 den 2 ^ 32 - 1. anh % cho 10 + 5 thi gia tri cua no chi con tu 5 den 14
- n = rand() % 10 + 5;
- for (int i = 0; i < n; i++)
- arr[i] = rand() % 100;
- }
- void print_array(int arr[], int n)
- {
- for (int i = 0; i < n; i++)
- printf("%d\t", arr[i]);
- printf("\n");
- }
- void swap(int &a, int &b)
- {
- int temp = a;
- a = b;
- b = temp;
- }
- void sort_array(int arr[], int n)
- {
- for (int i = 0; i < n - 1; i++)
- for (int j = i + 1; j < n; j++)
- if (arr[i] > arr[j])
- swap(arr[i], arr[j]);
- }
- void merge_array(int arr_a[], int size_a, int arr_b[], int size_b)
- {
- int temp = -1, min;
- for (int i = 0, j = 0; i < size_a || j < size_b;)
- {
- if (arr_a[i] < 0 || arr_b[j] < 0)
- int c = 1;
- if (arr_a[i] < arr_b[j] && i < size_a)
- {
- if (temp != arr_a[i] || j == size_b)
- {
- printf("%d\t", arr_a[i]);
- temp = arr_a[i];
- }
- i++;
- }
- else if (j < size_b)
- {
- if (temp != arr_b[j] || i == size_a)
- {
- printf("%d\t", arr_b[j]);
- temp = arr_b[j];
- }
- j++;
- }
- }
- }
- void main()
- {
- int arr_a[MAX], arr_b[MAX], size_a, size_b;
- // dung lenh nay de cac con so random ra khac nhau
- srand(time(0));
- random_array(arr_a, size_a);
- sort_array(arr_a, size_a);
- print_array(arr_a, size_a);
- random_array(arr_b, size_b);
- sort_array(arr_b, size_b);
- print_array(arr_b, size_b);
- merge_array(arr_a, size_a, arr_b, size_b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement