Question: [C++]. I need to implement the set operations intersection and relative complement. I've implemen……

[C++]. I need to implement the set operations intersection and relative complement. I’ve implemented union and subtraction. PLEASE DO NOT USE STL.

int main() {
    set set1;
    set set2;
    set set3;

    set1.insert(90);
    set1.insert(89);
    set1.insert(49);
    set1.insert(24);
    set1.insert(70);
    set1.insert(99);
    set1.insert(63);
    set1.insert(55);
    set1.insert(75);
    set1.insert(51);

    set2.insert(37);
    set2.insert(59);
    set2.insert(89);
    set2.insert(94);
    set2.insert(71);
    set2.insert(69);
    set2.insert(28);
    set2.insert(95);
    set2.insert(75);

    set1.erase(89);
    set2.erase_one(28);

    cout << "set1 = " << set1 << endl;
    cout << "set2 = " << set2 << endl;
    cout << endl;

    // UNION 
    cout << "set1 + set2 ==> " << endl << set1 + set2 << endl << endl;
    cout << endl;

    //SUBTRACTION
    cout << "set1 - set2 ==> " << endl << set1 - set2 << endl << endl;
    cout << endl;
}

set.cpp

Don't use plagiarized sources. Get Your Custom Essay on
Question: [C++]. I need to implement the set operations intersection and relative complement. I've implemen……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay
#include <algorithm> // Provides copy function
#include <cassert>
#include "set.h"
#include <iostream>
#include <unordered_set>
using namespace std;



bool set::contains(const value_type & target)const
{

    for(int k = 0;k < 1 ;k++ )

    {
        if(data[k]==target)
            return true;
    }
    return false;

}

 const set::size_type set::CAPACITY;

set::size_type set::erase(const value_type& target)
    {
        size_type index = 0;
        size_type many_removed = 0;

        while (index < used)
        {
            if (data[index] == target)
            {
                --used;
                data[index] = data[used];
                ++many_removed;
            }
            else
                ++index;
        }

        return many_removed;
    }

    bool set::erase_one(const value_type& target)
    {
        size_type index; // The location of target in the data array

        // First, set index to the location of target in the data array,
        // which could be as small as 0 or as large as used-1. If target is not
        // in the array, then index will be set equal to used.
        index = 0;
        while ((index < used) && (data[index] != target))
            ++index;

        if (index == used)
            return false; // target isnít in the set, so no work to do.

        // When execution reaches here, target is in the set at data[index].
        // So, reduce used by 1 and copy the last item onto data[index].
        --used;
        data[index] = data[used];
        return true;
    }

    void set::insert(const value_type& entry)
    // Library facilities used: cassert
    {
        assert(size() < CAPACITY);
        if (!this->contains(entry)) {
            data[used] = entry;
            ++used;
        }
    }

std::ostream &operator << (std::ostream&os, const set& arg_set)
{
    os << '{';

    set::size_type i;

    for (i = 0; i < arg_set.used; ++i)
    {
        os << arg_set.data[i];
        if (i != arg_set.used - 1)
            os << ", ";
    }

    os << '}';
    return os;
}

void set::operator +=(const set& arg_set)
// Library facilities used: algorithm, cassert
{

    // OLD ASSERT NO LONGER VALID
    // assert(size( ) + arg_set.size( ) <= CAPACITY);

    // OLD METHOD NO LONGER VALID
    // copy(arg_set.data, arg_set.data + arg_set.used, data + used);
    // used += arg_set.used;

    size_type i;

    for (i = 0; i < arg_set.size(); ++i)
        this->insert(arg_set.data[i]);
}

void set::operator -=(const set& arg_set)
{

    set *bufferSet;
    size_type i, loopVal;

    bufferSet = this;
    loopVal = bufferSet->used;

///*DEBUG*/ cout << "USED = " << bufferSet->used << endl;

    for (i = 0; i < loopVal; ++i)
    {

///*DEBUG*/ cout << "I = " << i << " DATA = " << bufferSet->data[i] << endl;

        if (arg_set.contains(bufferSet->data[i]))
        {

///*DEBUG*/ cout << "ERASING DATA[I] = " << bufferSet->data[i] << endl;

            this->erase(bufferSet->data[i]);
        }
    }
}
set operator +(const set& s1, const set& s2)
// Library facilities used: cassert
{
    set answer;

    // OLD ASSERT NO LONGER VALID
// assert(b1.size( ) + b2.size( ) <= set::CAPACITY);

    answer += s1;
    answer += s2;
    return answer;
}


