Advertisement
Guest User

Sets

a guest
Feb 28th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.67 KB | None | 0 0
  1. /*
  2.     First name  XXX
  3.     Last name   XXX
  4.     Student ID  XXXXXX (e.g. 01234567)
  5.     Username    XXX (e.g. aaa123)
  6. */
  7.  
  8. //IMPORTANT: before submitting your source code, please make sure to comment your main function (needed for automated testing)
  9.  
  10. #include <iostream>
  11.  
  12. // do not use using namespace std
  13.  
  14. // do not alter the typedef
  15. typedef int set_t;
  16. // easier to keep the type more abstract
  17. // (many operations would work also if
  18. // a type other than int was used to define set_t)
  19.  
  20.  
  21. struct set_int {
  22.     int capacity; // physical size of the set (i.e. the maximum number of elements it can contain)
  23.     int size;     // logical size of the set (i.e. the actual number of elements it contains)
  24.     set_t *data;  // dynamic array containing the elements of the set
  25. };
  26.  
  27. typedef set_int* setptr;
  28. // more abstract, avoids cumbersome notations
  29. // such as having to write set_int*& below
  30.  
  31. void set_init(setptr& s, int initial_capacity);
  32. // Initialises a set with the given initial capacity and dynamically allocates its internal array data.
  33. // The set contains no elements yet, and its size should be set to 0.
  34.  
  35. void set_copy(const setptr& original, setptr& copy);
  36. // Copies the original set elements into the given copy set.
  37. // (with appropriate memory allocation so that the original
  38. // and the copy dont share the same internal dynamic array).
  39. // The size and capacity of the copy set should naturally be initialised
  40. // using the size and the capacity of the original set.
  41.  
  42. bool set_is_empty(const setptr& original);
  43. // Returns true if the set is empty. Returns false otherwise.
  44.  
  45. int set_size(const setptr& s);
  46. // Returns the size of a set.
  47.  
  48. int set_capacity(const setptr& s);
  49. // Returns the capacity of a set.
  50.  
  51. bool set_is_valid(const setptr& s);
  52. // Returns true if a set is valid, i.e. it does not contain any duplicated values.
  53. // Returns false otherwise.
  54.  
  55. void set_print(const setptr& s);
  56. // Prints all the elements of a set.
  57. // (since there is no particular order among the elements of the set,
  58. // you can simply print them as they are stored in the dynamic array data.)
  59.  
  60. void set_insert(setptr& s, const set_t& element);
  61. // Inserts element into the set, making its internal array grow, potentially.
  62. //
  63. // If the set size reaches its capacity (i.e. if the set is full), then:
  64. // 1) the dynamic array data should be re-allocated by doubling (x2) its capacity,
  65. // 2) all the existing elements should be copied in the new array,
  66. // 3) the new element is added to the new array,
  67. // 4) the memory allocated for the old array should be released.
  68. // size and capacity should also be updated accordingly.
  69.  
  70. void set_remove(setptr& s, const set_t& element);
  71. // Removes element from the set.
  72.  
  73. bool set_contains(const setptr& s, const set_t& element);
  74. // Returns true if the set contains element. Returns false otherwise.
  75.  
  76. set_t set_min_value(const setptr& s);
  77. // Returns the minimum value of the set.
  78.  
  79. set_t set_max_value(const setptr& s);
  80. // Returns the maximun value of the set.
  81.  
  82. set_t set_average_value(const setptr& s);
  83. // Returns the average value of the set.
  84.  
  85. setptr set_intersection(const setptr& s1, const setptr& s2);
  86. // Returns a set representing the Intersection of two sets
  87. // (i.e. all the elements in both sets).
  88. // e.g. the intersection of {1, 2, 3} and {3, 4} should be {3})
  89.  
  90. setptr set_union(const setptr& s1, const setptr& s2);
  91. // Returns a set representing the Union of two sets
  92. // (i.e. all the elements of the two sets, without any duplicates).
  93. // e.g. the union of {1, 2, 3} and {3, 4} should be the set {1, 2, 3, 4}.
  94.  
  95. setptr difference(const setptr& s1, const setptr& s2);
  96. // Returns a set representing the Difference between two sets
  97. // (i.e. all the elements in one (s1 here), and not in the other (s2 here)).
  98. // e.g. the union of s1={1, 2, 3} and s2={3, 4} should be {1, 2})
  99.  
  100. void set_deallocate(setptr s);
  101. // Deallocates the memory of a set.
  102.  
  103. // you can define and add use additional functions if you might need to
  104.  
  105. ///////////////////
  106. // main function //
  107. ///////////////////
  108.  
  109. //IMPORTANT (Reminder): before submitting your source code, please make sure to comment your main function (needed for automated testing)
  110.  
  111. /////////////////////////////////////////////
  112. // Functions definitions (implementations) //
  113. /////////////////////////////////////////////
  114.  
  115. /*int main() {
  116.     std::cout<<"Testing of set functions"<<std::endl;
  117.     setptr setA = new set_int;
  118.     //Test init
  119.     set_init(setA,3);
  120.     std::cout<<"size: "<<set_size(setA)<<" capacity: "<<set_capacity(setA)<<std::endl;
  121.     std::cout<<"is set empty? "<<set_is_empty(setA)<<std::endl;
  122.     set_insert(setA,2);
  123.     set_insert(setA,4);
  124.     set_insert(setA,2);
  125.     set_insert(setA,5);
  126.  
  127.     std::cout<<"is set empty? "<<set_is_empty(setA)<<std::endl;
  128.     set_print(setA);
  129.     std::cout<<"set contains 1: "<<set_contains(setA,1)<<" set doesn't contain 6: "<<set_contains(setA,6)<<std::endl;
  130.     std::cout<<"size: "<<set_size(setA)<<" capacity: "<<set_capacity(setA)<<std::endl;
  131.     set_insert(setA,3);
  132.     set_remove(setA,2);
  133.     set_insert(setA,12);
  134.     set_insert(setA,334);
  135.     set_insert(setA,7);
  136.     std::cout<<"Maximum Value: "<<set_max_value(setA)<<std::endl;
  137.     std::cout<<"Minimum Value: "<<set_min_value(setA)<<std::endl;
  138.     std::cout<<"Average value: "<<set_average_value(setA)<<std::endl;
  139.     set_print(setA);
  140.     std::cout<<"size: "<<set_size(setA)<<" capacity: "<<set_capacity(setA)<<std::endl;
  141.  
  142.     setptr setB = new set_int;
  143.     //Test init
  144.     set_init(setB,5);
  145.     set_insert(setB,334);
  146.     set_insert(setB,60);
  147.     set_insert(setB,3);
  148.     set_insert(setB,9);
  149.     set_insert(setB,1);
  150.     set_print(setB);
  151.  
  152.     set_print(set_intersection(setA,setB));
  153.     set_print(set_union(setA,setB));
  154.     set_print(difference(setA,setB));
  155.     set_print(difference(setB,setA));
  156.     return 1;
  157. } */
  158.  
  159.  
  160. // YOUR CODE HERE
  161.  
  162. void set_init(setptr& s, int initial_capacity){
  163.     s->size=0;
  164.     s->capacity=initial_capacity;
  165.     s->data = new int[initial_capacity];
  166. }
  167.  
  168. void set_copy(const setptr& original, setptr& copy){
  169.     set_init(copy, original->capacity);
  170.     copy->size=original->size;
  171.     for(int i =0; i<original->capacity;i++){
  172.         copy->data[i] = original->data[i];
  173.     }
  174. }
  175.  
  176. bool set_is_empty(const setptr& original){
  177.     return (original->size==0);
  178. }
  179.  
  180. int set_size(const setptr& s){
  181.     return s->size;
  182. }
  183.  
  184. int set_capacity(const setptr& s){
  185.     return s->capacity;
  186. }
  187.  
  188. bool set_is_valid(const setptr& s){
  189.     bool result = true;
  190.     for (int i = 0; i<s->size-1; i++){
  191.         for (int j =i+1; j<s->size; j++){
  192.             if (s->data[i] == s->data[j]){
  193.                 result = false;
  194.             }
  195.         }
  196.     }
  197.  
  198.     return result;
  199. }
  200.  
  201. void set_print(const setptr& s){
  202.     for (int i=0; i<s->size; i++){
  203.         std::cout<<s->data[i]<<" ";
  204.     }
  205.     std::cout<<std::endl;
  206. }
  207.  
  208. void set_insert(setptr& s, const set_t& element){
  209.     if (set_contains(s,element)){
  210.         return;
  211.     } else if (s->size>=s->capacity){
  212.         set_t* doubled_list = new int[s->capacity*2];
  213.         for (int i = 0; i<s->capacity;i++){
  214.             doubled_list[i] = s->data[i];
  215.         }
  216.         doubled_list[s->capacity] = element;
  217.         set_t* old_array = s->data;
  218.         s->data = doubled_list;
  219.         delete old_array;
  220.         s->capacity = s->capacity*2;
  221.         s->size += 1;
  222.     } else {
  223.         s->data[s->size] = element;
  224.         s->size += 1;
  225.     }
  226. }
  227.  
  228. bool set_contains(const setptr& s, const set_t& element){
  229.     for (int i = 0; i<s->size; i++){
  230.         if (s->data[i] == element){
  231.             return true;
  232.         }
  233.     }
  234.     return false;
  235. }
  236.  
  237. void set_remove(setptr& s, const set_t& element){
  238.     if (set_contains(s,element)){
  239.         for(int i = 0;i<s->size;i++){
  240.             if (s->data[i] == element){
  241.                 for(int j = 0; j<s->size-1-i;j++){
  242.                     s->data[i+j] = s->data[i+j+1];
  243.                 }
  244.                 s->size-=1;
  245.                 return;
  246.             }
  247.         }
  248.     }
  249. }
  250.  
  251. set_t set_min_value(const setptr& s){
  252.     set_t minimum = INT32_MAX;
  253.     for(int i =0; i<s->size;i++){
  254.         if (s->data[i] < minimum){
  255.             minimum = s->data[i];
  256.         }
  257.     }
  258.     return minimum;
  259.  
  260. }
  261.  
  262. set_t set_max_value(const setptr& s){
  263.     set_t maximum = INT32_MIN;
  264.     for(int i =0; i<s->size;i++){
  265.         if (s->data[i] > maximum){
  266.             maximum = s->data[i];
  267.         }
  268.     }
  269.     return maximum;
  270. }
  271.  
  272. set_t set_average_value(const setptr& s){
  273.     int sum = 0;
  274.     for (int i = 0; i<s->size; i++){
  275.         sum += s->data[i];
  276.     }
  277.     return sum/s->size;
  278. }
  279.  
  280. setptr set_intersection(const setptr& s1, const setptr& s2){
  281.     setptr intersection = new set_int;
  282.     int size;
  283.     if (s1->size>s2->size){
  284.         size = s1->size;
  285.     } else {
  286.         size = s2->size;
  287.     }
  288.     set_init(intersection,size);
  289.     for (int i = 0; i<s1->size; i++){
  290.         if (set_contains(s2, s1->data[i])){
  291.             set_insert(intersection,s1->data[i]);
  292.         }
  293.     }
  294.     return intersection;
  295. }
  296.  
  297. setptr set_union(const setptr& s1, const setptr& s2){
  298.     setptr union_set = new set_int;
  299.     set_init(union_set,s1->size+s2->size);
  300.     for (int i = 0; i<s1->size;i++){
  301.         set_insert(union_set,s1->data[i]);
  302.     }
  303.     for (int i = 0; i<s2->size;i++){
  304.         if (!set_contains(union_set,s2->data[i])){
  305.             set_insert(union_set,s2->data[i]);
  306.         }
  307.     }
  308.     return union_set;
  309. }
  310.  
  311. setptr difference(const setptr& s1, const setptr& s2){
  312.     setptr difference = new set_int;
  313.     set_init(difference,s1->size);
  314.  
  315.     for(int i = 0; i<s1->size; i++){
  316.         if(!set_contains(s2,s1->data[i])){
  317.  
  318.             set_insert(difference,s1->data[i]);
  319.         }
  320.     }
  321.     return difference;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement