Question: I wrote this code for a data implementation of the set data structure in C. I'm having trouble with the test.c with regards to the
I wrote this code for a data implementation of the set data structure in C. I'm having trouble with the test.c with regards to the usage of the structure, especially in the functions verify_diff, verify_un and verify_inter when using: setInter->size and setInter->array. The error I get is \"error: invalid use of incomplete typedef set\". The structure set is only typedefed in set.h and so I understand why I get an invalid use of the typedef set, but I don't know how to solve that so hoping someone here can. If you do manage to solve it, please provide some explanation. NOTE: I am not allowed to move the struct set to set.h which, would solve my problem. But it's presence in set.c rather than set.h is mandatory for the assignment.
SET.H #ifndef SET_H #define SET_H
#include
// The type for the set. typedef struct set set;
// Creates a new empty set. set *set_empty();
// Creates a new set with a single member (value). set *set_single(const int value);
// Inserts the value in the set. void set_insert(const int value, set *s);
// Returns a new set that is the union of the two sets. set *set_union(const set *const s1, const set *const s2);
// Returns a new set that is the intersection of the two sets. set *set_intersection(const set *const s1, const set *const s2);
// Returns a new set that is the difference of the two sets (s1 \\ s2). set *set_difference(const set *const s1, const set *const s2);
// Checks if the set is empty. bool set_is_empty(const set *const s);
// Checks if the value is a member of the set. bool set_member_of(const int value, const set *const s);
// Returns a random member of the set (without removing it). int set_choose(const set *const s);
// Removes the value from the set. void set_remove(const int value, set *const s);
// Checks if the two sets are equal. bool set_equal(const set *const s1, const set *const s2);
// Checks if the first set is a subset of the second set. bool set_subset(const set *const s1, const set *const s2);
// Returns an array with all the values in the set. int *set_get_values(const set *const s);
// Returns the number of elements in the set. int set_size(const set *const s);
// Destroys the set. void set_destroy(set *s);
#endif /* SET_H */
SET.C #include #include #include #include \"set.h\"
struct set { int capacity; int size; char *array; };
void print_set(set *s) { for (int i = 0; i size; i++) if (i == (s->size - 1)) printf(\"%d \", s->array[i]); else printf(\"%d,\", s->array[i]); }
set *set_empty() {
struct set *ptr = malloc(sizeof(struct set));
ptr->size = 0;
ptr->array = malloc(sizeof(char));
ptr->capacity = 1;
return ptr; }
set *set_single(const int value) { struct set *s = malloc(sizeof(struct set));
s->size = 1;
s->array = malloc(sizeof(1));
s->capacity = 1;
set_insert(value, s);
return s; }
void set_insert(const int value, set *s) { if (!set_member_of(value, s)) { int bit_in_array = value;
// Increase the capacity if necessary if (bit_in_array >= s->capacity) { int no_of_bytes = bit_in_array / 8 + 1; s->array = realloc(s->array, no_of_bytes); for (int i = s->capacity / 8 ; i s->array[i] = 0; } s->capacity = no_of_bytes * 8; }
// Set the bit int byte_no = bit_in_array / 8; int bit = 7 - bit_in_array % 8; s->array[byte_no] = s->array[byte_no] | 1 s->size++; } }
set *set_union(const set *const s1, const set *const s2) { set *s = set_empty();
for (int i = 0 ; i capacity || i capacity ; i++) { if (set_member_of(i, s1) || set_member_of(i, s2)) { set_insert(i, s); } }
return s; }
set *set_intersection(const set *const s1, const set *const s2) { struct set *s = set_empty();
for (int i = 0; i size; i++) { for (int j = 0; j size; j++) { if (s1->array[i] == s2->array[j]) { set_insert(s1->array[i], s); } } } return s; }
set *set_difference(const set *const s1, const set *const s2) {
struct set *s = set_empty();
for (int i = 0; i size; i++) { if (!set_member_of(s1->array[i], s2)) { set_insert(s1->array[i], s); } }
for (int i = 0; i size; i++) { if (!set_member_of(s2->array[i], s1)) { set_insert(s2->array[i], s); } }
return s; }
bool set_is_empty(const set *const s) { return (s->size == 0); }
bool set_member_of(const int value, const set *const s) { int bit_in_array = value;
if (bit_in_array >= s->capacity) { return false; }
int byte_no = bit_in_array / 8; int bit = 7 - bit_in_array % 8; char the_byte = s->array[byte_no];
return the_byte & 1 }
int set_choose(const set *const s) {
srand(time(0)); int j = rand() % s->size;
return s->array[j]; }
void set_remove(const int value, set *const s) { for (int i = 0; i size; i++) { if (value == s->array[i]) { for (int j = i - 1; j size -1; j++) { s->array[j] = s->array[j+1]; } } else { printf(\"Value does not exist in set\"); } } s->size = s->size - 1; }
bool set_equal(const set *const s1, const set *const s2) {
if (s1->size != s2->size) { return false; }
return set_subset(s1, s2);
}
bool set_subset(const set *const s1, const set *const s2) {
for (int i = 0; i size; i++) { if (!set_member_of(s1->array[i], s2 )) { return false; } }
return true; }
int set_size(const set *const s) { return s->size; }
int *set_get_values(const set *const s) { int *array[s->size]; for (int i = 0; i size; i++) { *array[i] = s->array[i]; }
return array[s->size]; }
void set_destroy(set *s) { free(s->array); free(s); }
TEST.C #include #include \"set.h\"
bool verify_diff(const set *const setDiff, const set *const setA, const struct set *const setB);
bool verify_un(const set *const setUni, const set *const setA, const set *const setB);
bool verify_inter(const set *const setInter, const set *const setA, const set *const setB);
int main(){
struct set *setA = set_empty();
set_insert(69,setA ); set_insert(22,setA ); set_insert(15, setA); set_insert(7,setA );
struct set *setB = set_empty();
set_insert(12,setB ); set_insert(50,setB ); set_insert(15, setB); set_insert(3,setB );
print_set(setA); print_set(setB);
set *setUni = set_union(setA, setB); printf(\" \"); print_set(setUni);
set *setInter = set_intersection(setA, setB);
printf(\" \"); print_set(setInter);
set *setDiff = set_difference(setA, setB);
printf(\" \"); print_set(setDiff);
bool diff = verify_diff(setDiff, setA, setB); printf(\"Test if both sets contain unique values ... %s \", diff ? \"PASS\" : \"FAIL\");
bool uni = verify_un(setUni, setA, setB); printf(\"Test if both sets contain unique values ... %s \", uni ? \"PASS\" : \"FAIL\");
bool inter = verify_inter(setInter, setA, setB); printf(\"Test if both sets contain unique values ... %s \", inter ? \"PASS\" : \"FAIL\");
}
bool verify_diff(const set *const setDiff, const set *const setA, const struct set *const setB) { bool answer = true; for (int x = 0; x size; x++) { if (set_member_of(setDiff->array[x], setA) && set_member_of(setDiff->array[x], setB)) { answer = false; break; } } return answer; }
bool verify_un(const set *const setUni, const set *const setA, const set *const setB) { bool answer;
for (int x = 0; x size; x++) { if (set_member_of(setUni->array[x], setA) && set_member_of(setUni->array[x], setB)) { answer = true; } else { answer = false; break;
} }
return answer; }
bool verify_inter(const set *const setInter, const set *const setA, const set *const setB) { bool answer;
for (int x = 0; x size; x++) { if (!set_member_of(setInter->array[x], setA) && !set_member_of(setInter->array[x], setB)) { answer = true; } else { answer = false; break;
} } return answer; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
