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.

Second Year Computer Engineering Data Structure Programs:

Data Structure Lab:

Practical 1:

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

--------------------------------------------------------------------------------------------------------


#include <iostream>
using namespace std;
class set
{
int M[30],cri[30],bad[30],i,j,k,con,cob,n,cn,bn,cab[30],temp;
int cv[30],play_both[30],onc[30],onb[30],tmp[30],can[30];
int cuni,cint,conc,conb,cnne;
public:
void Str_n()   //takes the number of students;
{
cout<<"Enter the Class Strength :"<<endl;
cin>>n;
cob=0;
con=0;
cuni=0;
cint=0;
conc=0;
conb=0;
cnne=0;
}
void cri_str_n()   //takes the number of students playing Cricket;
{
cout<<"Number of Student playing Cricket :"<<endl;
cin>>cn;
}
void bad_str_n()   //takes the number of students playing Badminton;
{
cout<<"Number of Student playing Badminton :"<<endl;
cin>>bn;
}
void Strength() //Enters the roll numbers of students from 1 till end automatically;
{
for(i=0;i<n;i++)
{
onc[i]=0;
onb[i]=0;
M[i]=0;
}
for(i=0;i<n;i++)
{
M[i]=i+1;
}
}
void Cricket() //takes the roll no. playing Cricket;
{
con=0;
cout<<"Students playing Cricket :"<<endl;
for(i=0;i<cn;i++)
{
cin>>cri[i];
con++;
cob++;
}
// this loop sorts the set is Descending Order;
for (i=0;i<cn;i++)
{
for(j=0;j<cn;j++)
if (cri[i]<cri[j])
{
int tp=cri[i];
cri[i]=cri[j];
cri[j]=tp;
}
}
}
void Badminton() //takes the roll no. playing Badminton;
{
cout<<"Students playing Badminton :"<<endl;
for(i=0;i<bn;i++)
{
cin>>bad[i];
con++;
cob++;
}
// this loop sorts the set is Descending Order;
for (i=0;i<bn;i++)
{
for(j=0;j<bn;j++)
if (bad[i]<bad[j])
{
int tp=bad[i];
bad[i]=bad[j];
bad[j]=tp;
}

}
}

void Play_both() //Determines the roll nos. playing both the games;
{

for(j=0;j<cn;j++)
{
for(k=0;k<bn;k++)
{
if (cri[j]==bad[k])
{
play_both[cint]=cri[j];
cint++;
cob--;
}
}
}
}
void Play_any() //Determines the roll nos. playing atleast one of the games;
{
i=0;
for(j=0;j<cn;j++,i++)
{
cab[i]=cri[j];
}
for(k=0;k<bn;k++,i++)
{
cab[i]=bad[k];
}
for (i=0;i<con;i++)
{
for(j=0;j<con;j++)
if (cab[i]<cab[j])
{
int tp=cab[i];
cab[i]=cab[j];
cab[j]=tp;
}
}
for(j=0;j<con;j++)
{
if (cab[j]!=cab[j+1])
{
cv[cuni]=cab[j];
cuni++;
}
}
}
void Play_none() //Determines the roll nos. playing nothing;
{
i=0;
con=n+cuni;
for(j=0;j<n;j++,i++)
{
cab[i]=M[j];
}
for(k=0;k<cuni;k++,i++)
{
cab[i]=cv[k];
}
for (i=0;i<con;i++)
{
for(j=0;j<con;j++)
if (cab[i]<cab[j])
{
int tp=cab[i];
cab[i]=cab[j];
cab[j]=tp;
}

}
j=0;
while(j<con)
{
if (cab[j]!=cab[j+1] and j<con-1)
{
can[cnne]=cab[j];
cnne++;
j++;
}
if(cab[j]==cab[j+1])
{
j++;
j++;
}
if(j==con-1 and cab[j]!=cab[j-1])
{
can[cnne]=cab[j];
cnne++;
j++;
}
}
}

void OnCri() //Determines students playing ONLY CRICKET;
{
i=0;
con=cn+cint;
for(j=0;j<cn;j++,i++)
{
tmp[i]=cri[j];
}
for(k=0;k<cint;k++,i++)
{
tmp[i]=play_both[k];
}


for (i=0;i<con;i++)
{
for(j=0;j<con;j++)
if (tmp[i]<tmp[j])
{
int tp=tmp[i];
tmp[i]=tmp[j];
tmp[j]=tp;
}

}
conc=0;
j=0;
while(j<con)
{
if (tmp[j]!=tmp[j+1] and j<con-1)
{
onc[conc]=tmp[j];
conc++;
j++;
}
if(tmp[j]==tmp[j+1])
{
j++;
j++;
}
if(j==con-1 and tmp[j]!=tmp[j-1])
{
onc[conc]=tmp[j];
conc++;
j++;
}
}
}
void OnBad() //Determines students playing ONLY BADMINTON;
{
i=0;
con=bn+cint;
for(j=0;j<bn;j++,i++)
{
tmp[i]=bad[j];
}
for(k=0;k<cint;k++,i++)
{
tmp[i]=play_both[k];
}


for (i=0;i<con;i++)
{
for(j=0;j<con;j++)
if (tmp[i]<tmp[j])
{
int tp=tmp[i];
tmp[i]=tmp[j];
tmp[j]=tp;
}

}
conb=0;
j=0;
while(j<con)
{
if (tmp[j]!=tmp[j+1] and j<con-1)
{
onb[conb]=tmp[j];
conb++;
j++;
}
if(tmp[j]==tmp[j+1])
{
j++;
j++;
}
if(j==con-1 and tmp[j]!=tmp[j-1])
{
onb[conb]=tmp[j];
conb++;
j++;
}
}

}

void set_M() //set M={};
{
cout<<"Total Strength of Class:"<<endl<< "M = {"<<M[0];
for(i=1;i<n;i++)
{
cout<<","<<M[i];
}
cout<<"}"<<endl;
}
void set_Cri() //set Cricket C={};
{
cout<<"Students Playing Cricket :"<<endl<< "A = {"<<cri[0];
for(i=1;i<cn;i++)
{
cout<<","<<cri[i];
}
cout<<"}"<<endl;
}
void set_Bad() //set Badminton B={];
{
cout<<"Students Playing Badminton :"<<endl<< "B = {"<<bad[0];
for(i=1;i<bn;i++)
{
cout<<","<<bad[i];
}
cout<<"}"<<endl;
}
void set_both()
{
cout<<"Students Playing both Cricket and Badminton :"<<endl<<"Z = {"<<play_both[0];
for(i=1;i<cint;i++)
{
cout<<","<<play_both[i];
}
cout<<"}"<<endl;
}
void set_any()
{
cout<<"Students Playing either Cricket or Badminton or both :"<<endl<<"Y = {"<<cv[0];
for(i=1;i<cuni;i++)
{
cout<<","<<cv[i];
}
cout<<"}"<<endl;
}
void set_none()
{
cout<<"Student playing Nothing : "<<endl<<"X = {"<<can[0];
for(i=1;i<cnne;i++)
{
cout<<","<<can[i];
}
cout<<"}"<<endl;
}
void set_OnC()
{
cout<<"Student playing Only Cricket : "<<endl<<"C = {"<<onc[0];
for(i=1;i<conc;i++)
{
cout<<","<<onc[i];
}
cout<<"}"<<endl;
}
void set_OnB()
{
cout<<"Student playing Only Badminton : "<<endl<<"D = {"<<onb[0];
for(i=1;i<conb;i++)
{
cout<<","<<onb[i];
}
cout<<"}"<<endl;
}

};
int main() {
int s,y;
set obj1;
obj1.Str_n(); //takes the number of students;
obj1.Strength(); //takes the roll numbers of students from 1 till end;
obj1.cri_str_n(); //cin<<cn;
obj1.Cricket(); //cin<<A={};
obj1.bad_str_n(); //cin<<bn;
obj1.Badminton(); //cin<<B={};
obj1.Play_any();
obj1.Play_both();
obj1.Play_none();
obj1.OnCri();
obj1.OnBad();

//Sets OUT;
obj1.set_M(); //M={};
obj1.set_Cri(); //C={};
obj1.set_Bad(); //B={};*/
cout<<endl;

do
{
cout<<"\t Enter the Operation you want to perform :"<<endl;
cout<<"1. Student Playing Only Cricket:"<<endl<<"2. Student Playing Only Badminton:"<<endl<<"3. Students Playing Something (Union):"<<endl<<"4. Student Playing Both (Intersection):"<<endl<<"5. Student Playing Nothing:"<<endl;
cin>>s;
switch(s)
{
case 3: obj1.set_any();
break;
case 1: obj1.set_OnC();
break;
case 2: obj1.set_OnB();
break;
case 4: obj1.set_both();
break;
case 5: obj1.set_none();
break;
default: cout<<"!!!INVALID OPERATION!!!"<<endl<<" Try Again......";
}
cout<<endl<<"Do You Wish To Continue This Program ??"<<endl<<"Enter any Natural number if Yes Else Enter 0 or any -ve Integer"<<endl;
cin>>y;
}while(y>0);

return 0;
}
/*

 ******************************output**********************************
Enter the Class Strength :10
Number of Student playing Cricket :5
Students playing Cricket :1 5 6 9 8
Number of Student playing Badminton :3
Students playing Badminton :5 6 1
Total Strength of Class:
M = {1,2,3,4,5,6,7,8,9,10}
Students Playing Cricket :
A = {1,5,6,8,9}
Students Playing Badminton :
B = {1,5,6}
Enter the Operation you want to perform :
1. Student Playing Only Cricket:
2. Student Playing Only Badminton:
3. Students Playing Something (Union):
4. Student Playing Both (Intersection):
5. Student Playing Nothing:
enter your choice: 1
Student playing Only Cricket :
C = {8,9}
Do You Wish To Continue This Program ??
Enter any Natural number if Yes Else Enter 0 or any -ve Integer:1
Enter the Operation you want to perform :
1. Student Playing Only Cricket:
2. Student Playing Only Badminton:
3. Students Playing Something (Union):
4. Student Playing Both (Intersection):
5. Student Playing Nothing:
enter your choice:2
Student playing Only Badminton :
D = {0}
Do You Wish To Continue This Program ??
Enter any Natural number if Yes Else Enter 0 or any -ve Integer:1
Enter the Operation you want to perform :
1. Student Playing Only Cricket:
2. Student Playing Only Badminton:
3. Students Playing Something (Union):
4. Student Playing Both (Intersection):
5. Student Playing Nothing:
enter your choice:3
Students Playing either Cricket or Badminton or both :
Y = {1,5,6,8,9}
Do You Wish To Continue This Program ??
Enter any Natural number if Yes Else Enter 0 or any -ve Integer:1
 Enter the Operation you want to perform :
1. Student Playing Only Cricket:
2. Student Playing Only Badminton:
3. Students Playing Something (Union):
4. Student Playing Both (Intersection):
5. Student Playing Nothing:
enter your choice:4
Students Playing both Cricket and Badminton :
Z = {1,5,6}
Do You Wish To Continue This Program ??
Enter any Natural number if Yes Else Enter 0 or any -ve Integer:1
Enter the Operation you want to perform :
1. Student Playing Only Cricket:
2. Student Playing Only Badminton:
3. Students Playing Something (Union):
4. Student Playing Both (Intersection):
5. Student Playing Nothing:
enter your choice:5
Student playing Nothing :
X = {2,3,4,7,10}
Do You Wish To Continue This Program ??
Enter any Natural number if Yes Else Enter 0 or any -ve Integer:0
*/

Comments

Popular posts from this blog

Implement C++ program for expression conversion as infix to postfix and its evaluation using stack.

Write C++ program to maintain club member‘s information using singly linked list. Store student PRN and Name.

Write C++/Java program for line drawing using DDA or Bresenhams algorithm with patterns such as solid, dotted, dashed, dash dot and thick.