C Program source code to demonstrate common operations on Sets#include<stdio.h> int *setA,*setB; int ctr,octr,ictr,sizeA,sizeB; void unionsets() {//The function to calculate union of two sets int *unions=(int *)malloc((sizeA+sizeB)*sizeof(int)); ctr=0;//array unions declared to hold size of max possible union set for(octr=0;octr<sizeA;octr++) unions[ctr++]=setA[octr];//copy Set A for(octr=0;octr<sizeB;octr++) { int flag=0;//Copy those elements of set B which are not in set A for(ictr=0;ictr<sizeA;ictr++) { if(setA[ictr]==setB[octr]) flag=1;//flag keeps track whether element is found or not } if(flag==0) unions[ctr++]=setB[octr]; } printf("\nA Union B is \n"); for(octr=0;octr<ctr;octr++)//display set printf("%d\t",unions[octr]); free(unions); } void intersection() { int size; if(sizeA>=sizeB) size=sizeB; else//the sizxe of intersection set will be of the size of the smaller of two sets size=sizeA; ctr=0; int *intersection=(int *)malloc(size*sizeof(int)); for(octr=0;octr<sizeA;octr++) { for(ictr=0;ictr<sizeB;ictr++) { if(setA[octr]==setB[ictr])//copy those elemts which are common to both intersection[ctr++]=setB[ictr]; } } printf("\nA Intersection B is \n"); if(ctr==0) {//if no elemnts are there show null and return printf("NULL"); return; }//display for(octr=0;octr<ctr;octr++) printf("%d\t",intersection[octr]); free(intersection); } void difference() { int *BdiffA=(int *)malloc(sizeB*sizeof(int)); ctr=0;//the difeernce array can have size at max Of B if we find B-A for(octr=0;octr<sizeB;octr++) { int flag=0; for(ictr=0;ictr<sizeA;ictr++) { if(setA[ictr]==setB[octr]) flag=1;//if flag=1 the elemnt is in A and in B } if(flag==0)//copy only those elemnts which are in B not A BdiffA[ctr++]=setB[octr]; } printf("\nB - A is \n"); if(ctr==0) { printf("NULL"); return;//if ctr=0 then NULL } for(octr=0;octr<ctr;octr++) printf("%d\t",BdiffA[octr]);//display free(BdiffA); } void CardinalProduct() { int **cardinalProduct=(int **)malloc(sizeA*sizeB*sizeof(int));//There will be sizeA*sizeB rows in the cardinal product matrix for(ctr=0;ctr<sizeA*sizeB;ctr++) cardinalProduct[ctr]=(int *)malloc(2*sizeof(int));//1 cardinal product has 2 elements (a,b) ictr=0; for(ctr=0;ctr<sizeA*sizeB;ctr++) { cardinalProduct[ctr][0]=setA[ctr/sizeB];//way to fill the first element with appropriate set A element cardinalProduct[ctr][1]=setB[ctr%sizeB];//way to fill the second element with appropriate set B element } printf("\nA*B is \n"); for(ctr=0;ctr<sizeA*sizeB;ctr++) printf("(%d,%d)\t",cardinalProduct[ctr][0],cardinalProduct[ctr][1]);//displaying in cartesian form for(ctr=0;ctr<sizeA*sizeB;ctr++) free(cardinalProduct[ctr]); free(cardinalProduct); } int main() { printf("Enter the size for set A\n"); scanf("%d",&sizeA); printf("Enter the size for set B\n"); scanf("%d",&sizeB); setA=(int *)malloc(sizeA*sizeof(int)); setB=(int *)malloc(sizeB*sizeof(int)); printf("Enter the elements of set A\n"); for(ctr=0;ctr<sizeA;ctr++) scanf("%d",&setA[ctr]); printf("Enter the elements of set B\n"); for(ctr=0;ctr<sizeB;ctr++) scanf("%d",&setB[ctr]); unionsets(); intersection(); difference(); CardinalProduct(); free(setA); free(setB); getch(); } /*A sample run of the program runs as follows:- Enter the size for set A 3 Enter the size for set B 4 Enter the elements of set A 1 2 3 Enter the elements of set B 4 5 6 7 A Union B is 1 2 3 4 5 6 7 A Intersection B is NULL B - A is 4 5 6 7 A*B is (1,4) (1,5) (1,6) (1,7) (2,4) (2,5) (2,6) (2,7) (3,4) (3,5) (3,6) (3,7) */ |
Computer Science >