Breaking News

Assignment No-1 Implementation of Set Theory using C++ (Sets.cpp)-Prof.B.S prasad

DATA STRUCTURES AND ALGORITHM PROGRAMMES



Problem Statement:

In Second year Computer Engineering class of M students, set A of students play cricket and set B of students play badminton. Write C/C++ program to find and display
i. Set of students who play either cricket or badminton or both 
ii. Set of students who play both cricket and badminton 
iii. Set of students who play only cricket 
iv. Set of students who play only badminton 
v. Number of students who play neither cricket nor badminton

::Problem Solution::
#include<iostream>
using namespace std;
class M
{
int a[10],b[10],c[20],d[20],u[20];
int m,n,i,j,k,p,q,flag,e;
public:
       void get();
       void intersection();
       void Union();
       void only_A();
       void only_B();
       void neither();
};
void M::get()
{
cout<<"enter the total number of students :"<<"\n";
cin>>q;
cout<<"Enter the roll no of all students :"<<"\n";
        for(p=0;p<q;p++)
  {
    cin>>u[p];
  }

  cout<<"Enter total no of students playing cricket: "<<"\n";
  cin>>m;

  cout<<"Enter roll no of students playing cricket: "<<"\n";
  for(i=0;i<m;i++)
  {
    cin>>a[i];
  }


  cout<<"\nEnter total no of students playing badminton: "<<"\n";
  cin>>n;

  cout<<"Enter roll no of students playing badminton: "<<"\n";
  for(i=0;i<n;i++)
  {
    cin>>b[i];
  }

cout<<"\n\nUniversal = {";
  for(p=0;p<q;p++)
  {
  cout<<" "<<u[p];
  if(p!=q-1)
{
cout<<",";
}
  else
{
cout<<" }\n";
}
  }

cout<<"\n\nA = {";
  for(i=0;i<m;i++)
  {
  cout<<" "<<a[i];
  if(i!=m-1)
{
cout<<",";
}
  else
{
cout<<" }\n";
}
  }


cout<<"\nB= {";
  for(i=0;i<n;i++)
  {
  cout<<" "<<b[i];
  if(i!=n-1)
{
cout<<",";
}
  else
{
cout<<" }\n";
}
  }
}



void M::intersection()     //Intersection of two sets
{
k=0;
for(i=0;i<m;i++)
  {
  for(j=0;j<n;j++)
    {
    if(a[i]==b[j])
      {
      c[k]=a[i];
k++;
      }
    }
  }
  cout<<"\nSet of students who play both cricket and badminton = {";
  for(i=0;i<k;i++)
  {
    cout<<" "<<c[i];
  if(i!=k-1)
  {
  cout<<",";
  }
  }
  cout<<" }\n\n";
}


void M::Union()           //Union of Two sets
{
  k=0;
  for(i=0;i<m;i++)
  {
    flag=0;
    for(j=0;j<n;j++)
    {
    if(a[i]==b[j])
      {
flag=1;
}

    }
    if(flag==0)
{
c[k]=a[i];
k++;
}
  }
  for(i=0;i<n;i++)
  {
  c[k]=b[i];
k++;
  }
  cout<<"\nSet of students who play either cricket or badminton or both= {";
  for(i=0;i<k;i++)
  {
  cout<<" "<<c[i];
  if(i!=k-1)
{
cout<<",";
}
  }
  cout<<" }\n\n";
}


void M::only_A()
{
k=0;
  for(i=0;i<m;i++)
  {
    flag=0;
    for(j=0;j<n;j++)
    {
      if(a[i]==b[j])
        {
flag=1;
}
    }
  if(flag==0)
{
c[k]=a[i];
k++;
}
  }
  cout<<"\nSet of students who play only cricket= {";
  for(i=0;i<k;i++)
  {
  cout<<" "<<c[i];
  if(i!=k-1)
{
cout<<",";
}
  }
  cout<<" }\n\n";
}


void M::only_B()
{
  k=0;
  for(i=0;i<n;i++)
  {
    flag=0;
    for(j=0;j<m;j++)
    {
      if(a[j]==b[i])
       {
flag=1;
}
    }
    if(flag==0)
{
c[k]=b[i];
k++;
}
}
cout<<"\nSet of students who play only badminton  ={";
  for(i=0;i<k;i++)
  {
  cout<<" "<<c[i];
  if(i!=k-1)
{
cout<<",";
}
  }
  cout<<" }\n\n";
}

       void M::neither()
{
     
  k=0;
  for(i=0;i<m;i++)
  {
    flag=0;
    for(j=0;j<n;j++)
    {
    if(a[i]==b[j])
      {
flag=1;
}

    }
    if(flag==0)
{
c[k]=a[i];
k++;
}
  }
  for(i=0;i<n;i++)
  {
  c[k]=b[i];
k++;
  }
 


e=0;
for(p=0;p<q;p++)
{
flag=0;
  for(i=0;i<k;i++)
{
if(u[p]==c[i])
{
flag=1;
}
}
if(flag==0)

{
d[e]=u[p];
e++;
}
}
cout<<"\nSet of students who play neither cricket nor badminton= {";
  for(p=0;p<e;p++)
  {
  cout<<" "<<d[p];
  if(p!=e-1)
{
cout<<",";
}
  }
  cout<<" }\n\n";

int count=0;

for(p=0;p<e;p++)
{
count++;
}
cout<<"Number of students who play neither cricket nor badminton="<<count<<"\n";

}
int main()
{
 M s; int ch;
 char r,y;

do
 {
 cout<<" \n1. Intersection\n 2. Union\n 3. Only A\n 4. Only B\n 5.Neither\n";
 cout<<"\n\nEnter the your choice: ";
 cin>>ch;

 s.get();
 switch(ch)
 {
  case 1:
 s.intersection();
break;
  case 2:
 s.Union();
break;
  case 3:
s.only_A();
break;
  case 4:
s.only_B();
break;
 case 5:
s.neither();
break;
  default:
 cout<<"\nInvalid Data.\n";
 }
 cout<<"do you want to continue press 'y':"<<"\n";
cin>>r;
}
while(r=='y');
return 0;
}


OUTPUT:


No comments

Thank You For your Query !! Please Contact Us via Contact Form For any kind of Information.