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. 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 (Note- While realizing the set duplicate entries are to avoided)
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()
{
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()
{
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:
1. Intersection
2. Union
3. Only A
4. Only B
5.Neither
Enter the your choice: 1
enter the total number of students :
3
Enter the roll no of all students :
40
50
60
Enter total no of students playing cricket:
2
Enter roll no of students playing cricket:
40
60
Enter total no of students playing badminton:
1
Enter roll no of students playing badminton:
50
Universal = { 40, 50, 60 }
A = { 40, 60 }
B= { 50 }
Set of students who play both cricket and badminton = { }
do you want to continue press 'y':Y/N
Disclaimer: These codes are compiled and executed in Dev c/c++ You are requested to run this code in your compiler before using this program to see the expected output
Post a Comment