0
0
Embedded Cprogramming~20 mins

State transition table approach in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Machine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple state machine

Consider this embedded C code using a state transition table. What will be printed when input = 1?

Embedded C
typedef enum {STATE_IDLE, STATE_RUN, STATE_STOP} State;

typedef struct {
    State current_state;
    void (*action)(void);
} Transition;

void action_idle() { printf("Idle\n"); }
void action_run() { printf("Run\n"); }
void action_stop() { printf("Stop\n"); }

Transition state_table[3][2] = {
    {{STATE_IDLE, action_idle}, {STATE_RUN, action_run}},
    {{STATE_RUN, action_run}, {STATE_STOP, action_stop}},
    {{STATE_STOP, action_stop}, {STATE_IDLE, action_idle}}
};

int main() {
    State current = STATE_IDLE;
    int input = 1;
    Transition t = state_table[current][input];
    t.action();
    return 0;
}
AIdle
BRun
CStop
DNo output
Attempts:
2 left
💡 Hint

Look at the state_table row for STATE_IDLE and column for input = 1.

🧠 Conceptual
intermediate
1:30remaining
Understanding state transition table indexing

In a state transition table for a device with 4 states and 3 inputs, how many entries should the table have?

A12
B7
C4
D3
Attempts:
2 left
💡 Hint

Think about rows as states and columns as inputs.

🔧 Debug
advanced
2:30remaining
Identify the error in this state transition table code

What error will this code produce?

Embedded C
typedef enum {S0, S1} State;

void action0() { printf("A0\n"); }
void action1() { printf("A1\n"); }

typedef struct {
    State next_state;
    void (*action)();
} Transition;

Transition table[2][2] = {
    { {S1, action0}, {S0, action1} },
    { {S0, action1}, {S1, action0} }
};

int main() {
    table[0][1].action();
    return 0;
}
APrints 'A1' without error
BRuntime segmentation fault
CCompilation error due to missing initializer in table
DPrints 'A0' without error
Attempts:
2 left
💡 Hint

Check the initialization of the 2D array table.

📝 Syntax
advanced
1:30remaining
Correct syntax for function pointer in state transition struct

Which option correctly declares a function pointer named action in a struct for a function returning void and taking no parameters?

Avoid (*action)();
Bvoid *action(void);
Cvoid action(void*);
Dvoid (*action)(void);
Attempts:
2 left
💡 Hint

Remember the syntax for a pointer to a function with no parameters returning void.

🚀 Application
expert
3:00remaining
Determine final state after sequence of inputs

Given the state transition table and initial state STATE_A, what is the final state after inputs 0, 1, 1, 0?

Embedded C
typedef enum {STATE_A, STATE_B, STATE_C} State;

typedef struct {
    State next_state;
    void (*action)(void);
} Transition;

void actA() { printf("A\n"); }
void actB() { printf("B\n"); }
void actC() { printf("C\n"); }

Transition table[3][2] = {
    {{STATE_B, actB}, {STATE_C, actC}},
    {{STATE_A, actA}, {STATE_C, actC}},
    {{STATE_C, actC}, {STATE_A, actA}}
};

int main() {
    State current = STATE_A;
    int inputs[] = {0, 1, 1, 0};
    int len = 4;
    for (int i = 0; i < len; i++) {
        Transition t = table[current][inputs[i]];
        t.action();
        current = t.next_state;
    }
    printf("Final state: %d\n", current);
    return 0;
}
ASTATE_B
BSTATE_C
CCompilation error
DSTATE_A
Attempts:
2 left
💡 Hint

Trace each input step by step using the table.