Question

In: Computer Science

Code the FSM in C++, and show that the program works. Construct a Finite State Machine...

Code the FSM in C++, and show that the program works.

Construct a Finite State Machine that models an old-fashioned soda machine that accepts nickels, dimes, and

quarters. The soda machine accepts change until 35 cents have been put in. It gives change back

for any amount greater than 35 cents. Then the customer can push buttons to receive either a

cola, a root beer, or a ginger ale.

Solutions

Expert Solution

  • I have pasted the code and screenshot of successful run below.
  • In case of any doubts just comment down.
  • The code have following functions.
    acceptMoney() - to keep getting coins until the value is greater than or equal to 35 cents.
    chooseProduct() - to allow customer to choose any given product.
    giveBackChange() - to give the extra money back to customer
    giveProduct() - to give the final product to customer.

-----------------------------Screenshot-----------------------------

-----------------------------Code-----------------------------

#include <iostream>

using namespace std;

string products[] = {"Cola", "Root Beer", "Ginger Ale"};

int acceptMoney(){
   int money = 0;
   while(money<35){
       cout << "Add Money: ";
       int temp; cin >> temp;
       if(temp<0) continue;
       money += temp;
   }
   return money;
}

int chooseProduct(){
   int i = 0;
   cout << i << ". " << products[i++] << endl;
   cout << i << ". " << products[i++] << endl;
   cout << i << ". " << products[i++] << endl;
   int ind = -1;
   while(ind>2 || ind<0){
       cout << "Choose product(0/1/2) : ";
       cin >> ind;
   }
   return ind;
}

void giveBackChange(int money){
   if(money>35){
       cout << "Giving back " << money-35 << "." << endl;
   }
}

void giveProduct(int ind){
   cout << "Giving product: " << products[ind] << endl;
}

int main(){
   while(true){
       cout << endl << "Soda machine is ready!!" << endl;
       int money = acceptMoney();
       int ind = chooseProduct();
       giveBackChange(money);
       giveProduct(ind);
   }
}


Related Solutions

In C code Consider the finite state machine shown below that controls a two story elevator....
In C code Consider the finite state machine shown below that controls a two story elevator. The elevator has a toggle button that can be in the UP position or the DOWN position, and an LED light that can be RED or GREEN . When the elevator is on the GROUND floor the light is RED, and when the elevator is on the FIRST floor the light is GREEN. Assume the existence of function enum Button getButton(); that returns a...
Construct a finite-state machine, using combinational logic for an apple vending machine that only accepts nickels...
Construct a finite-state machine, using combinational logic for an apple vending machine that only accepts nickels (5 cents). When 15 cents is deposited the user can push a button to receive an apple and the machine resets. If the user inserts more than 15 cents no money will be returned. What are the machine states? What are the inputs? What are the outputs? Draw state table Draw state diagram Write the combinational logic for next state and output Explain if...
Write a VHDL code to implement a Finite State Machine that with an 8 bit sequence...
Write a VHDL code to implement a Finite State Machine that with an 8 bit sequence input (can be any sequence, but lets say it is 11001000), determine how many states there are as well; so if the input sequence is correct it will show the number 1 in a 7 segment display, otherwise it will be 0 in the same 7 segment display. If the input sequence is incorrect, start from the beginning.
Using vhdl in a FSM , how could you write code to delay a state change...
Using vhdl in a FSM , how could you write code to delay a state change for one hour? For example, you have a fan running in the 'on' state , but after 1 hour you would like that fan to switch back to the 'off' state. What is the best way going about doing this?
what is the best code to construct a C++ program that finds a five digit number;...
what is the best code to construct a C++ program that finds a five digit number; This number should reverses the order of its digits when multiplied by four. Also, how many five digits numbers are there in which the sum of the digits is even.
First, draw the state machine for the following program. Then write the corresponding Verilog behavioral code....
First, draw the state machine for the following program. Then write the corresponding Verilog behavioral code. Input: X - 1 bit number Output: Z - 1 bit number Clock: clk (State will change at positive edge of the clock) Output will be equal to one if Xn-2 Xn-1 Xn = 011 or Xn-2 Xn-1 Xn = 101
What is a finite-state machine ? What is a pushdown automaton ? What is a Turing...
What is a finite-state machine ? What is a pushdown automaton ? What is a Turing machine? What is a Turing complete language? Compare the finite-state machine , pushdown automaton , and Turing machine.?
Now considering an SR NAND latch as a finite state machine, please draw the corresponding state...
Now considering an SR NAND latch as a finite state machine, please draw the corresponding state transition diagram. You don’t have to show the outputs in the diagram.
Write a truth table for Moore finite state machine modeling a traffic light.
Write a truth table for Moore finite state machine modeling a traffic light.
Consider a finite state machine with a control input called mode. When mode = 0, the...
Consider a finite state machine with a control input called mode. When mode = 0, the machine operates as a mod-3 down counter, where the outputs are the count values. When mode = 1, the machine's output progresses through the last 4 digits of your WCU ID (1133) number (1 digit per clock cycle). Complete each of the steps which follow. (a) Draw the state diagram for this machine. (b) Write RTL Verilog code which implements this design. Submit your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT