Homework 2

CS17

40 pts.

  1. Design pseudocode to control a soda machine. (As an aside, why wouldn't we refer to this pseudocode as an algorithm?) The machine stocks 4 kinds of soda: Coke (50 cents), Diet Coke (50 cents), root beer (65 cents --- it's really good stuff), and Mountain Dew (40 cents, the favorite of hackers).

    Inputs:

    Outputs:

    I want you to do the requirements specification, analysis, and design steps for this problem. Your design is the most important part. Your pseudocode design should demonstrate top-down stepwise refinement.

Requirements Specification

Wait in a continuous loop, monitoring the machine:

The machine dispenses soda and returns any required change.

Analysis

Inputs:

Outputs:

Conditions:

Top-Down Stepwise Refinement

Design

let deposit = 0
while forever
   if quarter dropped
      let deposit = deposit + 25
   else if dime dropped
      let deposit = deposit + 10
   else if nickel dropped
      let deposit = deposit + 5
   else if mountain dew selected
      if deposit >= 40 and mountain dew is available
         drop mountain dew
         let deposit = deposit - 40
      end_if
      return remaining deposit
      let deposit = 0
   else if coke selected
      if deposit >= 50 and coke is available
         drop coke
         let deposit = deposit - 50
      end_if
      return remaining deposit
      let deposit = 0
   else if diet coke selected
      if deposit >= 50 and diet coke is available
         drop diet coke
         let deposit = deposit - 50
      end_if
      return remaining deposit
      let deposit = 0
   else if root beer selected
      if deposit >= 65 and root beer is available
         drop root beer
         let deposit = deposit - 65
      end_if
      return remaining deposit
      let deposit = 0
   end_if
end_while



Thomas P. Kelliher
Mon Mar 4 08:08:51 EST 1996
Tom Kelliher