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

Description: For this week\'s lab you will program a simple finite state machine

ID: 3815228 • Letter: D

Question

Description: For this week's lab you will program a simple finite state machine using structures. The FSM will model a rudimentary vending machine algorithm which will add nickels and dimes up to $0.20. Task Specifics: The Moore type finite state machine described n Table-1 is to be m pie men ted in a C program using structures and posters. We will assume that it will be impossible for A and B to both be 1 at the same time. The following structure is to be used to store data for each state. struct FSM_state {unsigned int id;//state number unsigned int z;//output variable Z struct FSM_state 'next;//pointer to next state} The program should start in state 0. The program should ask the user for the input values A and B (A represents a nickel. B represents a dime). The program should then go to the next state based on the inputs, display the current state number as well as the current output value of Z (which represents the current total), then wait for the next input values. Use a pointer to keep track of the current state. Allow the user to enter a "2" for input A to exit out of the program.

Explanation / Answer

A possible way for implementing a C program for given vending machine problem is presented below.

For each state (ID) and inputs (A, B) combination a unique state is defined with holds ID of present state, present output and a pointer to next state.

A state transitions table is also defined to easily transition from one state to another based on present state ID, input A and input B value.

File: vendingmachine.c

#include <stdio.h>
#include <stdlib.h>

struct FSM_state {
unsigned int id; // State ID
unsigned int z; // Output Z
struct FSM_state *next; // Pointer to next state
};

struct FSM_state state_100;
struct FSM_state state_200;
struct FSM_state state_300;
struct FSM_state state_400;

struct FSM_state state_000 = { 0, 0, &state_000 };
struct FSM_state state_010 = { 0, 0, &state_100 };
struct FSM_state state_001 = { 0, 0, &state_200 };
struct FSM_state state_100 = { 1, 5, &state_100 };
struct FSM_state state_110 = { 1, 5, &state_200 };
struct FSM_state state_101 = { 1, 5, &state_300 };
struct FSM_state state_200 = { 2, 10, &state_200 };
struct FSM_state state_210 = { 2, 10, &state_300 };
struct FSM_state state_201 = { 2, 10, &state_000 };
struct FSM_state state_300 = { 3, 15, &state_300 };
struct FSM_state state_310 = { 3, 15, &state_400 };
struct FSM_state state_301 = { 3, 15, &state_000 };
struct FSM_state state_400 = { 4, 20, &state_400 };
struct FSM_state state_410 = { 4, 20, &state_000 };
struct FSM_state state_401 = { 4, 20, &state_000 };

struct FSM_state *state_transitions[5][2][2] =
{
{
{
&state_000,
&state_001
},
{
&state_010,
0
}
},
{
{
&state_100,
&state_101
},
{
&state_110,
0
}
},
{
{
&state_200,
&state_201
},
{
&state_210,
0
}
},
{
{
&state_300,
&state_301
},
{
&state_310,
0
}
},
{
{
&state_400,
&state_401
},
{
&state_410,
0
}
}
};

int main( void ) {
struct FSM_state *current_state = state_transitions[0][0][0];
int a, b;

printf("Current state = (%d/%d) ", current_state->id, current_state->z);

do {
printf("Enter A: ");
scanf("%d", &a);

if (a == 2) break;

if ((a < 0) || (a > 2)) continue;

printf("Enter B: ");
scanf("%d", &b);

if ((b < 0) || (b > 1)) continue;

if ((a == 1) && (b == 1)) continue;

current_state = state_transitions[current_state->id][a][b]->next;

printf("New state = (%d/%d) ", current_state->id, current_state->z);
} while(a < 2);
}

Compilation: gcc vendingmachine.c

Execution (sample): ./a.out

Current state = (0/0)
Enter A: 1
Enter B: 0
New state = (1/5)
Enter A: 1
Enter B: 0
New state = (2/10)
Enter A: 1
Enter B: 0
New state = (3/15)
Enter A: 1
Enter B: 0
New state = (4/20)
Enter A: 1
Enter B: 0
New state = (0/0)
Enter A: 2