Write a program using structures and pointers to implement the finite state mach
ID: 3824871 • Letter: W
Question
Write a program using structures and pointers to implement the finite state machine described in the table below. The program should repeatedly display the current state ID number, current output value, then ask for a new input value. Follow a similar procedure to the lab 12 assignment. Start at state ID 0. Example output (highlighted text indicates example input): Current state ID: 0 Current output z: 4 Enter input value: 2 Current state ID: 2 Current output z: 15 Enter input value: Write a program that will ask the user to enter their name. The program should then create a string with the text: "Hello , have a great day!", replacing with the entered name. (Also make sure to keep the spaces). The program should then print the string. Use function from the library.Explanation / Answer
#include <stdio.h>
int main()
{
struct
{
int state_id;
int output;
int input;
int next_state_id;
}fsm;
fsm.state_id = 0;
fsm. input = 0;
while (1)
{
if(fsm.state_id == 0)
{
fsm.output = 4;
if(fsm.input == 0)
fsm.next_state_id = 0;
else if(fsm.input == 1)
fsm.next_state_id = 1;
else if(fsm.input == 2)
fsm.next_state_id = 2;
}
else if(fsm.state_id == 1)
{
fsm.output = 8;
if(fsm.input == 0)
fsm.next_state_id = 1;
else if(fsm.input == 1)
fsm.next_state_id = 2;
else if(fsm.input == 2)
fsm.next_state_id = 0;
}
else if(fsm.state_id == 2)
{
fsm.output = 15;
if(fsm.input == 0)
fsm.next_state_id = 2;
else if(fsm.input == 1)
fsm.next_state_id = 3;
else if(fsm.input == 2)
fsm.next_state_id = 3;
}
else
{
fsm.output = 16;
if(fsm.input == 0)
fsm.next_state_id = 3;
else if(fsm.input == 1)
fsm.next_state_id = 0;
else if(fsm.input == 2)
fsm.next_state_id = 1;
}
//fsm.state_id = fsm.next_state_id;
printf ("Current state_id = %d ",fsm.next_state_id);
printf("Current Output z = %d ",fsm.output);
printf("Enter input value:");
scanf("%d",&fsm.input);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.