Sunday, April 6 2025

Header Ads

Set Operations in C++

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
(Note- While realizing the set duplicate entries are to avoided)


Code:-


#include<iostream>
using namespace std;
class Student
{
int sid[50];
int total;
public:
Student()
{
total=0;
}
void accept();
void display();
void intersection(Student,Student);
int unionS(Student,Student);
void diff(Student,Student);
};
void Student::accept()
{
cout<<"\nENter Number of Students: ";
cin>>total;
for(int i=0;i<total;i++)
{
cout<<"Enter Student Id: ";
cin>>sid[i];
for(int j=0;j<i;j++)
{
if(sid[j]==sid[i])
{
cout<<"\nRepeated Record.Please Enter Again!!";
i--;
break;
}
}
}
}
void Student::display()
{
for(int i=0;i<total;i++)
{
cout.width(5);
cout<<sid[i];
}
cout<<endl;
}
void Student::intersection(Student s1,Student s2)
{
for(int i=0;i<s1.total;i++)
{
for(int j=0;j<s2.total;j++)
{
if(s1.sid[i]==s2.sid[j])
{
sid[total]=s1.sid[i];
total++;
break;
}
}
}
}
void Student::diff(Student s1,Student s2)
{
int flag=1;
for(int i=0;i<s1.total;i++)
{
for(int j=0;j<s2.total;j++)
{
if(s1.sid[i]==s2.sid[j])
{
flag=0;
break;
}
else flag=1;
}
if(flag==1)
{
sid[total]=s1.sid[i];
total++;
}
}
}
int Student::unionS(Student s1,Student s2)
{
int flag=0;
int i=0;
for(i=0;i<s1.total;i++)
{
sid[total]=s1.sid[i];
total++;
}
for(int j=0;j<s2.total;j++)
{
flag=0;
for(i=0;i<s1.total;i++)
{
if(s1.sid[i]==s2.sid[j])
{
flag=1;
break;
}
}
if(flag==0)
{
sid[total]=s2.sid[j];
total++;
}
}
return total;
}
int main()
{
Student cric,bad,sunion,onlycric,onlybad,both;
int class_total,union_total;
cout<<"\nENter Number of Students in a class : ";
cin>>class_total;
cout<<"\nEnter INFO FOR Students who play cricket: \n";
cric.accept();
cout<<"\nEnter INFO FOR Students who play Badminton: \n";
bad.accept();
cout<<"\n---------------------------------------------------\n";
cout<<"| Students' Information |";
cout<<"\n---------------------------------------------------\n";
cout<<"Cricket Set :";
cric.display();
cout<<"\nBadminton Students Set: ";
bad.display();
cout<<"\nCricket or badminton or both Students set: ";
union_total= sunion.unionS(cric,bad);
sunion.display();
cout<<"\nStudents play Both Cricket and Badminton Set: ";
both.intersection(cric,bad);
both.display();
cout<<"\nStudents play only cricket set: ";
onlycric.diff(cric,bad);
onlycric.display();
cout<<"\nStudents play only badminton set: ";
onlybad.diff(bad,cric);
onlybad.display();
cout<<"\nTotal Number of students who play neither cricket nor badminton: "<<class_total-union_total;
return 0;
}
view raw SoprtSet.CPP hosted with ❤ by GitHub



Output:-


No comments:

Powered by Blogger.