SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <stdio.h> | |
| 2 | #include <time.h> | |
| 3 | #include <stdlib.h> | |
| 4 | ||
| 5 | const int ARR_SIZE = 5; | |
| 6 | ||
| 7 | void fillArr(int arr[], int size); | |
| 8 | int Intersect(int arr1[], int arr2[], int sum_size, int *res); | |
| 9 | ||
| 10 | int main() | |
| 11 | {
| |
| 12 | srand(time(NULL)); | |
| 13 | int arr1[ARR_SIZE]; | |
| 14 | int arr2[ARR_SIZE]; | |
| 15 | - | int result[ARR_SIZE]; |
| 15 | + | int result[ARR_SIZE + 1]; |
| 16 | int *ptr = result; | |
| 17 | int i = 0; // TODO: ass | |
| 18 | fillArr(arr1, ARR_SIZE); fillArr(arr2, ARR_SIZE); | |
| 19 | printf("First array: \n");
| |
| 20 | for (i; i < ARR_SIZE; i++) | |
| 21 | {
| |
| 22 | printf("%d", arr1[i]);
| |
| 23 | printf("\n");
| |
| 24 | } | |
| 25 | printf("Second array: \n");
| |
| 26 | for (i = 0; i < ARR_SIZE; i++) | |
| 27 | {
| |
| 28 | printf("%d", arr2[i]);
| |
| 29 | printf("\n");
| |
| 30 | } | |
| 31 | int sum = 2 * ARR_SIZE; | |
| 32 | int size = Intersect(arr1, arr2, sum, ptr); | |
| 33 | printf("Result: \n");
| |
| 34 | for (i = 1; i <= size; i++) | |
| 35 | {
| |
| 36 | printf("%d\n", result[i]);
| |
| 37 | } | |
| 38 | getchar(); | |
| 39 | return 0; | |
| 40 | } | |
| 41 | ||
| 42 | void fillArr(int arr[], int size) | |
| 43 | {
| |
| 44 | int i = 0; | |
| 45 | for (i = 0; i < size; i++) | |
| 46 | {
| |
| 47 | arr[i] = rand() % 5; | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | int Intersect(int arr1[], int arr2[], int sum_size, int *res) | |
| 52 | {
| |
| 53 | int i = 0; | |
| 54 | int size = 0; | |
| 55 | int count = 0; | |
| 56 | short bad_num = 0; | |
| 57 | for (i = 0; i < ARR_SIZE; i++) | |
| 58 | {
| |
| 59 | int j = 0; | |
| 60 | int isBad = 0; | |
| 61 | for (j = 0; j < ARR_SIZE; j++) | |
| 62 | {
| |
| 63 | if (arr1[i] == arr2[j]) | |
| 64 | {
| |
| 65 | int x = 0; | |
| 66 | for (x; x < ARR_SIZE; x++) | |
| 67 | {
| |
| 68 | if (res[x] == arr1[i]) | |
| 69 | {
| |
| 70 | isBad = 1; | |
| 71 | } | |
| 72 | } | |
| 73 | if (isBad != 1) | |
| 74 | {
| |
| 75 | size++; | |
| 76 | res[size] = arr1[i]; | |
| 77 | } | |
| 78 | } | |
| 79 | } | |
| 80 | } | |
| 81 | return size; | |
| 82 | } |