Do any one have idea of implementing the moore machine by c Programming implementation??

Dani AI

Generated

asked for a C implementation of a Moore machine. 's minimal stub and 's note about using collections and operations point toward the common pattern: name states and inputs clearly, keep a table of state transitions, and associate each state with its output. The short example below shows that pattern in a small, self-contained program (a parity detector: states EVEN/ODD, output 0/1 determined only by the current state).

#include <stdio.h>

typedef enum { EVEN = 0, ODD, NUM_STATES } State;
typedef enum { IN0 = 0, IN1, NUM_INPUTS } Input;

/* transition[state][input] -> next state */
static const State transition[NUM_STATES][NUM_INPUTS] = {
    { EVEN, ODD },  /* from EVEN: on 0 -> EVEN, on 1 -> ODD */
    { ODD,  EVEN }  /* from ODD : on 0 -> ODD,  on 1 -> EVEN */
};

/* output associated with each state (Moore output) */
static const int state_output[NUM_STATES] = { 0, 1 };

int main(void)
{
    const char *stream = "1101001";
    State st = EVEN;

    /* initial state's output (Moore machines have state-based outputs) */
    printf("initial output: %d\n", state_output[st]);

    for (size_t i = 0; stream[i]; ++i) {
        int in = (stream[i] == '1') ? IN1 : IN0;
        st = transition[st][in];
        printf("after '%c' -> state=%d output=%d\n",
               stream[i], (int)st, state_output[st]);
    }
    return 0;
}

Notes and tips: emit the state's output either before processing the first input (initial output) or right after each transition, but be consistent. For larger machines, use a sentinel value for undefined transitions and validate input symbols. If outputs must depend on both state and input, a Mealy machine is a better fit. Unit-test with short input sequences that exercise every transition and every state output.

Recommended Answers

All 2 Replies

Hmmm... I think i'd start like this, just an idea though.

int main(void)
{
    return 0;
}

I guess you dont understand what a SET is?
you could use arrays to hold more than one value,
and by your experience I guess that's enough.

and you need to use functions to change the sets values.
good luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.