Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <stdio.

h>
#include <stdlib.h>

void print_array(int[], int);


void compute_union(int[], int[], int, int[]);
void compute_intersection(int, int[], int[], int[]);
int size1, size2;
int union_size=0;

int main()
{

printf("Enter the size of your first array: ");


scanf("%d", &size1);
printf("Enter the size of your second array: ");
scanf("%d", &size2);
printf("Enter the values of your array: ");

int i,j;
int arr1[size1];
int arr2[size2];
int set_union[size1+size2];
int set_intersection[(size1+size2)/2];

for(i=0; i<size1; i++)


{
scanf("%d", &arr1[i]);

for(j=0; j<size2; j++)


{
scanf("%d", &arr2[j]);
}

//print_array(arr1,size1);
compute_union(arr1, arr2, size1+size2, set_union);

//print_array(set_union, size1+size2);
compute_intersection((size1+size2)/2, arr1, arr2, set_intersection);

//print_array(set_intersection, (size1+size2)/2);

return 0;
}

void print_array(int array[], int size)


{
int x;
for(x=0; x<size; x++)
{
printf("%d ", array[x]);
}

printf("\n");
}
void compute_union(int s1[], int s2[], int max_size, int set_union[]){

int i=0;
int j=0;

while(i<size1 && j<size2)


{
if(s1[i]<s2[j])
{
set_union[i+j]=s1[i];
i++;
}
else if (s1[i]>s2[j])
{
set_union[i+j]=s2[j];
j++;
}
else
{
set_union[i+j]=s1[i];
i++;
j++;
}
}
//add remainder of nonempty array

while(i<size1)
{
set_union[i+j]=s1[i];
i++;
}
while(j<size2)
{
set_union[i+j]=s2[j];
j++;
}

print_array(set_union, size1+size2);
}

void compute_intersection(int max_size, int set1[], int set2[], int


set_intersection[])
{
int i, j, n=0;
for(i=0; i<size1; i++)
{
for(j=0; j<size2; j++)
{
if(set1[i]==set2[j])
{
set_intersection[k]=set1[i];
n++;
break;
}
}
}

print_array(set_intersection, n);
}

You might also like