50% found this document useful (2 votes)
4K views

Q.1 Design A Program For Creating A Machine That Accept Three Consecutive Ones. Program

The program implements a conversion of a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). It defines data structures to represent the states of the DFA and the transitions between states. These include a Dstate structure to represent DFA states with name, state string, transitions and whether it is a final state. A tran structure represents transitions with symbol and destination states. A state structure represents NFA states with number and list of transitions. The program uses stacks to perform the subset construction algorithm to determine the states and transitions of the equivalent DFA.

Uploaded by

Aashutosh Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
4K views

Q.1 Design A Program For Creating A Machine That Accept Three Consecutive Ones. Program

The program implements a conversion of a non-deterministic finite automaton (NFA) to a deterministic finite automaton (DFA). It defines data structures to represent the states of the DFA and the transitions between states. These include a Dstate structure to represent DFA states with name, state string, transitions and whether it is a final state. A tran structure represents transitions with symbol and destination states. A state structure represents NFA states with number and list of transitions. The program uses stacks to perform the subset construction algorithm to determine the states and transitions of the equivalent DFA.

Uploaded by

Aashutosh Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Q.

1 Design a program for creating a machine that accept three consecutive


ones.

Program:

#include<bits/stdc++.h>

using namespace std;

vector<char> v;

void q(string s, long curr_lenght,long l, long r)

if(!s[curr_lenght] )

if(curr_lenght>=l &&curr_lenght<=r){cout<<"Input string is valid";}

else{cout<<"Input string is invalid";}

else if(find(v.begin(),v.end(),s[curr_lenght])==v.end())

cout<<"Input string is invalid";

else

q(s,curr_lenght+1,l,r);

}
int main()

string s;

cout<<"Number of symbols in alphabets\n";

long long n;

cin>>n;

cout<<"input the symbols\n";

for(int i=0;i<n;i++)

char j;

cin>>j;

v.push_back(j);

cout<<"lower bound and upper bound of valid a string\n";

long long l,r;

cin>>l>>r;

cout<<"input string : ";

cin>>s;

q(s,0,l,r);

Output:
Q.2 Design a program for creating machine that accept the string always
ending with 101.
Program:

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

void state0(string,int,int);

void state1(string,int,int);

void state2(string,int,int);

void statef(string,int,int);

void statef(string str,int n,int i){

if(i==n)

cout<<"Accepted";

if(str[i]=='0')

state2(str,n,i+1);

if(str[i]=='1')

state1(str,n,i+1);

void state2(string str,int n,int i){

if(i==n)

cout<<"Rejected";

if(str[i]=='0')
state0(str,n,i+1);

if(str[i]=='1')

statef(str,n,i+1);

void state1(string str,int n,int i){

if(i==n)

cout<<"Rejected";

if(str[i]=='0')

state2(str,n,i+1);

if(str[i]=='1')

state1(str,n,i+1);

void state0(string str,int n,int i){

if(i==n)

cout<<"Rejected";

if(str[i]=='0')

state0(str,n,i+1);

if(str[i]=='1')

state1(str,n,i+1);

}
int main() {

string s;

cout<<"Input the string to be checked : ";

cin>>s;

int l=s.size();

state0(s,l,0);

return 0;

Output:
Q.3 Design a program for mod 3 machine.
Program:

#include <iostream>

#include<bits/stdc++.h>

using namespace std;

void state0(string,int,int);

void state1(string,int,int);

void state2(string,int,int);

void state2(string str,int n,int i){

if(i==n)

cout<<"Rejected";

if(str[i]=='0')

state1(str,n,i+1);

if(str[i]=='1')

state2(str,n,i+1);

void state1(string str,int n,int i){

if(i==n)

cout<<"Rejected";

if(str[i]=='0')

state2(str,n,i+1);

if(str[i]=='1')

state0(str,n,i+1);

}
void state0(string str,int n,int i){

if(i==n)

cout<<"Accepted";

if(str[i]=='0')

state0(str,n,i+1);

if(str[i]=='1')

state1(str,n,i+1);

int main() {

string s;

cout<<"Input the binary number to be checked : ";

cin>>s;

int l=s.size();

state0(s,l,0);

return 0;

Output:
Q.4 Design a program for accepting decimal number divisible by 2.
Program:

#include<bits/stdc++.h>

using namespace std;

void q0(string,int);

void q1(string s, int i)

int j,k,l,m,n;

if(i==s.size())

cout<<"Valid";

exit(0);

if(s[i]=='1' || s[i]=='3' ||s[i]=='5' ||s[i]=='7' ||s[i]=='9')

q0(s,i+1);

if(s[i]=='0' || s[i]=='2' ||s[i]=='4' ||s[i]=='6' ||s[i]=='8')

q1(s,i+1);

void q0(string s, int i)

int j,k,l,m,n;
if(i==s.size())

cout<<"Invalid";

exit(0);

if(s[i]=='0' || s[i]=='2' ||s[i]=='4' ||s[i]=='6' ||s[i]=='8')

q1(s,i+1);

if(s[i]=='1' || s[i]=='3' ||s[i]=='5' ||s[i]=='7' ||s[i]=='9')

q0(s,i+1);

int main()

int i,j,k,l,m,n;

string s;

cin>>s;

q0(s,0);

}
Output:
Q.5 Design a machine for machine that accept string equal number of 0’s
and 1’s.
Program:

#include<bits/stdc++.h>

#include<string.h>

using namespace std;

stack<int>st;

int flag=0;

string s="";

int i=0;

void q0();

void q1();

void q1()

cout<<"valid";

exit(1);

void q0()

if(st.empty())

if(i==s.size())

q1();

}
else

st.push(s[i]);

i++;

else if(s[i]==st.top())

st.push(s[i]);

i++;

q0();

else if(s[i]!=st.top())

st.pop();

i++;

q0();

int main()

long j,k,l,m,n;
cout<<"Input the string: ";

cin>>s;

cout<<"Input string is ";

q0();

cout<<"invalid";

return 0;

Output:
Q.6 design a machine to count number of 1’s and 0’s in input string.
Program:

#include <iostream>

using namespace std;

int main()

int count0=0,count1=0;

string str;

cout<<"Enter Your string:";

cin>>str;

for(int i=0;i<str.length();i++)

if(str[i]=='1')

count1++;

if(str[i]=='0')

count0++;

cout<<"\nNumber of 1's="<<count1;

cout<<"\nNumber of 0's="<<count0;

}
Output:
Q.7 Design a program to find 2’s complement of a given binary number.
Program:

#include<bits/stdc++.h>

#include<string.h>

using namespace std;

void q0(string s,longi)

if(i==s.size()){return;}

cout<<1-s[i]+'0';

q0(s,i+1);

int main()

long i,j,k,l,m,n;

string s;

cout<<"Input the binary number: ";

cin>>s;

l=s.size();

cout<<"complement of input binary number is: ";

q0(s,0);

return 0;

}
Output:
Q.8 Design a program which will increment the given number by 1.
Program:

#include<bits/stdc++.h>

#include<string.h>

using namespace std;

stack<int>st;

int flag=0;

string ans;

void q0()

if(flag)

if(st.empty())

return;

else

ans.push_back(st.top());

st.pop();

q0();

else

{
if(st.empty())

ans.push_back('1');

q0();

else

if(st.top()=='0'){flag=1;}

ans.push_back(1-(st.top()-'0')+'0');

st.pop();

q0();

int main()

long i,j,k,l,m,n;

string s;

cout<<"Input the binary number: ";

cin>>s;

l=s.size();

i=0;

while(i!=s.size())
{

st.push(s[i]);

i++;

q0();

reverse(ans.begin(),ans.end());

cout<<"incremented value of input binary number is: "<<ans;

return 0;

Output:
Q.10 Design a program to create PDA machine that accept the well-formed
parenthesis.
Program:

#include<bits/stdc++.h>

#include<string.h>

using namespace std;

stack<int>st;

int flag=0;

string s="";

int i=0;

void q0();

void q1();

void q1()

cout<<"Valid";

exit(1);

void q0()

if(i==s.size() &&st.empty())

q1();

else if(st.empty() && s[i]=='(')

{
st.push('(');

i++;

q0();

else if(st.top()=='(' && s[i]=='(')

st.push('(');

i++;

q0();

else if(st.top()=='(' && s[i]==')')

st.pop();

i++;

q0();

int main()

long j,k,l,m,n;

cout<<"Input the parenthesis string: ";

cin>>s;
cout<<"Input string is ";

q0();

cout<<"Invalid";

return 0;

Output:
Q.11 Design a PDA to accept WCWR where W is a string and WR is reverse
of it and C is a special character.
Program:

#include<bits/stdc++.h>

#include<string.h>

using namespace std;

stack<int>st;

int flag=0;

string s="";

int i=0;

void q0();

void q1();

void q1()

if(st.empty() &&i==s.size())

return;

else if(s[i]==st.top())

st.pop();

i++;

q1();

else
{

cout<<"Invalid";

exit(1);

void q0()

if(s[i]=='c')

i++;

q1();

else

st.push(s[i]);

i++;

q0();

int main()

long j,k,l,m,n;

cout<<"Input the string: ";

cin>>s;
cout<<"Input string is ";

q0();

cout<<"Valid";

return 0;

Output:
Q.12 Design a Turing Machine that accept the following language anbncn
where n>0.
Program:

#include<bits/stdc++.h>

#include<string.h>

using namespace std;

stack<int>st;

int flag=0;

string s="";

vector<char> v;

int i=1;

void q0();

void q1();

void q2();

void q3();

void q4();

void q5();

void q5()

cout<<"Valid";

exit(1);

void q4()

if(v[i]=='Y' || v[i]=='Z')
{

i++;

q4();

else if(v[i]=='B')

q5();

void q3()

if(v[i]=='a' || v[i]=='Y' || v[i]=='b' || v[i]=='Z')

i--;

q3();

else if(v[i]=='X')

i++;

q0();

void q2()
{

if(v[i]=='b' || v[i]=='Z')

i++;

q2();

else if(v[i]=='c')

v[i]='Z';

i--;

q3();

void q1()

if(v[i]=='a')

i++;

q1();

else if(v[i]=='Y')

i++;

q1();
}

else if(v[i]='b')

v[i]='Y';

i++;

q2();

void q0()

if(v[i]=='a')

v[i]='X';

i++;

q1();

else if(v[i]=='Y')

i++;

q4();

int main()
{

long j,k,l,m,n;

cout<<"Input the string: ";

cin>>s;

s="B"+s+"B";

for(i=0;i<s.size();i++)

v.push_back(s[i]);

i=1;

cout<<"Input string is ";

q0();

cout<<"Invalid";

return 0;

Output:
Q.9 Design a program to convert NFA to DFA.
Program:
//NFA to DFA conversion

#include <stdio.h>

#include <string.h>

#define STATES 50

structDstate

char name;

char StateString[STATES+1];

char trans[10];

intis_final;

}Dstates[50];

structtran

char sym;

inttostates[50];

intnotran;

};

struct state

int no;

structtrantranlist[50];

};

intstackA[100],stackB[100],C[100],Cptr=-1,Aptr=-1,Bptr=-1;

struct state States[STATES];

char temp[STATES+1],inp[10];
intnos,noi,nof,j,k,nods=-1;

void pushA(int z)

{stackA[++Aptr]=z;}

void pushB(int z)

{stackB[++Bptr]=z;}

intpopA()

{return stackA[Aptr--];}

void copy(inti)

char temp[STATES+1]=" ";

int k=0;

Bptr=-1;

strcpy(temp,Dstates[i].StateString);

while(temp[k]!='\0')

pushB(temp[k]-'0');

k++;

intpopB()

{return stackB[Bptr--];}

intpeekB()

{return stackA[Bptr];}

intpeekA()

{return stackA[Aptr];}

int seek(intarr[],intptr,int s)

{
inti;

for(i=0;i<=ptr;i++)

if(s==arr[i])

return 1;

return 0;

void sort()

inti,j,temp;

for(i=0;i<Bptr;i++)

for(j=0;j<(Bptr-i);j++)

if(stackB[j]>stackB[j+1])

temp=stackB[j];

stackB[j]=stackB[j+1];

stackB[j+1]=temp;

void tostring()

inti=0;
sort();

for(i=0;i<=Bptr;i++)

temp[i]=stackB[i]+'0';

temp[i]='\0';

void display_DTran()

inti,j;

printf("\n\t\t DFA Transition Table ");

printf("\n\t\t -------------------- ");

printf("\nStates\tString\tInputs\n ");

for(i=0;i<noi;i++)

printf("\t%c",inp[i]);

printf("\n \t----------");

for(i=0;i<nods;i++)

if(Dstates[i].is_final==0)

printf("\n%c",Dstates[i].name);

else

printf("\n*%c",Dstates[i].name);

printf("\t%s",Dstates[i].StateString);
for(j=0;j<noi;j++)

printf("\t%c",Dstates[i].trans[j]);

printf("\n");

void move(intst,int j)

intctr=0;

while(ctr<States[st].tranlist[j].notran)

pushA(States[st].tranlist[j].tostates[ctr++]);

void lambda_closure(intst)

intctr=0,in_state=st,curst=st,chk;

while(Aptr!=-1)

curst=popA();

ctr=0;

in_state=curst;

while(ctr<=States[curst].tranlist[noi].notran)

chk=seek(stackB,Bptr,in_state);

if(chk==0)
pushB(in_state);

in_state=States[curst].tranlist[noi].tostates[ctr++];

chk=seek(stackA,Aptr,in_state);

if(chk==0 &&ctr<=States[curst].tranlist[noi].notran)

pushA(in_state);

main()

int final[20],start,fin=0,i;

char c,ans,st[20];

printf("\nEnter no. of states in NFA : ");

scanf("%d",&nos);

for(i=0;i<nos;i++)

{States[i].no=i;}

printf("\nEnter the start state : ");

scanf("%d",&start);

printf("Enter the no. of final states : ");

scanf("%d",&nof);

printf("\nEnter the final states : \n");

for(i=0;i<nof;i++)

scanf("%d",&final[i]);

printf("\nEnter the no. of input symbols : ");


scanf("%d",&noi);

c=getchar();

printf("\nEnter the input symbols : \n ");

for(i=0;i<noi;i++)

scanf("%c",&inp[i]);

c=getchar();

inp[i]='e';

printf("\nEnter the transitions : (-1 to stop)\n");

for(i=0;i<nos;i++)

for(j=0;j<=noi;j++)

States[i].tranlist[j].sym=inp[j];

k=0;

ans='y';

while(ans=='y')

printf("move(%d,%c) : ",i,inp[j]);

scanf("%d",&States[i].tranlist[j].tostates[k++]);

if(States[i].tranlist[j].tostates[k-1]==-1)

{
k--;ans='n';

break;

States[i].tranlist[j].notran=k;

//Conversion

i=0;nods=0;fin=0;

pushA(start);

lambda_closure(peekA());

tostring();

Dstates[nods].name='A';

nods++;

strcpy(Dstates[0].StateString,temp);

while(i<nods)

for(j=0;j<noi;j++)

fin=0;

copy(i);

while(Bptr!=-1)

move(popB(),j);

}
while(Aptr!=-1)

lambda_closure(peekA());

tostring();

for(k=0;k<nods;k++)

if((strcmp(temp,Dstates[k].StateString)==0))

Dstates[i].trans[j]=Dstates[k].name;

break;

if(k==nods)

nods++;

for(k=0;k<nof;k++)

fin=seek(stackB,Bptr,final[k]);

if(fin==1)

Dstates[nods-1].is_final=1;

break;

}
}

strcpy(Dstates[nods-1].StateString,temp);

Dstates[nods-1].name='A'+nods-1;

Dstates[i].trans[j]=Dstates[nods-1].name;
}

i++;

display_DTran();

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy