Write a C Program to Find Union and Intersection of two Arrays

By | 17.11.2016

C Find Union and Intersection of two Arrays


Write a C Program to Find Union and Intersection of two Arrays. Here’s simple Program to Find Union and Intersection of two Arrays in C Programming Language.


What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Here is source code of the C Program to Find Union and Intersection of 2 Arrays. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/* C Program to Find Union and Intersection of 2 Arrays */

#include <stdio.h>
#define SIZE 5

void get_value(int arr[]);
void print_value(int arr[], int n);
void function_sort(int arr[]);
int find_intersection(int array1[], int array2[], int intersection_array[]);
int find_union(int array1[], int array2[], int union_array[]);

void main()
{
    int array1[SIZE], array2[SIZE], intersection_array[SIZE], union_array[SIZE*2];
    int num_elements;

    //input elements of Array1
    printf("\n Enter the elements of Array 1: \n");
    get_value(array1);
    printf("\n\n Elements of Array 1: ");
    print_value(array1, SIZE);

    //Sort array 1
    function_sort(array1);
    printf("\n\nSorted elements of Array 1: ");
    print_value(array1, SIZE);

    //input elements of Array2
    printf("\n\nEnter the elements of Array 2: \n");
    get_value(array2);
    printf("\n\n Elements of Array 2: ");
    print_value(array2, SIZE);

    //Sort array 2
    function_sort(array2);
    printf("\n\nSorted elements of Array 2: ");
    print_value(array2, SIZE);

    //Find Intersection
    num_elements = find_intersection(array1, array2, intersection_array);
    printf("\n\n Intersection is: ");
    print_value(intersection_array, num_elements);

    //Find Union
    num_elements = find_union(array1, array2, union_array);
    printf("\n\n Union is: ");
    print_value(union_array, num_elements);
}

void get_value(int arr[])
{
    int i, j;
    for (i = 0; i < SIZE; i++)
    {
        j = i + 1;
        printf("\n Enter element %d: ", j);
        scanf("%d", &arr[i]);
    }
}

void print_value(int arr[], int n)
{
    int i;
    printf("{ ");
    for (i = 0; i < n; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("}");
}

void function_sort(int arr[])
{
    int i, j, temp, swapping;

    for (i = 1; i < SIZE; i++)
    {
        swapping = 0;
        for (j = 0; j < SIZE-i; j++)
        {
            if (arr[j] > arr[j+1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapping = 1;
            }
        }
        if (swapping == 0)
        {
            break;
        }
    }
}

int find_intersection(int array1[], int array2[], int intersection_array[])
{
    int i = 0, j = 0, k = 0;
    while ((i < SIZE) && (j < SIZE))
    {
        if (array1[i] < array2[j])
        {
            i++;
        }
        else if (array1[i] > array2[j])
        {
            j++;
        }
        else
        {
            intersection_array[k] = array1[i];
            i++;
            j++;
            k++;
        }
    }
    return(k);
}

int find_union(int array1[], int array2[], int union_array[])
{
    int i = 0, j = 0, k = 0;
    while ((i < SIZE) && (j < SIZE))
    {
        if (array1[i] < array2[j])
        {
            union_array[k] = array1[i];
            i++;
            k++;
        }
        else if (array1[i] > array2[j])
        {
            union_array[k] = array2[j];
            j++;
            k++;
        }
        else
        {
            union_array[k] = array1[i];
            i++;
            j++;
            k++;
        }
    }
    if (i == SIZE)
    {
        while (j < SIZE)
        {
            union_array[k] = array2[j];
            j++;
            k++;
        }
    }
    else
    {
        while (i < SIZE)
        {
            union_array[k] = array1[i];
            i++;
            k++;
        }
    }
    return(k);
}

OUTPUT : :


/* C Program to Find Union and Intersection of 2 Arrays */

Enter the elements of Array 1:

 Enter element 1: 1

 Enter element 2: 8

 Enter element 3: 7

 Enter element 4: 3

 Enter element 5: 5


 Elements of Array 1: { 1 8 7 3 5 }

Sorted elements of Array 1: { 1 3 5 7 8 }

Enter the elements of Array 2:

 Enter element 1: 4

 Enter element 2: 7

 Enter element 3: 6

 Enter element 4: 1

 Enter element 5: 2


 Elements of Array 2: { 4 7 6 1 2 }

Sorted elements of Array 2: { 1 2 4 6 7 }

 Intersection is: { 1 7 }

 Union is: { 1 2 3 4 5 6 7 8 }

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3 2 votes
Article Rating
Category: Arrays Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments