#include<iostream>
using namespace std;
class Sports
{
    public:
        int tennis;
        int badminton;
        int cricket;
        int setA[20],setB[20],setC[20];
        int ab[20],bc[20],ca[20],abc[20];
        int n1,n2,n3,n4=0,total;
        void accept();  // method for accept the input
        void intersection();    //method for calculate intersection
        void display(); //method for display intersections
};
void Sports :: accept()
{
    cout<<"Enter the total number of players who play Tennis: "<<endl;
    cin>>tennis;
    cout<<"Enter the student roll no who play Tennis "<<endl;
    for(int i=0; i<tennis; i++)
        cin>>setA[i];

    cout<<"Enter the total number of players who play Badminton: "<<endl;
    cin>>badminton;
    cout<<"Enter the student roll no who play Badminton "<<endl;
    for(int i=0; i<badminton; i++)
        cin>>setB[i];

    cout<<"Enter the total number of players who play cricket: "<<endl;
    cin>>cricket;
        cout<<"Enter the student roll no who play cricket "<<endl;
    for(int i=0; i<cricket; i++)
        cin>>setC[i];

}
void Sports :: intersection()
{
    // Logic for intersection SET-A and SET-B

    for(int i=0;i<cricket;i++)
    {
         for(int j=0;j<badminton;j++)
         {
            if(setA[i]==setB[j])
            {
                ab[n1]=setA[i];
                n1++;
            }
         }  
    }

    // Logic for intersection SET-B and SET-C
    for(int i=0;i<badminton;i++)
    {
         for(int j=0;j<cricket;j++)
         {
            if(setB[i]==setC[j])
            {
                bc[n2]=setB[i];
                n2++;
            }
         }  
    }
    // Logic for intersection SET-C and SET-A
    for(int i=0;i<cricket;i++)
    {
         for(int j=0;j<tennis;j++)
         {
            if(setC[i]==setA[j])
            {
                ca[n3]=setC[i];
                n3++;
            }
         }  
    }
    //logic for A union B union C
    for(int i=0;i<tennis;i++)
    {
         for(int j=0;j<badminton;j++)
         {
            for(int k=0;k<cricket; k++)
            {
                if(setA[i]==setB[j] && setA[i]==setC[k])
                {
                    abc[n4]=setA[i];
                    n4++;
                }
            }
         }  
    }
   total=tennis+badminton+cricket-n1-n2-n3+n4;  
}
void Sports :: display()
{
    //logic for print intersection of tennis and Badminton
    cout<<"\nIntersection of SET-A and SET-B: ";
    for(int i=0; i<n1; i++)
        cout<<ab[i]<<" ";

    //logic for print intersection of Badminton and Cricket  
    cout<<"\nIntersection of SET-B and SET-C: ";
    for(int i=0; i<n2; i++)
        cout<<bc[i]<<" ";

    //logic for print intersection of Cricket and Tennis
    cout<<"\nIntersection of SET-C and SET-A: ";
    for(int i=0; i<n3; i++)
        cout<<ca[i]<<" ";

    // logic for print intersection of Tennis and Badminton and Cricket
    cout<<"\nIntersection of SET-A and SET-B and SET-C: ";
    for(int i=0; i<n4; i++)
        cout<<abc[i]<<" ";

    // logic for print total number of students in a class
    cout<<"\nTotal number of students in class are : "<<total;
}
int main()
{
    Sports s;
    s.accept();
    s.intersection();
    s.display();
    return 0;
}

pll.png

Recommended Answers

All 4 Replies

Did you have a question, or need assistance in some way?

commented: i need to show the intersection of set a and b & set b and c & set c and set a and union pf set a,b and c in output but it is not displaying. +0

Line# 41 should be tennis, isn' it? Also, n1, n2, n3 and total aren't intialized to zero.

commented: i initialized the ni,n2,n3 and total but intersection of set c and set a and union of set a,b,c result is not being displayed +0

Traditionally, sets are represented as bitfields, which could then be manipulated with the bitwise operators - & for union, | for intersection. Since you know the whole set has at most 20 elements, the representation can fit into a single 32-bit unsigned integer.

Also, would it make more sense to make a class Set to factor all of the intersection and union logic out into one place?

By combining these approaches you'd be saving yourself a great deal of effort, I think.

commented: Right, only note '&' is for intersection and '|' for union. +8

Did you replace criket by tennis in line number 41? Do you receive any error message?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.