In C Programming: You are asked to develop a set library (using two different re
ID: 664827 • Letter: I
Question
In C Programming:
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 from user and apply the given operations.
Develop a set library:
For interface, i.e. set.h: export setElementT whose base type is int, setADT whose underlying type is hidden (will be defined later in setXYZ.c), and the following functions:
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 user may */
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, i.e., setXYZ.c, you will be asked to have two different implementations:
First implement this library as setArrayImp.c which uses a constant size array to
store set elements (suppose max set size is 100). [hint: see ch2 slides 80-86 ]
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
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 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);
4.3 if Q is entered, quit from this loop.
5. free A and B
To easily compile the set library and driver.c program with setArrayImp.c and setLinkedListImp.c,
you must have a Makefile and use "make" to compile your code.
make array: should compile it with setArrayImp.c;
make list: should compile it with setLinkedListImp.c
Explanation / Answer
// links.cpp : Defines the entry point for the console application.
//
//#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#define max 100;
typedef int * setADT;
setADT setUnion(setADT, setADT);
setADT setIntersection(setADT, setADT);
setADT setDifference(setADT, setADT);
int setCardinality(setADT) ;
setADT setUnion(setADT A, setADT B)
{
setADT newSet;
int i,j,k=0;
for( i=0;i<setCardinality(A) ;i++)
{
newSet[k]=A[i];
k++;
}
for(j=0;j<setCardinality(B) ;j++)
{
newSet[k]=A[j];
k++;
}
return newSet;
}
setADT setIntersection(setADT A, setADT B)
{
setADT newSet;
int i,j,k=0;
for( i=0;i<setCardinality(A) ;i++)
{
for(j=0;j<setCardinality(B) ;j++)
{
if(A[i]==B[j])
{
newSet[k]=A[j];
k++;
}
}
}
return newSet;
}
setADT setDifference(setADT A, setADT B)
{
setADT newSet;
int i=0,j=0,k=0;
for(i=0;i<setCardinality(A);i++)
{
for(j=0;j<setCardinality(B);j++)
{
if(A[i]!=B[j])
{
newSet[k]=A[j];
k++;
}
}
}
}
int setCardinality(setADT S)
{
int i=0;
while(S[i])
{
i++;
}
return i;
}
void setPrint(setADT S, char *name) /* print elements of S, A = {2, 5, 7} */
{
int i=0;
printf("S={");
while(S[i])
{
printf("%d,"S[i]);
i++;
}
printf("}");
return i;
}
int main()
{
int *A;int setASize,setBSize;
int *B;
int i,j;
char ch;
printf("How many values do you wnat to enter values into set A:");
scanf("%d",&setASize);
printf("Enter values into set A:");
for(i=0;i<setASize;i++)
scanf("%d",&A[i]);
printf("How many values do you wnat to enter values into set B:");
scanf("%d",&setBSize);
printf("Enter values into set B:");
for(j=0;j<setBSize;j++)
scanf("%d",&B[j]);
while(1)
{
printf("1.Union 2.Intersection 3. Difference 4. Quit Enter your choice:");
scanf("%c",&ch);
switch(ch)
{
case 'U': setUnion(A,B);break;
case 'I': setIntersection(A,B); break;
case 'D': setDifference(A,B);break;
case 'Q': exit(1);break;
default: printf("Eneter correct choice");
}
}
//system("puase");
return 0;
}