Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sets of numbers can be represented using array of Os and 1s. The idea is that a

ID: 3665593 • Letter: S

Question

Sets of numbers can be represented using array of Os and 1s. The idea is that a [i ]! = 0 if i is in the set, and a [i] == 0 if it is not. For example, the array a [10] = {0, 0, 1, 0, 1, 1, 0, 0, 0, 0} would represent the set { 2, 4, 5} because a [2] , a [4] , and a [5] have the value 1, and everywhere else a contains zeros. Since the array has a fixed bound, say N, the values in the set are restricted to the range 0...N-1. Write a C program that reads in two sets of numbers A and B, and calculates and print their union (A union B) and intersection (A intersect B). A union B is the set of elements that appear in either A or B, and that (A intersect B is the set of elements that appear in both A and B. The values in the sets are restricted to the range 0... 9. Name your program sets. c. The program will read in the number of element in the first set, for example, 4, then read in the numbers in the set, for example, 3 6 8 9. The repeat for the second set. The two sets do not necessarily are of the same size. The sets are stored using arrays of 0s and is as described above. Calculate the union and intersection of the two sets and display the result. Sample input/output: Please enter the number of elements in set A: 3 Enter the numbers in set A: 3 5 8 Please enter the number of elements in set B: 4 Enter the numbers in set B: 7 5 9 3 Output: The union of set A and B is: 3 5 7 8 9 The intersection of set A and B is: 3 5

Explanation / Answer

#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;

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

  
function_sort(array1);
printf("nnSorted elements of Array 1: ");
print_value(array1, SIZE);

  
printf("nnEnter the elements of Array 2: n");
get_value(array2);
printf(" Elements of Array 2: ");
print_value(array2, SIZE);

  
function_sort(array2);
printf(" Sorted elements of Array 2: ");
print_value(array2, SIZE);


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

  
num_elements = find_union(array1, array2, union_array);
printf(" 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(" 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);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote