1: /*
2: * Title : Set Operations Using Array
3: *
4: * Author : Shivkumar, Mujahid
5: *
6: */
7: #include<stdio.h>
8: int uni(int a[10],int b[10],int k,int l);
9: int inter(int a[10],int b[10],int k,int l);
10: int diff(int a[10],int b[10],int k,int l);
11: int symdiff(int a[10],int b[10],int k,int l);
12: /*----- Main Function -----*/
13: int main()
14: {
15: int a[10],b[10],i,j,k,l,choice,count;
16: printf("Enter number of elements in set A: ",k);
17: scanf("%d",&k);
18: printf("Enter elements in set A: ");
19: printf("\n");
20: for(i=0;i<k;i++)
21: scanf("%d",&a[i]);
22: printf("\nEnter number of elements in set B: ",l);
23: scanf("%d",&l);
24: printf("Enter elements in first set B:");
25: printf("\n");
26: for(i=0;i<l;i++)
27: scanf("%d",&b[i]);
28: do
29: {
30: printf("\n---------------------------------------------------------------");
31: printf("\nChoose the operation which you want to perform:");
32: printf("\n 1.Union\n 2.Intersection\n 3.Difference\n 4.Symmetric difference\n 5.Exit",choice);
33: printf("\nYour choice is: ");
34: scanf("%d",&choice);
35: printf("\n\n");
36: switch(choice)
37: {
38: case 1:
39: if(choice==1)
40: uni(a,b,k,l);
41: break;
42: case 2:
43: if(choice==2)
44: inter(a,b,k,l);
45: break;
46: case 3:
47: if(choice==3)
48: diff(a,b,k,l);
49: break;
50: case 4:
51: if(choice==4)
52: symdiff(a,b,k,l);
53: break;
54: case 5:
55: exit(0);
56: break;
57: default:
58: printf("Invalid choice.....!!!");
59: }
60: }while(choice!=5);
61: return 0;
62: }
63: /*----- Union Function ------*/
64: int uni(int a[10],int b[10],int k,int l)
65: {
66: int i,j, flag=1;
67: printf("Union of set A and B is: ");
68: for(i=0;i<k;i++)
69: printf(" %d ",a[i]);
70: for(i=0;i<l;i++)
71: {
72: flag=1;
73: for(j=0;j<k;j++)
74: {
75: if(a[j]==b[i] && flag==1)
76: {
77: flag=0;
78: }
79: }
80: if(flag!=0)
81: printf(" %d ",b[i]);
82: }
83: return 0;
84: }
85: /*----- Intersection Function -----*/
86: int inter(int a[10],int b[10],int k,int l)
87: {
88: int i,j;
89: printf("Intersection of set A and B is: ");
90: for(i=0;i<l;i++)
91: {
92: for(j=0;j<k;j++)
93: {
94: if(a[j]==b[i])
95: {
96: printf(" %d ",b[i]);
97: }
98: }
99: }
100: return 0;
101: }
102: /*----- Difference function ------*/
103: int diff(int a[10],int b[10],int k,int l)
104: {
105: int i,j,choice,flag=1;
106: printf("Choose the operation which you want to perform:");
107: printf("\n 1.a-b \n 2.b-a\n \n",choice);
108: printf("\nYour choice is: ");
109: scanf("%d",&choice);
110: printf("\n");
111: printf("\nDifference between set A and B is: \n");
112: switch(choice)
113: {
114: case 1: /*---- a-b case ------*/
115: if(choice==1)
116: {
117: printf("a-b: ");
118: for(i=0;i<k;i++)
119: {
120: flag=1;
121: for(j=0;j<l;j++)
122: {
123: if(a[i]==b[j] && flag==1)
124: {
125: flag=0;
126: }
127: }
128: if(flag!=0)
129: {
130: printf(" %d ",a[i]);
131: }
132: }
133: }
134: break;
135: case 2: /*------ b-a ---------*/
136: if(choice==2)
137: {
138: printf("b-a: ");
139: for(j=0;j<l;j++)
140: {
141: flag=1;
142: for(i=0;i<k;i++)
143: {
144: if(b[j]==a[i] && flag==1)
145: {
146: flag=0;
147: }
148: }
149: if(flag!=0)
150: {
151: printf(" %d ",b[j]);
152: }
153: }
154: }
155: break;
156: }
157: return 0;
158: }
159: /*----- Symmetric Difference ------*/
160: int symdiff(int a[10],int b[10],int k,int l)
161: {
162: int i,j,flag=1;
163: printf("Symmetric Difference between set A and B is: ");
164: for(i=0;i<k;i++)
165: {
166: flag=1;
167: for(j=0;j<l;j++)
168: {
169: if(a[i]==b[j] && flag==1)
170: {
171: flag=0;
172: }
173: }
174: if(flag!=0)
175: {
176: printf(" %d ",a[i]);
177: }
178: }
179: for(j=0;j<l;j++)
180: {
181: flag=1;
182: for(i=0;i<k;i++)
183: {
184: if(b[j]==a[i] && flag==1)
185: {
186: flag=0;
187: }
188: }
189: if(flag!=0)
190: {
191: printf(" %d ",b[j]);
192: }
193: }
194: return 0;
195: }
196: ===============OUTPUT==================
197: Enter number of elements in set A: 6
198: Enter elements in set A:
199: 10
200: 12
201: 13
202: 15
203: 16
204: 18
205: Enter number of elements in set B: 5
206: Enter elements in first set B:
207: 10
208: 12
209: 15
210: 20
211: 25
212: ---------------------------------------------------------------
213: Choose the operation which you want to perform:
214: 1.Union
215: 2.Intersection
216: 3.Difference
217: 4.Symmetric difference
218: 5.Exit
219: Your choice is: 1
220: Union of set A and B is: 10 12 13 15 16 18 20 25
221: ---------------------------------------------------------------
222: Choose the operation which you want to perform:
223: 1.Union
224: 2.Intersection
225: 3.Difference
226: 4.Symmetric difference
227: 5.Exit
228: Your choice is: 2
229: Intersection of set A and B is: 10 12 15
230: ---------------------------------------------------------------
231: Choose the operation which you want to perform:
232: 1.Union
233: 2.Intersection
234: 3.Difference
235: 4.Symmetric difference
236: 5.Exit
237: Your choice is: 3
238: Choose the operation which you want to perform:
239: 1.a-b
240: 2.b-a
241: Your choice is: 1
242: Difference between set A and B is:
243: a-b: 13 16 18
244: ---------------------------------------------------------------
245: Choose the operation which you want to perform:
246: 1.Union
247: 2.Intersection
248: 3.Difference
249: 4.Symmetric difference
250: 5.Exit
251: Your choice is: 4
252: Symmetric Difference between set A and B is: 13 16 18 20 25
253: ---------------------------------------------------------------
254: Choose the operation which you want to perform:
255: 1.Union
256: 2.Intersection
257: 3.Difference
258: 4.Symmetric difference
259: 5.Exit
260: Your choice is: 5
No comments:
Post a Comment