Question

Use C++ programing language

Please don't use other Chegg solved solutions, make it new thanks.

Assignment requirements

Pre-requisite: Please read zyBook Chapter 6 Sets and understand the three operations of Sets (union, intersection, and difference) .

You will be creating your custom Set (think a list of unique elements) class in C++ or a language of your choice. Please do NOT use STL, or any pre-defined library for this assignment.

1. The data type of the set collection: array (or vector).

2. Create three operations (based on Set ADT descriptions in zyBook Chapter 6):

-union()

-intersection()

-difference()

3. Put some test code in main()



6.2 Set operations Union, intersection, and difference The union of sets X and Y, denoted as XU Y, is a set that contains eve
Start 2x speed Set Union(setl, not2) set1 = ( 93, 61, 82, 76 ) result - Create new empty set set2 = 82, 23, 46, 93 ) for each
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please look at my code and in case of indentation issues check the screenshots.

-------------main.cpp---------------

#include <iostream>
#include <vector>
using namespace std;

class Set{
   private:
       int numOfElements;           //set with vector to store elements and numOfElements to store size
       vector <int>setElements;

   public:
       Set()
       {
           numOfElements = 0;       //default constructor sets numOfElements to 0
       }

       void insert(int value)       //inserts a new value into set, only if the value is not already present
       {
           for(int i = 0; i < numOfElements; i++)
           {
               if(setElements[i] == value){   //if value already exists, just return
                   return;
               }
           }
           setElements.push_back(value);
           numOfElements++;
       }

       int setSize(){               //returns size of set
           return numOfElements;
       }


       int search(int value){       //searches if a value exists in set,
           for(int i = 0; i < numOfElements; i++){
               if(setElements[i] == value)       //if value exists, then return 1
                   return 1;
           }
           return 0;                           //if value does not exists, return 0
       }

       int getElementAtIndex(int i){           //returns element of set at a given index
           return setElements[i];
       }

       void printSet(){                       //prints the set
           cout << "{";
           int i;
           for(i = 0; i < numOfElements - 1; i++){
               cout << setElements[i] << ", ";
           }
           cout << setElements[i] << "}\n\n";
       }
};


Set SetUnion(Set set1, Set set2)   //returns Union of 2 sets
{
   Set result;
   for(int i = 0; i < set1.setSize(); i++){
       result.insert(set1.getElementAtIndex(i));
   }
   for(int i = 0; i < set2.setSize(); i++){
       result.insert(set2.getElementAtIndex(i));
   }
   return result;
}

Set SetIntersection(Set set1, Set set2)   //returns Intersection of 2 sets
{
   Set result;
   for(int i = 0; i < set1.setSize(); i++){
       if (set2.search(set1.getElementAtIndex(i)) != 0){
           result.insert(set1.getElementAtIndex(i));
       }
   }
   return result;
}


Set SetDifference(Set set1, Set set2)   //returns SetDifference of 2 sets
{
   Set result;
   for(int i = 0; i < set1.setSize(); i++){
       if (set2.search(set1.getElementAtIndex(i)) == 0){
           result.insert(set1.getElementAtIndex(i));
       }
   }
   return result;
}

int main()
{
   Set set1;           //create empty set1
   set1.insert(93);   //insert elements
   set1.insert(61);
   set1.insert(82);
   set1.insert(76);

   Set set2;           //create empty set2
   set2.insert(82);   //insert elements
   set2.insert(23);
   set2.insert(46);
   set2.insert(93);

   cout << "set 1 is : ";   //print sets
   set1.printSet();

   cout << "set 2 is : ";
   set2.printSet();

   Set unionSet = SetUnion(set1, set2);       //call set functions
   Set intSet = SetIntersection(set1, set2);
   Set diffSet1 = SetDifference(set1, set2);
   Set diffSet2 = SetDifference(set2, set1);

   cout << "Union of sets is is: ";           //print results
   unionSet.printSet();

   cout << "Intersection of sets is is: ";
   intSet.printSet();

   cout << "Difference of set1, set2 is: ";
   diffSet1.printSet();

   cout << "Difference of set2 set1 is: ";
   diffSet2.printSet();

   return 0;
}

--------------Screenshots--------------

File Edit Selection Find View Goto Tools Project Preferences Help www TUIS TE main.cpp X 1 #include <iostream> 2 #include <veFile Edit Selection Find View Goto Tools Project Preferences Help www TUIS TE main.cpp х 43 } 44 45 void printSet() { //printFile Edit Selection Find View Goto Tools Project Preferences Help main.cpp X Set result; for(int i = 0; i < seti.setSize(); i

