Announcement:

This is just the beginning of this blog, please don't copy any of my posts.

Tuesday, 18 December 2012

[PL 1]A Discrete Structure Program Using C Programming To Perform Various Set Operation


/*Program for various operations on set.
Operations covered :
1) Create() : for creating a new set with initial members
of the set
2) print() : diaplays all members of the set
3) Union() : finds union of two sets, set1[] and set2[] and
stores the result in set3[]
4) intersection() : finds intersection of two sets, set1[] and set2[]
and stores the result in set3[]
5) difference() :finds difference of two sets, set1[] and set2[]
and stores the result in set3[]
6) member() :function returns 1 or 0 ,depending onwhether the
element x belongs or not to a set.

7) symmdiff() : Finds Symmetric difference of two sets
Representation of a set
-----------------------
A set is representrd using an array of integers.
It may be declared as:
int set[30];
set[0] - gives number of elements in a set.
set[1] to set[29] are for storing set members.

Example :
A set,[2,11,3,5 6],when represebted will appear as:
[5][2][3][5][6][11][ ][ ][ ] 0 1 2 3 4 5 6 7 8 */

#define MAX 30
#include"STDIO.H"
#include"CONIO.H"
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set3[]);
void difference(int set1[],int set2[],int set3[]);
void symmdiff(int set1[],int set2[],int set3[]);
int member(int set[],int x);


void main()
{
int set1[MAX],set2[MAX],set3[MAX];
int x,op;
clrscr();
flushall();
set1[0]=set2[0]=set3[0]=0;
do
{
printf("\n1)Create\n2)Print\n3)Union\n4)Intersection\n5)Difference");
printf("\n6Symmetrec Difference \n7)Quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nCreting First Set*******");
create(set1);
printf("\nCreating Second Set*****");
create(set2);
break;
case 2: printf("\nFirst Set :\n");
print(set1);
printf("\n\nSecond Set :\n");
print(set2);
printf("\n\nThird Set :\n");
print(set3);
break;
case 3: Union(set1,set2,set3);print(set3);break;
case 4: intersection(set1,set2,set3);print(set3);break;
case 5: difference(set1,set2,set3);print(set3);break;
case 6: symmdiff(set1,set2,set3);print(set3);break;
}
printf("\npress a key............");
getch();
}while(op!=7);
}
/*creates set[] with initial elements*/
void create(int set[])
{ int n,i,x;
set[0]=0;/*make it a null set*/
printf("\n No. of elements in the set:");
scanf("%d",&n);
printf("\n enter set elements :");
for(i=1;i scanf("%d",&set[i]);
set[0]=n; //Number of elements.

}
void print(int set[])
{ int i,n;
n=set[0];/* number of elements in the set */
printf("\Members of the set :-->");
for(i=1;i printf("%d ",set[i]);
}
/* union of set1[] and set2[] is stored in set3[]*/
void Union(int set1[],int set2[],int set3[])
{ int i,n;
/* copy set1[] to set3[]*/
set3[0]=0;/*make set3[] a null set */
n=set1[0];/* number of elements in the set*/
//Union of set1,set2= set1 + (set2-set1)
for(i=0;i set3[i]=set1[i];

n=set2[0];
for(i=1;i if(!member(set3,set2[i]))
set3[++set3[0]]=set2[i]; // insert and increment no. of elements
}
/*function returns 1 or 0 depending on whether x belongs
to set[] or not */
int member(int set[],int x)
{ int i,n;
n=set[0]; /* number of elements in the set*/
for(i=1;i if(x==set[i])
return(1);

return(0);
}
/*intersection of set1[] and set2[] is stored in set3[]*/
void intersection(int set1[],int set2[],int set3[])
{
int i,n;
set3[0]=0; /* make a NULL set*/
n=set1[0];/* number of elements in the set*/
for(i=1;i if(member(set2,set1[i])) /* all common elements are inserted in set3[]*/
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
}
/*difference of set1[] and set2[] is stored in set3[]*/
void difference(int set1[],int set2[],int set3[])
{ int i,n;
n=set1[0];/* number of elements in the set*/
set3[0]=0;/*make it a null set*/
for(i=1;i if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
}
void symmdiff(int set1[],int set2[],int set3[])
{ int i,n;
n=set1[0];/* number of elements in the set*/
set3[0]=0;/*make it a null set*/
//Calculate set1-set2
for(i=1;i if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i]; // insert and increment no. of elements
//Calculate set2-set1
n=set2[0];
for(i=1;i if(!member(set1,set2[i]))
set3[++set3[0]]=set2[i]; // insert and increment no. of elements

}
Share it Please

Vikram

Pankaj Gaikar is a professional blogger from Pune, India who writes on Technology, Android, Gadgets, social media and latest tech updates at Punk Tech , Being Android & Shake The Tech . Email me HERE

0 comments :

Post a Comment

Copyright @ 2013 Pune University Bachelor of Engineering . Designed by Pankaj Gaikar | Love for The Tricks Machine