CS501 TOC Lab File-Pages-Deleted
CS501 TOC Lab File-Pages-Deleted
S.
No. Name of Experiment Cos
CL501.1
01 Design a Program for creating DFA that starts and end with ‘a’ from input (a, b). CL501.2
CL501.5
CL501.1
Design a Program for creating DFA which will accept all the strings ending
02 CL501.2
with 00 over an alphabet {0, 1}
CL501.5
CL501.1
03 Design a Program for Mode 3 Machine. CL501.2
CL501.5
CL501.1,
Design a program for accepting decimal number divisible
04 CL501.3,
by 3.
CL501.5
Design a program for creating a machine which counts number of 1’s and 0’s in
05 CL501.4
a given string.
CL501.1,
06 Design a Program to find 2’s complement of a given binary number. CL501.2,
CL501.5
CL501.1,
07 Design a Program Generate Binary Numbers from 1 to n.
CL501.2
CL501.1,
08 Design a Program to convert NDFA to DFA.
CL501.2
Design a PDA to accept WCWR where w is any string and WR is reverse of that CL501.1,
09
string and C is a Special symbol. CL501.4
Design a Turing machine that’s accepts the following language a n b n c n where CL501.1,
10
n>=1 CL501.4
Experiment No. 1
Objective: Design a Program for creating DFA that starts and end with ‘a’ from input (a, b).
#include <iostream>
#include <time.h>
using namespace
std; int main()
{
cout<< "YES\n";
while (i< max) {
c = 'a' + rand() % 2;
+;
cout<< "\nYES\n";
}
// if character is 'b' and it
// is the last character
else if (i == max) {
cout<< "\nNO\n";
}
}
}
// first character is 'b' so no matter
// what the string is, it is not going
// to be
accepted else {
while (i< max) {
c = 'a' + rand() % 2;
cout<< c << " ";
i++;
}
cout<< "\nNO\n";
}
}
return 0;
}
OUTPUT
Experiment No. 2
Objective: Design a Program for creating DFA which will accept all the strings ending with 00 over an
alphabet {0, 1}
int main()
charstr[max],f='a';
inti;
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
switch(f)
break;
break;
break;
break;
}
OUTPUT
Experiment No. 3
Objective: Design a Program for Mode 3 Machine.
#include <stdio.h>
intmaxValue = 0, maxCount = 0, i,
j;
{ int count = 0;
{ if (a[j] == a[i])
++count;
if (count >maxCount)
{ maxCount = count;
maxValue = a[i];
returnmaxValue;
int main() {
int n = 5;
int a[] = {0,6,3,2,3};
return 0;
OUTPUT
Experiment No. 4
Objective: Design a program for accepting decimal number divisible by 3.
#include <stdio.h>
#include <stdlib.h>
// Function to build DFA for divisor k
void preprocess(int k, int Table[][2])
{
int trans0, trans1;
// The following loop calculates the two transitions for each state,
// starting from state 0
for (int state=0; state<k; ++state)
{
// Calculate next state for bit
0 trans0 = state<<1;
Table[state][0] = (trans0 < k)? trans0: trans0-k;
if (remainder ==
printf("Divisible\n");
0) else
printf("Not Divisible: Remainder is %d\n", remainder);
return 0;
}
OUTPUT
Experiment No. 5
Objective: Design a program for creating a machine which count number of 1’s and 0’s in a given string.
#include <bits/stdc++.h>
// Function to check
// if bit is 1 or not
boolisOne(inti)
if (i == 1)
return true;
else
return false;
}
// Driver Code
int main()
int a[] = { 1, 0, 0, 1, 0, 0, 1 };
return 0;
}
OUTPUT
Experiment No. 6
Objective: Design a Program to find 2’s complement of a given binary
int main()
{
char binary[SIZE + 1], onesComp[SIZE + 1], twosComp[SIZE + 1];
inti, carry=1;
/*
* Add 1 to the ones complement
*/
for(i=SIZE-1; i>=0; i--)
{
if(onesComp[i] == '1' && carry == 1)
{
twosComp[i] = '0';
}
else if(onesComp[i] == '0' && carry == 1)
{
twosComp[i] = '1';
carry = 0;
}
else
{
twosComp[i] = onesComp[i];
}
}
twosComp[SIZE] = '\0';
return 0;
}
OUTOUT
Experiment No. 7
Objective: Design a Program Generate Binary Numbers from 1 to n.
#include<stdio.h>
#include<string.h>
#include<math.h>
int ninputs;
int main()
int f[10];
inti,j=3,s=0,final=0,flag=0,curr1,curr2,k,l;
int c;
scanf("%d",&st);
for(i=0;i<st;i++)
state[(int)(pow(2,i))] = 1;
for(i=0;i<fin;i++)
scanf("%d",&f[i]);
int p,q,r,rel;
scanf("%d",&rel);
scanf("%d%d%d",&p,&q,&r);
if (q==0)
dfa[p][0][r] = 1;
else
dfa[p][1][r] = 1;
scanf("%d",&in);
in = pow(2,in);
i=0;
int x=0;
for(i=0;i<st;i++)
{
for(j=0;j<2;j++)
intstf=0;
for(k=0;k<st;k++)
if(dfa[i][j][k]==1)
stf = stf + pow(2,k);
go[(int)(pow(2,i))][j] = stf;
printf("%d-%d-->%d\n",(int)(pow(2,i)),j,stf);
if(state[stf]==0)
arr[x++] = stf;
state[stf] = 1;
for(i=0;i<x;i++)
printf("for %d-----",arr[x]);
for(j=0;j<2;j++)
int new=0;
for(k=0;k<st;k++)
int h = pow(2,k);
if(new==0)
new = go[h][j];
if(state[new]==0)
arr[x++] = new;
state[new] = 1;
printf("STATE 0 1\n");
for(i=0;i<10000;i++)
if(state[i]==1)
//printf("%d**",i);
int y=0;
if(i==0)
printf("q0 ");
else
for(j=0;j<st;j++)
int x = 1<<j;
if(x&i)
printf("q%d ",j);
y = y+pow(2,j);
//printf("y=%d ",y);
//printf("%d",y);
printf(" %d %d",go[y][0],go[y][1]);
printf("\n");
j=3;
while(j--)
printf("\nEnter string");
scanf("%s",str);
l = strlen(str);
curr1 = in;
flag = 0;
printf("%d-",curr1);
for(i=0;i<l;i++)
curr1 = go[curr1]
[str[i]-'0'];
printf("%d-",curr1);
for(i=0;i<fin;i++)
flag = 1;
break;
if(flag)
printf("\nString Accepted");
else
printf("\nString Rejected");
return 0; }
}
OUTPUT
Experiment No. 8
Objective: Design a PDA to accept WCWR where w is any string and WR is reverse of that string and
C is a Special symbol.
Soln:-
Some string will come followed by one 'c', followed by reverse of the string before 'c'.
So we get to know that 'c' will work as an alarm to starting popping STACK.
So we will pop every 'a' with 'a' and every 'b' with 'b'.
For every two a's and b's push them into STACK
When 'c' comes do nothing.
Starting popping STACK: 'a' for 'a' and 'b' for
'b'. We have designed the PDA for the problem:
δ(q1, ε, Z) = (qf, Z)
Note: qf is Final State
Explanation
Let’s see, how this DPDA is working:
We will take one input string: "abbcbba"
Objective: Design a Turing machine that’s accepts the following language a n b n c n where n>=1.
Soln:-
Approach used –
First replace a 0 from front by X, then keep moving right till you find a 1 and replace this 1 by Y.
Again, keep moving right till you find a 2, replace it by Z and move left. Now keep moving left
till you find a X. When you find it, move a right, then follow the same procedure as above.
A condition comes when you find a X immediately followed by a Y. At this point we keep moving
right and keep on checking that all 1’s and 2’s have been converted to Y and Z. If not then string
is not accepted. If we reach $ then string is accepted.
Step-1:
Replace 0 by X and move right, Go to state Q1.
Step-2:
Replace 0 by 0 and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
Replace 1 by Y and move right, go to state Q2.
Step-3:
Replace 1 by 1 and move right, Remain on same state
Replace Z by Z and move right, Remain on same
state Replace 2 by Z and move right, go to state Q3.
Step-4:
Replace 1 by 1 and move left, Remain on same state
Replace 0 by 0 and move left, Remain on same state
Replace Z by Z and move left, Remain on same state
Replace Y by Y and move left, Remain on same
state Replace X by X and move right, go to state Q0.
Step-5:
If symbol is Y replace it by Y and move right and Go to state
Q4 Else go to step 1
Step-6:
Replace Z by Z and move right, Remain on same state
Replace Y by Y and move right, Remain on same state
If symbol is $ replace it by $ and move left, STRING IS ACCEPTED, GO TO FINAL
STATE Q5