Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_operations_arrays.cpp #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions operations on sets
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <iostream>
using namespace std;
class set
{
int set_array[10];
int size;
int i;
int j=0;
int k=0;
int m=0;
int flag=0;
public:
void accept();
void display();
void intersect(set B);
void uniset(set B);
void subtract(set B);
};
void set::accept()
{
cout<<endl<<"enter no of elements "<<endl;
cin>>size;
cout<<"enter elements "<<endl;
for(i=0;i<size;i++)
{
cin>>set_array[i];
}
}
void set::display()
{
cout<<"elements are"<<endl;
for(i=0;i<size;i++){
cout<<set_array[i]<<",";
}
cout<<endl;
}
void set::intersect(set B)
{
set C;
cout<<"Intersection is "<<endl;
for(i=0;i<size;i++)
{
for(j=0;j<B.size;j++)
{
if(set_array[i]==B.set_array[j])
{
C.set_array[k]=B.set_array[j];
cout<<C.set_array[k]<<",";
k++;
}
}
}

}


void set::uniset(set B){
set D;
int k=0;
int count=0;
cout<<"union is "<<endl;
for(j=0,k=0;j<B.size;j++,k++)
{
D.set_array[k]=B.set_array[j];
}
for(i=0;i<size;i++)
{
count=0;
for(j=0;j<B.size;j++)
{
if(set_array[i]!=B.set_array[j])
{
count++;
}
if(count==B.size)
{ D.set_array[k]=set_array[i];
k++;
}
}
}
D.size=k;
D.display();
}


void set::subtract(set B)
{
set E;
int k=0;
int count=0;
for(i=0;i<size;i++)
{
count=0;
for(j=0;j<B.size;j++)
{
if(set_array[i]!=B.set_array[j])
{
count++;
}
if(count==B.size)
{ E.set_array[k]=set_array[i];
k++;
}
}
}
E.size=k;
cout<<"a-b is ";
E.display();
}
int main() {
set A;
set B;
A.accept();
A.display();
B.accept();
B.display();
int choice;

do{
cout<<endl;
cout<<"Enter choice"<<endl;
cout<<"enter one for intersection"<<endl;
cout<<"enter two for union"<<endl;
cout<<"enter three for subtract"<<endl;
cin>>choice;
switch(choice)
{
case 1:
A.intersect(B);
break;
case 2:
A.uniset(B);
break;
case 3:
A.subtract(B);
break;
}
}
while(choice!=0);
return 0;
}