IntSet IntSet: :unionWith(const IntSet& other IntSet) const { } T for (int i = 0; i < otherIntSet.used; ++i) { IntSet.data[i]); add (other }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

void IntSet::resize(int new_capacity)
{
      if (new_capacity < used)
    new_capacity = used;

  if (new_capacity < 1)
    new_capacity = 1;

  capacity = new_capacity;

  int *newData = new int[capacity];

  for (int i = 0; i < used; ++i)
    newData[i] = data[i];

  delete[] data;

  data = newData;
}
    

IntSet::IntSet(int initial_capacity)
{
   if (initial_capacity >= 1)
   {
        capacity = initial_capacity;
   }
   else
   {
    capacity = DEFAULT_CAPACITY;
   }
}

IntSet::IntSet(const IntSet& src)
{
  capacity = src.capacity;
  used = src.used;
  data = new int[capacity];
  for (int i = 0; i < used; ++i)
    data[i] = src.data[i];
}

IntSet::~IntSet()
{
     delete[] data;
}

IntSet& IntSet::operator=(const IntSet& rhs)
{
     if (this != &rhs) {
    int *newData = new int[rhs.capacity];

    for (int i = 0; i < rhs.used; ++i)
      newData[i] = rhs.data[i];

    delete[] data;
    data = newData;
    capacity = rhs.capacity;
    used = rhs.used;
  }
  return *this;
}

int IntSet::size() const
{
   return used;
}

bool IntSet::isEmpty() const
{
  if (used == 0)
    return true;

  else
    return false;
}

bool IntSet::contains(int anInt) const
{
   for (int i = 0; i < used; i++) {
    if (data[i] == anInt)
      return true;
  }

  return false;
}

bool IntSet::isSubsetOf(const IntSet& otherIntSet) const
{
    for (int i = 0; i < used; i++) { //Use the contains method of IntSet class to check whether the otherIntSet contains data[i]
    if (otherIntSet.contains(data[i]) == 0) { // If it returns 0, then the other IntSet does not contain any of the elements of data. Thus, return False.
      return false;
    }
  }

  return true;
}


void IntSet::DumpData(ostream& out) const
{  // already implemented ... DON'T change anything
   if (used > 0)
   {
      out << data[0];
      for (int i = 1; i < used; ++i)
         out << "  " << data[i];
   }
}

IntSet IntSet::unionWith(const IntSet& otherIntSet) const
{
       for (int i = 0; i < otherIntSet.used; ++i) {
    add(otherIntSet.data[i]);
  }

}

IntSet IntSet::intersect(const IntSet& otherIntSet) const
{
     IntSet interSet = (*this); // Create copy of invoking IntSet

    for (int index = 0; index < size(); index++) { //Loop through each of the elements in the data array.
        if(!otherIntSet.contains(data[index])){ //If the otherIntSet does not contain the data[index],
            interSet.remove(data[index]); //then remove the data[index] using the remove method.
        }
    }
    return interSet;
}

IntSet IntSet::subtract(const IntSet& otherIntSet) const
{
   IntSet S3 = IntSet(1);
   return S3; 
}

void IntSet::reset()
{
    used = 0;
  delete[] data;
  int *newData = new int[DEFAULT_CAPACITY];
  data = newData;
  capacity = 1;
}

bool IntSet::add(int anInt)
{
   if (contains(anInt) == 0) {  
    if (used > capacity)    // check if the array is not full
      resize(int(1.5 * capacity) + 1);

    data[used] = anInt;
    used++;
    return true; //if not full then return to data
  }

  return false;
}

bool IntSet::remove(int anInt)
{
    bool alreadyFound = false;
  if (contains(anInt) == 0) {
    return false;
  }

  for (int i = 0; i < used; i++) {
    if (data[i] == anInt) {
      alreadyFound = true;
    }

    if (alreadyFound == true && i != used - 1) {
      data[i] = data[i + 1];
    }
  }

  used--;

  return true;
}

bool operator==(const IntSet& is1, const IntSet& is2)
{
    if (is1.isSubsetOf(is2) && is2.isSubsetOf(is1)) {
    return true;
  }

  return false;
}

188
189
190
191
100
IntSet IntSet: :unionWith(const IntSet& other IntSet) const
{
192
193
194 }
1 for (int i = 0; i < otherIntSet.used; ++i) {
add (other IntSet.data[i]);
}
Transcribed Image Text:188 189 190 191 100 IntSet IntSet: :unionWith(const IntSet& other IntSet) const { 192 193 194 } 1 for (int i = 0; i < otherIntSet.used; ++i) { add (other IntSet.data[i]); }
191
C:\Users\r1821655\OneDrive\Dropbox\Screenshots\...
28 C:\Users\r1821655\OneDrive\Dropbox\Screenshots\One...
In member function 'IntSet IntSet::unionWith(const IntSet&) const':
[Error] passing 'const IntSet' as 'this' argument of 'bool IntSet::add(int)' discards qualifiers [-fpermissive]
Transcribed Image Text:191 C:\Users\r1821655\OneDrive\Dropbox\Screenshots\... 28 C:\Users\r1821655\OneDrive\Dropbox\Screenshots\One... In member function 'IntSet IntSet::unionWith(const IntSet&) const': [Error] passing 'const IntSet' as 'this' argument of 'bool IntSet::add(int)' discards qualifiers [-fpermissive]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education