C program to find intersection and union of 2 arrays.

C program to find intersection and union of 2 arrays is as follows:

#include<stdio.h>
#include<conio.h>

#define SIZE 5

void get_data(int arr[]);
void print_data(int arr[], int n);
void bubble_sort(int arr[]);
int find_intersection(int array_1[], int array_2[], int intersect_result[]);
int find_union(int array_1[], int array_2[], int union_result[]);

void main()
{
	int array_1[SIZE], array_2[SIZE], intersect_result[SIZE], union_result[SIZE*2];
	int num_elements;
	clrscr();

	//Get the elements of Array1
	printf("\nEnter the elements of Array 1: \n");
	get_data(array_1);

	printf("\n\nElements of Array 1: ");
	print_data(array_1, SIZE);

	//Sort array 1
	bubble_sort(array_1);
	printf("\n\nSorted elements of Array 1: ");
	print_data(array_1, SIZE);

	//Get the elements of Array2
	printf("\n\nEnter the elements of Array 2: \n");
	get_data(array_2);

	printf("\n\nElements of Array 2: ");
	print_data(array_2, SIZE);

	//Sort array 2
	bubble_sort(array_2);
	printf("\n\nSorted elements of Array 2: ");
	print_data(array_2, SIZE);

	//Find Intersection and print the result
	num_elements = find_intersection(array_1, array_2, intersect_result);	
	printf("\n\nIntersection is: ");
	print_data(intersect_result, num_elements);

	//Find Union
	num_elements = find_union(array_1, array_2, union_result);
	printf("\n\nUnion is: ");
	print_data(union_result, num_elements);

	getch();
}

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

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

void bubble_sort(int arr[])
{
	int i,j,temp,swapped;

	for(i=1; i<SIZE; i++)
	{
		swapped = 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;
				swapped = 1;
			}
		}

		if(swapped == 0)
		{
			break;
		}
	}
}

int find_intersection(int array_1[], int array_2[], int intersect_result[])
{
	int i = 0, j = 0, k = 0;
	while((i<SIZE) && (j<SIZE))
	{
		if(array_1[i] < array_2[j])
		{
			i++;
		}
		else if(array_1[i] > array_2[j])
		{
			j++;
		}
		else
		{
			intersect_result[k] = array_1[i];
			i++;
			j++;
			k++;
		}
	}

	return(k);
}

int find_union(int array_1[], int array_2[], int union_result[])
{
	int i = 0, j = 0, k = 0;	
	while((i<SIZE) && (j<SIZE))
	{
		if(array_1[i] < array_2[j])
		{
			union_result[k] = array_1[i];
			i++;
			k++;
		}
		else if(array_1[i] > array_2[j])
		{
			union_result[k] = array_2[j];
			j++;
			k++;
		}
		else
		{
			union_result[k] = array_1[i];
			i++;
			j++;
			k++;
		}
	}

	if(i == SIZE)
	{
		while(j<SIZE)
		{
			union_result[k] = array_2[j];
			j++;
			k++;
		}
	}
	else
	{
		while(i<SIZE)
		{
			union_result[k] = array_1[i];
			i++;
			k++;
		}
	}

	return(k);
}