Question: Please modify the following program in C given below that works as a State Machine. There are some codes that work perfectly and some need

Please modify the following program in C given below that works as a State Machine. There are some codes that work perfectly and some need some modifiying such as change() and some needs implementing such as a function to delete the states and another function to garbage them. Please make you changes in BOLD font

The code works as follows:

The first thing the code does is it builds the state machine given in the following table. The starting state in 'B'...if the user enters'1' it goes from 'B' to 'F' and the current state becomes 'F'...after that if the user presses '0' then it goes from 'F' to 'C'. If it helps draw a diagram to see it visually.

The user has the option of pressing 'p' to print, 'c' to change, 'd' to delete, 'g' to garbage. Here's what each of them does:

Printing (p) : if the user enters p it prints out all the states with their respective state 0 and state 1. This works perfectly in my code. Just press 'p' to see what i mean.

Change (c): if the user presses 'c' followed by state 0 or state 1 followed by a new state (A-H) it changes the state 0 or state 1 of the current state to the new state (A-H). For example if the current state is 'B' and the user enters 'c' followed by '1' followed by 'D' then the change() function should change the state '1' of 'B' from 'F' to 'D' and if the user enters 'p' after that..it should show this new change instead of the old one. 'c' has no output. Please modify my code for change. It's not changing that state 1 or state 0

Garbage (g): if the user only presses 'g' then it should identify all the states that are reachable and not reachable. Lets say if the user presses 'c' and changes the states of all states enought times and all states still has another state pointing at it then the program should print "No Garbage" other wise it should say "Garbage: " where unreachable state is the state not being pointed at by other states. Code not implemented. Please do so.

Delete (d): if the user only presses 'd' it deletes all the unreachable states. If there are no unreachable states then the program should say " no states deleted" other wise it should say " deleted " Code not implemented please do so.

Here's the list of states with their respective state 0 and state 1

States State 0 State 1
A B

H

B G F
C E C
D A G
E H D
F C B
G A C
H F D

Here's my code:

#include #include int i=0; char State[8]; char State0[8]; char State1[8];

void SimState(){ buildState('A','H','B'); buildState('B','F','G'); buildState('C','C','E'); buildState('D','G','A'); buildState('E','D','H'); buildState('F','B','C'); buildState('G','C','A'); buildState('H','D','F'); }

void buildState(char S, char S1, char S0){ if( i != 8){ State[i] =S; State0[i] = S0; State1[i] = S1; } else { printf("Sorry no more State can be added"); } i++; }

char ns0(char state){ switch(state){ case 'A': state = State0[0]; break; case 'B': state = State0[1]; break; case 'C': state = State0[2]; break; case 'D': state = State0[3]; break; case 'E': state = State0[4]; break; case 'F': state = State0[5]; break; case 'G': state = State0[6]; break; case 'H': state = State0[7]; break; } return state; } char ns1(char state){ switch(state){ case 'A': state = State1[0]; break; case 'B': state = State1[1]; break; case 'C': state = State1[2]; break; case 'D': state = State1[3]; break; case 'E': state = State1[4]; break; case 'F': state = State1[5]; break; case 'G': state = State1[6]; break; case 'H': state = State1[7]; break; } return state; } void change(char next, char stateC, char state){ int j; int index; for(j =0 ;j < 8; j++ ){ if(State[j] == state){ index = j; } }

switch(next){ case '1': State1[index] = stateC; break; case '0': State0[index] = stateC; break; } } void print() { int k =0; for(k = 0; k < 8; k++){ printf("%c %c %c ", State[k],State0[k], State1[k]); } }

int main() { SimState(); char S = 'B'; char Sc; char n; printf("%c ", S); printf("Type either 1 , 0 ,(c) to change,(d) to delete , (p) to print ,or (e) to exit "); while (1){ printf("Command : "); scanf("%c", &n); switch(n){ case '1': S = ns1(S); printf("%c ", S); break; case '0': S = ns0(S); printf("%c ", S); break; case 'c': scanf("%c %c ", &n,&Sc); change(n,Sc,S); break; case 'p': print(); break; case 'e': exit(0); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!