The second year of the Computer Technology class of M students, group A of students plays cricket and group B of students plays badminton. Write a C/C++ program to search and display. A set of students who play either cricket or badminton or both ii. A set of students who play both cricket and badminton iii. A set of students who only play cricket iv. Set of students who play only badminton v. Number of students who play neither cricket nor badminton.

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 displayi. 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;

//---------------------------------------------------------------Global variables

int M[10],A[5],B[5],AUB[10];
int msize = 10;
int absize = 5;
int AUBsize = 0;

//---------------------------------------------------------------Input Function

void input() //To input into Arrays
OUTPUT FOR THE PROGRAM
{
   int i;

   cout<<"\n\n Enter 10 values in Set M: ";
   for(i=0; i<msize; i++) //To fill M[]
      cin>>M[i];

   cout<<"\n\n Enter 5 values in Set A: ";
   for(i=0; i<absize; i++) //To fill A[]
      cin>>A[i];

   cout<<"\n\n Enter 5 values in Set B: ";
   for(i=0; i<absize; i++) //To fill B[]
      cin>>B[i];
}

//---------------------------------------------------------------Display Function

void display() //To input into Arrays
{
   int i;

   cout<<"\n\t Set M : ";
   for(i=0; i<msize; i++) //To display M[]
      cout<<M[i]<<", ";

   cout<<"\n\t Set A : ";
   for(i=0; i<absize; i++) //To display A[]
      cout<<A[i]<<", ";

   cout<<"\n\t Set B : ";
   for(i=0; i<absize; i++) //To display B[]
      cout<<B[i]<<", ";
}

//---------------------------------------------------------------AUB Function
void AuniB()
{
   int i,j,k,flag;
   AUBsize = 0;

   for(i=0; i<absize; i++) //To fill A[] into AUB[]
   {
      AUB[AUBsize] = A[i];
      AUBsize++;
   }

   for(i=0; i<absize; i++) //To copy B[] into AUB[]
   {
       flag = 0;
       for(j=0; j<absize; j++) //To compare B[] with A[]
       {
           if(B[i] == A[j])
           {
               flag = 1;
               break;
           }
       }
       if(flag == 0)
       {
          AUB[AUBsize] = B[i];
          AUBsize++;
       }
   }

   cout<<"\n\t Set AUB : ";
   for(i=0; i<AUBsize; i++) //To display AUB[]
      cout<<AUB[i]<<", ";
}

//---------------------------------------------------------------AintB Function
void AintB()
{
   int i,j,k,flag,AintB[10];
   k = 0;

   for(i=0; i<absize; i++) //To copy B[] into AUB[]
   {
       flag = 0;
       for(j=0; j<absize; j++) //To compare B[] with A[]
       {
           if(B[i] == A[j])
           {
               flag = 1;
               break;
           }
       }
       if(flag == 1)
       {
          AintB[k] = B[i];
          k++;
       }
   }

   cout<<"\n\t Set AintB : ";
   for(i=0; i<k; i++) //To display AintB[]
      cout<<AintB[i]<<", ";
}

//...................................... AminB
void AminB()
{
   int i,j,k,flag,AminB[10];
   k = 0;

   for(i=0; i<absize; i++) //For A[]
   {
       flag = 0;
       for(j=0; j<absize; j++) //for B[]
       {
           if(A[i] == B[j])
           {
               flag = 1;
               break;
           }
       }
       if(flag == 0)
       {
          AminB[k] = A[i];
          k++;
       }
   }

   cout<<"\n\t Set AminB : ";
   for(i=0; i<k; i++) //To display AminB[]
      cout<<AminB[i]<<", ";

}

void BminA()
{
   int i,j,k,flag,BminA[10];
   k = 0;

   for(i=0; i<absize; i++) //For B[]
   {
       flag = 0;
       for(j=0; j<absize; j++) //for A[]
       {
           if(B[i] == A[j])
           {
               flag = 1;
               break;
           }
       }
       if(flag == 0)
       {
          BminA[k] = B[i];
          k++;
       }
   }

   cout<<"\n\t Set BminA : ";
   for(i=0; i<k; i++) //To display BminA[]
      cout<<BminA[i]<<", ";

}

void MminAUB()
{
   int i,j,k,flag,MminAUB[10];
   k = 0;

   for(i=0; i<msize; i++) //For M[]
   {
       flag = 0;
       for(j=0; j<AUBsize; j++) //for AUB[]
       {
           if(M[i] == AUB[j])
           {
               flag = 1;
               break;
           }
       }
       if(flag == 0) //Not matching then copy...
       {
          MminAUB[k] = M[i];
          k++;
       }
   }

   cout<<"\n\t Set M-AUB : ";
   for(i=0; i<k; i++) //To display M-AUB[]
      cout<<MminAUB[i]<<", ";

}


int main()
{
   cout<<"\n\n ***-------Set Operations-------***";
   input();
   cout<<"\n\n ***Inputs:";
   display();

   cout<<"\n\n ***Outputs:";
   AuniB();
   AintB();
   AminB();
   BminA();
   MminAUB();

   cout<<"\n\n";
   return 0;
}



Post a Comment

Previous Post Next Post