Assignment 1

advertisement
Assignment 1
1. Problem Statement:
The goal of this programming assignment is to develop a program for counting change from a
vending machine.
As an interesting aside, this can actually be a relatively difficult task if we were doing it with
arbitrary currency values (for example: 57-cent, 31-cent, 7-cent, and 3-cent coins!). However, it
becomes immensely more simple when we use the "round" values found in American (and many
other) currency (e.g., pennies, nickels, dimes, quarters, and so forth).
At the beginning, the program should print out your Name. The user should then be prompted to
choose one of three types of beverages to purchase:
1. a Can of Dr. Pepper (costs $0.75),
2. a 20-oz Bottle of Orange Fanta (costs $1.25),
3. or, a Liter of Cola (costs $2.32).
Then, the program should print out the beverage cost. Next, the program should ask the user how
much money they are depositing in US currency. To simplify matters, we will assume that the
vending machine only accepts payment in $1 bills. The user then just has to tell the program how
many dollars they are submitting.
Finally, the program will print out the change due. The change MUST be displayed in terms of
dollars and coins. We will use standard American currency, so $1.00 = 1 dollar, $0.25 = 1
quarter, $0.10 = 1 dime, $0.05 = 1 nickel, and $0.01 = 1 penny. For example, if we wanted a liter
of cola and put in $5, the program would tell us that our change was 2 dollars, 2 quarters, 1 dime,
1 nickel and 3 pennies.
The program should also do some basic error checking to make sure the user enters appropriate
values when prompted. For example, if the user enters a negative number, an error message
should be displayed and the program should end.
2. Design:
Looking at the requirements above, try to sketch out the "flow" of the program using either a
diagram or a sequence of conditional statements written in pseudo-code. Mentally walk through
each of the possible paths, and think about what conditions and calculations need to be
performed. Once you have done this, work out the basic formulas you will use to count change,
with a calculator, or with a spreadsheet to verify they are correct. The following algorithm
description is designed to help you along in this process.
An algorithm is a step-by-step procedure, composed of a finite list of well-defined
instructions, used to accomplish some task.
Assignment 1
To simplify the design, we will use what is called a "greedy algorithm." That is, we will make
the best possible (i.e., optimal) choice that we can for each given step as we go. In terms of
calculating a vending machine's change, we will do the following:






Display the menu of items on the screen (1. Dr. Pepper, 2. Orange Fanta etc..)
Tell the user to select one item from the items displayed
If the selection is incorrect
Display an error message on the screen
Else (if the selection is correct)
Display the price on the screen
Ask the user to enter money in bills
If the value that is entered is correct
Calculate the change and display it on the screen
Else (if the value is incorrect)
Display an error message on the screen
3. Implementation:
Start your program with comments based on your design and add portions of code a little at a
time. Compile and run your program on a regular basis, so you always have something that runs,
even if it only does part of the job. Make sure you have completed the first task, e.g., printing
your name to the screen, before you start writing code for the second task.
4. Testing and Debugging:
Test your program to check that it operates correctly for all of the requirements listed above.
Also check for the error handling capabilities of the code. Save your testing output in text files
for submission on the program due date.
For debugging purposes, it is useful to "echo" any variables you input, and to print intermediate
results as they are calculated. You should comment out your debugging when you are finished.
Download