Sunday, 27 August 2017

Set Theory: P1 Intersection of two set in C Language

Prepared By: Niraj Bhagchandani
Email: niraj.bhagchandani@live.com
Set Theory: Intersection of two set
IN C LANGUAGE
Intersection_of_two_sets_A_and_B.svg.pngIntroduction:
In mathematics, the intersection AB of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements.[2]
---Program: ---
#include <stdio.h>
#define SIZE 10

int main(){
   int i,j;
   int a[SIZE]; /*={3,4,5,6,7,9,'\0'};*/
   int b[SIZE]; /*={4,6,9,10,12,'\0'};*/
   char ch='y';
   /* Taking input from user for SET A */
   printf("Enter the value for A: (not more than %d Elements:\n",SIZE-1);
   for(i=0;ch!='n' && i<SIZE;i++){
       printf("\na[%d] = ", i);
       scanf("%d",&a[i]);
       printf("Do you want to add more element: ");
       fflush(stdin);
       scanf("%c", &ch);
   }
   a[i]='\0'; /* Making the last value as null*/
   ch='y'; /* Changing the value to yes to input set B */
   /* Taking input from user for SET B */
   printf("Enter the value for B: (not more than %d Elements:\n",SIZE-1);
   for(i=0;ch!='n' && i<SIZE;i++){
       printf("b[%d] = ", i);
       scanf("%d",&b[i]);
       printf("Do you want to add more element: ");
       fflush(stdin);
       scanf("%c", &ch);
   }
   b[i]='\0'; /* Making the last value as null*/

   printf("\n\n The intersection value for A ∩ B = { ");
   for(i=0;a[i];i++){
       for(j=0;b[j];j++){
           if(a[i]==b[j]){
               printf(" %d ", a[i]);
           }
       }
   }
   printf("} \n");
   return 0;
}
---Output:---


Enter the value for A: (not more than 9 Elements:
a[0] = 2
Do you want to add more element: y
a[1] = 3
Do you want to add more element: y
a[2] = 4
Do you want to add more element: y
a[3] = 5
Do you want to add more element: y
a[4] = 6
Do you want to add more element: n
Enter the value for B: (not more than 9 Elements:
b[0] = 1
Do you want to add more element: y
b[1] = 3
Do you want to add more element: y
b[2] = 5
Do you want to add more element: y
b[3] = 8
Do you want to add more element: y
b[4] = 9
Do you want to add more element: n
The intersection value for A ∩ B = {  3  5 }
Process returned 0 (0x0)   execution time : 81.064 s
Press any key to continue.
References.
  1. K, Petr. “Intersection of Two Sets.”Https://Commons.wikimedia.org/Wiki/File:Intersection_of_two_sets_A_and_B.Svg, Wikimedia.org, upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Intersection_of_two_sets_A_and_B.svg/2000px-Intersection_of_two_sets_A_and_B.svg.png.
  2. “Intersection (Set Theory).” Wikipedia, Wikimedia Foundation, 16 Aug. 2017, en.wikipedia.org/wiki/Intersection_(set_theory).
About This Article:
  1. Blog Post: Blog Post - Intersection of Two Sets

No comments:

Post a Comment