Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Follow all the instructions given below (especially the instructions in bold): W

ID: 3872646 • Letter: F

Question

Follow all the instructions given below (especially the instructions in bold):

Write a C program that reads in two sets of numbers A and B, and calculates and print their difference of set A and B: A - B, complement of set A: Aand complement of set B: B . A -B is the set of elements that appear in A but not in B, and that is the set of every element that is not in A. The values in the sets are restricted to the range 0... 9 and should include the following functions within the program: void set difference(int *a, int *b, int n, int *difference); void setacomplement(int *a, int n, int *complement); set difference function:it should find the difference of the set represented by array a and set represented by array b and store the result in the set represented by array difference set complement function: it should find the complement of the set represented by array a store the result in the set represented by array complement. Both functions should use pointer arithmetic - not subscripting - to visit array elements. In other words, eliminate the loop index varables and all use of the operator in the function Do not declare an integer variable within your function and use that integer variable to iterate through the loop instead declare a pointer and use that pointer to iterate through the loop in the function: inti; Do not use the above code or anything similar in your functions but instead use a pointer to loop through the for loop! The program will read in the number of element in the first set, for example, 4, then read in the numbers in the set, for example, 3 6 8 9. The repeat for the second set. The two sets do not necessarily are of the same size The sets are stored using arrays of Os and 1s. Calculate the difference of A and B, and complements of the two sets and display the result. Sample input/output Please enter the number of elements in set A: 3 Enter the numbers in set A: 3 5 8 Please enter the number of elements in set B: 4 Enter the numbers in set B: 7 59 3 Output The difference of set A and B is: 8 The complement of set A is: 0 124679 The complement of set B is: 0 1 246 8

Explanation / Answer

Source Code:

#include<stdio.h>

#define MAX 30
void create(int set[]);
void print(int set[]);
void Union(int set1[],int set2[],int set3[]);
void intersection(int set1[],int set2[],int set4[]);
void difference(int set1[],int set2[],int set5[]);
void symmetric(int set1[],int set2[],int set6[]);
int member(int set[],int x);

int main()
{ int set1[MAX],set2[MAX],set3[MAX];
int x,op;
set1[0]=set2[0]=set3[0]=0;
printf("------------------------------------------------------------- ");
printf("----------------made by C code champ ------------------------ ");
printf("------------------------------------------------------------- ");
printf(" MENU BASED C PROGRAM OF SET OPERATIONS ");
do
{
printf(" 1)Create two sets.");
printf(" 2)Print the set.");
printf(" 3)Union of two sets.");
printf(" 4)Intersection of two sets A intesection B.");
printf(" 5)Difference between two sets A - B");
printf(" 6)Symmetrec Difference between two sets i.e (A-B)U(B-A).");
printf(" 7)Exit from program ");
printf(" Enter Your Choice : ");
scanf("%d",&op);
printf(" ");
switch(op)
{
case 1: printf(" Create Set A: ");
create(set1);
printf(" Create Set B: ");
create(set2);
break;
case 2: printf(" Set A: ");
print(set1);
printf(" Set B: ");
print(set2);
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: symmetric(set1,set2,set3);
print(set3);
break;
case 7: break;
default:
printf(" Please Enter a valid choice ");
break;
}
printf(" Press a key to continue...... ");

}while(op!=7);
}

void create(int set[])
{ int n,i,x;
set[0]=0;
printf(" No. of elements in the set : ");
scanf("%d",&n);
printf(" Enter set elements : ");
for(i=1;i<=n;i++)
scanf("%d",&set[i]);
set[0]=n;

}

void print(int set[])
{
int i,n;
n=set[0];
printf(" Members of the set :--> ");
for(i=1;i<=n;i++)
printf("%d ",set[i]);
}

void Union(int set1[],int set2[],int set3[])
{ int i,n;
set3[0]=0;
n=set1[0];
for(i=0;i<=n;i++)
set3[i]=set1[i];

n=set2[0];
for(i=1;i<=n;i++)
if(!member(set3,set2[i]))
set3[++set3[0]]=set2[i];
}

int member(int set[],int x)
{  
int i,n;
n=set[0];
for(i=1;i<=n;i++)
if(x==set[i])
return(1);
return(0);
}

void intersection(int set1[],int set2[],int set3[])
{
int i,n;
set3[0]=0;
n=set1[0];
for(i=1;i<=n;i++)
if(member(set2,set1[i]))
set3[++set3[0]]=set1[i];
}

void difference(int set1[],int set2[],int set3[])
{  
int i,n;
n=set1[0];
set3[0]=0;
for(i=1;i<=n;i++)
if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i];
}

void symmetric(int set1[],int set2[],int set3[])
{  
int i,n;
n=set1[0];
set3[0]=0;
//Calculate set1-set2
for(i=1;i<=n;i++)
if(!member(set2,set1[i]))
set3[++set3[0]]=set1[i];
//Calculate set2-set1
n=set2[0];
for(i=1;i<=n;i++)
if(!member(set1,set2[i]))
set3[++set3[0]]=set2[i];

}