This Post Contains A Mini Project :~ C Program For Various Operations On Set [Such As : Scanning, Union, Intersection, Difference, Symmetric Difference] With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Do-While Loop', 'Switch Cases', 'Function Call' & 'Menu Driven Format' From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
Operations covered :
Mini Project :~ C Program For Various Operations On Set.
/* Program To Carry Out Different Operations On Sets By Using 'Menu Driven' Concept */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define Max 100
void Scaning();
void Union();
void Intersection();
void Difference(int *,int *,int ,int);
void SymetricDifference();
void Display();
void Display1();
void Display2(char ,char ,int );
void Display3();
int m,n,i,j,k,p,q,r,s,a[Max],b[Max],c[Max];
int flag=1;
char char1,char2,char3;
void main()
{
int Num=0;
clrscr();
printf("\n\t\t::Welcome To The 'Set Operation' Program::\n");
printf("\nPlease Select One Of The Following Operations To Proceed Further :: ");
do
{
printf("\n\t1. Union Of Sets");
printf("\n\t2. Intersection Of Sets");
printf("\n\t3. Difference Of Sets");
printf("\n\t4. Symmetric Difference Of Sets");
printf("\n\t5. Exit From The Program\n");
printf("\nPlease Select The Operation No:: ");
scanf("%2d",&Num);
switch(Num)
{
case 1:Scaning();
Union();
Display(); // Function For Displaying Resultant Union Of 2 Sets.
break;
case 2:Scaning();
Intersection();
Display1(); // Function For Displaying Resultant Intersection Of 2 Sets.
break;
case 3:Scaning();
Difference(a,b,m,n); // Function For Displaying Resultant Difference Of 2 Sets. i.e A-B.
Display2('A','B',m);
Difference(b,a,n,m);
Display2('B','A',n); // Function For Displaying Resultant Difference Of 2 Sets. i.e B-A.
break;
case 4:Scaning();
SymetricDifference();
Display3(); // Function For Displaying Resultant Symmetric Difference Of 2 Sets.
break;
default:abort();
}
}
while(Num<=4);
getch();
}
void Scaning()
{
printf("\nPlease Enter The No Of Elements In A :\t");
scanf("%d",&m);
printf("\nPlease Enter The Elements For Set A :\n");
printf(" { ");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("}");
printf("\nPlease Enter The No Of Elements In B:\t");
scanf("%d",&n);
printf("\nPlease Enter The Elements For Set B :\n");
printf(" { ");
for(j=0;j<n;j++)
{
scanf("%d",&b[j]);
}
printf("}");
}
void Union()
{
k=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
p=m;
for(j=0;j<n;j++)
{
flag=1;
for(k=0;k<m;k++)
{
if(c[k]==b[j])
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=b[j];
p++;
}
}
}
void Display()
{
printf("\nThe Union Of Two Sets Is : { ");
for(k=0;k<p;k++)
{
printf("%2d",c[k]);
}
printf(" }");
}
void Intersection()
{
q=0;
p=0;
for(j=0;j<n;j++)
{
flag=1;
for(k=0;k<m;k++)
{
if(a[k]==b[j])
{
flag=1;
q++;
break;
}
else
{
flag=0;
}
}
if(flag==1)
{
c[p]=b[j];
p++;
}
}
}
void Display1()
{
printf("\nThe Intersection Of Two Sets Is : { ");
for(p=0;p<q;p++)
{
printf("%2d",c[p]);
}
printf(" }");
}
void Difference(int *a1,int *b1,int m1,int n1)
{
q=0;
p=0;
i=0;
for(k=0;k<m1;k++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(b1[j]==a1[k])
{
flag=1;
q++;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=a1[k];
p++;
}
}
}
void Display2(char ac,char bc,int m1)
{
printf("\nThe Difference Of Two Sets i.e '%c - %c' Is : { ",ac,bc);
r = m1 - q;
for(p=0;p<r;p++)
{
printf("%2d",c[p]);
}
printf(" }");
}
void SymetricDifference()
{
q=0;
p=0;
i=0;
for(k=0;k<m;k++)
{
flag=1;
for(j=0;j<n;j++)
{
if(b[j]==a[k])
{
flag=1;
q++;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=a[k];
p++;
}
}
for(j=0;j<n;j++)
{
flag=1;
for(k=0;k<m;k++)
{
if(a[k]==b[j])
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=b[j];
p++;
}
}
}
void Display3()
{
printf("\nThe Symetric Difference Of Two Sets Is : { ");
for(k=0;k<p;k++)
{
printf("%2d",c[k]);
}
printf(" }");
}
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
Operations covered :
- Scanning() : For creating a new set with initial members of the set.
- Union() : Finds union of two sets, set1[] and set2[] and stores the result in set3[]
- Intersection() : Finds intersection of two sets, set1[] and set2[] and stores the result in set3[]
- Difference() : Finds difference of two sets, set1[] and set2[] and stores the result in set3[]
- Symmetric Difference() : Finds Symmetric difference of two sets
Mini Project :~ C Program For Various Operations On Set.
/* Program To Carry Out Different Operations On Sets By Using 'Menu Driven' Concept */
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define Max 100
void Scaning();
void Union();
void Intersection();
void Difference(int *,int *,int ,int);
void SymetricDifference();
void Display();
void Display1();
void Display2(char ,char ,int );
void Display3();
int m,n,i,j,k,p,q,r,s,a[Max],b[Max],c[Max];
int flag=1;
char char1,char2,char3;
void main()
{
int Num=0;
clrscr();
printf("\n\t\t::Welcome To The 'Set Operation' Program::\n");
printf("\nPlease Select One Of The Following Operations To Proceed Further :: ");
do
{
printf("\n\t1. Union Of Sets");
printf("\n\t2. Intersection Of Sets");
printf("\n\t3. Difference Of Sets");
printf("\n\t4. Symmetric Difference Of Sets");
printf("\n\t5. Exit From The Program\n");
printf("\nPlease Select The Operation No:: ");
scanf("%2d",&Num);
switch(Num)
{
case 1:Scaning();
Union();
Display(); // Function For Displaying Resultant Union Of 2 Sets.
break;
case 2:Scaning();
Intersection();
Display1(); // Function For Displaying Resultant Intersection Of 2 Sets.
break;
case 3:Scaning();
Difference(a,b,m,n); // Function For Displaying Resultant Difference Of 2 Sets. i.e A-B.
Display2('A','B',m);
Difference(b,a,n,m);
Display2('B','A',n); // Function For Displaying Resultant Difference Of 2 Sets. i.e B-A.
break;
case 4:Scaning();
SymetricDifference();
Display3(); // Function For Displaying Resultant Symmetric Difference Of 2 Sets.
break;
default:abort();
}
}
while(Num<=4);
getch();
}
void Scaning()
{
printf("\nPlease Enter The No Of Elements In A :\t");
scanf("%d",&m);
printf("\nPlease Enter The Elements For Set A :\n");
printf(" { ");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("}");
printf("\nPlease Enter The No Of Elements In B:\t");
scanf("%d",&n);
printf("\nPlease Enter The Elements For Set B :\n");
printf(" { ");
for(j=0;j<n;j++)
{
scanf("%d",&b[j]);
}
printf("}");
}
void Union()
{
k=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
p=m;
for(j=0;j<n;j++)
{
flag=1;
for(k=0;k<m;k++)
{
if(c[k]==b[j])
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=b[j];
p++;
}
}
}
void Display()
{
printf("\nThe Union Of Two Sets Is : { ");
for(k=0;k<p;k++)
{
printf("%2d",c[k]);
}
printf(" }");
}
void Intersection()
{
q=0;
p=0;
for(j=0;j<n;j++)
{
flag=1;
for(k=0;k<m;k++)
{
if(a[k]==b[j])
{
flag=1;
q++;
break;
}
else
{
flag=0;
}
}
if(flag==1)
{
c[p]=b[j];
p++;
}
}
}
void Display1()
{
printf("\nThe Intersection Of Two Sets Is : { ");
for(p=0;p<q;p++)
{
printf("%2d",c[p]);
}
printf(" }");
}
void Difference(int *a1,int *b1,int m1,int n1)
{
q=0;
p=0;
i=0;
for(k=0;k<m1;k++)
{
flag=1;
for(j=0;j<n1;j++)
{
if(b1[j]==a1[k])
{
flag=1;
q++;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=a1[k];
p++;
}
}
}
void Display2(char ac,char bc,int m1)
{
printf("\nThe Difference Of Two Sets i.e '%c - %c' Is : { ",ac,bc);
r = m1 - q;
for(p=0;p<r;p++)
{
printf("%2d",c[p]);
}
printf(" }");
}
void SymetricDifference()
{
q=0;
p=0;
i=0;
for(k=0;k<m;k++)
{
flag=1;
for(j=0;j<n;j++)
{
if(b[j]==a[k])
{
flag=1;
q++;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=a[k];
p++;
}
}
for(j=0;j<n;j++)
{
flag=1;
for(k=0;k<m;k++)
{
if(a[k]==b[j])
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==0)
{
c[p]=b[j];
p++;
}
}
}
void Display3()
{
printf("\nThe Symetric Difference Of Two Sets Is : { ");
for(k=0;k<p;k++)
{
printf("%2d",c[k]);
}
printf(" }");
}
No comments:
Post a Comment