This C Program finds union & intersection of 2 arrays. Union here refers to the set of all the elements of the 2 arrays. Intersection here refers to the set of elements which are in both the arrays.
Here is source code of the C Program to find union & intersection of 2 arrays. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Find Union & 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("nnSorted elements of Array 1: ");
print_value(array1, SIZE);
//input elements of Array2
printf("nnEnter 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);
}
$ cc pgm98.c $ a.out Enter the elements of Array 1: Enter element 1: 12 Enter element 2: 34 Enter element 3: 23 Enter element 4: 56 Enter element 5: 45 Elements of Array 1: { 12 34 23 56 45 } Sorted elements of Array 1: { 12 23 34 45 56 } Enter the elements of Array 2: Enter element 1: 34 Enter element 2: 56 Enter element 3: 12 Enter element 4: 78 Enter element 5: 66 Elements of Array 2: { 34 56 12 78 66 } Sorted elements of Array 2: { 12 34 56 66 78 } Intersection is: { 12 34 56 } Union is: { 12 23 34 45 56 66 78 }
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy C Books
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Practice Computer Science MCQs
- Buy Computer Science Books