Set Operations program in C
Programs source code to perform Set Operations program in C and C++ Language.Operations on set to be performed are Given below
- Input Operations – This operation should allow the user to provide input to the program. Program should check the provided input to check whether its valid or not. If the input is not valid then program should as the user to provide input again.
- Display Operations – This operation should present the user with the requested output. Display operations will be having another sub menu to select other options.
- Union Operation – This operation of program will implement the union operation on the provided input and then displays the final output.
- Intersection Operations – This option should implement intersection operation on the provided input and output the result.
- Difference Operations – This operation should implement difference operation on the provided sets. After calculating difference user should be provided with the output.
- Symmetric Difference Operations – This option of Set Operations program in C should implement symmetric difference operation to the sets.
- Exit – Upon selecting this option program should quit.
These operations on sets are performed by implementing a simple logic in C language code . User have to provide input to Operations on Set program and it will print the selected operation on Set .
Set Operations program Code
#include #include int _union(int[],int[],int[],int,int); int input(int[]); int intersection(int[],int[],int[],int,int); int difference(int[],int[],int[],int,int); int symdiff(int[],int[],int[],int,int); void display(int[],int); void main() { int set1[20],set2[20],set3[40],s1,s2,s3,ch,ch1,ch2; clrscr(); do { clrscr(); printf("\n\n\n\t\t\t------: MENU :--------\n\n"); printf("\n\t1.\tINPUT\n"); printf("\n\t2.\tDISPLAY\n"); printf("\n\t3.\tUNION \n"); printf("\n\t4.\tINTERSECTION\n"); printf("\n\t5.\tDIFFERENCE\n"); printf("\n\t6.\tSYMMETRIC DIFFERENCE\n"); printf("\n\t7.\tEXIT\n"); printf("\n\n\n\t\tPLEASE ENTER UR CHOICE\t:- "); scanf("%d",&ch); switch(ch) { case 1 : do{ clrscr(); printf("\n\n\n\t\t\t-------: INPUT MENU :-------\n\n"); printf("\n\t1.\tSET1\n"); printf("\n\t2.\tSET2\n"); printf("\n\t3.\tRETURN TO MAIN MENU\n"); printf("\n\t\tENTER UR CHOICE :- "); scanf("%d",&ch1); switch(ch1) { case 1: s1=input(set1); break; case 2: s2=input(set2); break; case 3: break; default: printf("\n\n\tINVALID CHOICE"); getch(); } }while(ch1!=3); break; case 2: do { clrscr(); printf("\n\n\n\t\t\t-------: OUTPUT MENU :-------\n\n"); printf("\n\t1.\tSET1\n"); printf("\n\t2.\tSET2\n"); printf("\n\t3.\tRETURN TO MAIN MENU\n"); printf("\n\t\tENTER UR CHOICE :- "); scanf("%d",&ch1); switch(ch1) { case 1: display(set1,s1); break; case 2: display(set2,s2); break; case 3: break; default: printf("\n\n\tINVALID CHOICE"); getch(); } }while(ch1!=3); break; case 3 : if(s1==0 &&s2==0) printf("\n\n\t\tU HAVE TO ENTER SET FIRST"); else { s3=_union(set1,set2,set3,s1,s2); display(set3,s3); } break; case 4 : if(s1==0 &&s2==0) printf("\n\n\t\tU HAVE TO ENTER SET FIRST"); else { s3=intersection(set1,set2,set3,s1,s2); display(set3,s3); } break; case 5 : if(s1==0 &&s2==0) { printf("\n\n\t\tU HAVE TO ENTER SET FIRST"); break; } do { clrscr(); printf("\n\n\n\t\t\t-------: DIFFERENCE MENU :-------\n\n"); printf("\n\t1.\tSET1 - SET2\n"); printf("\n\t2.\tSET2 - SET1\n"); printf("\n\t3.\tRETURN TO MAIN MENU\n"); printf("\n\t\tENTER UR CHOICE :- "); scanf("%d",&ch1); switch(ch1) { case 1: s3=difference(set1,set2,set3,s1,s2); display(set3,s3); break; case 2: s3=difference(set2,set1,set3,s2,s1); display(set3,s3); break; case 3: break; default: printf("\n\n\tINVALID CHOICE"); getch(); } }while(ch1!=3); break; case 6: if(s1==0 &&s2==0) { printf("\n\n\t\tU HAVE TO ENTER SET FIRST"); break; } s3=symdiff(set1,set2,set3,s1,s2); display(set3,s3); break; case 7: break; default: printf("\n\n\tINVALID CHOICE"); getch(); } }while(ch!=7); getch(); } //END OF MAIN /***************************************************** INPUT FUNCTION *****************************************************/ int input(int set[]) { int i=0,j,s; printf("\n\tENTER THE SIZE OFTHE SET\t"); scanf("%d",&s); printf("\n\tENTER THE SET\t\t\n"); while(i<s) <br=""> { scanf("%d",&set[i]); for(j=0;j<i;j++) <br=""> if(set[i]==set[j]) { printf("\n\n\t!!!! DUPLICATED ENTRY !!!!\n\n"); printf("\t\t----------: ENTER AGAIN"); break; } if(i==j) i++; } return s; } /********************************************************* UNION FUNCTION *********************************************************/ int _union(int set1[],int set2[],int set3[],int s1,int s2) { int s3,i,j; for(i=0;i<s1;i++) <br=""> set3[i]=set1[i]; s3=i; for(i=0;i<s2;i++) <br=""> { for(j=0;j<s1;j++) <br=""> if(set1[j]==set2[i]) break; if(j==s1) set3[s3++]=set2[i]; } return s3; } /************************************************************* INTERSECTION FUNCTION ***************************************************************/ int intersection(int set1[],int set2[],int set3[],int s1,int s2) { int i,j,s3=0; for(i=0;i<s1;i++) <br=""> { for(j=0;j<s2;j++) <br=""> if(set1[i]==set2[j]) { set3[s3++]=set1[i]; break; } } return s3; } /**************************************************************** DIFFERENCE FUNCTION ******************************************************************/ int difference(int set1[],int set2[],int set3[],int s1,int s2) { int i,j,s3=0; for(i=0;i<s1;i++) <br=""> { for(j=0;j<s2;j++) <br=""> if(set1[i]==set2[j]) break; if(j==s2) set3[s3++]=set1[i]; } return s3; } /********************************************************** SYMDIFF FUNCTION ***********************************************************/ int symdiff(int set1[],int set2[],int set3[],int s1,int s2) { int s3=0,se1[20],se2[20],si1,si2; si1=difference(set1,set2,se1,s1,s2); si2=difference(set2,set1,se2,s2,s1); s3=_union(se1,se2,set3,si1,si2); return s3; } /********************************************************* DISPLAY FUNCTION **********************************************************/ void display(int set[],int size) { int i; printf("\n\n\t{ "); for(i=0;i<size;i++) <br=""> printf("\t%d",set[i]); printf("\t} "); getch(); }
Asadullah Muradi says
best ever… but little long…
ProjectsGeek says
Thanks