--------------------Output-----------------------

vc@vaio:-/vs/cpp/custom set a 3 - vc@vaio:-/vs/cpp/custom set$ g++ main.cpp vc@vaio:-/vs/cpp/custom set$ /a.out set 1 is : {9

------------------------------------------------------------------------------------

Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou

Add a comment
Know the answer?
Add Answer to:
Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program has to be in C# language Write a computer program that takes two sets (let...

    Program has to be in C# language Write a computer program that takes two sets (let the user determine the cardinality of each set and enter them manually) and calculates the following operations: Union Intersection Difference (set1 - set2) Cartesian product (set2 X set1) Check whether set2 is a subset of set1 or set1 is a subset of set2 Find the powerset of set1 and print out its elements and cardinality

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • PLEASE ANSWER THE FOLLOWING IN C++!! PLEASE READ THE QUESTION CAREFULLY!!! AS WELL AS WHOEVER ANSWERS...

    PLEASE ANSWER THE FOLLOWING IN C++!! PLEASE READ THE QUESTION CAREFULLY!!! AS WELL AS WHOEVER ANSWERS THIS CORRECTLY I WILL UPVOTE!!! In this project you will design, implement and test the ADT set using both Arrays and Linked Lists and implement all the operations described in the following definitions in addition to the add and remove operations. Sets are one of the basic building blocks for the types of objects considered in discrete mathematics. A set is an unordered collection...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • == Programming Assignment == For this assignment you will write a program that controls a set...

    == Programming Assignment == For this assignment you will write a program that controls a set of rovers and sends them commands to navigate on the Martian surface where they take samples. Each rover performs several missions and each mission follows the same sequence: deploy, perform one or more moves and scans, then return to base and report the results. While on a mission each rover needs to remember the scan results, in the same order as they were taken,...

  • The attached document is a C++ source code, INTERFACE__Class{aSet}.cpp, which contains an interface for class aSet and an implementation section which is largely left empty, so that students could fill in the missing code, where it is written // C++ code

    /* FILE NAME: Class{aSet}.cpp FUNCTION: A template class for a set in C++. It implements all the set operations, except set compliment:  For any two sets, S1 and S2 and an element, e  A. Operations which result in a new set:  (1) S1 + S2 is the union of S1 and S2 (2) S1 - S2 is the set difference of S1 and S2, S1 - S2 (3) S1 * S2 is the set intersection of S1 and S2, S1 * S2 (4) S1 + e (or e +...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • (IN C LANGUAGE) 4.33 (Roman-Numeral Equivalent of Decimal Values) Write a program that prints a table...

    (IN C LANGUAGE) 4.33 (Roman-Numeral Equivalent of Decimal Values) Write a program that prints a table of all the Roman-numeral equivalents of the decimal numbers in the range 1 to 100. Decimal→→Roman↵ -------→→-----↵ 1→→I↵ 2→→II↵ 3→→III↵ 4→→IV↵ 5→→V↵ 6→→VI↵ 7→→VII↵ 8→→VIII↵ 9→→IX↵ 10→→X↵ 11→→XI↵ 12→→XII↵ 13→→XIII↵ 14→→XIV↵ 15→→XV↵ 16→→XVI↵ 17→→XVII↵ 18→→XVIII↵ 19→→XIX↵ 20→→XX↵ 21→→XXI↵ 22→→XXII↵ 23→→XXIII↵ 24→→XXIV↵ 25→→XXV↵ 26→→XXVI↵ 27→→XXVII↵ 28→→XXVIII↵ 29→→XXIX↵ 30→→XXX↵ 31→→XXXI↵ 32→→XXXII↵ 33→→XXXIII↵ 34→→XXXIV↵ 35→→XXXV↵ 36→→XXXVI↵ 37→→XXXVII↵ 38→→XXXVIII↵ 39→→XXXIX↵ 40→→XL↵ 41→→XLI↵ 42→→XLII↵ 43→→XLIII↵ 44→→XLIV↵ 45→→XLV↵ 46→→XLVI↵ 47→→XLVII↵...

  • C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with...

    C# 1. Given two lengths between 0 and 9, create an rowLength by colLength matrix with each element representing its column and row value, starting from 1. So the element at the first column and the first row will be 11. If either length is out of the range, simply return a null. For exmaple, if colLength = 5 and rowLength = 4, you will see: 11 12 13 14 15 21 22 23 24 25 31 32 33 34...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT