You are on page 1of 6

McCoy, Daniel

CISP 300
Assignment 7

Statement of Problem
Redo exercise 15 in chapter 5 using a modular approach.

Design an algorithm for the following problem You are in a candy store. Each of your customers buys exactly one
item costing $1.00 or less. Each customer pays for the item with exactly $1.00. Your job is to give each customer the
right amount of change in some combination of quarters, dimes, nickels, and pennies. The combination must be the
minimum number of coins. The input is composed of customer records, each containing customer name, and item
cost. The output is composed of lines, each containing customer name, item cost, change, number of quarters,
number of dimes, number of nickels, number of pennies. A cost of $0.00 will be used to signal the end of the input.

Structure Chart
O VE R A L L
CON TROL

A 0 00

P R O C E SS P RO C E SS P R O C E SS P R O CE SS P R O C E SS P RO CE SS
IN I T IA L I Z A T I O N Q U A RT ER D IM E N IC K E L P E NN Y LOO P
N UM BER N UM BE R N U M BE R N UM BE R OU TP U T

B000 B010 B020 B 0 30 B040 B050

Table of Variables
Variable Name Type Range Description
NAME String A-Z, a-z The customer name
COST Number 0-1 The cost of the item
CHANGEP Number 0-1 The permanent change value
CHANGE Number 0-1 The amount of change
QUARTER Number 0-4 The number of quarters
DIME Number 0-2 The number of dimes
NICKEL Number 0-1 The number of nickels
PENNY Number 0-4 The number of pennies
A000
START
WRITE "Enter: Name, Cost"
READ NAME, COST
DOWHILE COST > 0
Process initialization (B000)
DOWHILE CHANGE >= 0.25
Process quarter number (B010)
ENDDO
DOWHILE CHANGE >= 0.10
Process dime number (B020)
ENDDO
DOWHILE CHANGE >= 0.05
Process nickel number (B030)
ENDDO
DOWHILE CHANGE > 0
Process penny number (B040)
ENDDO
Process loop output (B050)
READ NAME, COST
ENDDO
STOP

B000
ENTER
QUARTER = 0
DIME = 0
NICKEL = 0
PENNY = 0
CHANGE = 1 - COST
CHANGEP = CHANGE
RETURN

B010
ENTER
CHANGE = CHANGE - 0.25
QUARTER = QUARTER + 1
RETURN

B020
ENTER
CHANGE = CHANGE - 0.10
DIME = DIME + 1
RETURN

BO30
ENTER
CHANGE = CHANGE - 0.05
NICKEL = NICKEL + 1
RETURN

B040
ENTER
CHANGE = CHANGE - 0.01
PENNY = PENNY + 1
RETURN
B050
ENTER
WRITE "Customer Name: ", NAME
WRITE "Item Cost: ", COST
WRITE "Change: ", CHANGEP
WRITE "Number of quarters: ", QUARTER
WRITE "Number of dimes: ", DIME
WRITE "Number of nickels: ", NICKEL
WRITE "Number of pennies: ", PENNY
WRITE "-------------------------------"
RETURN
Qbasic Source Code

DECLARE SUB ProcessInitialization()


DECLARE SUB QuarterNumber()
DECLARE SUB DimeNumber()
DECLARE SUB NickelNumber()
DECLARE SUB PennyNumber()
DECLARE SUB LoopOutput()

DIM SHARED NAME$


DIM SHARED COST
DIM SHARED QUARTER
DIM SHARED DIME
DIM SHARED NICKEL
DIM SHARED PENNY
DIM SHARED CHANGE
DIM SHARED CHANGEP

CLS
PRINT "Enter: Name, Cost"
INPUT NAME$, COST
DO WHILE (COST > 0)
CALL ProcessInitialization
DO WHILE (CHANGE >= .25)
CALL QuarterNumber
LOOP
DO WHILE (CHANGE >= .1)
CALL DimeNumber
LOOP
DO WHILE (CHANGE >= .05)
CALL NickelNumber
LOOP
DO WHILE (CHANGE > 0)
CALL PennyNumber
LOOP
CALL LoopOutput
INPUT NAME$, COST
LOOP
END

REM B000
SUB ProcessInitialization
QUARTER = 0
DIME = 0
NICKEL = 0
PENNY = 0
CHANGE = 0
CHANGEP = 0
CHANGE = (1 - COST)
CHANGEP = CHANGE
END SUB

REM B010
SUB QuarterNumber
CHANGE = (CHANGE - .25)
QUARTER = (QUARTER + 1)
END SUB
REM B020
SUB DimeNumber
CHANGE = (CHANGE - .1)
DIME = (DIME + 1)
END SUB

REM B030
SUB NickelNumber
CHANGE = (CHANGE - .05)
NICKEL = (NICKEL + 1)
END SUB

REM B040
SUB PennyNumber
CHANGE = (CHANGE - .01)
PENNY = (PENNY + 1)
END SUB

REM B050
SUB LoopOutput
PRINT "Customer Name: ", NAME$
PRINT "Item Cost: ", COST
PRINT "Change: ", CHANGEP
PRINT "Number of quarters: ", QUARTER
PRINT "Number of dimes: ", DIME
PRINT "Number of nickels: ", NICKEL
PRINT "Number of pennies: ", PENNY
PRINT "-------------------------------"
END SUB
Program Output:

Enter: Name, Cost


? Bill,.23
Customer Name: Bill
Item Cost: .23
Change: .77
Number of quarters: 3
Number of dimes: 0
Number of nickels: 0
Number of pennies: 2
-------------------------------
? Sam,.51
Customer Name: Sam
Item Cost: .51
Change: .49
Number of quarters: 1
Number of dimes: 2
Number of nickels: 0
Number of pennies: 4
-------------------------------
? Sue,.97
Customer Name: Sue
Item Cost: .97
Change: 2.999997E-02
Number of quarters: 0
Number of dimes: 0
Number of nickels: 0
Number of pennies: 3
-------------------------------
? Richard,0

Press any key to continue