CSCE 2004 - Programming Assignment 2
Due: Wednesday, 16 Feb 2011 at 11:59pm

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 and UARK ID number. 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.47).

Then, the program should announce how much the charge will be, and ask the user how much money they are depositing in US currency. (Utilize basic error-checking and looping to force a positive value to be entered before continuing.) The program will then calculate the change the user is owed.

Importantly, 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.30, the program would tell us that our change was 2 dollars, 3 quarters, 1 nickel, and 3 pennies.

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. 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.

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:

  1. First, we figure out (in terms of a raw number) how much money is owed.
  2. Then, in a loop, we subtract $1.00 from the amount owed (unless doing so would cause the amount owed to go negative) and keep track of how many dollars that is.
  3. Then, in a loop, we subtract $0.25 from the amount owed (unless doing so would cause the amount owed to go negative) and keep track of how many quarters that is.
  4. We repeat this procedure for dimes...
  5. ...and nickels...
  6. ...and finally, pennies. This should leave the final amount owed at $0.00.
  7. Last, we display the counts of dollars and coins in a user-friendly manner.

3. Implementation:

Start your program with comments based on your design and (the following cannot be emphasized enough..) 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. 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 code before you hand in your final version of the program.

5. Documentation:

When you have completed your C++ program, write a short report (less than half of a page long; a few sentences will suffice) describing what the objectives were, what you did, and the status of the program. Does it work properly for all test cases? Are there any known problems?

Include this report in the comments at the top of your source code file.

6. Project Submission:

NOTE: In the future, we will use Turing's PC2 submission system to submit homework assignemnts. (Snow gets in the way of everything!) For homework 2, we will do something different...

To Submit Homework 2:

  1. Begin an email to your TA with the title "CSCE 2004 HW2".
  2. Copy/paste the contents of your source code file into the body of the email.

    (Note: Do not add anything extra to the body of the email! And make sure your FULL name and uark ID number are printed out at the beginning of your program...)

  3. Send the email!

The date/time-stamp on your email will be used to verify that you met the due date above. All late projects will receive reduced credit (10% off if less than 24 hours late, no credit if more than 24 hours late), so hand in your best effort on the due date.