Monday, 28 August 2017

Set Theory: P4 Union of Two Sets A and B in C Language

Prepared By: Niraj Bhagchandani
Email: niraj.bhagchandani@live.com
Set Theory: Union of two set A and B
IN C LANGUAGE
VDUnion.pngIntroduction:
In set theory, the union (denoted by ∪) of a collection of sets is the set of all elements in the collection.[1] It is one of the fundamental operations through which sets can be combined and related to each other.


---Program: ---
#include <stdio.h>
#define SIZE 10
void display(int disp[]){
   int i;
   for(i=0;disp[i];i++){
       printf(" %d ", disp[i]);
   }
}
int main(){
   int i,j,k=0, flag,elem1, elem2;
   int a[SIZE]; /*={3,4,5,6,7,9,'\0'}; */
   int b[SIZE]; /*={4,6,9,10,12,'\0'}; */
   int c[SIZE]={'\0'}; /* For storing intersection values */
   char ch='y';
   /* Taking input from user for SET A */
   printf("How many elements in A?: ");
   scanf("%d", &elem1);
   printf("Enter the value for A: (not more than %d Elements:\n",SIZE-1);

   for(i=0;i<elem1 && elem1 < SIZE;i++){
       printf("a[%d] = ", i);
       scanf("%d",&a[i]);
   }
   a[i]='\0'; /* Making the last value as null*/
   printf("How many elements in B?: ");
   scanf("%d", &elem2);
   /* Taking input from user for SET B */
   printf("Enter the value for B: (not more than %d Elements:\n",SIZE-1);
   for(i=0;i<elem2 && elem2 <SIZE ;i++){
       printf("b[%d] = ", i);
       scanf("%d",&b[i]);
   }
   b[i]='\0'; /* Making the last value as null */
   printf("\nThe value of a={ ");
   display(a);
   printf(" } ");
   printf("\nThe value of b={ ");
   display(b);
   printf(" } ");

   printf("\n\nThe Union value for a U b = { ");
   /* Implementation of Union in C Language */
   for(k=0;a[k];k++){
       c[k]=a[k];
   }
   for(i=0;i< elem2 ;i++){
       flag=1;
       for(j=0;j< elem1 ;j++){
           if(b[i]==a[j]){
               flag=0; break;
           }
       }
       if(flag==1){
           c[k]=b[i]; k++;
       }
   }
   display(c);
   printf("} \n");
   return 0;
}
---Output:---


How many elements in A?: 4
Enter the value for A: (not more than 9 Elements:
a[0] = 2
a[1] = 4
a[2] = 6
a[3] = 8
How many elements in B?: 5
Enter the value for B: (not more than 9 Elements:
b[0] = 1
b[1] = 3
b[2] = 5
b[3] = 7
b[4] = 9

The value of a={  2  4  6  8  }
The value of b={  1  3  5  7  9  }

The Union value for a U b = {  2  4  6  8  1  3  5  7  9 }

Process returned 0 (0x0)   execution time : 16.273 s
Press any key to continue.
References.
  1. “Union (Set Theory).” Wikipedia, Wikimedia Foundation, 19 Aug. 2017, en.wikipedia.org/wiki/Union_(set_theory).
    To cite this article add this reference and give credits to author.

    1. Bhagchandani, Niraj D. “Set Theory: P4 Union of Two Sets A and B in C Language.”Everything Data Structure in C Language, 28 Aug. 2017, edatastructure.blogspot.in/2017/08/set-theory-p4-union-of-two-sets-and-b.html.

    No comments:

    Post a Comment