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;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class Students{
int m,A[100],B[100],a,b,count;
public:
Students(){
m=a=b=count=0;
}
void getData(){
cout<<"\nEnter total no. of students :";
cin>>m;
cout<<"\nEnter no. of students who play cricket :";
cin>>a;
cout<<"\nEnter the roll nos : ";
for(int i=0 ; i<a ; i++){
cin>>A[i];
}
cout<<"\Enter no. of students who play badminton : ";
cin>>b;
cout<<"\nEnter the roll nos : ";
for(int i=0 ; i<b ; i++){
cin>>B[i];
}
}
void neither(){
cout<<"\nNo. of Students who play neither of the games are : ";
cout<<m-count;
}
void either(){
int flag =0;
cout<<"\nStudents who play either of the games : ";
for(int i=0 ; i<a ; i++){
cout<<"\n"<<A[i];
count++;
}
for(int i=0 ; i<b ; i++){
for(int j=0 ; j<a ; j++){
if(B[i]==A[j]){
flag=1;}
}
if(flag==0){
cout<<"\n"<<B[i];
count++;
}
flag=0;
}
}
void both(){
int flag=0;
cout<<"\nStudents who play both games are : ";
for(int i=0 ; i<a ; i++){
for(int j=0 ; j<b ; j++){
if(A[i] == B[j])
flag=1;
}
if(flag==1){
cout<<"\n"<<A[i];
flag=0;
}
}
}
void ony_Cricket(){
int flag=0;
cout<<"\nStudents who play only Cricket are : ";
for(int i=0 ; i<a ; i++){
for(int j=0 ; j<b ; j++){
if(A[i] == B[j]){
flag=1;
}
}
if(flag==0){
cout<<"\n"<<A[i];
}
flag=0;
}
}
void ony_Badminton(){
int flag=0;
cout<<"\nStudents who play only Badminton are : ";
for(int i=0 ; i<b ; i++){
for(int j=0 ; j<a ; j++){
if(B[i] == A[j]){
flag=1;
}
}
if(flag==0){
cout<<"\n"<<B[i];
}
flag=0;
}
}
};
int main(){
Students s;
int ch;
do{
cout<<"\n1.Get Data \n2.Only Badminton \n3.Only Cricket \n4.Both \n5.Either \n6.Neither \n7.EXIT";
cin>>ch;
switch(ch){
case 1:s.getData();break;
case 2:s.ony_Badminton();break;
case 3:s.ony_Cricket();break;
case 4:s.both();break;
case 5:s.either();break;
case 6:s.neither();break;
}
}while(ch!=7);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
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;
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
class Students{
int m,A[100],B[100],a,b,count;
public:
Students(){
m=a=b=count=0;
}
void getData(){
cout<<"\nEnter total no. of students :";
cin>>m;
cout<<"\nEnter no. of students who play cricket :";
cin>>a;
cout<<"\nEnter the roll nos : ";
for(int i=0 ; i<a ; i++){
cin>>A[i];
}
cout<<"\Enter no. of students who play badminton : ";
cin>>b;
cout<<"\nEnter the roll nos : ";
for(int i=0 ; i<b ; i++){
cin>>B[i];
}
}
void neither(){
cout<<"\nNo. of Students who play neither of the games are : ";
cout<<m-count;
}
void either(){
int flag =0;
cout<<"\nStudents who play either of the games : ";
for(int i=0 ; i<a ; i++){
cout<<"\n"<<A[i];
count++;
}
for(int i=0 ; i<b ; i++){
for(int j=0 ; j<a ; j++){
if(B[i]==A[j]){
flag=1;}
}
if(flag==0){
cout<<"\n"<<B[i];
count++;
}
flag=0;
}
}
void both(){
int flag=0;
cout<<"\nStudents who play both games are : ";
for(int i=0 ; i<a ; i++){
for(int j=0 ; j<b ; j++){
if(A[i] == B[j])
flag=1;
}
if(flag==1){
cout<<"\n"<<A[i];
flag=0;
}
}
}
void ony_Cricket(){
int flag=0;
cout<<"\nStudents who play only Cricket are : ";
for(int i=0 ; i<a ; i++){
for(int j=0 ; j<b ; j++){
if(A[i] == B[j]){
flag=1;
}
}
if(flag==0){
cout<<"\n"<<A[i];
}
flag=0;
}
}
void ony_Badminton(){
int flag=0;
cout<<"\nStudents who play only Badminton are : ";
for(int i=0 ; i<b ; i++){
for(int j=0 ; j<a ; j++){
if(B[i] == A[j]){
flag=1;
}
}
if(flag==0){
cout<<"\n"<<B[i];
}
flag=0;
}
}
};
int main(){
Students s;
int ch;
do{
cout<<"\n1.Get Data \n2.Only Badminton \n3.Only Cricket \n4.Both \n5.Either \n6.Neither \n7.EXIT";
cin>>ch;
switch(ch){
case 1:s.getData();break;
case 2:s.ony_Badminton();break;
case 3:s.ony_Cricket();break;
case 4:s.both();break;
case 5:s.either();break;
case 6:s.neither();break;
}
}while(ch!=7);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------
**If some mistakes, plz let me know!-----Vivek S. Sharma
No comments:
Post a Comment