CSE 490c/303 Homework 3b, Q1 Suggestion

One common pattern for implementing programs is the state machine. A state machine is an abstract machine that can be in one of a finite number of discrete states. The machine operates by reading input values one at a time. Depending on the state the machine is currently in, an input may cause it to take some action and transition to another (or the same) state.

State machines are often represented by graphs, like the one below. The circles are the states, the arrows the transitions between states, and the labels on transitions the description of what action(s) to take, depending on the input value read.

The example state machine below implements a solution to the wc problem. There are only two states, in word and not in word. (The initial state is not in word.) In simulating the state machine you use a variable in your program to represent the current state of that machine. When you read an input (a single character in this case), you take the action and make the state transition indicated by the diagram.

Thinking of this program as implementing a state machine is almost counter-productive for this simple situation -- your reaction is probably that you could just write the program without this abstraction more easily than with it. That's probably right; you could. We're bringing up state machines here because we'll be seeing them again later in the course, and because they're a very important abstract pattern used in designing programs (e.g., user interfaces).