Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the Data Structure of the following Program? Also Answer the following q

ID: 668076 • Letter: W

Question

What is the Data Structure of the following Program?

Also Answer the following questions: How might a function work that identifies the intersection of two Sets?

#include<iostream>

#include<set> // to declare a set

#include<string> // to deal with strings

#include<sstream> // to split string
#include <cstdlib>
using namespace std;

//class set. Each object of class Set represents a set

class Set
{
     set<int> seti;
     set<int>::iterator setIt;
public:
     Set()
     {
     }

     //removes all the elements and makes a set empty

     void empty()

     {

          seti.clear();

     }

     //inserts an item into the current set

     void insert(int item)

     {

          seti.insert(item);

     }

     //deletes an item form the current set

     void remove(int item)

     {

          seti.erase(item);

     }

     //performs unino of two sets

     void unionOfSets(Set i,Set j)

     {

        

          for(setIt=i.seti.begin();setIt!=i.seti.end();setIt++)

          {

              seti.insert(*setIt);

          }

          for(setIt=j.seti.begin();setIt!=j.seti.end();setIt++)

          {

              seti.insert(*setIt);

          }

        

     }

     //returns the size of a set

     int size()

     {

          return seti.size();

     }

     //returns true if the item is exist in the current set. otherwise returns false

     bool memberOfSet(int item)

     {

          setIt=seti.find(item);

          if(setIt!=seti.end())

              return true;

          else

              return false;

     }

     //displays the items in the set

     void printSet()

     {

          int si=1;

          cout<<"{";

          for(setIt=seti.begin();setIt!=seti.end();setIt++)

          {

              cout<<(*setIt);

              if(si!=seti.size())

                   cout<<",";

              si++;

          }

          cout<<"}"<<endl;

     }

};

int main()

{

     //vlaue

     int v=0;

     //set number

     int n=0;

     //temparary values

     int n1,n2,n3;

     //initialize three sets

     Set *s=new Set[3]; //s[0] s[1] s[2]

     string command,temp;

     //repeat the menu until user wants to quit

     while(1)

     {

          cout<<"Enter a command according to this menu:"<<endl;

          cout<<"e n-Make set n empty:"<<endl;

          cout<<"i v n - Insert the value v in set n"<<endl;

          cout<<"r v n - Remove the value v from set n"<<endl;

          cout<<"u n1 n2 n3 - Assign the union of sets n1 and n2 to set n3"<<endl;

          cout<<"s n - Report the size of set n"<<endl;

          cout<<"p v n - Is value v present in set n?"<<endl;

          cout<<"w n - Write the contents of set n to the terminal"<<endl;

          cout<<"h - See this menu"<<endl;

          cout<<"q - Quit"<<endl;

          //read command from standard input

          getline(std::cin,command);

          //pass the sub string of the after command letter

          string subCommand=command.substr(1,command.length());

          stringstream ss(subCommand); //E.g: for command "i 10 1", subCommand is " 10 1"

        

          switch(command.at(0))

          {

          case 'e':     ss>>temp; //read numbers form the subCommand

                        n=atoi(temp.c_str()); //converting string character to integer

                        s[n].empty();// call empty to make a set empty

                        break;

          case 'i': ss>>temp;

                        v=atoi(temp.c_str());

                        ss>>temp;

                        n=atoi(temp.c_str());

                        //insert value v into set n

                        s[n].insert(v);break;

          case 'r': ss>>temp;

                        v=atoi(temp.c_str());

                        ss>>temp;

                        n=atoi(temp.c_str());

                        //delete a value v from set n

                        s[n].remove(v); break;

          case 'u': ss>>temp;

                        n1=atoi(temp.c_str());

                        ss>>temp;

                        n2=atoi(temp.c_str());

                        ss>>temp;

                        n3=atoi(temp.c_str());

                        //perfoms uninon of set n1 and set 2 and store the resultent set into set n3

                        s[n3].unionOfSets(s[n1],s[n2]); break;

          case 's': ss>>temp;

                        n=atoi(temp.c_str());

                        //prints how many vlaue the set n contains

                        cout<<"Set "<<n<<" contains "<<s[n].size()<<" values"<<endl;break;

          case 'p': ss>>temp;

                        v=atoi(temp.c_str());

                        ss>>temp;

                        n=atoi(temp.c_str());

                        //if condition true means that the value v is exist in set n; otherwise the value does not exist.

                        if(s[n].memberOfSet(v))

                             cout<<"value "<<v<<" is exist in the set "<<n<<endl;

                        else

                             cout<<"value "<<v<<" does not exist in the set "<<n<<endl; break;

          case 'w': ss>>temp;

                        n=atoi(temp.c_str());

                        cout<<"Set "<<n<<" =";

                        //display the set

                        s[n].printSet(); break;

          case 'h': //re displays the menu

                        break;

          case 'q'://quit from the menu

                exit(0); break;

          }

     }

     system("pause");

     return 0;

}

Explanation / Answer

#include<stdio.h>
#include<conio.h>
void Union(int set1[10],int set2[10],int m,int n);
void Intersection(int set1[10],int set2[10],int m,int n);
void main()
{
int a[10],b[10],m,n,i,j;
int ch;
clrscr();
printf(" Enter the number of elements in first set: ");
scanf("%d",&m);
printf(" Enter the elements: ");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf(" Element of First set: ");
for(i=0;i<m;i++)
{
printf("%d ",a[i]);
}
printf(" Enter the number of elements in second set: ");
scanf("%d",&n);
printf(" Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf(" Element of second set ");
for(i=0;i<n;i++)
{
printf("%d ",b[i]);
}
for(;;)
{
printf(" Menu 1.Union 2.Intersection");
printf(" 3.exit");
printf(" Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
Union(a,b,m,n);
break;
case 2:
Intersection(a,b,m,n);
break;
case 3:
exit(0);
}
getch();
}
}
void Union(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==c[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}
printf(" Element of resultant set ");
for(i=0;i<k;i++)
{
printf(" %d",c[i]);
}
}
void Intersection(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
flag=1;
break;
}
}
if(flag==1)
{
c[k]=a[i];
k++;
}
}
if(k==0)
{
printf(" Resultant set is null set! ");
}
else
{
printf(" Element of resultant set ");
for(i=0;i<k;i++)
{
printf(" %d",c[i]);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote