Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 55

Name : Kanchan Sagar Roll No.

: 20/ CS/21
Discrete Structure Practical File
Program – 1

//Program - 1 : Write a program to accept a set, find its cardinality and find its
power set.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

bool check_member(char s[],int c,char element);


void decimal_to_binary(int dec,int binary[]);
void power_set(char s[],int counter);

int main()
{
char Set[50],element;
int i,j,k,counter=0;

cout<<"\n\n Enter the elements for the set (press '*' to stop) :\n";

i=-1;
do
{
i++;
cout<<" ";
cin>>Set[i];
}while(Set[i]!='*' && (i<50));

counter = i ;
for(i=0;i<counter;i++)
{
for(j=i+1;j<counter;j++)
{
if(Set[i]==Set[j])
{
for(k=j;k<counter;k++)
{
Set[k]=Set[k+1];
}
counter--;
j--;
}
}
}
cout<<"\n The set is : { ";
for(i=0;i<counter;i++)
{
cout<<Set[i]<<" , ";
}
cout<<"\b\b}";
cout<<"\n\n The cardinality of set is : "<<counter;
cout<<"\n\n Enter an element : ";
cin>>element;
if(check_member(Set,counter,element)==true)
cout<<"\n "<<element<<" is present in the set.\n\n";
else
cout<<"\n "<<element<<" is not present in the set.\n\n";

cout<<"\n\n Power Set of entered set is :\n\n { ";


power_set(Set,counter);
getch();
return 0;
}

bool check_member(char s[],int c,char element)


{
for(int a=0;a<c;a++)
{
if(element==s[a])
return true;
}
return false;
}

void decimal_to_binary(int dec,int binary[],int counter)


{
counter--;
for(int i=counter;i>=0;i--)
{
binary[i]=dec%2;
dec/=2;
}
}

void power_set(char s[],int counter)


{
int binary[50];
double size=pow(2,counter); //no. of elements in power set.
cout<<" { ";
for(int i=0;i<size;i++)
{
cout<<" {";
decimal_to_binary(i,binary,counter);

for(int j=0;j<counter;j++)
{
if(binary[j]==1)
cout<<s[j]<<",";
}
cout<<"\b";
cout<<"} , ";
}
cout<<"\b\b }";
}
Output
Program – 2

/* Program 2 : Create a class SET and take 2 sets as inputs from user to perform
the following set operations :

Subset, union, intersection, set difference, symmetric difference, complement and


cartesian product. */

#include<iostream>
#include<conio.h>
using namespace std;

class SET
{
char ar[100];
int Size;

public:
void Accept();
void Display();
void Normalize();
void Union(SET s);
void Inter(SET s);
void Complement(SET U);
bool is_subset_of(SET s);
SET Set_diff(SET s);
void Cartesian(SET s);
};

void SET::Normalize()
{
int i,j,k;
for(i=0;i<Size;i++)
{
for(j=i+1;j<Size;j++)
{
if(ar[i]==ar[j])
{
for(k=j;k<Size;k++)
{
ar[k]=ar[k+1];
}
Size--;
j--;
}
}
}
}

void SET::Display()
{
cout<<"\n\n { ";
for(int i=0;i<Size;i++)
{
cout<<" "<<ar[i]<<" , ";
}
cout<<"\b\b } ";
}

void SET::Accept()
{
int i,j,k;
i=-1;
do
{
i++;
cout<<" ";
cin>>ar[i];
}while(ar[i]!='*' && (i<100));
Size = i ;
Normalize();
cout<<"\n\n Set entered is : ";
Display();
}

void SET::Union(SET s)
{
SET uni;
int i,j,k=0,l,f=1;
for(i=0;i<Size;i++)
{
uni.ar[k++]=ar[i];
}
for(j=0;j<s.Size;j++)
{
uni.ar[k++]=s.ar[j];
}
uni.Size=k;
uni.Normalize();
uni.Display();
}

void SET::Inter(SET s)
{
int i,j,k=0;
SET Inter;
for(i=0;i<Size;i++)
{
for(j=0;j<s.Size;j++)
{
if(ar[i]==s.ar[j])
Inter.ar[k++]=ar[i];
}
}
Inter.Size=k;
Inter.Normalize();
Inter.Display();
}

bool SET::is_subset_of(SET s)
{
int i,j;
bool f;
for(i=0;i<Size;i++)
{
f=false;
for(j=0;j<s.Size;j++)
{
if(ar[i]==s.ar[j])
{
f=true;
}
}
if(f==false)
break;
}
return f;
}

void SET::Complement(SET U)
{
SET comple;
int i,j,k=0;
bool f;
for(i=0;i<U.Size;i++)
{
f=true;
for(j=0;j<Size;j++)
{
if(U.ar[i]==ar[j])
f=false;
}
if(f==true)
comple.ar[k++]=U.ar[i];
}
comple.Size=k;
comple.Display();
}

SET SET::Set_diff(SET s)
{
SET diff;
int i,j,k=0;
bool f;
for(i=0;i<Size;i++)
{
f=true;
for(j=0;j<s.Size;j++)
{
if(ar[i]==s.ar[j])
f=false;
}
if(f==true)
diff.ar[k++]=ar[i];
}
diff.Size=k;
return diff;
}

void SET::Cartesian(SET s)
{
int i,j;
cout<<"\n\n { ";
for(i=0;i<Size;i++)
{
for(j=0;j<s.Size;j++)
{
cout<<" ("<<ar[i]<<" , "<<s.ar[j]<<") , ";
}
}
cout<<"\b\b } ";
}

int main()
{
SET s1 , s2 , U , diff1 , diff2;
int opt;
cout<<"\n\n For set 1 : \n";
s1.Accept();
cout<<"\n\n For set 2 : \n";
s2.Accept();
cout<<"\n\n Enter universal set : \n";
U.Accept();

for(int i=0;i<80;i++)cout<<"\n";
do
{
cout<<"\n\n\t\t\t\t\tMENU\n\n\n";
cout<<"\n\n 1. Enter array again.\n\n 2. Display both the arrays.\n\n 3. Check for subset.\n\
n 4. Find union.";
cout<<"\n\n 5. Find intersection.\n\n 6. Find Set difference.\n\n 7. Find Symmetric
difference.\n\n 8. Find Complement.";
cout<<"\n\n 9. Find cartesian product. \n\n 10. Exit.\n\n Enter your option : ";
cin>>opt;

if(opt<1||opt>10)
cout<<"\n\n Invalid option.\n\n";

switch(opt)
{
case 1 :
cout<<"\n\n For set 1 : \n";
s1.Accept();
cout<<"\n\n For set 2 : \n";
s2.Accept();
cout<<"\n\n Enter universal set : \n";
U.Accept();

break;
case 2 :
cout<<"\n\n Set 1 is : ";
s1.Display();
cout<<"\n\n Set 2 is : ";
s2.Display();
break;
case 3 :
if(s1.is_subset_of(s2))
cout<<"\n\n Set 1 is a subset of Set 2. ";
else
cout<<"\n\n Set 1 is not a subset of Set 2. ";
if(s2.is_subset_of(s1))
cout<<"\n\n Set 2 is a subset of Set 1. ";
else
cout<<"\n\n Set 2 is not a subset of Set 1. ";
break;
case 4 :
cout<<"\n\n Union of both the sets is : ";
s1.Union(s2);
break;
case 5 :
cout<<"\n\n Intersection of both the sets is : ";
s1.Inter(s2);
break;
case 6 :
cout<<"\n\n Set 1 - Set 2 is : ";
diff1=s1.Set_diff(s2);
diff1.Display();
cout<<"\n\n Set 2 - Set 1 is : ";
diff2=s2.Set_diff(s1);
diff2.Display();
break;
case 7 :
cout<<"\n\n Symmetric difference of both the sets is : ";
diff1.Union(diff2);
break;
case 8 :
cout<<"\n\n Complement of Set 1 is : ";
s1.Complement(U);
cout<<"\n\n Complement of Set 2 is : ";
s2.Complement(U);
break;
case 9 :
cout<<"\n\n Cartesian product (i.e. s1 x s2 )of both the sets is : ";
s1.Cartesian(s2);
}
}while(opt!=10);
return 0;
}
Output
Program – 3 & 4

/* Program - 3 : Create a class RELATION , use matrix notation to represent a


relation. Include functions to check if a relation

is reflexive, symmetric, anti-symmetric and transitive. Write a program to use this


class.

Program - 4 : Use the functions defined in Ques 3 to check whether the given
relation is :
a) Equivalent, or
b) Partial order relation, or
c) None. */

#include<iostream>
#include<conio.h>
using namespace std;

class RELATION
{
int R[100][100],n;
public:
void accept();
void display();
int check_reflexive();
int check_symmetric();
int check_anti_symmetric();
int check_transitive();
};

void RELATION::accept()
{
cout<<"\n Enter the number of elements in the set on which relation is defined : ";
cin>>n;
cout<<"\n\n Enter the relation in matrix notation (0/1) : ";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<"\n\n Enter element (0/1) : ";
cin>>R[i][j];

while(R[i][j]!=0 && R[i][j]!=1)


{
cout<<"\n\n Please enter either 0 or 1 only : ";
cin>>R[i][j];
}
}
}
display();
}

void RELATION::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<R[i][j];
}
}
int RELATION::check_reflexive()
{
int i,j,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i==j)&&(R[i][j]!=1))
{
f=0;
break;
}
}
}
return f;
}

int RELATION::check_symmetric()
{
int i,j,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((R[i][j]==1)&&R[j][i]!=1)
{
f=0;
break;
}
}
}
return f;
}

int RELATION::check_anti_symmetric()
{
int i,j,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i!=j)&&(R[i][j]==1)&&(R[j][i]==1))
{
f=0;
break;
}
}
}
return f;
}

int RELATION::check_transitive()
{
int i,j,k,f=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
if((R[i][j]==1)&&(R[j][k]==1)&&(R[i][k]!=1))
{
f=0;
break;
}
}
}
}
return f;
}

int main()
{
RELATION r;
r.accept();

if(r.check_reflexive()==1)
cout<<"\n\n Entered relation is reflexive !! " ;
else
cout<<"\n\n Entered relation is not reflexive !! " ;

if(r.check_symmetric()==1)
cout<<"\n\n Entered relation is symmetric !! " ;
else
cout<<"\n\n Entered relation is not symmetric !! " ;

if(r.check_anti_symmetric()==1)
cout<<"\n\n Entered relation is anti-symmetric !! " ;
else
cout<<"\n\n Entered relation is not anti-symmetric !! " ;
if(r.check_transitive()==1)
cout<<"\n\n Entered relation is transitive !! " ;
else
cout<<"\n\n Entered relation is not transitive !! " ;

if((r.check_reflexive()==1)&&(r.check_symmetric()==1)&&(r.check_transitive()==1))
cout<<"\n\n Entered relation is an equivalence relation !!";
else if((r.check_reflexive()==1)&&(r.check_anti_symmetric()==1)&&(r.check_transitive()==1))
cout<<"\n\n Entered relation is a partial ordering relation relation !!";
else
cout<<"\n\n Entered relation is neither equivalence nor partial ordering relation !!";
getch();
return 0;
}

Output
Program – 5
/* Program -5 : Write a program to implement bubble sort. Find the number of
comparisons during each pass and display the intermediate result. */

#include<iostream>
#include<conio.h>
using namespace std;

void bub_sort(int ar[],int size);

int main()
{
int ar[100],s;
cout<<"\n\n Enter the size of the array : ";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"\n\n Enter value : ";
cin>>ar[i];
}
bub_sort(ar,s);
getch();
return 0;
}

void bub_sort(int ar[],int size)


{
int temp,count;
for(int i=0;i<size;i++)
{
count=0;
for(int j=0 ; j<size-1-i ; j++)
{
if(count++,ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
cout<<"\n\n Number of comparisons in pass "<<i+1<<" is "<<count<<"\n\n";
for(int k=0;k<size;k++)
cout<<" "<<ar[k];
}

cout<<"\n\n List after sorting in ascending order is : ";


for(int i=0;i<size;i++)
cout<<" "<<ar[i];
}
Output
Program – 5
/* Program -5 : Write a program to implement bubble sort. Find the number of
comparisons during each pass and display the intermediate result. */

#include<iostream>
#include<conio.h>
using namespace std;

void bub_sort(int ar[],int size);

int main()
{
int ar[100],s;
cout<<"\n\n Enter the size of the array : ";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"\n\n Enter value : ";
cin>>ar[i];
}
bub_sort(ar,s);
getch();
return 0;
}

void bub_sort(int ar[],int size)


{
int temp,count;
for(int i=0;i<size;i++)
{
count=0;
for(int j=0 ; j<size-1-i ; j++)
{
if(count++,ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
cout<<"\n\n Number of comparisons in pass "<<i+1<<" is "<<count<<"\n\n";
for(int k=0;k<size;k++)
cout<<" "<<ar[k];
}

cout<<"\n\n List after sorting in ascending order is : ";


for(int i=0;i<size;i++)
cout<<" "<<ar[i];
}
Output
Program – 6
/* Program – 6 : Write a program to implement insertion sort. Find the number of
comparisons during each pass and display the intermediate
result. */

#include<iostream>
#include<conio.h>
using namespace std;

void insert_sort(int ar[],int s);

int main()
{
int ar[100],s;
cout<<"\n\n Enter the size of the array : ";
cin>>s;
for(int i=0;i<s;i++)
{
cout<<"\n\n Enter value : ";
cin>>ar[i];
}
insert_sort(ar,s);
getch();
return 0;
}

void insert_sort(int ar[],int s)


{
int temp,i,j,Count;
for(int i=0;i<s;i++)
{
Count=0;
temp=ar[i];
j=i-1;
while(Count++,j>=0 && ar[j]>temp)
{
ar[j+1]=ar[j];
j--;
}
ar[j+1]=temp;
cout<<"\n\n Number of comparisons in pass "<<i+1<<" is "<<Count<<"\n\n";
for(int k=0;k<s;k++)
cout<<" "<<ar[k];
}

cout<<"\n\n List after sorting in ascending order is : ";


for(int i=0;i<s;i++)
cout<<" "<<ar[i];
}

Output
Program – 7
/* Program - 7 : Write a program that generates all the permutations of a given set
of
digits, with or without repetition. */

#include<iostream>
#include<conio.h>
#include<ctype.h>
using namespace std;

void swap_nos(char &x,char &y);


void permutation(char nums[],int l,int r); //l for left and r for right.

int main()
{
char nums[100];
int i,n;
cout<<"\n\n Enter the number of digits whose permutations you want to find (less than 100) :
";
cin>>n;
while(n>100)
{
cout<<"\n\n Please enter a number less than 100 : ";
cin>>n;
}

for(i=0;i<n;i++)
{
cout<<"\n\n Enter digit "<<i+1<<" : ";
cin>>nums[i];
while(!isdigit(nums[i]))
{
cout<<"\n\n Please enter a digit : ";
cin>>nums[i];
}
}
nums[i]='\0';
cout<<"\n\n Entered set of digits is : { ";
for(i=0;i<n;i++)
cout<<nums[i]<<" , ";
cout<<"\b\b } ";
cout<<"\n\n All permutations of { ";
for(i=0;i<n;i++)
cout<<nums[i]<<" , ";
cout<<"\b\b } are :-\n\n";
permutation(nums,0,n-1);
getch();
return 0;
}

void permutation(char nums[],int l,int r) //l for left and r for right.
{
int i;
if(l==r)
cout<<nums<<"\n\n";

else
{
for(i=l ; i<=r ; i++)
{
swap_nos(nums[l],nums[i]);
permutation(nums,l+1,r);
swap_nos(nums[l],nums[i]);
}
}
}

void swap_nos(char &x,char &y)


{
char temp=x;
x=y;
y=temp;
}
Output

Program – 8
/* Program - 8 : Write a program to calculate permutation and combination for an
input value n and r using recursive formula of
P(n,r) and C(n,r). */

#include<iostream>
#include<conio.h>
using namespace std;
double factorial(double a);

int main()
{
double n,r,p,c;
cout<<"\n\n Enter the value for n : ";
cin>>n;
do
{
cout<<"\n\n Enter the value for r (less than or equal to n): ";
cin>>r;
}while(r>n);
cout<<"\n\n Formula for P(n,r) is n!\n\t\t\t------\n\t\t\t(n-r)!";
cout<<"\n\n Formula for C(n,r) is n!\n\t\t\t--------\n\t\t\tr!(n-r)!";
p=factorial(n)/factorial(n-r);
c=factorial(n)/(factorial(r)*factorial(n-r));
cout<<"\n\n Permutation for n and r , P("<<n<<","<<r<<") is "<<p;
cout<<"\n\n Combination for n and r , C("<<n<<","<<r<<") is "<<c<<"\n\n";
getch();
return 0;
}

double factorial(double a)
{
if(a==0||a==1)
return 1;
return a*factorial(a-1);
}
Output

Program 9
//Program – 9: For any number n write a program to list all the solutions of the
equaqtion x1+x2+x3+…+xn=C where C is a constant and x1 x2 x3 … x4 are non
negative integers using brute force integer.
#include<iostream> using namespace std;

int comb(int n ,int r) {

if(r==0 || r==n) return 1;

else

return ( comb(n-1,r-1) + comb(n-1,r)) ;

int main() {

int n ,r;

cout<<"\nx1+x2+x3+---+xn=c";

cout<<"\nENTER THE NO OF VARIABLES (n) : "; cin>>n;

cout<<"\nENTER THE VALUE OF TOTAL SUM (c<=10) : "; cin>>r;

cout<<"\nNUMBER OF POSSIBLE SOLUTIONS OF THE GIVEN EQUATION IS : ";

cout<<comb(n+r-1,r); }

Output
Program – 10
/* Program - 10 : WAP to print the truth table of following logical operations for
variables
x and y :
Conjunction , Disjunction , Exclusive OR , Conditional , Bi-conditional , Exclusive
NOR ,
Negation , NAND , NOR.
*/

#include<iostream>
#include<conio.h>
using namespace std;

void print_conjunction(char x[],char y[]);


void print_disjunction(char x[],char y[]);
void print_xor(char x[],char y[]);
void print_conditional(char x[],char y[]);
void print_biconditional(char x[],char y[]);
void print_xnor(char x[],char y[]);
void print_negation(char x[],char y[]);
void print_nand(char x[],char y[]);
void print_nor(char x[],char y[]);
int main()
{
char x[4],y[4];
int i,j,opt;
for(i=0;i<4;i++)
{
if(i<2)
x[i]='F';
else
x[i]='T';
}
for(j=0;j<4;j++)
{
if(j%2==0)
y[j]='F';
else
y[j]='T';
}
do
{
cout<<"\n\n\n\n\t\t\t Want to print truth table of ?";
cout<<"\n\n\t 1. Conjunction.\n\n\t 2. Disjunction.\n\n\t 3. Exclusive OR.\n\n\t 4.
Conditional.";
cout<<"\n\n\t 5. Bi-conditional.\n\n\t 6. Exclusive NOR.\n\n\t 7. Negation.\n\n\t 8.
NAND.";
cout<<"\n\n\t 9. NOR.\n\n\t 10. Exit.\n\n Enter your option : ";
cin>>opt;
if(opt<1||opt>10)
cout<<"\n\n\tInvalid option.";
switch(opt)
{
case 1 : print_conjunction(x,y);
break;
case 2 : print_disjunction(x,y);
break;
case 3 : print_xor(x,y);
break;
case 4 : print_conditional(x,y);
break;
case 5 : print_biconditional(x,y);
break;
case 6 : print_xnor(x,y);
break;
case 7 : print_negation(x,y);
break;
case 8 : print_nand(x,y);
break;
case 9 : print_nor(x,y);
break;
}
}while(opt!=10);
}

void print_conjunction(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of conjunction of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\tConjunction\n";
for(i=0;i<4;i++)
{
if(x[i]=='T'&&y[i]=='T')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'T';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'F';
}
}

void print_disjunction(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of disjunction of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\tDisjunction\n";
for(i=0;i<4;i++)
{
if(x[i]=='T'||y[i]=='T')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'T';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'F';
}
}

void print_xor(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of Exclusive OR of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\tExclusive OR\n";
for(i=0;i<4;i++)
{
if(x[i]!=y[i])
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'T';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'F';
}
}

void print_conditional(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of Conditional of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\t x->y\n";
for(i=0;i<4;i++)
{
if(x[i]=='T'&&y[i]=='F')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'F';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'T';
}
}

void print_biconditional(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of Bi-conditional of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\t x<->y\n";
for(i=0;i<4;i++)
{
if(x[i]!=y[i])
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'F';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t\t"<<'T';
}
}

void print_xnor(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of Exclusive NOR of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\tNOR x & y\n";
for(i=0;i<4;i++)
{
if(x[i]==y[i])
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'T';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'F';
}
}

void print_negation(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of Negation of x and Negation of y is :\n";
cout<<"\n\t\tx\t\ty\t\t-x\t\t-y\n";
for(i=0;i<4;i++)
{
if(x[i]=='F'&&y[i]=='F')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'T'<<"\t\t"<<'T';
else if(x[i]=='F'&&y[i]=='T')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'T'<<"\t\t"<<'F';
else if(x[i]=='T'&&y[i]=='F')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'F'<<"\t\t"<<'T';
else if(x[i]=='T'&&y[i]=='T')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'F'<<"\t\t"<<'F';
}
}

void print_nand(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of NAND of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\tNAND of x & y\n";
for(i=0;i<4;i++)
{
if(x[i]=='T'&&y[i]=='T')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'F';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'T';
}
}

void print_nor(char x[],char y[])


{
int i;
cout<<"\n\n Truth Table of NOR of x and y is :\n";
cout<<"\n\t\tx\t\ty\t\tNOR of x & y\n";
for(i=0;i<4;i++)
{
if(x[i]=='F'&&y[i]=='F')
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'T';
else
cout<<"\n\n\t\t"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<'F';
}
}
Output
Program – 11
/* Program - 11 : Write a program to store a polynomial function and then
evaluate the
polynomial. */

#include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;

int main()
{
int deg, coeff[100], i, n, ans=0 ;
do
{
cout<<"\n\n Enter the degree of polynomial you want to have (less than 100) : ";
cin>>deg;
}while(deg<0||deg>100);

for(i=deg;i>=0;i--)
{
cout<<"\n\n Enter the coefficient for degree "<<i<<" : ";
cin>>coeff[i];
}
cout<<"\n\n Polynomial entered is f(x) = ";
for(i=deg;i>=0;i--)
{
if(i==0)
cout<<coeff[i];
else if(i==1)
cout<<coeff[i]<<".x";
else
cout<<coeff[i]<<".x^"<<i;

if(i!=0)
cout<<" + ";
}
cout<<"\n\n Enter the value for n for which you want to find f(n) : ";
cin>>n;
for(i=deg ; i>=0 ; i--)
{
ans += pow(n,i) * coeff[i] ;
}
cout<<"\n\n f("<<n<<") = "<<ans<<"\n\n";
return 0;
}
Output
Program –
12
/* Program - 12 : Write a program to represent graphs using the adjacency
matrices
and check if it is a complete graph. */

#include<iostream>
#include<conio.h>
using namespace std;

int check_complete(int g[10][10],int n);

int main()
{
int g[10][10],n,i,j;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter either 0 or 1 : ";
cin>>g[i][j];
while(g[i][j]!=0 && g[i][j]!=1)
{
cout<<"\n\n Please enter either 0 or 1 : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
int ans = check_complete(g,n);

if(ans==1)
cout<<"\n\n Entered graph is a complete graph.";
else
cout<<"\n\n Entered graph is not a complete graph.";
getch();
return 0;
}

int check_complete(int g[10][10],int n)


{
int i,j,result=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j && g[i][j]==1) //self-loop.
result=0;

if(i!=j && g[i][j]==0)


result=0;
}
}
return result;}
Output
Program – 13
/* Program - 13 : Write a program to accept a directed graph G and compute the
in-degree and out-degree of each vertex. */

#include<iostream>
#include<conio.h>
using namespace std;

void calc_degree(int g[10][10],int n);

int main()
{
int g[10][10],n,i,j;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter either 0 or 1 : ";
cin>>g[i][j];
while(g[i][j]!=0 && g[i][j]!=1)
{
cout<<"\n\n Please enter either 0 or 1 : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
calc_degree(g,n);
getch();
return 0;
}

void calc_degree(int g[10][10],int n)


{
int i,j,in_deg,out_deg;
cout<<"\n\n";
for(i=0;i<n;i++)
{
out_deg=0;
in_deg=0;
for(j=0;j<n;j++)
{
out_deg+=g[i][j];
in_deg+=g[j][i];
}
cout<<"\n\n Out_degree of vertex "<<i+1<<" is "<<out_deg;
cout<<"\n\n In_degree of vertex "<<i+1<<" is "<<in_deg;
}
}
Output
Program – 14
/* Program - 14 : Given a graph G, write a program to find the number of paths of
length n
between the source and destination entered by the user. */

#include<iostream>
#include<conio.h>
using namespace std;

int calc_path(int g[10][10],int n,int len,int src,int des);


void multiply(int g[10][10],int p[10][10],int n);
int main()
{
int g[10][10],n,i,j,source,destination,length ;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter either 0 or 1 : ";
cin>>g[i][j];
while(g[i][j]!=0 && g[i][j]!=1)
{
cout<<"\n\n Please enter either 0 or 1 : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
do
{
cout<<"\n\n Enter the length of path (greater than zero) : ";
cin>>length;
}while(length<=0);
do
{
cout<<"\n\n Enter the source vertex (less than or equal to total no. of vertices) : ";
cin>>source;
}while(source>n);
do
{
cout<<"\n\n Enter the destination vertex (less than or equal to total no. of vertices) : ";
cin>>destination;
}while(destination>n);
int num = calc_path(g,n,length,source,destination);
cout<<"\n\n Number of paths of length "<<length<<" from vertex "<<source<<" to vertex
"<<destination<<" is "<<num;
getch();
return 0;
}

int calc_path(int g[10][10],int n,int len,int src,int des)


{
int i,j;
src--;
des--;
int p[10][10];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
p[i][j]=g[i][j];
}
for(i=0;i<len-1;i++)
{
multiply(g,p,n);
}
return p[src][des];
}

void multiply(int g[10][10],int p[10][10],int n)


{
int b[10][10],i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=0;
for(k=0;k<n;k++)
b[i][j]+=g[i][k]*p[k][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
p[i][j]=b[i][j];
}}
Output

Program – 15
/* Program - 15 : Given an adjacency matrix of a graph, write a program to check
whether a
given set of vertices {v1,v2,v3,...,vk} forms an Euler path/Euler circuit. */

#include<iostream>
#include<conio.h>
using namespace std;

int check_euler_circuit_directed(int g[10][10],int n);


int check_euler_path_directed(int g[10][10],int n);
int check_euler_circuit_undirected(int g[10][10],int n);
int check_euler_path_undirected(int g[10][10],int n);

int main()
{
int g[10][10],n,i,j,flag,ans;
cout<<"\n\n Enter the number of vertices you want in the graph (less than 10) : ";
cin>>n;
while(n>10)
{
cout<<"\n\n Please enter a value less than 10 : ";
cin>>n;
}
cout<<"\n\n Enter adjacency matrix :-\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\n\n Enter the number of edges directed from vertex "<<i+1<<" to vertex
"<<j+1<<" : ";
cin>>g[i][j];
while(i==j && g[i][j]>1)
{
cout<<"\n\n Sorry!! Only one self-loop allowed.";
cout<<"\n\n Enter again : ";
cin>>g[i][j];
}
while(g[i][j]<0)
{
cout<<"\n\n Please enter a positive integer : ";
cin>>g[i][j];
}
}
}
cout<<"\n\n Entered adjacency matrix for the graph is :-\n\n";
for(i=0;i<n;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<" "<<g[i][j];
}
flag=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(g[i][j]!=g[j][i])
flag=0;
}
}
if(flag==0) //graph is directed.
{
ans = check_euler_circuit_directed(g,n);

if(ans==1)
cout<<"\n\n Entered directed graph has an Euler circuit.";
else
cout<<"\n\n Entered directed graph does not have an Euler circuit.";
ans = check_euler_path_directed(g,n);
if(ans==1)
cout<<"\n\n Entered directed graph has an Euler path.";
else
cout<<"\n\n Entered directed graph does not have an Euler path.\n\n";
}
if(flag==1) //graph is undirected.
{
ans = check_euler_circuit_undirected(g,n);

if(ans==1)
cout<<"\n\n Entered undirected graph has an Euler circuit.";
else
cout<<"\n\n Entered undirected graph does not have an Euler circuit.";

ans = check_euler_path_undirected(g,n);

if(ans==1)
cout<<"\n\n Entered undirected graph has an Euler path.";
else
cout<<"\n\n Entered undirected graph does not have an Euler path.\n\n";
}
getch();
return 0;
}

int check_euler_circuit_directed(int g[10][10],int n)


{
int i,j,degree,flag=1;
for(i=0;i<n;i++)
{
degree=0;
for(j=0;j<n;j++)
{
degree+=g[i][j]+g[j][i];
}
if(degree%2!=0)
flag=0;
}
return flag;
}

int check_euler_path_directed(int g[10][10],int n)


{
int i,j,degree,flag,counter=0;
for(i=0;i<n;i++)
{
degree=0;
for(j=0;j<n;j++)
{
degree+=g[i][j]+g[j][i];
}
if(degree%2!=0)
counter++;
}
if(counter==2)
flag=1;
else
flag=0;

return flag;
}

int check_euler_circuit_undirected(int g[10][10],int n)


{
int i,j,degree,flag=1;
for(i=0;i<n;i++)
{
degree=0;
for(j=0;j<n;j++)
{
degree+=g[i][j];
}
if(degree%2!=0)
flag=0;
}
return flag;
}

int check_euler_path_undirected(int g[10][10],int n)


{
int i,j,degree,flag,counter=0;
for(i=0;i<n;i++)
{
degree=0;
for(j=0;j<n;j++)
{
degree+=g[i][j];
}
if(degree%2!=0)
counter++;
}
if(counter==2)
flag=1;
else
flag=0;
return flag;
}
Output
Program – 16
//Program - 16 : Given a full m-ary tree with i internal vertices, write a program to
find the no. of leaf nodes.
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int i,m,l;
cout<<"\n\n Enter the value of m : ";
cin>>m;
cout<<"\n\n Enter the value of i : ";
cin>>i;
l=(m-1)*i+1;
cout<<"\n\n The number of leaf nodes is "<<l;
getch();
return 0;
}
Output

You might also like