C programming, deterministic machine, states should be given by the user as the example below in a dynamic array, the same for alphabet (symbols), final states, the first state given by the user will be used as initial/starting state by default, transition function same as before given by the user for that I was thinking in something as a simple table.
The user will provide an entrance string "w" and this one will print "accepted" when the last state given by the user in string "w" is a final state declared by the user before, if the last memeber of "w" is not a a final state will print "not accepted". The program should display each change of state after the user gives the entrnace string "w".
PLEASE EXPLAIN EXACTLY HOW WORKS EACH PART OF THE PROGRAM, COMMENT IT, THANKS!!!
p.d. The simplest way possible
#include<stdio.h>
int main()
{
int n=0,i=0;
printf("Write the number of states: \n");
scanf("%d", &n);
char state[n][20] ;
for (i = 0; i<n; i++){
printf("Write state # %d: \n",i);
printf("%d",i);
scanf("%s", &state[i]);
}
printf("%s %s %s %s %s",
state[0],state[1],state[2],state[3],state[4]);
return 0;
}
#include<stdio.h> //Header files
int main() //Main function starts here and it runs first
{
int n=0,i=0; //Integers n and i were declared as 0.
printf("Write the number of states: \n"); //Printing given string
scanf("%d", &n); // Taking input from user that number of states into n variable.
char state[n][20] ; //Initializing a character array as state[][] with dimensions n and 20.
for (i = 0; i<n; i++){ //Initializing for loop from 0 to n
printf("Write state # %d: \n",i); //Printing the given string
printf("%d",i); //Printing the integer i
scanf("%s", &state[i]); //Taking input string from user into state[i] i.e., we are taking string directly into state[i][0],state[i][1],state[i][2]............state[i][19].
}
printf("%s %s %s %s %s", state[0],state[1],state[2],state[3],state[4]); //Here we are printing in string format where state[0] prints state array first row totally and state[1] prints second row continues upto 5th row.
return 0;
} //In this program we have taken character array and we are taking inputs in string format and they will be stored in charater format in the memory and if we print state[0][0]state[0][1]state[0][2]state[0][3]state[0][4].......state[0][19] it also prints first row of array i.e., same as state[o]. But if we take 2Dinteger array and if we print int[0] it prints address of first integer of first row and int[1] prints first integer of second row.
Get Answers For Free
Most questions answered within 1 hours.