0% found this document useful (0 votes)
16 views5 pages

Set Operations

The document contains a C program that performs various set operations including union, intersection, difference, and symmetric difference on two sets. Users can input the sets, display them, and choose operations through a menu interface. The program continues to run until the user decides to exit.

Uploaded by

kaveri.nagare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Set Operations

The document contains a C program that performs various set operations including union, intersection, difference, and symmetric difference on two sets. Users can input the sets, display them, and choose operations through a menu interface. The program continues to run until the user decides to exit.

Uploaded by

kaveri.nagare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name:=Kaveri Nagare

Rollno:=UIT2025005
Title:=Set Operations
______________________________________________________________________________
#include <stdio.h>

int setA[10], setB[10], setU[20];


int i, j, k, flag;
int cardA, cardB, cardU;

int Accept(int setX[]) {


int cardX = 0;
printf("Enter the Cardinality of the set: ");
fflush(stdout);
scanf("%d", &cardX);

printf("Enter %d elements: ", cardX);


fflush(stdout);
for (i = 0; i < cardX; i++) {
scanf("%d", &setX[i]);
}
return cardX;
}

void Display(int setX[], int cardX) {


printf("{ ");
for (i = 0; i < cardX; i++) {
printf("%d ", setX[i]);
}
printf("}\n");
fflush(stdout);
}

int Union(int setA[], int cardA, int setB[], int cardB, int setU[]) {
k = 0;
for (i = 0; i < cardA; i++) {
setU[k++] = setA[i];
}
for (j = 0; j < cardB; j++) {
flag = 0;
for (i = 0; i < cardA; i++) {
if (setB[j] == setA[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
setU[k++] = setB[j];
}
}
return k;
}

int Intersection(int setA[], int cardA, int setB[], int cardB, int setI[]) {
k = 0;
for (i = 0; i < cardA; i++) {
for (j = 0; j < cardB; j++) {
if (setA[i] == setB[j]) {
setI[k++] = setA[i];
break;
}
}
}
return k;
}

int Difference(int setA[], int cardA, int setB[], int cardB, int setD[]) {
k = 0;
for (i = 0; i < cardA; i++) {
flag = 0;
for (j = 0; j < cardB; j++) {
if (setA[i] == setB[j]) {
flag = 1;
break;
}
}
if (flag == 0) {
setD[k++] = setA[i];
}
}
return k;
}

int SymmetricDifference(int setA[], int cardA, int setB[], int cardB, int setS[]) {
int temp1[20], temp2[20];
int cardD1 = Difference(setA, cardA, setB, cardB, temp1);
int cardD2 = Difference(setB, cardB, setA, cardA, temp2);
return Union(temp1, cardD1, temp2, cardD2, setS);
}

int main() {
int ch;
while (1) {
printf("\n--- Menu ---\n");
printf("1. Accept setA and setB\n");
printf("2. Display sets\n");
printf("3. Union of sets\n");
printf("4. Intersection of sets\n");
printf("5. Difference (A - B)\n");
printf("6. Symmetric Difference\n");
printf("7. Exit\n");
printf("Enter your choice: ");
fflush(stdout);
scanf("%d", &ch);

switch (ch) {
case 1:
cardA = Accept(setA);
cardB = Accept(setB);
break;
case 2:
printf("Set A: ");
Display(setA, cardA);
printf("Set B: ");
Display(setB, cardB);
break;
case 3:
cardU = Union(setA, cardA, setB, cardB, setU);
printf("Union: ");
Display(setU, cardU);
break;
case 4: {
int setI[20], cardI = Intersection(setA, cardA, setB, cardB, setI);
printf("Intersection: ");
Display(setI, cardI);
break;
}
case 5: {
int setD[20], cardD = Difference(setA, cardA, setB, cardB, setD);
printf("Difference (A - B): ");
Display(setD, cardD);
break;
}
case 6: {
int setS[20], cardS = SymmetricDifference(setA, cardA, setB, cardB, setS);
printf("Symmetric Difference: ");
Display(setS, cardS);
break;
}
case 7:
printf("Exiting...\n");
fflush(stdout);
return 0;
default:
printf("Invalid choice! Try again.\n");
fflush(stdout);
}
}
return 0;
}
OUTPUT:
Menu ---
1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 1
Enter the Cardinality of the set: 4
Enter 4 elements: 1 3 2 4
Enter the Cardinality of the set: 4
Enter 4 elements: 2 1 3 6

--- Menu ---


1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 3
Union: { 1 3 2 4 6 }

--- Menu ---


1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 4
Intersection: { 1 3 2 }

--- Menu ---


1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 5
Difference (A - B): { 4 }

--- Menu ---


1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 6
Symmetric Difference: { 4 6 }

--- Menu ---


1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 2
Set A: { 1 3 2 4 }
Set B: { 2 1 3 6 }

--- Menu ---


1. Accept setA and setB
2. Display sets
3. Union of sets
4. Intersection of sets
5. Difference (A - B)
6. Symmetric Difference
7. Exit
Enter your choice: 7
Exiting...

You might also like