List of Experiment
S.No. Name of Experiment Cos
Design a Program for creating DFA that starts and end with ‘a’ from input (a, b). AL601.1
01 AL601.2
AL601.3
Design a Program for creatingDFA which will accept all the strings ending with 00 over AL601.1
02
an alphabet {0, 1} AL601.2
AL601.3
AL601.1
03 Design a Program for Mode 3 Machine. AL601.2
AL601.3
AL601.1
04 Design a program for accepting decimal number divisible by 3. AL601.2
AL601.3
AL601.1
Design a program for creating a machine which count number of 1’s and 0’s in a given
05 AL601.2
string.
AL601.3
AL601.1
06 Design a Program to find 2’s complement of a given binary number. AL601.2
AL601.3
AL601.1
07 Design a Program Generate Binary Numbers from 1 to n. AL601.2
AL601.3
AL601.1
08 Design a Program to convert NDFA to DFA. AL601.2
AL601.3
R
Design a PDA to accept WCW where w is any string and WR is reverse of that string AL601.1
09 and C is a Special symbol. AL601.2
AL601.3
n n n
10 Design a Turing machine that’s accepts the following language a b c where n>=1 AL601.1
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()
{
// for producing different random
// numbers every time.
srand(time(0));
// random length of string from 1 - 16
// we are taking input from input stream,
// we can take delimiter to end the string int
max = 1 + rand() % 15;
// generating random string and processing
it int i = 0; while (i< max) {
// producing random character
over // input alphabet (a, b)
char c = 'a' + rand() % 2; cout<< c <<
" "; i++;
// first character is 'a'
if (c == 'a') {
// if there is only 1 character
// i.e. 'a' if (i == max) cout<< "YES\n";
while (i< max) {
c = 'a' + rand() % 2;
cout<< c << " ";
i++;
// if character is 'a' and it
// is the last character
if (c == 'a' &&i == max) {
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 creatingDFA which will accept all the strings ending with 00 over an
alphabet {0, 1}
#define max 100 int
main()
{ char
str[max],f='a'; int
i;
printf("enter the string to be checked: ");
scanf("%s",str); for(i=0;str[i]!='\0';i++)
{
switch(f)
{ case 'a': if(str[i]=='0')
f='b'; else if(str[i]=='1')
f='a'; break; case 'b':
if(str[i]=='0') f='c'; else
if(str[i]=='1') f='d'; break;
case 'c': if(str[i]=='0') f='c';
else if(str[i]=='1') f='d';
break; case 'd':
if(str[i]=='0') f='b'; else
if(str[i]=='1') f='d'; break;
} } if(f=='c') printf("\nString is accepted as it reached the final state %c at the
end.",f); else printf("\nString is not accepted as it reached %c which is not the final
state.",f); }
OUTPUT
Experiment No. 3
Objective: Design a Program for Mode 3 Machine.
#include <stdio.h> int mode(int a[],int
n) { int maxValue = 0, maxCount =
0, i, j;
for (i = 0; i< n; ++i) {
int count = 0;
for (j = 0; j < n; ++j) {
if (a[j] == a[i])
++count;
}
if (count >maxCount) {
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
int main() { int n =
5; int a[] =
{0,6,3,2,3};
printf("Mode = %d ", mode(a,n));
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;
// Calculate next state for bit 1
trans1 = (state<<1) + 1;
Table[state][1] = (trans1 < k)? trans1: trans1-k;
}
}
// A recursive utility function that takes a 'num' and DFA (transition
// table) as input and process 'num' bit by bit over DFA void
isDivisibleUtil(int num, int* state, int Table[][2])
{
// process "num" bit by bit from MSB to LSB
if (num != 0)
{
isDivisibleUtil(num>>1, state, Table);
*state = Table[*state][num&1];
}
}
// The main function that divides 'num' by k and returns the remainder int
isDivisible (int num, int k)
{
// Allocate memory for transition table. The table will have k*2 entries
int (*Table)[2] = (int (*)[2])malloc(k*sizeof(*Table));
// Fill the transition table preprocess(k,
Table);
// Process ‘num’ over DFA and get the remainder int
state = 0; isDivisibleUtil(num, &state, Table);
// Note that the final value of state is the remainder
return state;
}
// Driver program to test above functions int
main()
{
int num = 47; // Number to be divided
int k = 5; // Divisor
int remainder = isDivisible (num, k);
if (remainder == 0)
printf("Divisible\n");
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>
using namespace std;
// Function to check //
if bit is 1 or not bool
isOne(int i)
{
if (i == 1)
return true;
else
return false;
}
// Driver Code int
main()
{
int a[] = { 1, 0, 0, 1, 0, 0, 1 };
int n = sizeof(a) / sizeof(a[0]);
int count_of_one = count_if(a, a + n, isOne);
cout<< "1's: " <<count_of_one<<endl;
cout<< "0's: " << (n - count_of_one) <<endl;
return 0;
}
OUTPUT