Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Q2) (15 points) Consider the following state transition diagram for a finite st

ID: 2266607 • Letter: #

Question

(Q2) (15 points) Consider the following state transition diagram for a finite state machine. The circles represent the states, and are labeled A through D. The arrows represent transitions between states. The input is represented by x and the output is y. Assume that the variable "Porta” provides the input value x, and that the output value is written to the variable "PortB". Write the appropriate C code to implement this state transition diagram. Hint: Use the C construct "Struct" or arrays. Be sure to include comments to describe your code. x=0/y=0 X=0/y=1 x=1/y=0 x=0/y=1 X=1/y=0 x=1/y=0 x=0/y=1 x=1/y=0

Explanation / Answer

There are several ways to execute this in C. I have used simple if /else statements as that is the easiest way to deal with Boolean inputs. The code is below. This has been executed on an online compiler and the state transitions have been verified. Enter 0 or 1 for the input; any other input makes the program take no action. Program executes infinitely, until Control +C is pressed. For each x as input, output and new state are printed. Starting state is assumed as A.

#include<stdio.h>

int main() {

   int x;

   int y;

   char state='A';

   int i=0;

   while (i<5) {

       printf ("enter x ");

       scanf ("%d", &x);

    if (state == 'A') {

        if (x ==0) {

            y=0;

            state = 'A';

            printf ("state is %c, y is %d ", state,y);

        }

        else if (x ==1) {

            state = 'D';

            y=0;

            printf ("state is %c, y is %d ", state,y);

        }

    }

    else if (state == 'D') {

        if (x==0) {

            state='A';

            y=1;

            printf ("state is %c, y is %d ", state,y);

        }

        else if (x==1) {

            state = 'C';

            y=0;

            printf ("state is %c, y is %d ", state,y);

        }

        }

  else if (state == 'C') {

        if (x==0) {

            state = 'A';

            y=1;

            printf ("state is %c, y is %d ", state,y);

        }

        else if (x==1) {

            state = 'B';

            y=0;

            printf ("state is %c, y is %d ", state,y);

        }

    }

    else if (state == 'B') {

        if (x==1) {

            state = 'B';

            y=0;

            printf ("state is %c, y is %d ", state,y);

        }

        else if (x==0) {

            state = 'A';

            y=0;

            printf ("state is %c, y is %d ", state,y);

        }

    }

}

}