SlideShare a Scribd company logo
1 of 13
Download to read offline
Question: In C Programming: In mathematics, a set is a colle...
Save
In C Programming:
In mathematics, a set is a collection of distinct elements (say integer numbers). For example,
A={2, 5, 7} and B={3, 5, 8, 10} are two different sets. There are several basic operations for
constructing new sets from given two sets, but let's just consider three basic ones:
C = union(A,B); ---> C = A B={2, 5, 7, 8, 10} C = intersection(A,B); ---> C = A B={5}
C = difference(A,B); ---> C = A-B = A  B ={2, 7} also aka. complement
You are asked to develop a set library (using two different representations: array and link list)
and then implement a driver program that gets two sets and one of the above operation as a
command to apply, then it prints the resulting set...
Developing set library: For interface, create an interface file set.h which contains boiler plate and
the followings:
typedef int setElementT;
typedef struct setCDT *setADT;
setADT setNew(); /* create a new empty set */
void setFree(setADT S); /* free the space allocated for the set S */
int setInsertElementSorted(setADT S, setElementT E);
/* if not successful, return 0; otherwise, return the num of elements after the insertion. Also note
that the elements might be given in different orders, but your function should always keep the set
in a sorted manner after each insertion */
setADT setUnion(setADT A, setADT B);
/* returns a new set containing A B */
setADT setIntersection(setADT A, setADT B);
/* returns a new set containing A B */
setADT setDifference(setADT A, setADT B);
/* returns a new set containing A  B */
int setCardinality(setADT S);
/* return the number of elements in S */
void setPrint(setADT S, char *name);
/* print elements of S, A = {2, 5, 7} */
For implementation, you will be asked to have two different implementations:
(40pt) First implement set library as setArrayImp.c which uses a constant size array to store set
elements (suppose max set size is 100). [hint: see ch2 slides 72-78 ]
(40pt) Second implement this library as setLinkedListImp.c which uses a dynamic single linked
list to store set elements.
Develop a driver.c program and compile it with two different imp of set library (20 pt)
1. Create two sets called A and B.
2. Ask user to enter positive integers for set A (end input when user enters -1)
3. Ask user to enter positive integers for set B (end input when user enters -1)
4. In a loop 4.1. Ask user to enter a command:
4.2 If Q is entered, quit from this loop.
4.3 If U, I, or D is entered, compute set C as union, intersection, or difference. setPrint(A, "A");
setPrint(A, "B"); setPrint(C, "C"); print the number of elements in C setFree(C);
5. free A and B
Compile/execute driver.c with setArrayImp.c as well as setListImp.c
//no include files needed
Solution
Create set.h first with the following
typedef int setElementT;
typedef struct setCDT *setADT;
setADT setNew(); /* Creates a new empty set */
void setFree(setADT S); /* Frees the space allocated for the set S */
int setInsertElementSorted(setADT S, setElementT E);
/* if not successful, returns 0; otherwise, returns the num of elements after the insertion. This
function will always keep the set in a sorted manner after each insertion */
setADT setUnion(setADT A, setADT B);
/* returns a new set containing A B */
setADT setIntersection(setADT A, setADT B);
/* returns a new set containing A B */
setADT setDifference(setADT A, setADT B);
/* returns a new set containing A  B */
int setCardinality(setADT S);
/* returns the number of elements in S */
void setPrint(setADT S, char *name);
/* prints elements of S, A = {2, 5, 7} */
Next, create setArrayImp.c
#include "set.h"
#include
typedef struct setCDT{
setElementT *setElements;
setElementT maxSize;
setElementT setNumElements;
} *setADT;
setADT setNew(){
setADT newSet = (setADT)malloc(sizeof(struct setCDT));
newSet->maxSize = 100;
newSet->setElements = (int *)calloc(newSet->maxSize,sizeof(int));
newSet->setNumElements = 0;
return newSet;
}
void setFree(setADT S){
free(S);
}
int setInsertElementSorted(setADT S, setElementT E){
int cntr,temp1,temp2;
for(cntr = 0;cntrsetNumElements;cntr++){
if(S->setElements[cntr] >= E){
temp1 = S->setElements[cntr];
break;
}
}
S->setElements[cntr++] = E;
for(;cntr<=S->setNumElements;cntr++){
temp2 = S->setElements[cntr];
S->setElements[cntr] = temp1;
temp1 = temp2;
}
S->setNumElements++;
return S->setNumElements;
}
setADT setUnion(setADT A, setADT B){
int cntr1,cntr2;
setADT U = setNew();
for(cntr1=0,cntr2=0;cntr1setNumElements && cntr2setNumElements;){
if(A->setElements[cntr1] == B->setElements[cntr2]){
setInsertElementSorted(U,A->setElements[cntr1]);
cntr1++;
cntr2++;
}
else if(A->setElements[cntr1] < B->setElements[cntr2]){
setInsertElementSorted(U,A->setElements[cntr1]);
cntr1++;
}
else if(A->setElements[cntr1] > B->setElements[cntr2]){
setInsertElementSorted(U,B->setElements[cntr2]);
cntr2++;
}
}
for(;cntr1setNumElements;cntr1++){
setInsertElementSorted(U,A->setElements[cntr1]);
}
for(;cntr2setNumElements;cntr2++){
setInsertElementSorted(U,B->setElements[cntr2]);
}
return U;
}
setADT setIntersection(setADT A, setADT B){
int cntr1,cntr2;
setADT I = setNew();
for(cntr1=0,cntr2=0;cntr1setNumElements && cntr2setNumElements;){
if(A->setElements[cntr1] == B->setElements[cntr2]){
setInsertElementSorted(I,A->setElements[cntr1]);
cntr1++;
cntr2++;
}
else if(A->setElements[cntr1] < B->setElements[cntr2]){
cntr1++;
}
else if(A->setElements[cntr1] > B->setElements[cntr2]){
cntr2++;
}
}
return I;
}
setADT setDifference(setADT A, setADT B){
int cntr1,cntr2;
setADT D = setNew();
for(cntr1=0,cntr2=0;cntr1setNumElements && cntr2setNumElements;){
if(A->setElements[cntr1] == B->setElements[cntr2]){
cntr1++;
cntr2++;
continue;
}
else if(A->setElements[cntr1] < B->setElements[cntr2]){
setInsertElementSorted(D,A->setElements[cntr1]);
cntr1++;
}
else if(A->setElements[cntr1] > B->setElements[cntr2]){
cntr2++;
}
}
for(;cntr1setNumElements;cntr1++){
setInsertElementSorted(D,A->setElements[cntr1]);
}
return D;
}
void setPrint(setADT S, char *name){
int i;
printf("%d elements in Set %s, Elements are: ",S->setNumElements,name);
for(i=0;isetNumElements;i++){
printf("%d ",S->setElements[i]);
}
printf(" ");
}
int setCardinality(setADT S){
return S->setNumElements;
}
Run: gcc -o set.o -c setArrayImp.c. This would create a set.o in your folder.
Now, have the driver.c as follows
#include "set.h"
#include
#include
void main(){
int setInput;
char option;
setADT A = setNew();
setADT C;
while(1){
printf("Enter Positive Values for Set A. Press -1 to End:");
scanf("%d",&setInput);
if(setInput < 0)
break;
setInsertElementSorted(A,setInput);
}
setADT B = setNew();
while(1){
printf("Enter Positive Values for Set B. Press -1 to End:");
scanf("%d",&setInput);
if(setInput < 0)
break;
setInsertElementSorted(B,setInput);
}
while(1){
printf("Options: ");
printf("Enter Q to quit! ");
printf("Enter U for A Union B! ");
printf("Enter I for A Intersection B! ");
printf("Enter D for A Difference B! ");
printf("Your option:");
fflush(stdin);
fflush(stdout);
scanf(" %c",&option);
switch(option){
case 'U':
C = setUnion(A,B);
setPrint(A,"A");
setPrint(B,"B");
setPrint(C,"C");
setFree(C);
break;
case 'I':
C = setIntersection(A,B);
setPrint(A,"A");
setPrint(B,"B");
setPrint(C,"C");
setFree(C);
break;
case 'D':
C = setDifference(A,B);
setPrint(A,"A");
setPrint(B,"B");
setPrint(C,"C");
setFree(C);
break;
default:
exit(0);
}
}
}
Sample Run Output:
gcc driver.c set.o
./a.out
Enter Positive Values for Set A. Press -1 to End:2
Enter Positive Values for Set A. Press -1 to End:5
Enter Positive Values for Set A. Press -1 to End:7
Enter Positive Values for Set A. Press -1 to End:-3
Enter Positive Values for Set B. Press -1 to End:3
Enter Positive Values for Set B. Press -1 to End:5
Enter Positive Values for Set B. Press -1 to End:8
Enter Positive Values for Set B. Press -1 to End:10
Enter Positive Values for Set B. Press -1 to End:-1
Options:
Enter Q to quit!
Enter U for A Union B!
Enter I for A Intersection B!
Enter D for A Difference B!
Your option:U
3 elements in Set A, Elements are:
2 5 7
4 elements in Set B, Elements are:
3 5 8 10
6 elements in Set C, Elements are:
2 3 5 7 8 10
Options:
Enter Q to quit!
Enter U for A Union B!
Enter I for A Intersection B!
Enter D for A Difference B!
Your option:I
3 elements in Set A, Elements are:
2 5 7
4 elements in Set B, Elements are:
3 5 8 10
1 elements in Set C, Elements are:
5
Options:
Enter Q to quit!
Enter U for A Union B!
Enter I for A Intersection B!
Enter D for A Difference B!
Your option:D
3 elements in Set A, Elements are:
2 5 7
4 elements in Set B, Elements are:
3 5 8 10
2 elements in Set C, Elements are:
2 7
Options:
Enter Q to quit!
Enter U for A Union B!
Enter I for A Intersection B!
Enter D for A Difference B!
Your option:Q
The process is similar with setListImp.c. No need to change anything, just use setListImp.c while
generating set.o file, i.e. Run gcc -o set.o -c setListImp.c.
setListImp.c
#include "set.h"
#include
struct setList{
setElementT data;
struct setList *next;
};
typedef struct setCDT{
struct setList *head;
setElementT setNumElements;
} *setADT;
setADT setNew(){
setADT newSet = (setADT)malloc(sizeof(struct setCDT));
newSet->setNumElements = 0;
return newSet;
}
void setFree(setADT S){
free(S);
}
int setInsertElementSorted(setADT S, setElementT E){
int cntr,temp1,temp2;
struct setList *temp,*prev;
struct setList *setElement = (struct setList *)malloc(sizeof(struct setList));
setElement->data = E;
setElement->next = NULL;
if(S->head == NULL){
S->head = setElement;
}
else{
temp = S->head;
prev = NULL;
while(temp!=NULL && temp->data < setElement->data){
prev = temp;
temp = temp->next;
}
if(prev == NULL){
setElement->next = S->head;
S->head = setElement;
}
else{
prev->next = setElement;
setElement->next = temp;
}
}
S->setNumElements++;
return S->setNumElements;
}
setADT setUnion(setADT A, setADT B){
setADT U = setNew();
struct setList *cntr1 = A->head, *cntr2 = B->head;
for(;cntr1!=NULL && cntr2!=NULL;){
if(cntr1->data == cntr2->data){
setInsertElementSorted(U,cntr1->data);
cntr1 = cntr1->next;
cntr2 = cntr2->next;
}
else if(cntr1->data < cntr2->data){
setInsertElementSorted(U,cntr1->data);
cntr1 = cntr1->next;
}
else if(cntr1->data > cntr2->data){
setInsertElementSorted(U,cntr2->data);
cntr2 = cntr2->next;
}
}
for(;cntr1!=NULL;cntr1=cntr1->next){
setInsertElementSorted(U,cntr1->data);
}
for(;cntr2!=NULL;cntr2=cntr2->next){
setInsertElementSorted(U,cntr2->data);
}
return U;
}
setADT setIntersection(setADT A, setADT B){
setADT I = setNew();
struct setList *cntr1 = A->head, *cntr2 = B->head;
for(;cntr1!=NULL && cntr2!=NULL;){
if(cntr1->data == cntr2->data){
setInsertElementSorted(I,cntr1->data);
cntr1 = cntr1->next;
cntr2 = cntr2->next;
}
else if(cntr1->data < cntr2->data){
cntr1 = cntr1->next;
}
else if(cntr1->data > cntr2->data){
cntr2 = cntr2->next;
}
}
return I;
}
setADT setDifference(setADT A, setADT B){
setADT D = setNew();
struct setList *cntr1 = A->head, *cntr2 = B->head;
for(;cntr1!=NULL && cntr2!=NULL;){
if(cntr1->data == cntr2->data){
cntr1 = cntr1->next;
cntr2 = cntr2->next;
}
else if(cntr1->data < cntr2->data){
setInsertElementSorted(D,cntr1->data);
cntr1 = cntr1->next;
}
else if(cntr1->data > cntr2->data){
cntr2 = cntr2->next;
}
}
for(;cntr1!=NULL;cntr1=cntr1->next){
setInsertElementSorted(D,cntr1->data);
}
return D;
}
void setPrint(setADT S, char *name){
struct setList *temp = S->head;
printf("%d elements in Set %s, Elements are: ",S->setNumElements,name);
while(temp != NULL){
printf("%d ",temp->data);
temp = temp->next;
}
printf(" ");
}
int setCardinality(setADT S){
return S->setNumElements;
}

