Write a program in c create two sets and perform the symmetric difference operation.
Answers
Explanation:
// Write a program in c to create two sets and
// perform Symmetric operation on sets
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n=0,n1,n2,l,i,j,sy[100];
printf("Enter size of set A:-\n");
scanf("%d",&n1);
printf("Enter element of set:-\n");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B:-\n");
scanf("%d",&n2);
printf("Enter element of set:-\n");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);
// logic for find A-B
for( i=0;i<n1;i++)
{
// here we check that is b[i] already present in the answer set
// if present then ignore it otherwise add it to the answer set
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}
}
// logic for find B-A
for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
// here we check that is b[i] already present in the answer set
// if present then ignore it otherwise add it to the answer set
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}
}
printf("\n set A is :-\n");
for(i=0; i<n1;i++)
{
printf("%d ", a[i]);
}
printf("\n set B is :-\n");
for(i=0; i<n2;i++)
{
printf("%d ", b[i]);
}
//logic for symmetric Difference
for(i=0;i<k;i++)
{
sy[n]=c[i];
n++;
}
for(i=0;i<m;i++)
{
sy[n]=d[i];
n++;
}
printf("\nsymmetric Difference of sets is:-\n");
for(i=0;i<n;i++)
printf("%d ",sy[i]);
getch();
}
When the above program is executed, it produces following results --
Enter size of set A:-
5
Enter element of set:-
12
54
23
9
4
Enter size of set B:-
7
Enter element of set:-
33
65
90
23
9
1
4
set A is :-
12 54 23 9 4
set B is :-
33 65 90 23 9 1 4
symmetric Difference of sets is:-
12 54 33 65 90 1
Here's an example program in C that creates two sets of integers and performs the symmetric difference operation:
SOURCE CODE:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int set1[MAX_SIZE], set2[MAX_SIZE], sym_diff[MAX_SIZE];
int size1, size2, i, j, k, is_unique;
// Input the size and elements of the first set
printf("Enter the size of the first set (up to %d): ", MAX_SIZE);
scanf("%d", &size1);
printf("Enter the elements of the first set:\n");
for (i = 0; i < size1; i++) {
scanf("%d", &set1[i]);
}
// Input the size and elements of the second set
printf("Enter the size of the second set (up to %d): ", MAX_SIZE);
scanf("%d", &size2);
printf("Enter the elements of the second set:\n");
for (i = 0; i < size2; i++) {
scanf("%d", &set2[i]);
}
// Perform the symmetric difference operation
k = 0;
for (i = 0; i < size1; i++) {
is_unique = 1;
for (j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
is_unique = 0;
break;
}
}
if (is_unique) {
sym_diff[k] = set1[i];
k++;
}
}
for (i = 0; i < size2; i++) {
is_unique = 1;
for (j = 0; j < size1; j++) {
if (set2[i] == set1[j]) {
is_unique = 0;
break;
}
}
if (is_unique) {
sym_diff[k] = set2[i];
k++;
}
}
// Print the symmetric difference
printf("The symmetric difference of the two sets is:\n");
if (k == 0) {
printf("Empty set\n");
} else {
for (i = 0; i < k; i++) {
printf("%d ", sym_diff[i]);
}
printf("\n");
}
return 0;
}
The program first prompts the user to input the size and elements of the two sets. It then performs the symmetric difference operation by iterating through the elements of each set and checking if they are unique to that set. If an element is unique to one set, it is added to the symmetric difference array. Finally, the program prints the symmetric difference array.
#SPJ3