donmike25 icon

tuto 7 q2

donmike25 | PRO | 01/08/14 07:14:49 AM UTC | 0 ⭐ | 182 👁️ | Never ⏰ | []
C |

1.28 KB

|

None

|

0 👍

/

0 👎

#include<stdio.h>
#define SETSIZE 8
 
typedef unsigned int Set;
void printSet(Set s);
 
int main()
{ 
    Set a,b;
    a = (1<<3)|(1<<4)|(1<<5)|(1<<7);                     // create set A
    printf("A = ");printSet(a);                          // 10111000
    b = (1<<1)|(1<<3)|(1<<5);                            // create set B
    printf("B= ");printSet(b);
    printf("A union B = ");printSet(a|b);                // find union of And B
    printf("A intersect B = ");printSet(a&b);            // find intersection of A and B 
    printf("insert 6 into A.");                          // insert 6 into A
    a = (1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7);  
    printf("A =");printSet(a);               // print the new value of the set after element 6 is inserted
    printf("Is 3 an element of B? ");                    // to check if 3 is an element of B
    if ( b & (1<<3))
        printf("yes\n");
    else 
        printf("No\n");
 
    return 0;
}
 
void printSet(Set s)
{ 
    int j;
    printf("{ ");
    for (j=0; j<SETSIZE; j++)
    if ((1<<j)&s)printf("%d ",j);
    printf("}\n");
    }
 
 
 
 
/*
Example:
 
write set A= {1,3} and insert element 2 into A:
 
set A initially = 0000 0000
so, 
(1<<1) will be    0000 0010
(1<<3) will be    0000 1000
then,
(1<<1) | (1<<3) is 0000 1010
 
to insert:
(1<<2) | A      is 0000 1110
 
*/

Comments