More Related Content

Similar to Question In C Programming In mathematics, a set is a colle...Sav.pdf

ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdfsyedabdul78662
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07HUST
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdfarihantelehyb
 
Can you help me by answering this- The following function defined in c.pdf
Can you help me by answering this- The following function defined in c.pdfCan you help me by answering this- The following function defined in c.pdf
Can you help me by answering this- The following function defined in c.pdfSeanIC4Jamesn
 
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfSolve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfsnewfashion
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfinfo114
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptxNelyJay
 
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdfUse Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdfashishgargjaipuri
 
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfSuggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfssuser58be4b1
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdfrushabhshah600
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++NUST Stuff
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfaromanets
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부mosaicnet
 

Similar to Question In C Programming In mathematics, a set is a colle...Sav.pdf (20)

ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdf
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07
 
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
(Parent reference for BST) Redefine TreeNode by adding a reference to.pdf
 
Can you help me by answering this- The following function defined in c.pdf
Can you help me by answering this- The following function defined in c.pdfCan you help me by answering this- The following function defined in c.pdf
Can you help me by answering this- The following function defined in c.pdf
 
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdfSolve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
Solve the coding errors for upvotemake test-statsg++ -g -std=c++.pdf
 
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdfNeed done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
Bcsl 033 solve assignment
Bcsl 033 solve assignmentBcsl 033 solve assignment
Bcsl 033 solve assignment
 
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdfUse Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
Use Netbeans to copy your last lab (Lab 07) to a new project called La.pdf
 
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdfSuggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
Suggestion- Use Netbeans to copy your last lab (Lab 07) to a new proje.pdf
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Op ps
Op psOp ps
Op ps
 

