Write a program with a language of your choice takes two sets as input and outpu
ID: 3881886 • Letter: W
Question
Write a program with a language of your choice takes two sets as input and outputs the intersection, union, difference, and Cartesian product of the two sets. NOTE 1/ Your program must have functions for each operation. 2/ Please submit the complete program to Beachboard under Lab 2 by 02/15/2018 at the beginning of class. 3/ You will demo vour program during the lab. 4/ Here is a sample run: Set A: 11, 2, 3, 41 Set B: 12, 3, 4, 5} The intersection of A and B: 12, 3, 4 The union of A and B: 11, 2, 3, 4, 5]} The difference of A and B: 11} The Cartesian product of A and B: ((1, 2), (1, 3), (1, 4), (1, 5), (2, 5/ Here is another sample run: Set A: {1) Set B: 12, 3, 4, 5} The intersection of A and B: t h The union of A and B: 11, 2, 3, 4, 5} The difference of A and B: 11} The Cartesian product of A and B: ((1, 2), (1, 3), (1, 4), (1, 5)>Explanation / Answer
ANSWER: Here I am giving you this code in java and output also.
CODE:
import java.util.*;
class UnionAndIntersection
{
public static void diffrence (int arr1[],int arr2[])
{
int count=0;
System.out.print(" The difference of sets A and B are: ");
for (int i = 0; i < arr1.length; i++)
{
count=0;
for (int j = 0; j < arr2.length ; j++)
{
if ((arr1[i] != arr2[j]))
{
count++;
}
if(count==arr2.length){
System.out.print(arr1[i]+" ");
}
}
}
}
public static void cartesianProduct(int[] s1, int[] s2) {
List<int[]> list = new ArrayList<>();
int n=list.size();
for (int v1: s1) {
for (int v2: s2) {
list.add(new int[]{v1, v2});
}
}
int[][] result = new int[list.size()][2];
int k=0;
for(int[] i: list){
result[k++] = i;
}
System.out.println(" The cartesianProduct will be : ");
for (int[] x : result)
{
for (int y : x)
{
System.out.print(y + " ");
}
System.out.println();
}
}
void printUnion(int arr1[], int arr2[], int m, int n)
{
if (m > n)
{
int tempp[] = arr1;
arr1 = arr2;
arr2 = tempp;
int temp = m;
m = n;
n = temp;
}
Arrays.sort(arr1);
for (int i = 0; i < m; i++)
System.out.print(arr1[i] + " ");
for (int i = 0; i < n; i++)
{
if (binarySearch(arr1, 0, m - 1, arr2[i]) == -1)
System.out.print(arr2[i] + " ");
}
}
void printIntersection(int arr1[], int arr2[], int m, int n)
{
if (m > n)
{
int tempp[] = arr1;
arr1 = arr2;
arr2 = tempp;
int temp = m;
m = n;
n = temp;
}
Arrays.sort(arr1);
for (int i = 0; i < n; i++)
{
if (binarySearch(arr1, 0, m - 1, arr2[i]) != -1)
System.out.print(arr2[i] + " ");
}
}
// A recursive binary search function. It returns location of x in
// given array arr[l..r] is present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
public static void main(String[] args)
{
UnionAndIntersection u_i = new UnionAndIntersection();
int arr1[] = {7, 1, 5, 2, 3, 6};
int arr2[] = {3, 8, 6, 20, 7};
int m = arr1.length;
int n = arr2.length;
System.out.println("Union of two arrays is ");
u_i.printUnion(arr1, arr2, m, n);
System.out.println("");
System.out.println("Intersection of two arrays is ");
u_i.printIntersection(arr1, arr2, m, n);
diffrence(arr1,arr2);
cartesianProduct(arr1,arr2);
}
}
OUTPUT:
Union of two arrays is
3 6 7 8 20 1 5 2
Intersection of two arrays is
7 3 6
The difference of sets A and B are:
1 5 2
The cartesianProduct will be :
7 3
7 6
7 7
7 8
7 20
1 3
1 6
1 7
1 8
1 20
5 3
5 6
5 7
5 8
5 20
2 3
2 6
2 7
2 8
2 20
3 3
3 6
3 7
3 8
3 20
6 3
6 6
6 7
6 8
6 20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.