Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please Answer with C++. I need help ASAP! Need intset.h, intset.cpp, lab1.cpp Answer Input: lab1.cpp // DO NOT change anything in this file. Your code
Please Answer with C++. I need help ASAP! Need intset.h, intset.cpp, lab1.cpp
Answer Input: lab1.cpp
// DO NOT change anything in this file. Your code must compile and give the // correct output with this main on the linux machines. // Do NOT put an endl in operator using namespace std; int main() { IntSet A(9), B(15,3), C(10,5,8), D(12,5,10,12,-500), E, X(D), Y; // sets A and B acquire desired values // A = { 3 7 9} // B = { 3 5 9 12 15} cout > A; cout > B; // As a shortcut when testing, you can use insert to avoid typing each time // A.insert(3); // A.insert(7); // B.insert(5); // B.insert(9); // B.insert(12); coutPart 2: Programming Create the abstract data type (ADT) for an integer set called IntSet One IntSet object represents one mathematical set of non-negative integers (includes zero). One set keeps track of unique integers, whether integers are in the set or not. Implement the internal representation as follows. An IntSet object's implementation is represented a bool array internally, containing true and false. For example, if you call the array set, then set[num] is true if the integer num is in the set, false if num is not in the set. Use "new" to dynamically allocate memory for the array. One object's array stores one math set's values. Develop a full class with the following operations: 1. operatort: returns the union of two sets, the set of elements that are contained in either or both sets. (Note - do not call any identifiers "union" as that is a keyword in C++.) 2. operator*: returns the intersection of two sets, the set of all elements that are common to both sets. 3. operator-: returns the difference of two sets, say AB, which is the set of all elements that are in set A but not in set B. (E.g., A={23530},B={351040}, then AB is {230} ) 4. operator=: the assignment operator to assign one set to another. Righthand side replaces lefthand side. 5. operators (+=,=,=) : the union, intersection, difference assignment. 6. operators (==,!=) : bool equality and inequality. A==B if the mathematical sets contain the same element 7. insert: insert an element into a set, has bool return value, e.g., bool success = A.insert (5); (Note: ignore invalid integers, return false, e.9., bool success = A. insert (10); If a value is already in the set and is inserted, return true). 8. remove: remove an element from a set, bool return value, e.g., bool success = A.remove (10); (Note: ignore invalid integers, return false, e.g, bool success = A. remove (10); ) . 9. isEmpty: determine if a set is empty or not, bool return value, e.g., if (A. isempty ()) ... 10. is InSet: determine if an integer is in the set or not, e.9., if (A.isinset (5)) ... (Note: returns false for invalid integers). 11. operator{510122025100} to represent the set containing the integers 5,10,12,20,25, and 100. The empty set is displayed exactly as: \{\} (Output exactly as shown, inside curly braces with one blank before each number: output no other blanks. Note that operator >: to input an entire set. (Note: This is a simple loop to take integers; terminate at some sentinel value, say -1. Ignore invalid integers. Zero is valid as it is non-negative.) 13. All proper constructor and destructor functions; constructor(s) initialize a set to (1) The "empty set" (a set which has no elements) or, (2) A set that can take up to five values to be inserted into the set e.9., IntSet A,B(3),C(12,5,5),D(5,10,12),E(100,2,5,45),F(1,2,3,4,5); IntSet X(D); //X is an exact copy of D. Set A is the empty set. (For implementation, you could use nullptr, but using an array of one element keeps it cleaner.) While A is currently the empty set, after the operator >>, operator=, etc. is used, the set could contain many values. Set B holds the value 3 . Set C has a current largest value of 12 , also contains the value 5 . The constructor for D will ignore the 10, but is initialized to contain 5 and 12,E is 245100}.F is {12 345}. X is a deep copy of D, is {512}. You must initially size the arrays by the parameters, e.g., A's array is size 1, B's array is size 4, C and D's array are size 13, E's array size is 101, etc. Later, after operations, the size may change. You may need to make arrays larger for operations later (e.9.,A=B;); but since new and delete are costly operations, typically, for efficiency reasons, you don't shrink arrays if less space is needed. One component of this assignment is to be efficient, to minimize unnecessary new and delete operations. Often, when more memory is needed for an array, applications will double the array size to minimize the number of new and delete operations. Do not do that in your program. This assignment is about practice of memory management (proper memory allocation and deallocation). Having a larger than needed array can mask errors. As always expected when programming, comment clearly and thoroughly. Comments in the class definition file should describe the ADT (in the beginning comment block of the class definition). Additionally, clearly state any assumptions and implementation details you make. See the Rational class and List class for sample comments. Functions in the .cpp file must be separated using comment separator lines as shown in code examples. The two sample programs (CSS 342 sample code page) that will help guide you through implementation are the Rational class example for the use of its operators overloading and the Array class example for its use of memory management: http://courses.washington.edu/css 342/ zander/code.html You must handle errors involving a set, e.g., trying to access a non-existent value, but you do not need to worry about data type. In other words, you will not get non-integers, e.g., char instead of an int as input. This is best handled with exception handling (that we do not cover in C++ ). The ultimate rule is that all operations must work correctly and not crash under any circumstances (given integers). Do NOT print any error messages; deal with errors appropriately and document the use. Appropriately means that you handle it in a reasonable way. For example, if the user tries to insert a negative integer, handle it without crashing. In this situation, ignoring the bad value makes the most sense. Sometimes there may be different ways to handle an error. You choose how you will handle it and document your use. Just don't crash. We are using the C philosophy of warning the user in documentation about the usage but letting them do what they want. Basically, if the user does something incorrect, the results are unreliable. Test your code thoroughly. Write, compile, and test one function or a few at a time. Start with the constructor, the destructor, and operator. While the given main doesn't thoroughly test your IntSet class, my grading main will contain similar code. The given main helps assure you have correct spelling, parameters, and return types for all your IntSet functions. In general, when testing arrays, always test inside the array (elements in the middle), the boundaries of the array (elements at either end of the array and just before the first and just after the last element), and outside the array (values before the first element and after the last element). Do NOT discuss implementation (arrays) on publicly (electronically or with a person). Any questions having to do with arrays come to only me. I.e., if the question has the word array in it, ask me (email, zoom, in person). Any public questions would have to do with the IntSet ADT, and of course, any class-related stuff (software, etc). About the bool array - You don't store a number in the array. The subscript represents the number in the set. True value means the subscript is a number in the set; false means it is not. With respect to the STL (Standard Template Library), our ADT IntSet contains a set of non-redundant integers, but the data structure, the implementation of our ADT, is a bool array. This is different from the STL set implementation. For example, suppose our IntSet object is {2568}. Without searching, you can pop into the array to see if a value is in the set. Check set[3] for true or false to know whether 3 is in the set. A visualization of the object x : A true value is shown as T, false as F. This technique of using a bool array is handy. Suppose you have a game where someone is guessing letters and you need to check if they have already guessed a letter. You can easily map the letters, ' a ' ' b ' ' c ' etc. to the subscripts. 0,1,2, etc. To determine if the letter has been chosen, check: usedLetters [letter-' a ]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started