set operator -(const set& s1, const set& s2)
// Library facilities used: cassert
{
    set answer;

    answer = s1;
    answer -= s2;
    return answer;
}

set.h

// FILE: set.h
// CLASS PROVIDED: set(part of the namespace main_savitch_3)
//
// TYPEDEF and MEMBER CONSTANTS for the set class:
//   typedef ____ value_type
//     set::value_type is the data type of the items in the set. It may be any of
//     the C++ built-in types (int, char, etc.), or a class with a default
//     constructor, an assignment operator, and operators to
//     test for equality (x == y) and non-equality (x != y).
//
//   typedef ____ size_type
//     set::size_type is the data type of any variable that keeps track of how many items
//     are in a set.
//
//   static const size_type CAPACITY = _____
//     set::CAPACITY is the maximum number of items that a set can hold.
//
// CONSTRUCTOR for the set class:
//   set( )
//     Postcondition: The set has been initialized as an empty set.
//
// MODIFICATION MEMBER FUNCTIONS for the set class:
//   size_type erase(const value_type& target);
//     Postcondition: All copies of target have been removed from the set.
//     The return value is the number of copies removed (which could be zero).
//
//   void erase_one(const value_type& target)
//     Postcondition: If target was in the set, then one copy has been removed;
//     otherwise the set is unchanged. A true return value indicates that one
//     copy was removed; false indicates that nothing was removed.
//
//   void insert(const value_type& entry)
//     Precondition:  size( ) < CAPACITY.
//     Postcondition: A new copy of entry has been added to the set.
//
//   void operator +=(const set& addend)
//     Precondition:  size( ) + addend.size( ) <= CAPACITY.
//     Postcondition: Each item in addend has been added to this set.
//
// CONSTANT MEMBER FUNCTIONS for the set class:
//   size_type size( ) const
//     Postcondition: The return value is the total number of items in the set.
//
//   size_type count(const value_type& target) const
//     Postcondition: The return value is number of times target is in the set.
//
//    bool set::contains (const value_type& target) const;
//     Postcondition: The return value is true if
//     target is in the set; otherwise the return
//     value is false.
//
// NONMEMBER FUNCTIONS for the set class:
//   set operator +(const set& b1, const set& b2)
//     Precondition:  b1.size( ) + b2.size( ) <= set::CAPACITY.
//     Postcondition: The set returned is the union of b1 and b2.
//
// VALUE SEMANTICS for the set class:
//    Assignments and the copy constructor may be used with set objects.

#ifndef HW3_SET_H
#define HW3_SET_H
#include "bag1.h"

class set
{
public:
    // TYPEDEFS and MEMBER CONSTANTS
    typedef int value_type;
    typedef std::size_t size_type;
    static const size_type CAPACITY = 30;
    // CONSTRUCTOR
    set( ) { used = 0; }
    // MODIFICATION MEMBER FUNCTIONS
    size_type erase(const value_type& target);
    bool erase_one(const value_type& target);
    void insert(const value_type& entry);
    void ints(const set& set1, const set& set2);
    void operator +=(const set& addend);
    void operator -=(const set& addend);
    // CONSTANT MEMBER FUNCTIONS
    size_type size( ) const { return used; }
    size_type count(const value_type& target) const;
    bool contains(const value_type &target) const;

    value_type data[CAPACITY];  // The array to store items
    size_type used;             // How much of array is used
};

// NONMEMBER FUNCTIONS for the set class
    set operator+(const set &s1, const set &s2);
    set operator-(const set &s1, const set &s2);

#endif //HW3_SET_H

Expert Answer

answers

#include <bits/stdc++.h>
using namespace std;

int main() {
set<int>s1; set<int>s2;

s1.insert(1);
s1.insert(2);
s1.insert(3);

s2.insert(2);
s2.insert(3);
s2.insert(4);

set<int>::iterator it1;
set<int>::iterator it2;

// For intersection
for(it1=s1.begin(); it1!=s1.end(); it1++){
for(it2=s2.begin(); it2!=s2.end(); it2++){
if(*it1 ==*it2){
cout<<*it1<<” “;
}
}
}
cout<<endl;

// For relative complement
for(it1=s1.begin(); it1!=s1.end(); it1++){
int complement = 1;
for(it2=s2.begin(); it2!=s2.end(); it2++){
if(*it1 == *it2){
complement = 0;
}
}
if(complement == 1){
cout<<*it1<<” “;
}
}
return 0;
}

Top Grade Homework
Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents