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 .
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;
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;