Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the isDisjoint method for this data structure. This is a bool method that takes one parameter of type Set. The method returns true if

Write the isDisjoint method for this data structure. This is a bool method that takes one parameter of type Set. The method returns true if the owning set and the parameter set are disjoint where two sets are said to be disjoint if they have no elements in common. You may find it helpful to use the member function when you write the isDisjoint method.

*use C++ syntax

code:

#include

using namespace std;

#include "Set.h"

Set::Set(){

// Note #1: The array named elements has already been created (we do not have to)

// Note #2: However, the array (when created) happens to contain whatever 'junk' values

// were in memory at that location beforehand

size = 0;

}

// ==========================================================================================

bool Set::isMember(int item){

// Iterate through each item in the elements array ...

for(int i = 0; i < size; i++){

// If we find a match, we can return true immediately ...

if(elements[i] == item){

return true;

}

}

// Otherwise, if we go through the entire array and no match is found, then return false

return false;

}

// ==========================================================================================

void Set::insert(int item){

if(size == MAX){

cout << "Error: The set is full already" << endl;

}

else if(!isMember(item)){

// We use the isMember( ) method to return True/False whether the item is already

// in the set -- since Sets are not allowed to contain duplicate items

elements[size] = item;

size++;

}

}

int Set::cardinality(){

return size;

}

void Set::display(){

cout << "{ ";

for(int i = 0; i < size; i++){

cout << elements[i] << " ";

}

cout << "}";

}

void Set::remove(int numRemove){

if(isMember(numRemove)){

for(int i = 0; i < size; i++){

if(elements[i] == numRemove){

elements[i] = elements[size-1];

size--;

}

}

}

}

void Set::computeUnion(Set a, Set b){

for(int i = 0; i < a.size; i++){

insert(a.elements[i]);

}

for(int j = 0; j < b.size; j++){

insert(b.elements[j]);

}

}

void Set::computeIntersection(Set a, Set b){

for (int i = 0; i < a.size; i++){

for(int j = 0; j < b.size; j++){

if(a.elements[i] == b.elements[j]){

insert(a.elements[i]);

}

}

}

}

void Set::computeDifference(Set a, Set b){

for (int i = 0; i < a.size; i++){

for(int j = 0; j < b.size; j++){

if(a.elements[i]==b.elements[i]){

remove(a.elements[i]);

}

else{

insert(a.elements[i]);

}

}

}

bool Set::isSame(Set a){

for(int = 0; i

if(!a.isMember(elements{i})){

return false;

}

}

return true;

}

bool Set::isDisjoint(Set a){

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions