Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Ans:

#include<stdio.h>
int A[30],B[30],C[60];
void Creat(int S[],int n)
{
    int i;
    for(i=1;i<=n;i++)
        scanf("%d",&S[i]);
    S[0]=n;
}

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

int elements(int S[],int x)
{ 
    int n,i;
    n=S[0];
    for(i=1;i<=n;i++)
        if(S[i]==x)
            return 1;
    return 0;
}

void set_union(int A[],int B[])
{
    int n,i,m;
    n=A[0];
    for(i=1;i<=n;i++)
        C[i]=A[i];
    m=B[0];
    for(i=1;i<=m;i++)
        if(!elements(A,B[i]))
            C[++n]=B[i];
    C[0]=n;
}

void set_intersection(int A[],int B[])
{
    int n,i,j=0;
    n=A[0];
    for(i=1;i<=n;i++)
        if(elements(B,A[i]))
            C[++j]=A[i];
    C[0]=j;
}

int main()
{
    int n;
    
    printf("\nHow many elements for Set-1 ");
    scanf("%d",&n);
    printf("\nEnter elements\n");
    Creat(A,n);
    
    printf("\nHow many elements for Set-2 ");
    scanf("%d",&n);
    printf("\nEnter elements\n");
    Creat(B,n);
    
    set_union(A,B);
    printf("\nA union B = C = ");
    print(C);
    
    set_intersection(A,B);
    printf("\nA intersection B = C = ");
    print(C);
    
}

Result:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.