Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this assignment to implement a set data structure into C. The set is constructed as a Bit vector, which in turn is implemented

I have this assignment to implement a set data structure into C. The set is constructed as a Bit vector, which in turn is implemented as an array of the data type char. I'm a bit confused on the assignment so feel free to edit the functions below without adding/removing any functions while keeping the same parameters. The functions set_inset and set_union should not be changed. Otherwise, just provide an explanation with an example code of how I should approach this program, how do I use bitvector to add ints into an array of char.

HEADER FILE:

#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 */

MAIN FILE: #include #include #include #include "set.h"

struct set { int capacity; int size; char *array; };

set *set_empty() { // allocate space for the set struct set *ptr = malloc(sizeof(struct set));

// initially the set will be empty with no members, so set length to 0 ptr->size = 0;

// allocate enough space to store 1 member, we'll expand this as needed ptr->array = calloc(sizeof(char));

ptr->capacity = 1;

// return the new Set (or more specifically, a pointer to it) return ptr; }

set *set_single(const int value) { }

void set_insert(const int value, set *s) { if (!set_member_of(value, s)) { int bit_in_array = value; // To make the code easier to read

// 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 < no_of_bytes ; 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 << bit; s->size++; } }

// Note: More effective implementations are possible, but this is // okay for this assignment. set *set_union(const set *const s1, const set *const s2) { set *s = set_empty();

for (int i = 0 ; i < s1->capacity || i < s2->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) { }

set *set_difference(const set *const s1, const set *const s2) { }

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; // To make the code easier to read

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 << bit; }

int set_choose(const set *const s) { }

void set_remove(const int value, set *const s) { }

bool set_equal(const set *const s1, const set *const s2) { }

bool set_subset(const set *const s1, const set *const s2) { }

int set_size(const set *const s) { }

int *set_get_values(const set *const s) { }

void set_destroy(set *s) { }

int main(){

set *setA = set_empty(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

What is the critical value of F on your Excel printout?

Answered: 1 week ago