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

1 Overview For this assignment, you\'re going to write a C program to operate on

ID: 3598073 • Letter: 1

Question

1 Overview For this assignment, you're going to write a C program to operate on sets. Our universe of elements consists of all lowercase letters. Your program should prompt the user for the elements of the first set (called A): the user enters the elements, followed by . Do the same for the second set (B). Then print the following information: . The elements of A in the usual format: (e,e,., en) . The elements of B . The cardinality of A and B 2 Details . You should prompt the user for their two input strings. - Remember, a set cannot contain more than one copy of an element. If the user enters an element twice, it should only appear in the set once. . You may assume the elements of a set are entered without any spaces between them. Remember to ignore any trailing newlines in your input. , the empty set is a valid set . Submit your .c program via Canvas by the due date/time. If your code includes multiple files, submit them as a tar file (tar).

Explanation / Answer

#include <stdio.h>

#include<limits.h>

#include<stdlib.h>

#include<stdbool.h>

void union_set(int set1[], int set2[], int Num1, int Num2);

void intersection_set(int set1[], int set2[], int Num1, int Num2);

void complemet_set(int set1[], int set2[], int Num1, int Num2);

// to sort the array

void merge_sort(int arr[],int,int);

void merge(int arr[],int,int,int);

int main()

{

    int arr1[1000], arr2[1000], arr3[1000], temp[1000];

    int i, Num1, j, Num2 ;

    printf("Enter size of set A: ");

    scanf("%d", &Num1);

    printf("Enter the size of set B: ");

    scanf("%d", &Num2);

    printf("Enter %d elements in set A : ", Num1);

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

    {

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

    }

    printf("Enter %d elements in set B: ", Num2);

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

    {

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

    }

    merge_sort(arr1, 0, Num1 - 1);

    printf(" Elements in set A are: ");

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

    {

        printf("%d", arr1[i]);

        if(i != Num1 - 1)

            printf(", ");

    }

    merge_sort(arr2, 0, Num2 - 1);

    printf(" Elements in set B are: ");

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

    {

        printf("%d", arr2[j]);

        if(i != Num2 - 1)

            printf(", ");

    }

    // find the union of two sets

    union_set(arr1, arr2, Num1, Num2);

    // find the intersection of two sets

    intersection_set(arr1, arr2, Num1, Num2);

    // find arr1 arr2

    complemet_set(arr1, arr2, Num1, Num2);

     

    return 0;

}

// to sort the array using merge sort

void merge_sort(int arr[],int p,int r)

{

    if(p<r)

    {

        // find the mid index

        int q=(p+r)/2;

     

        // recursively sort the left subarray

        merge_sort(arr,p,q);

     

        // recursively sort the right subarray

        merge_sort(arr,q+1,r);

     

        // merge the two subarrays

        merge(arr,p,q,r);

    }

}

// merge the two subarrays

void merge(int arr[],int p,int q,int r)

{

    // get the length of the left half of array

    int n1=q-p+1;

    // get the length of the right half of array

    int n2=r-q;

    int *left,*right,i,j,k;

    // create array of size n1 + 1

    left=(int *)malloc((n1+1)*sizeof(int));

// create array of size n2 + 1

    right=(int *)malloc((n2+1)*sizeof(int));

    // copy the required elements of arr in left

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

        left[i]=arr[p+i];

     

    // copy the required elements of arr in right

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

        right[i]=arr[q+i+1];

     

    // set the last element of left to infinity

    left[n1]=INT_MAX;

    // set the last element of right to infinity

    right[n2]=INT_MAX;

    i=0;

    j=0;

    // merge the two subarrays

    for(k=p;k<=r;k++)

    {

        // if the element of left is smaller the

        // element of right

        if(left[i]<=right[j])

        {

            // set the k th element of arr to i th element of left

            arr[k]=left[i];

            i++;

        }

        else

        {

            // set the k th element of arr to j th element of right

            arr[k]=right[j];

            j++;

        }

    }

}

void union_set(int set1[], int set2[], int Num1, int Num2)

{

    int x = 0;

    int y = 0;

    printf(" Union : ");

    // traverse until iteration of any one

    // of the array finishes

    while(x < Num1 && y < Num2)

    {

        // if current element of set2 is

        // larger then the larger elemene of set1

        if(set1[x] < set2[y])

        {

            printf("%d ", set1[x]);

            x++;

        }

        // if current element of set1 is

        // larger then the larger elemene of set2

        else if(set2[y] < set1[x])

        {

            printf("%d ", set2[y]);

          y++;

        }

        // if both the elements are equal

        else

        {

            printf("%d ", set1[x]);

            x++;

            y++;

        }

    }

    // print the left over elements of set1

    while(x < Num1)

    {

        printf("%d ", set1[x]);

        x++;

    }

    // print the left over elements of set2

    while(y < Num2)

    {

        printf("%d ", set2[y]);

        y++;

    }

}

void intersection_set(int set1[], int set2[], int Num1, int Num2)

{

    int x = 0;

    int y = 0;

    printf(" Intersection : ");

    // traverse until iteration of any one

    // of the array finishes

    while(x < Num1 && y < Num2)

    {

        // if current element of set2 is

        // larger then the larger elemene of set1

        if(set1[x] < set2[y])

            x++;

        // if current element of set1 is

        // larger then the larger elemene of set2

        else if(set2[y] < set1[x])

            y++;

        // if both the elements are equal

        else

        {

            printf("%d ", set1[x]);

            x++;

            y++;

        }

    }

}

// return true if target is present in set arr

// else return false

bool if_in_set(int arr[], int n, int target)

{

    int i;

    // traverse the set

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

    {

        // if the required element is found

        if(arr[i] == target)

        {

            return true;

        }

    }

         

    // if the required element is not found

    return false;

}

// find set1 set2 i.e : all elements of set1 that are not present in set2

void complemet_set(int set1[], int set2[], int Num1, int Num2)

{

    int i;

    printf(" Complement : ");

// traverse set1

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

    {

        // check if the current element in set1

        // is not present in set2

        if(!if_in_set(set2, Num2, set1[i]))

        {

            printf("%d ", set1[i]);

        }

    }

}