Saturday 1 August 2015

c program for checking mutual exhaustivity of two sets

I love to code much more than anything else.I am just a noob in the world of programming but still I try to code any possible problem that comes in my mind.Today in my discrete mathematics class mutual exhaustivity and mutual exclusivity was going on.I thought why not design a program for checking whether two sets are mutually exhaustive or not.
I have written code for this program using two approaches
In the first approach i will straight out compare whether all elements of U are contained in A and B or not,In the second approach i will be first forming a union of set A and B and then comparing it to universal set.
the below text is in reference to first approach 

What are mutually exhaustive sets??
the sets whose union when combined together forms universal set then such set is known as mutually exhaustive sets.for example
U={1,2,3,4,5}
A={1,2,3}
B={1,4,5}
then as A union B = U,set A and B are mutually exhaustive sets
PSUEDOCODE
  • Firstly we will take input for universal set,set A and set B
  • Then we will check whether set A and set B are valid sets or not i.e. all elements contained in set A and set B belong to universal set or not .
  • If any exception is found we will immediately exit mentioning the sets are not mutually exhaustive
  • If number of elements in set A and set B are equal to or greater than elements in universal set then we will continue
  • otherwise exit  because if  number of elements in set A and set B are less than elements in universal set then how is it possible that a union B will form universal set
  • then we will pick one by element of Universal set and check whether it is contained in A or B if any element is found that is not in A ir B then it is not mutually exhaustive if found then counter is increased by one
  •  if at the end the value of counter is equal to number of elements n universal set than they are mutually exclusive set
CODE
___________________________________________________________________________________
#include<stdio.h>
main()
{
    int u[100],a[100],b[100],nu,na,nb,i,j,k,count=0;
    printf("\nenter the number of elements in universal set,set a& set b\n");
    scanf("%d%d%d",&nu,&na,&nb);
    printf("\nenter the elements in universal set\n");
    enterElements(u,nu);
    printf("\nenter the elements in set A\n");
    enterElements(a,na);
    if(validSet(u,nu,a,na)==-1) /* because function returns -1 if it is not a valid set */    {
        printf("non mutually exhaustive");
        exit(0);
    }
    printf("\nenter the elements in set B\n");
    enterElements(b,nb);
    if(validSet(u,nu,b,nb)==-1) /* because function returns -1 if it is not a valid set*/
    {
        printf("non mutually exhaustive");
        exit(0);
    }
    if(na+nb>=nu &&(nu>na && nu>nb)) /* the number of elements in set A and set B should be equal or greater than number of elements is universal set to be mutually exclusive*/
    {
        for(i=0; i<nu; i++)
        {
            for(j=0; j<na; j++)
            {
                if(*(u+i)==*(a+j)) /* for exiting the inner loop of set A */
                {
                    count++;
                    break;
                }
            }
            if(*(u+i)==*(a+j)) // for going on to next iteration            

{
                continue;
            }
            for(k=0; k<nb; k++) // for exiting the loop of set B
            {
                if(*(u+i)==*(b+k))
                {
                    count++;
                }
            }
        }
        if(count==nu)
        {
            printf("\nthe sets are mutually exhaustive\n");
        }
        else
            printf("\n the sets are not mutually exhaustive");
    }
    else
        printf("\n the sets are not mutually exhaustive");
}
enterElements(int *arr,int len) //function for entering elements in an array
{
    int i;
    for(i=0; i<len; i++)
    {
        scanf("%d",&*(arr+i));
    }
}
validSet(int *u,int nu,int *arr, int len) /* function for checking whether an entered set is valid subset of universal set*/{
    int i,j,count=0;
    if(len>nu)
    {
        printf("invalid set");
        return -1;
    }
    for(i=0; i<len; i++) /* this whole loop is for comparing each element in set A with universal set */
    {
        for(j=0; j<nu; j++)
        {
            if(*(arr+i)==*(u+j))
            {
                count++;
                break;
            }
        }
    }
    if(count==len) /* if count is not equal to length of array that means some other element other than universal set is present in Set */    {
        return 1;
    }
    else
    {
        return -1;
    }
}

___________________________________________________________________________________ 

NOTE:
this code is written in codeblocks GNU GCC compiler
this code might encounter issues when tried to run in turboC or any other compiler

2 comments:

  1. Really grt wrk,very efficient code and easy to undrstand....

    ReplyDelete
  2. Thanxx bro...
    jst trying to make code as easy to understand as possible

    ReplyDelete