Monday, 28 August 2017

Set Theory: P3 Difference of Two Sets B-A in C Language


Prepared By: Niraj Bhagchandani

Email: niraj.bhagchandani@live.com
Set Theory: Difference of two set B-A
IN C LANGUAGE
Figure.pngIntroduction:
  • If A and B are two sets, then their difference is given by A - B or B - A. 
    • If A = {2, 3, 4} and B = {4, 5, 6} 
    • A - B means elements of A which are not the elements of B. 
    • i.e., in the above example A - B = {2, 3} 
  • In general, B - A = {x : x ∈ B, and x ∉ A} 
  • If A and B are disjoint sets, then A – B = A and B – A = B [1]

---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 Difference value for b - a = { ");
   /* Implementation of Set Difference in C Language */
   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] = 3
a[2] = 4
a[3] = 5
How many elements in B?: 6
Enter the value for B: (not more than 9 Elements:
b[0] = 2
b[1] = 4
b[2] = 5
b[3] = 6
b[4] = 7
b[5] = 8

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

The Difference value for b - a = {  6  7  8 }

Process returned 0 (0x0)   execution time : 12.057 s
Press any key to continue.
References.
  1. “Difference of Two Sets | Difference between the Two Sets | Solved Examples.” Math Only Math, Http://Www.math-Only-Math.com, www.math-only-math.com/difference-of-two-sets.html.
    To cite this article add this reference and give credits to author.
    1. Bhagchandani, Niraj D. “Set Theory: P3 Difference of Two Sets B-A in C Language.”Everything Data Structure in C Language, Blogger, 28 Aug. 2017, edatastructure.blogspot.in/2017/08/set-theory-p2-difference-of-two-sets-b_28.html.

    No comments:

    Post a Comment