More from arihantcomp1008

Can someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdfCan someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdfarihantcomp1008
 
Analysis of data collected in a study of educational levels (Did not .pdf
Analysis of data collected in a study of educational levels (Did not .pdfAnalysis of data collected in a study of educational levels (Did not .pdf
Analysis of data collected in a study of educational levels (Did not .pdfarihantcomp1008
 
As individuals age, they are likely to becomeMore alike and less .pdf
As individuals age, they are likely to becomeMore alike and less .pdfAs individuals age, they are likely to becomeMore alike and less .pdf
As individuals age, they are likely to becomeMore alike and less .pdfarihantcomp1008
 
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdfAPARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdfarihantcomp1008
 
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdf
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdfWhy does Ernst Mayrs biological species concept NOT apply to Archae.pdf
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdfarihantcomp1008
 
Write a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdfWrite a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdfarihantcomp1008
 
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdfWhich of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdfarihantcomp1008
 
Which of the following is NOT a measure of precision 8 O (A) standar.pdf
Which of the following is NOT a measure of precision 8 O (A) standar.pdfWhich of the following is NOT a measure of precision 8 O (A) standar.pdf
Which of the following is NOT a measure of precision 8 O (A) standar.pdfarihantcomp1008
 
Write C++ variable declarations to create one int variable called num.pdf
Write C++ variable declarations to create one int variable called num.pdfWrite C++ variable declarations to create one int variable called num.pdf
Write C++ variable declarations to create one int variable called num.pdfarihantcomp1008
 
Why do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdfWhy do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdfarihantcomp1008
 
What is the United States Comparative Advantage....please be specifi.pdf
What is the United States Comparative Advantage....please be specifi.pdfWhat is the United States Comparative Advantage....please be specifi.pdf
What is the United States Comparative Advantage....please be specifi.pdfarihantcomp1008
 
true false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdftrue false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdfarihantcomp1008
 
The rearrangement of alleles by the process of crossing over is call.pdf
The rearrangement of alleles by the process of crossing over is call.pdfThe rearrangement of alleles by the process of crossing over is call.pdf
The rearrangement of alleles by the process of crossing over is call.pdfarihantcomp1008
 
Susie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdfSusie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdfarihantcomp1008
 
Section 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdfSection 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdfarihantcomp1008
 
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdfarihantcomp1008
 
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdfQ1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdfarihantcomp1008
 
Normality assumption is very important. A researcher is interested i.pdf
Normality assumption is very important. A researcher is interested i.pdfNormality assumption is very important. A researcher is interested i.pdf
Normality assumption is very important. A researcher is interested i.pdfarihantcomp1008
 
Punnett square Father and mother are heterozygous and each are .pdf
Punnett square Father and mother are heterozygous and each are .pdfPunnett square Father and mother are heterozygous and each are .pdf
Punnett square Father and mother are heterozygous and each are .pdfarihantcomp1008
 
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdfProperty Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdfarihantcomp1008
 

More from arihantcomp1008 (20)

Can someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdfCan someone please help me write a case brief for two supreme court .pdf
Can someone please help me write a case brief for two supreme court .pdf
 
Analysis of data collected in a study of educational levels (Did not .pdf
Analysis of data collected in a study of educational levels (Did not .pdfAnalysis of data collected in a study of educational levels (Did not .pdf
Analysis of data collected in a study of educational levels (Did not .pdf
 
As individuals age, they are likely to becomeMore alike and less .pdf
As individuals age, they are likely to becomeMore alike and less .pdfAs individuals age, they are likely to becomeMore alike and less .pdf
As individuals age, they are likely to becomeMore alike and less .pdf
 
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdfAPARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf
APARTMENT BUILDING HOMEWORK1 What’s the problemSuppose you want.pdf
 
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdf
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdfWhy does Ernst Mayrs biological species concept NOT apply to Archae.pdf
Why does Ernst Mayrs biological species concept NOT apply to Archae.pdf
 
Write a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdfWrite a A minimum of 2 pages of text describing the hardware compone.pdf
Write a A minimum of 2 pages of text describing the hardware compone.pdf
 
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdfWhich of the following would be sufficient for the Hardy-Weinberg equ.pdf
Which of the following would be sufficient for the Hardy-Weinberg equ.pdf
 
Which of the following is NOT a measure of precision 8 O (A) standar.pdf
Which of the following is NOT a measure of precision 8 O (A) standar.pdfWhich of the following is NOT a measure of precision 8 O (A) standar.pdf
Which of the following is NOT a measure of precision 8 O (A) standar.pdf
 
Write C++ variable declarations to create one int variable called num.pdf
Write C++ variable declarations to create one int variable called num.pdfWrite C++ variable declarations to create one int variable called num.pdf
Write C++ variable declarations to create one int variable called num.pdf
 
Why do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdfWhy do a lot of scientist feel like god doesnt exist What is th.pdf
Why do a lot of scientist feel like god doesnt exist What is th.pdf
 
What is the United States Comparative Advantage....please be specifi.pdf
What is the United States Comparative Advantage....please be specifi.pdfWhat is the United States Comparative Advantage....please be specifi.pdf
What is the United States Comparative Advantage....please be specifi.pdf
 
true false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdftrue false 1, Sampling is always wrong because it is stup.pdf
true false 1, Sampling is always wrong because it is stup.pdf
 
The rearrangement of alleles by the process of crossing over is call.pdf
The rearrangement of alleles by the process of crossing over is call.pdfThe rearrangement of alleles by the process of crossing over is call.pdf
The rearrangement of alleles by the process of crossing over is call.pdf
 
Susie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdfSusie has influenza A. She lives in a residence hall where there hav.pdf
Susie has influenza A. She lives in a residence hall where there hav.pdf
 
Section 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdfSection 5 Controlling Risk Given the following categories or areas .pdf
Section 5 Controlling Risk Given the following categories or areas .pdf
 
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf5)Sonny has the following account balances as of January 1, 2018 bef.pdf
5)Sonny has the following account balances as of January 1, 2018 bef.pdf
 
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdfQ1. What is the primary reason that Fenders blue butterflies are e.pdf
Q1. What is the primary reason that Fenders blue butterflies are e.pdf
 
Normality assumption is very important. A researcher is interested i.pdf
Normality assumption is very important. A researcher is interested i.pdfNormality assumption is very important. A researcher is interested i.pdf
Normality assumption is very important. A researcher is interested i.pdf
 
Punnett square Father and mother are heterozygous and each are .pdf
Punnett square Father and mother are heterozygous and each are .pdfPunnett square Father and mother are heterozygous and each are .pdf
Punnett square Father and mother are heterozygous and each are .pdf
 
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdfProperty Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
Property Values Is this rooted tree Leaf vertices Intenal vertices A.pdf
 

Recently uploaded

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 

Recently uploaded (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 

Question In C Programming In mathematics, a set is a colle...Sav.pdf

  • 1. Question: In C Programming: In mathematics, a set is a colle... Save In C Programming: In mathematics, a set is a collection of distinct elements (say integer numbers). For example, A={2, 5, 7} and B={3, 5, 8, 10} are two different sets. There are several basic operations for constructing new sets from given two sets, but let's just consider three basic ones: C = union(A,B); ---> C = A B={2, 5, 7, 8, 10} C = intersection(A,B); ---> C = A B={5} C = difference(A,B); ---> C = A-B = A B ={2, 7} also aka. complement You are asked to develop a set library (using two different representations: array and link list) and then implement a driver program that gets two sets and one of the above operation as a command to apply, then it prints the resulting set... Developing set library: For interface, create an interface file set.h which contains boiler plate and the followings: typedef int setElementT; typedef struct setCDT *setADT; setADT setNew(); /* create a new empty set */ void setFree(setADT S); /* free the space allocated for the set S */ int setInsertElementSorted(setADT S, setElementT E); /* if not successful, return 0; otherwise, return the num of elements after the insertion. Also note that the elements might be given in different orders, but your function should always keep the set in a sorted manner after each insertion */ setADT setUnion(setADT A, setADT B); /* returns a new set containing A B */ setADT setIntersection(setADT A, setADT B); /* returns a new set containing A B */ setADT setDifference(setADT A, setADT B); /* returns a new set containing A B */ int setCardinality(setADT S); /* return the number of elements in S */ void setPrint(setADT S, char *name); /* print elements of S, A = {2, 5, 7} */ For implementation, you will be asked to have two different implementations: (40pt) First implement set library as setArrayImp.c which uses a constant size array to store set elements (suppose max set size is 100). [hint: see ch2 slides 72-78 ] (40pt) Second implement this library as setLinkedListImp.c which uses a dynamic single linked
  • 2. list to store set elements. Develop a driver.c program and compile it with two different imp of set library (20 pt) 1. Create two sets called A and B. 2. Ask user to enter positive integers for set A (end input when user enters -1) 3. Ask user to enter positive integers for set B (end input when user enters -1) 4. In a loop 4.1. Ask user to enter a command: 4.2 If Q is entered, quit from this loop. 4.3 If U, I, or D is entered, compute set C as union, intersection, or difference. setPrint(A, "A"); setPrint(A, "B"); setPrint(C, "C"); print the number of elements in C setFree(C); 5. free A and B Compile/execute driver.c with setArrayImp.c as well as setListImp.c //no include files needed Solution Create set.h first with the following typedef int setElementT; typedef struct setCDT *setADT; setADT setNew(); /* Creates a new empty set */ void setFree(setADT S); /* Frees the space allocated for the set S */ int setInsertElementSorted(setADT S, setElementT E); /* if not successful, returns 0; otherwise, returns the num of elements after the insertion. This function will always keep the set in a sorted manner after each insertion */ setADT setUnion(setADT A, setADT B); /* returns a new set containing A B */ setADT setIntersection(setADT A, setADT B); /* returns a new set containing A B */
  • 3. setADT setDifference(setADT A, setADT B); /* returns a new set containing A B */ int setCardinality(setADT S); /* returns the number of elements in S */ void setPrint(setADT S, char *name); /* prints elements of S, A = {2, 5, 7} */ Next, create setArrayImp.c #include "set.h" #include typedef struct setCDT{ setElementT *setElements; setElementT maxSize; setElementT setNumElements; } *setADT; setADT setNew(){ setADT newSet = (setADT)malloc(sizeof(struct setCDT)); newSet->maxSize = 100; newSet->setElements = (int *)calloc(newSet->maxSize,sizeof(int)); newSet->setNumElements = 0; return newSet; } void setFree(setADT S){ free(S); } int setInsertElementSorted(setADT S, setElementT E){ int cntr,temp1,temp2;
  • 4. for(cntr = 0;cntrsetNumElements;cntr++){ if(S->setElements[cntr] >= E){ temp1 = S->setElements[cntr]; break; } } S->setElements[cntr++] = E; for(;cntr<=S->setNumElements;cntr++){ temp2 = S->setElements[cntr]; S->setElements[cntr] = temp1; temp1 = temp2; } S->setNumElements++; return S->setNumElements; } setADT setUnion(setADT A, setADT B){ int cntr1,cntr2; setADT U = setNew(); for(cntr1=0,cntr2=0;cntr1setNumElements && cntr2setNumElements;){ if(A->setElements[cntr1] == B->setElements[cntr2]){ setInsertElementSorted(U,A->setElements[cntr1]); cntr1++; cntr2++; } else if(A->setElements[cntr1] < B->setElements[cntr2]){ setInsertElementSorted(U,A->setElements[cntr1]); cntr1++; } else if(A->setElements[cntr1] > B->setElements[cntr2]){ setInsertElementSorted(U,B->setElements[cntr2]); cntr2++; } } for(;cntr1setNumElements;cntr1++){ setInsertElementSorted(U,A->setElements[cntr1]);
  • 5. } for(;cntr2setNumElements;cntr2++){ setInsertElementSorted(U,B->setElements[cntr2]); } return U; } setADT setIntersection(setADT A, setADT B){ int cntr1,cntr2; setADT I = setNew(); for(cntr1=0,cntr2=0;cntr1setNumElements && cntr2setNumElements;){ if(A->setElements[cntr1] == B->setElements[cntr2]){ setInsertElementSorted(I,A->setElements[cntr1]); cntr1++; cntr2++; } else if(A->setElements[cntr1] < B->setElements[cntr2]){ cntr1++; } else if(A->setElements[cntr1] > B->setElements[cntr2]){ cntr2++; } } return I; } setADT setDifference(setADT A, setADT B){ int cntr1,cntr2; setADT D = setNew(); for(cntr1=0,cntr2=0;cntr1setNumElements && cntr2setNumElements;){ if(A->setElements[cntr1] == B->setElements[cntr2]){ cntr1++; cntr2++; continue; } else if(A->setElements[cntr1] < B->setElements[cntr2]){
  • 6. setInsertElementSorted(D,A->setElements[cntr1]); cntr1++; } else if(A->setElements[cntr1] > B->setElements[cntr2]){ cntr2++; } } for(;cntr1setNumElements;cntr1++){ setInsertElementSorted(D,A->setElements[cntr1]); } return D; } void setPrint(setADT S, char *name){ int i; printf("%d elements in Set %s, Elements are: ",S->setNumElements,name); for(i=0;isetNumElements;i++){ printf("%d ",S->setElements[i]); } printf(" "); } int setCardinality(setADT S){ return S->setNumElements; } Run: gcc -o set.o -c setArrayImp.c. This would create a set.o in your folder. Now, have the driver.c as follows #include "set.h" #include #include void main(){ int setInput; char option; setADT A = setNew();
  • 7. setADT C; while(1){ printf("Enter Positive Values for Set A. Press -1 to End:"); scanf("%d",&setInput); if(setInput < 0) break; setInsertElementSorted(A,setInput); } setADT B = setNew(); while(1){ printf("Enter Positive Values for Set B. Press -1 to End:"); scanf("%d",&setInput); if(setInput < 0) break; setInsertElementSorted(B,setInput); } while(1){ printf("Options: "); printf("Enter Q to quit! "); printf("Enter U for A Union B! "); printf("Enter I for A Intersection B! "); printf("Enter D for A Difference B! "); printf("Your option:"); fflush(stdin); fflush(stdout); scanf(" %c",&option); switch(option){ case 'U': C = setUnion(A,B); setPrint(A,"A"); setPrint(B,"B"); setPrint(C,"C"); setFree(C); break;
  • 8. case 'I': C = setIntersection(A,B); setPrint(A,"A"); setPrint(B,"B"); setPrint(C,"C"); setFree(C); break; case 'D': C = setDifference(A,B); setPrint(A,"A"); setPrint(B,"B"); setPrint(C,"C"); setFree(C); break; default: exit(0); } } } Sample Run Output: gcc driver.c set.o ./a.out Enter Positive Values for Set A. Press -1 to End:2 Enter Positive Values for Set A. Press -1 to End:5 Enter Positive Values for Set A. Press -1 to End:7 Enter Positive Values for Set A. Press -1 to End:-3 Enter Positive Values for Set B. Press -1 to End:3 Enter Positive Values for Set B. Press -1 to End:5 Enter Positive Values for Set B. Press -1 to End:8 Enter Positive Values for Set B. Press -1 to End:10 Enter Positive Values for Set B. Press -1 to End:-1 Options: Enter Q to quit!
  • 9. Enter U for A Union B! Enter I for A Intersection B! Enter D for A Difference B! Your option:U 3 elements in Set A, Elements are: 2 5 7 4 elements in Set B, Elements are: 3 5 8 10 6 elements in Set C, Elements are: 2 3 5 7 8 10 Options: Enter Q to quit! Enter U for A Union B! Enter I for A Intersection B! Enter D for A Difference B! Your option:I 3 elements in Set A, Elements are: 2 5 7 4 elements in Set B, Elements are: 3 5 8 10 1 elements in Set C, Elements are: 5 Options: Enter Q to quit! Enter U for A Union B! Enter I for A Intersection B! Enter D for A Difference B! Your option:D 3 elements in Set A, Elements are: 2 5 7 4 elements in Set B, Elements are: 3 5 8 10 2 elements in Set C, Elements are: 2 7 Options: Enter Q to quit!
  • 10. Enter U for A Union B! Enter I for A Intersection B! Enter D for A Difference B! Your option:Q The process is similar with setListImp.c. No need to change anything, just use setListImp.c while generating set.o file, i.e. Run gcc -o set.o -c setListImp.c. setListImp.c #include "set.h" #include struct setList{ setElementT data; struct setList *next; }; typedef struct setCDT{ struct setList *head; setElementT setNumElements; } *setADT; setADT setNew(){ setADT newSet = (setADT)malloc(sizeof(struct setCDT)); newSet->setNumElements = 0; return newSet; } void setFree(setADT S){ free(S); } int setInsertElementSorted(setADT S, setElementT E){ int cntr,temp1,temp2; struct setList *temp,*prev; struct setList *setElement = (struct setList *)malloc(sizeof(struct setList)); setElement->data = E;
  • 11. setElement->next = NULL; if(S->head == NULL){ S->head = setElement; } else{ temp = S->head; prev = NULL; while(temp!=NULL && temp->data < setElement->data){ prev = temp; temp = temp->next; } if(prev == NULL){ setElement->next = S->head; S->head = setElement; } else{ prev->next = setElement; setElement->next = temp; } } S->setNumElements++; return S->setNumElements; } setADT setUnion(setADT A, setADT B){ setADT U = setNew(); struct setList *cntr1 = A->head, *cntr2 = B->head; for(;cntr1!=NULL && cntr2!=NULL;){ if(cntr1->data == cntr2->data){ setInsertElementSorted(U,cntr1->data); cntr1 = cntr1->next; cntr2 = cntr2->next; } else if(cntr1->data < cntr2->data){ setInsertElementSorted(U,cntr1->data);
  • 12. cntr1 = cntr1->next; } else if(cntr1->data > cntr2->data){ setInsertElementSorted(U,cntr2->data); cntr2 = cntr2->next; } } for(;cntr1!=NULL;cntr1=cntr1->next){ setInsertElementSorted(U,cntr1->data); } for(;cntr2!=NULL;cntr2=cntr2->next){ setInsertElementSorted(U,cntr2->data); } return U; } setADT setIntersection(setADT A, setADT B){ setADT I = setNew(); struct setList *cntr1 = A->head, *cntr2 = B->head; for(;cntr1!=NULL && cntr2!=NULL;){ if(cntr1->data == cntr2->data){ setInsertElementSorted(I,cntr1->data); cntr1 = cntr1->next; cntr2 = cntr2->next; } else if(cntr1->data < cntr2->data){ cntr1 = cntr1->next; } else if(cntr1->data > cntr2->data){ cntr2 = cntr2->next; } } return I; } setADT setDifference(setADT A, setADT B){
  • 13. setADT D = setNew(); struct setList *cntr1 = A->head, *cntr2 = B->head; for(;cntr1!=NULL && cntr2!=NULL;){ if(cntr1->data == cntr2->data){ cntr1 = cntr1->next; cntr2 = cntr2->next; } else if(cntr1->data < cntr2->data){ setInsertElementSorted(D,cntr1->data); cntr1 = cntr1->next; } else if(cntr1->data > cntr2->data){ cntr2 = cntr2->next; } } for(;cntr1!=NULL;cntr1=cntr1->next){ setInsertElementSorted(D,cntr1->data); } return D; } void setPrint(setADT S, char *name){ struct setList *temp = S->head; printf("%d elements in Set %s, Elements are: ",S->setNumElements,name); while(temp != NULL){ printf("%d ",temp->data); temp = temp->next; } printf(" "); } int setCardinality(setADT S){ return S->setNumElements; }